在 OS X pthreads 实现(http://www.opensource.apple.com/source/Libc/Libc-825.26/pthreads/thread_setup.c?txt)中,它们在线程堆栈(第 140 行)上提供了一个虚假的返回地址:
ts->rip = (uintptr_t) routine;
/*
** We need to simulate a 16-byte aligned stack frame as if we had
** executed a call instruction. The stack should already be aligned
** before it comes to us and we don't need to push any arguments,
** so we shouldn't need to change it.
*/
ts->rdi = (uintptr_t) thread; /* argument to function */
*--sp = 0; /* fake return address */
ts->rsp = (uintptr_t) sp; /* set stack pointer */
我不明白当线程正在执行的函数调用“ret”并从堆栈中弹出返回地址时,这不会因非法指令/段错误而崩溃。谁能解释这是如何预防/处理的?