apolukhin / Boost.DLL

Library for comfortable work with DLL and DSO
https://boost.org/libs/dll
110 stars 69 forks source link

Semgentation fault when using function pointers in shared_library #49

Closed lukaszrachwalski closed 4 years ago

lukaszrachwalski commented 4 years ago

I have come across this issue when using function pointer type instead of function type. In this case calling a function after getting symbol address causes application crash: segmentation fault.

OS: Ubuntu 18.04.4 LTS Compiler: clang 9 Boost version is: 1.67.0

Please check attached example: BoostDllTest.zip

apolukhin commented 4 years ago

This is not a bug. It's a feature: lib.get<void()>("func") imports a function void func(){} lib.get<void(*)()>("func_var") imports a variable auto func_var = &func

P.S.: I'd appreciate a PR to clarify that behavior in FAQ section of the docs

lukaszrachwalski commented 4 years ago

Hi, I have no permission to create branch in this repository. BTW The only comment I can leave in FAQ section is a short description this problematic case that could look like this:

* [*Question:] Can I use function pointer type instead of function type to import function symbols?
  * [*Answer:] No you can't. The address you get is not the address to the function pointer you want. Even though your code is compiled correctly, the application will crash with SegFault at the first try to call the function.

It would be much better reduce number of wrong usages of the library by adding some mechanism to forbid using function pointers or returning the correct address to function pointer by choosing the correct version of the overload lib.get() member function.

apolukhin commented 4 years ago

Thanks for the report!

Added infor into FAQ