RFR: 8346433: Cannot use DllMain in hotspot for static builds
Magnus Ihse Bursie
ihse at openjdk.org
Wed Dec 18 09:55:41 UTC 2024
On Wed, 18 Dec 2024 05:17:25 GMT, David Holmes <dholmes at openjdk.org> wrote:
> But the other pre-initialize functions are also an issue as they need to be called as soon as possible in the VM's lifecycle (os::init could be way too late!) in case of crashes.
Hm, I did not think about crashes. I see that there could be a potential problem with WindowsDbgHelp::miniDumpWriteDump which is called from os::abort. I understand that there is a risk of crashes during bootstrapping, but I'd like to stress that os::init is called *very* early in the bootstrapping process. I have single-stepped through until I got there, and it is not much being done, so the likelihood of anything going wrong there seems minuscule. But I understand that we'd want to do this as early as possible. I'll check this once more, looking out for the potential for crashes.
However, for a static build, there is not much more I can do, unless I want to break a lot of abstractions and add a hook from the launcher into Hotspot, and I think neither you nor me would want that. I could add an new additional call in JNI_CreateJavaVM or Threads::create_vm (`os::pre_init()`?) but that will also clutter the code for everyone, to the benefit of just one special case (static builds on Windows).
> . I don't know when the DllMain detach code runs in relation to when an at_exit function may run! ?? [...] For a static build how are you figuring out and controlling the order of all of the normal "load time" initialization: static constructors, DllMain, ... ??
In a normal dynamic build, the DllMain is called by Windows when jvm.dll is loaded by the launcher. Static constructors are called prior to this, or after -- I don't know, and I don't know if it is specified. Then DllMain is called again when the dll is unloaded. In our case, this only happens at process exit. Windows will unload all dll:s, and for each of them, call DllMain and then the static destructors. I don't know if this order is specified or not either, but that is how it has been working in practice. (I know since I have another open bug on AWT regarding this.) Finally, when all dlls have been unloaded, atexit handlers are run. (At least I think so, it might be that they are called on a per-dll basis as well.)
In a static build, there are no dll:s so DllMain is never called. Static constructors are called at process startup, presumably before main() is called. Then atexit handlers and static destructors are called. I don't recall in which order, and once again, I don't know if it is specified.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/22793#issuecomment-2550874998
More information about the hotspot-runtime-dev
mailing list