Commit 6bb2034b authored by jan.koester's avatar jan.koester
Browse files

thread: pthread_exit's jump back without __builtin_setjmp off x86_64

clang's __builtin_setjmp saves the FPRs with stfd even under -msoft-float,
so every thread start on powerpc e500v2 (no classic FPU) died with SIGILL
in sys::thread::_start since 427d6789. Off x86_64 sys::thread now uses
systempp's own _setjmp/_longjmp (arch/<arch>/setjmp.S: integer callee-saved
registers only) with a jmp_buf-sized _exitjmp. x86_64 is unchanged.

Checked under qemu-ppc (e500v2): a C pthread_create/join program and Rust
programs with threads run again.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent f250c1c0
Loading
Loading
Loading
Loading
+14 −2
Changes for src/linux/thread.cpp: 14 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -305,9 +305,21 @@ void sys::thread::start(){
    for (;;) syscall1(__NR_exit, 0);  /* another thread is running exit() */
}

#if defined(__x86_64__)
#define EXIT_SETJMP(buf) __builtin_setjmp(buf)
#define EXIT_LONGJMP(buf) __builtin_longjmp(buf, 1)
#else
/* systempp's own (arch/<arch>/setjmp.S): only the integer callee-saved
 * registers, so nothing FP on soft-float targets */
#include <setjmp.h>
/* jmp_buf (<= 32 words on every arch) fits sys::thread::_exitjmp */
#define EXIT_SETJMP(buf) _setjmp(buf)
#define EXIT_LONGJMP(buf) _longjmp(buf, 1)
#endif

void sys::thread::exitThread(void* retval) {
    _Retval = retval;
    __builtin_longjmp(_exitjmp, 1);
    EXIT_LONGJMP(_exitjmp);
}

void* sys::thread::_start(void *ins) noexcept{
@@ -315,7 +327,7 @@ void* sys::thread::_start(void *ins) noexcept{
    sys::thread* volatile mythread=(sys::thread*)((__ThreadArguments*)ins)->ins;
    void* volatile ret;
    try {
        if (__builtin_setjmp(mythread->_exitjmp) == 0)
        if (EXIT_SETJMP(mythread->_exitjmp) == 0)
            mythread->_Retval = mythread->run(((__ThreadArguments*)ins)->args);
        /* Real POSIX requirement: pthread_key destructors (which is
         * also what __cxa_thread_atexit's real thread_local destructor
+7 −0
Changes for src/thread.h: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -137,7 +137,14 @@ namespace sys {

    private:
        static void* _start(void* ins) noexcept;
#if defined(__x86_64__)
        void* _exitjmp[5]; /* __builtin_setjmp buffer, set in _start */
#else
        /* a jmp_buf for systempp's own _setjmp (arch/<arch>/setjmp.S;
         * powerpc needs 24 words): clang's __builtin_setjmp saves the
         * FPRs with stfd even under -msoft-float, SIGILL on e500v2 */
        long _exitjmp[32];
#endif

        /* _reclaim bits: run() is over / detach() was called. Whichever
         * side sets the second bit queues the object for _reapDetached(). */