sustrik / libdill

Structured concurrency in C
MIT License
1.69k stars 155 forks source link

segfault launching coroutine on M1 OSX #208

Open MobiusHorizons opened 3 years ago

MobiusHorizons commented 3 years ago

Attempting to run a trivial example (the one from the libdill homepage) on osx on an M1 (arm 64) results in a segfault.

Debugging the code points to an invalid memory access in the ___chkstk_darwin function, which seems to have been added by the compiler, since I don't see any references to it in the code.

I attempted to build the code with -fno-stack-protector but that didn't fix the problem.

cc -g3 -gdwarf-4  -fno-stack-protector test-dill.c -o test-dill -ldill

If you anyone can point me in the right direction I will be happy to work on a fix.

MobiusHorizons commented 3 years ago
(lldb) target create "bin/test-dill"
Current executable set to '/Users/paulmartin/src/c_modules/bin/test-dill' (arm64).
(lldb) r
Process 98145 launched: '/Users/paulmartin/src/c_modules/bin/test-dill' (arm64)
Process 98145 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16f603ff8)
    frame #0: 0x0000000188a04de4 libsystem_pthread.dylib`___chkstk_darwin + 64
libsystem_pthread.dylib`___chkstk_darwin:
->  0x188a04de4 <+64>: ldur   x11, [x11, #-0x8]
    0x188a04de8 <+68>: mov    x10, sp
    0x188a04dec <+72>: cmp    x9, #0x1, lsl #12         ; =0x1000
    0x188a04df0 <+76>: b.lo   0x188a04e08               ; <+100>
Target 0: (test-dill) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=2, address=0x16f603ff8)
  * frame #0: 0x0000000188a04de4 libsystem_pthread.dylib`___chkstk_darwin + 64
    frame #1: 0x0000000100003db0 test-dill`main at test-dill.c:16:2
    frame #2: 0x0000000188a25f34 libdyld.dylib`start + 4
(lldb) f 1
frame #1: 0x0000000100003db0 test-dill`main at test-dill.c:16:2
   13   }
   14
   15   int main () {
-> 16       go(worker("HELLO"));
   17       go(worker("WORLD"));
   18       msleep(now() + 5000);
   19       return 0;
MobiusHorizons commented 3 years ago

Interestingly this only happens when using the apple provided compiler cc when using clang installed from homebrew it works fine.

❯ cc --version
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: arm64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

vs

❯ clang --version
clang version 11.0.1
Target: arm64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin
smurfd commented 3 years ago

Thanks MobiusHorizons, has the same issue. you should be able to use gcc aswell, if you use the one provided by homebrew, like : /opt/homebrew/bin/gcc-11

joemoulton commented 1 year ago

-fno-stack-check