Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segfault while linking wasm binaries against libopenlibm #271

Open
cardamom-ai opened this issue Jan 14, 2023 · 2 comments
Open

Segfault while linking wasm binaries against libopenlibm #271

cardamom-ai opened this issue Jan 14, 2023 · 2 comments

Comments

@cardamom-ai
Copy link

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
Copy link

nsajko commented Jan 14, 2023

SIGSEV and an invalid memory address or null pointer dereference

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

  • Compile with -ggdb and and run with GDB. Don't know how much that applies to wasm, though
  • Compile with ubsan or asan: -g -fsanitize=undefined or -g -fsanitize=address. I think that may even work with wasm. When compiled like so, the execution will stop with a more informative error than otherwise (line number, type of error, etc.)

EDIT: you may need to disable optimizations, too.

@cardamom-ai
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants