Cacao vs. OS X

I took a little time trying to get Cacao to run on x86 OS X again today. Porting the code is relatively easy — some tweaks to support the x86 Darwin signal handling, and some fixes for Darwin’s weird assembler, and it compiles fine. The big problem with getting it to run, however, is still stack alignment. Darwin’s x86 ABI is the same as that on Linux-x86 and FreeBSD-x86, except that the stack must be aligned on a 16-byte boundary. Somewhere in the call stack, however (maybe in the assembly support, maybe in the jitted code or one of the generated trampolines — I’m not certain yet) the stack gets misaligned, and this breaks things badly, eventually.

For the most part, the code will happily run with a misaligned stack; the problem manifests itself when a JNI dynamic symbol is resolved: the _dyld_stub_binding_helper_interface routine loads values from the stack into XMM registers, which will fail with an illegal instruction error if the stack is not properly aligned. So what happens is that the VM bootstrap starts, and runs, but as soon as a JNI method is called with the stack misaligned, the program aborts with SIGILL.

There is some code that tries to align the stack properly here and there, but it clearly isn’t working. Something I’m not sure about is the interaction of the code that tries to keep the stack aligned, and the call/push instructions.