both __clone and __syscall_cp_asm failed to restore the original value of r6 after using it as a syscall argument register. the extent of breakage is not known, and in some cases may be mitigated by the only callers being internal to libc; if they used r6 but no longer needed its value after the call, they may not have noticed the problem. however at least posix_spawn (which uses __clone) was observed returning to the application with the wrong value in r6, leading to crash. since the call frame ABI already provides a place to spill registers, fixing this is just a matter of using it. in __clone, we also spuriously restore r6 in the child, since the parent branch directly returns to the caller. this takes the value from an uninitialized slot of the child's stack, but is harmless since there is no caller to return to in the child.
55 lines
1.0 KiB
ArmAsm
55 lines
1.0 KiB
ArmAsm
.text
|
|
.global __clone
|
|
.hidden __clone
|
|
.type __clone, %function
|
|
__clone:
|
|
# int clone(
|
|
# fn, a = r2
|
|
# stack, b = r3
|
|
# flags, c = r4
|
|
# arg, d = r5
|
|
# ptid, e = r6
|
|
# tls, f = *(r15+160)
|
|
# ctid) g = *(r15+168)
|
|
#
|
|
# pseudo C code:
|
|
# tid = syscall(SYS_clone,b,c,e,g,f);
|
|
# if (!tid) syscall(SYS_exit, a(d));
|
|
# return tid;
|
|
|
|
# preserve call-saved register used as syscall arg
|
|
stg %r6, 48(%r15)
|
|
|
|
# create initial stack frame for new thread
|
|
nill %r3, 0xfff8
|
|
aghi %r3, -160
|
|
lghi %r0, 0
|
|
stg %r0, 0(%r3)
|
|
|
|
# save fn and arg to child stack
|
|
stg %r2, 8(%r3)
|
|
stg %r5, 16(%r3)
|
|
|
|
# shuffle args into correct registers and call SYS_clone
|
|
lgr %r2, %r3
|
|
lgr %r3, %r4
|
|
lgr %r4, %r6
|
|
lg %r5, 168(%r15)
|
|
lg %r6, 160(%r15)
|
|
svc 120
|
|
|
|
# restore call-saved register
|
|
lg %r6, 48(%r15)
|
|
|
|
# if error or if we're the parent, return
|
|
ltgr %r2, %r2
|
|
bnzr %r14
|
|
|
|
# we're the child. call fn(arg)
|
|
lg %r1, 8(%r15)
|
|
lg %r2, 16(%r15)
|
|
basr %r14, %r1
|
|
|
|
# call SYS_exit. exit code is already in r2 from fn return value
|
|
svc 1
|