JuliaMath / openlibm

High quality system independent, portable, open source libm implementation
https://openlibm.org
Other
507 stars 139 forks source link

Segfault while linking wasm binaries against libopenlibm #271

Closed cardamom-ai closed 1 month ago

cardamom-ai commented 1 year ago

I have some basic c code to run the naive bayes function, and I'm using libopenlibm for logf and powf.

Building libopenlibm with gnu make and running the executable natively works exactly as expected. However, generating a wasm binary linked against libopenlibm.a results in a SIGSEV and an invalid memory address or null pointer dereference error.

I tried linking in libopenlibm built with gnu make with no arguments and with ARCH=wasm32. Linking the wasm binary against both libraries resulted in the same error as above.

Any suggestions?

nsajko commented 1 year ago

SIGSEV and an invalid memory address or null pointer dereference

In such cases there's two things that one should try first:

EDIT: you may need to disable optimizations, too.

cardamom-ai commented 1 year ago

Hey this ended up being a bit of a rat's nest to untangle but I found a workaround.

It turned out my binary wasn't exporting any symbols, because I was failing to find the symbols from openlibm when compiling. The lack of symbols was due to macOS' ranlib not understanding the archive created with make ARCH=wasm32, and stating that the archive had no symbols (even though an alpine docker image had no such issue). (Also there were some errors about the symbol table being too big, so it's possible that was the issue). I found a workaround by just globbing together all the object files with ld -r -o (rather than ar -rcs), and feeding that into my compilations, so I'm generally happy.

I'm not sure if this should be considered a clang bug, or a macOS bug, or what.