inventory icon indicating copy to clipboard operation
inventory copied to clipboard

Figure out and document how registration works when plugins are loaded dynamically by dlopen

Open dtolnay opened this issue 6 years ago • 8 comments

dtolnay avatar Dec 24 '18 16:12 dtolnay

We may need to set up a dtor to unregister plugins on dlclose.

dtolnay avatar Dec 24 '18 16:12 dtolnay

Note: #[dtor] might not be enough to unregister plugins as it is currently using a libc::atexit hook. I may have to figure out a way to generate the appropriate library hooks for the ctor library for shared libraries.

I believe that I'd have to change over to using __cxa_atexit for this case. In the meantime I should also add a caveat noting that #[dtor] is probably not yet appropriate for shared libraries.

mmastrac avatar Dec 29 '18 20:12 mmastrac

Thanks for the warning! I filed https://github.com/mmastrac/rust-ctor/issues/3 to follow up.

dtolnay avatar Dec 29 '18 21:12 dtolnay

While trying to fully understand how this works, I also discovered this comment that details conditions under which OSX libraries won't be unloaded. In this case, the dtor won't run until process exit.

It turns out that dragging in core::fmt is enough to prevent a rust cdylib from unloading.

mmastrac avatar Dec 30 '18 15:12 mmastrac

Also note that the lifetime of any symbols stored in the loaded plugin is tied to the library being loaded. So any function pointers stored anywhere while it is loaded become undefined behavior. Maybe if there were some way to attach a library lifetime to any symbol lookups, that would work to avoid unsafe behavior? Marking library unloading as unsafe with a big comment about the perils of unloading might be sufficient.

mathstuf avatar Nov 11 '19 03:11 mathstuf

I'm a bit confused, based off the title/description of this issue it sounds like registration doesn't work with dlopen, but the README.md says that registration does happen at the time of dlopen.

I'm trying to use inventory when loading .dll's, I've tried with both the dlopen and libloading crates and plugins don't seem to be added to the registry. I don't know all the finer details of this crate and dll loading crates, so am I misunderstanding how it works? (Also, I'm on Windows just in case there's a possibility that it's an OS-specific problem)

Hpmason avatar Dec 27 '22 07:12 Hpmason

I'm a bit confused, based off the title/description of this issue it sounds like registration doesn't work with dlopen, but the README.md says that registration does happen at the time of dlopen.

To the best of my knowledge, the registration happens during in the main application bootstrap routine (hence before main), but it does not mean that dlopen is involved. If dlopen is called as part of your application at run-time, that is not registered.

Enet4 avatar Dec 27 '22 12:12 Enet4

Gotcha, that makes sense. I was trying to figure out how a dynamically loaded dependency could access an existing static, which it can't lol. Thanks for clarifying!

Hpmason avatar Dec 27 '22 21:12 Hpmason