[D-runtime] Runtime issue on Mac OS X

Sean Kelly sean at invisibleduck.org
Wed Jun 20 10:28:35 PDT 2012


A lot of work needs to be done to support dynamic libraries properly.  Thread lists need to be dynamically merged and unmerged, static segment info, ModuleInfo, etc.  We already have Runtime.load/unloadLibrary, so if we assume that D programmers will always call these instead of dlopen/dlclose directly then we just need to make sure the routines handle both C and D dynamic libraries appropriately.  Also, it should be possible to use the same D dynamic library with both C and D apps, which I guess means that Druntime would have to live in its own dynamic library as well.

On Jun 2, 2012, at 10:15 AM, Jacob Carlborg wrote:

> When I was trying to fix issue 7995 I created the pull request:
> 
> https://github.com/D-Programming-Language/druntime/pull/228
> 
> In that pull request it has come to my attention that following system functions doesn't play nice with dynamic libraries:
> 
> _dyld_register_func_for_add_image
> _dyld_register_func_for_remove_image
> 
> http://developer.apple.com/library/mac/#documentation/DeveloperTools/Reference/MachOReference/Reference/reference.html
> 
> This two functions are used in memory_osx.d to install callbacks for getting various data out of the loaded images, like TLS data, module infos, exception handling tables and so on. These callbacks are called, among other cases, when dlopen/dlclose is called.
> 
> In the discussion in the pull request dawgfoto (Martin Nowak) pointed out that it's not very good to use any of the above two functions in a dynamic library loaded via "dlopen".
> 
> The problem is that the callbacks are never removed from the internal list of callbacks. So if a dynamic library uses these functions and then gets unloaded. Next time dlopen/dlclose is called the application will crash because it tries to call the callback from the now unloaded library.
> 
> There doesn't seem to be a way to unregister these callbacks.
> 
> This example shows the problem
> 
> http://pastebin.com/yUKBgu8Y
> 
> When running "main" from the above a Bus error will occur.
> 
> I did some digging and found a solutions that seems to work:
> 
> http://pastebin.com/qZfiWRbN
> 
> The only difference compared to the implementation we have now is that the callback called when unloading a dylib is not called for the dylib that installed it. In the example above that would mean that "onUnload" is not called when foo.dylibis unloaded. Note that the callback is still called when bar.dylibis unloaded.
> 
> The problem with the implementation above is that "dyld_register_image_state_change_handler" is a private undocumented function in libdyld.dylib. This function has been available since Mac OS X 10.5 and is used by the Objective-C runtime.
> 
> All of this is only an issue if the D runtime is dynamically loaded, which isn't supported yet.
> 
> -- 
> /Jacob Carlborg
> 
> _______________________________________________
> D-runtime mailing list
> D-runtime at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/d-runtime



More information about the D-runtime mailing list