Skip to content

Commit

Permalink
8346433: Cannot use DllMain in hotspot for static builds
Browse files Browse the repository at this point in the history
Reviewed-by: dholmes, stuefe
  • Loading branch information
magicus committed Jan 16, 2025
1 parent 0330ca4 commit 1f365cc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
48 changes: 32 additions & 16 deletions src/hotspot/os/windows/os_windows.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -146,26 +146,34 @@ LPTOP_LEVEL_EXCEPTION_FILTER previousUnhandledExceptionFilter = nullptr;

HINSTANCE vm_lib_handle;

static void windows_preinit(HINSTANCE hinst) {
vm_lib_handle = hinst;
if (ForceTimeHighResolution) {
timeBeginPeriod(1L);
}
WindowsDbgHelp::pre_initialize();
SymbolEngine::pre_initialize();
}

static void windows_atexit() {
if (ForceTimeHighResolution) {
timeEndPeriod(1L);
}
#if defined(USE_VECTORED_EXCEPTION_HANDLING)
if (topLevelVectoredExceptionHandler != nullptr) {
RemoveVectoredExceptionHandler(topLevelVectoredExceptionHandler);
topLevelVectoredExceptionHandler = nullptr;
}
#endif
}

BOOL WINAPI DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
switch (reason) {
case DLL_PROCESS_ATTACH:
vm_lib_handle = hinst;
if (ForceTimeHighResolution) {
timeBeginPeriod(1L);
}
WindowsDbgHelp::pre_initialize();
SymbolEngine::pre_initialize();
windows_preinit(hinst);
break;
case DLL_PROCESS_DETACH:
if (ForceTimeHighResolution) {
timeEndPeriod(1L);
}
#if defined(USE_VECTORED_EXCEPTION_HANDLING)
if (topLevelVectoredExceptionHandler != nullptr) {
RemoveVectoredExceptionHandler(topLevelVectoredExceptionHandler);
topLevelVectoredExceptionHandler = nullptr;
}
#endif
windows_atexit();
break;
default:
break;
Expand Down Expand Up @@ -4427,6 +4435,14 @@ bool os::message_box(const char* title, const char* message) {

// This is called _before_ the global arguments have been parsed
void os::init(void) {
if (is_vm_statically_linked()) {
// Mimick what is done in DllMain for non-static builds
HMODULE hModule = NULL;
GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, NULL, &hModule);
windows_preinit(hModule);
atexit(windows_atexit);
}

_initial_pid = _getpid();

win32::initialize_windows_version();
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/os/windows/symbolengine.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -526,7 +526,7 @@ namespace { // Do not export.
};
}

// Called at DLL_PROCESS_ATTACH.
// Called at DLL_PROCESS_ATTACH for dynamic builds, and from os::init() for static builds.
void SymbolEngine::pre_initialize() {
::InitializeCriticalSection(&g_cs);
}
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/os/windows/symbolengine.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017 SAP SE. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
Expand Down Expand Up @@ -58,7 +58,7 @@ namespace SymbolEngine {
// missing - if any, and the dbhelp API version)
void print_state_on(outputStream* st);

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

};
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/os/windows/windbghelp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -141,7 +141,7 @@ namespace { // Do not export.
};
}

// Called at DLL_PROCESS_ATTACH.
// Called at DLL_PROCESS_ATTACH for dynamic builds, and from os::init() for static builds.
void WindowsDbgHelp::pre_initialize() {
::InitializeCriticalSection(&g_cs);
}
Expand Down
4 changes: 2 additions & 2 deletions src/hotspot/os/windows/windbghelp.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -67,7 +67,7 @@ namespace WindowsDbgHelp {
// missing - if any, and the dbhelp API version)
void print_state_on(outputStream* st);

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

};
Expand Down

0 comments on commit 1f365cc

Please sign in to comment.