fungos / cr

cr.h: A Simple C Hot Reload Header-only Library
https://fungos.github.io/cr-simple-c-hot-reload/
MIT License
1.56k stars 104 forks source link

A function to test plugin file validity #19

Closed rokups closed 4 years ago

rokups commented 5 years ago

Do you think it would be useful to have a function to test plugin file validity before it is actually loaded? Im thinking it would be very useful in cases where plugin files and there names are not known at a build time and there is no distinction between plugin files and other libraries (like special plugin folder or something). I know we can try to load library and look up cr_main, however this may cause execution of code and depending on library it may even lead to crashes or some other nasty side effects.

What i am proposing is to have cr_is_valid_plugin(file_path). This function should read dynamic library file, parse it's headers and look for cr_main export. If export is found then we can be reasonably sure it is a plugin.

Good thing about this is that i already have implementation for linux and windows here. So just MacOS bit is missing.

fungos commented 5 years ago

The idea is interesting, but I'm a bit unsure about it. I mean, it will mostly be dead-code inside cr.h as cr itself will not use this, so this looks like an extension or misc functionality useful for some cases but not always enabled. It could be gated, but then it would require specific tests to run in the CI. I'm open to it, but not entirely sold on it.

rokups commented 5 years ago

Ah but it does not have to be a dead code. cr_plugin_load should use such function to evaluate plugin file before loading it. This would allow user to naively throw anything at cr without risk of executing any code of non-plugins (DllMain, static initialization). Unless there is a way to utilize LoadLibrary/dlopen to do the same thing? Any idea?

fungos commented 5 years ago

Right, that could work. If you're willing to do it, this looks good to me! :)

rokups commented 5 years ago

Once i finish MachO parsing part ill submit a PR then :+1:

fungos commented 4 years ago

Closing due inactivity, thank you.