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

Thread safety issue when loading dynamic modules on Linux #1314

Closed
jackcmay opened this issue Sep 23, 2018 · 1 comment
Closed

Thread safety issue when loading dynamic modules on Linux #1314

jackcmay opened this issue Sep 23, 2018 · 1 comment
Assignees
Milestone

Comments

@jackcmay
Copy link
Contributor

jackcmay commented Sep 23, 2018

Observed while using crate libloading

Given a multi-threaded test that loads a dynamic module, calls a function, and then unloads the module in a loop, the expected behavior is for the module to be loaded/unloaded and it's init/fini called respectively everytime. Like so:

test start
init
dynamic function
fini
init
dynamic function
fini
test end

The expected behavior is observed when running on MacOS but not on Linux.

On Linux we observe the module's init/deinit function being called at the beginning and end of the test but also sporadically (but always in order) throughout the test. This hints that modules are not being unloaded each time (and instead shared amongst threads).

test start
init
dynamic function
.....
fini
init
dynamic function
fini
init
dynamic function
...
fini
test end

In addition to the behavior difference, on Linux this behavior also mostly results in a segmentation fault at some point in the program's execution.

No workaround to the behavior differences is known, but there is a workaround to the segmentation fault. Using RTLD_NODELETE when loading the module forces the loader to not unload the module until the process is killed (init called at beginning of test and fini at the very end).

libloading::os::unix::Library::open(Some(path), libc::RTLD_NODELETE | libc::RTLD_NOW)

The following repository will recreate the issue: https://github.com/jackcmay/dynamic_module_segfault

This issue is not to be confused with another loading issue on Linux having to do with TLS:

@jackcmay
Copy link
Contributor Author

Solana uses dynamic loading for native programs and native programs are loaded once and kept loaded so this issue does not apply

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