Skip to content

Commit 1f365cc

Browse files
committed
8346433: Cannot use DllMain in hotspot for static builds
Reviewed-by: dholmes, stuefe
1 parent 0330ca4 commit 1f365cc

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

src/hotspot/os/windows/os_windows.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -146,26 +146,34 @@ LPTOP_LEVEL_EXCEPTION_FILTER previousUnhandledExceptionFilter = nullptr;
146146

147147
HINSTANCE vm_lib_handle;
148148

149+
static void windows_preinit(HINSTANCE hinst) {
150+
vm_lib_handle = hinst;
151+
if (ForceTimeHighResolution) {
152+
timeBeginPeriod(1L);
153+
}
154+
WindowsDbgHelp::pre_initialize();
155+
SymbolEngine::pre_initialize();
156+
}
157+
158+
static void windows_atexit() {
159+
if (ForceTimeHighResolution) {
160+
timeEndPeriod(1L);
161+
}
162+
#if defined(USE_VECTORED_EXCEPTION_HANDLING)
163+
if (topLevelVectoredExceptionHandler != nullptr) {
164+
RemoveVectoredExceptionHandler(topLevelVectoredExceptionHandler);
165+
topLevelVectoredExceptionHandler = nullptr;
166+
}
167+
#endif
168+
}
169+
149170
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
150171
switch (reason) {
151172
case DLL_PROCESS_ATTACH:
152-
vm_lib_handle = hinst;
153-
if (ForceTimeHighResolution) {
154-
timeBeginPeriod(1L);
155-
}
156-
WindowsDbgHelp::pre_initialize();
157-
SymbolEngine::pre_initialize();
173+
windows_preinit(hinst);
158174
break;
159175
case DLL_PROCESS_DETACH:
160-
if (ForceTimeHighResolution) {
161-
timeEndPeriod(1L);
162-
}
163-
#if defined(USE_VECTORED_EXCEPTION_HANDLING)
164-
if (topLevelVectoredExceptionHandler != nullptr) {
165-
RemoveVectoredExceptionHandler(topLevelVectoredExceptionHandler);
166-
topLevelVectoredExceptionHandler = nullptr;
167-
}
168-
#endif
176+
windows_atexit();
169177
break;
170178
default:
171179
break;
@@ -4427,6 +4435,14 @@ bool os::message_box(const char* title, const char* message) {
44274435

44284436
// This is called _before_ the global arguments have been parsed
44294437
void os::init(void) {
4438+
if (is_vm_statically_linked()) {
4439+
// Mimick what is done in DllMain for non-static builds
4440+
HMODULE hModule = NULL;
4441+
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &hModule);
4442+
windows_preinit(hModule);
4443+
atexit(windows_atexit);
4444+
}
4445+
44304446
_initial_pid = _getpid();
44314447

44324448
win32::initialize_windows_version();

src/hotspot/os/windows/symbolengine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -526,7 +526,7 @@ namespace { // Do not export.
526526
};
527527
}
528528

529-
// Called at DLL_PROCESS_ATTACH.
529+
// Called at DLL_PROCESS_ATTACH for dynamic builds, and from os::init() for static builds.
530530
void SymbolEngine::pre_initialize() {
531531
::InitializeCriticalSection(&g_cs);
532532
}

src/hotspot/os/windows/symbolengine.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2017 SAP SE. All rights reserved.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -58,7 +58,7 @@ namespace SymbolEngine {
5858
// missing - if any, and the dbhelp API version)
5959
void print_state_on(outputStream* st);
6060

61-
// Call at DLL_PROCESS_ATTACH.
61+
// Called at DLL_PROCESS_ATTACH for dynamic builds, and from os::init() for static builds.
6262
void pre_initialize();
6363

6464
};

src/hotspot/os/windows/windbghelp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -141,7 +141,7 @@ namespace { // Do not export.
141141
};
142142
}
143143

144-
// Called at DLL_PROCESS_ATTACH.
144+
// Called at DLL_PROCESS_ATTACH for dynamic builds, and from os::init() for static builds.
145145
void WindowsDbgHelp::pre_initialize() {
146146
::InitializeCriticalSection(&g_cs);
147147
}

src/hotspot/os/windows/windbghelp.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -67,7 +67,7 @@ namespace WindowsDbgHelp {
6767
// missing - if any, and the dbhelp API version)
6868
void print_state_on(outputStream* st);
6969

70-
// Call at DLL_PROCESS_ATTACH.
70+
// Called at DLL_PROCESS_ATTACH for dynamic builds, and from os::init() for static builds.
7171
void pre_initialize();
7272

7373
};

0 commit comments

Comments
 (0)