osx - Mac OS X pthreads fake return address -
in os x pthreads implementation (http://www.opensource.apple.com/source/libc/libc-825.26/pthreads/thread_setup.c?txt) provide fake return address on thread stack (line 140):
ts->rip = (uintptr_t) routine; /* ** need simulate 16-byte aligned stack frame if had ** executed call instruction. stack should aligned ** before comes , don't need push arguments, ** shouldn't need change it. */ ts->rdi = (uintptr_t) thread; /* argument function */ *--sp = 0; /* fake return address */ ts->rsp = (uintptr_t) sp; /* set stack pointer */
i not understand how not crash illegal instruction/segfault when function thread executing calls 'ret' , pops return address stack. can explain how prevented/handled?
without looking @ rest of code, can venture guess. intuition says, called thread procedure (the user-supplied start_routine
parameter) should never return calling function.
think it: if new thread did return, have 2 threads running on same original code path. imagine thread function actually called wrapper calls user-supplied start_routine
. when start_routine
returns, wrapper calls pthread_exit
.
(main thread) v pthread_create v thread_setup (sets stack), , spawns new thread v | return main thread | | | v wrapper_function v user-supplied start_routine | (returns) v wrapper_function calls v pthread_exit
again, guess, whole point is, new thread should never return code called pthread_create
. purpose of wrapper ensure pthread_exit
gets called.
i have see passing routine
thread_setup
.
my feelings confirmed fact you don't have call pthread_exit
.
Comments
Post a Comment