Skip to content

Commit 8efc558

Browse files
committed
8346378: Cannot use DllMain in libnet for static builds
Reviewed-by: djelinski
1 parent 73b5dba commit 8efc558

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

src/java.base/share/native/libnet/net_util.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2024, 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
@@ -63,6 +63,10 @@ DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
6363
return JNI_EVERSION; /* JNI version not supported */
6464
}
6565

66+
if (NET_PlatformInit() != 0) {
67+
return JNI_ERR;
68+
}
69+
6670
iCls = (*env)->FindClass(env, "java/lang/Boolean");
6771
CHECK_NULL_RETURN(iCls, JNI_VERSION_1_2);
6872
mid = (*env)->GetStaticMethodID(env, iCls, "getBoolean", "(Ljava/lang/String;)Z");

src/java.base/share/native/libnet/net_util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, 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
@@ -155,6 +155,8 @@ int NET_IsEqual(jbyte* caddr1, jbyte* caddr2);
155155

156156
int NET_IsZeroAddr(jbyte* caddr);
157157

158+
int NET_PlatformInit();
159+
158160
/* Socket operations
159161
*
160162
* These work just like the system calls, except that they may do some

src/java.base/unix/native/libnet/net_util_md.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, 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
@@ -50,6 +50,15 @@
5050
#define IPV6_FLOWINFO_SEND 33
5151
#endif
5252

53+
/* Perform platform specific initialization.
54+
* Returns 0 on success, non-0 on failure */
55+
int
56+
NET_PlatformInit()
57+
{
58+
// Not needed on unix
59+
return 0;
60+
}
61+
5362
void
5463
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
5564
const char *defaultDetail) {

src/java.base/windows/native/libnet/net_util_md.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,21 @@ static struct {
101101
{ WSA_OPERATION_ABORTED, 0, "Overlapped operation aborted" },
102102
};
103103

104-
/*
105-
* Initialize Windows Sockets API support
106-
*/
107-
BOOL WINAPI
108-
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
104+
static void at_exit_callback(void)
109105
{
110-
WSADATA wsadata;
106+
WSACleanup();
107+
}
111108

112-
switch (reason) {
113-
case DLL_PROCESS_ATTACH:
114-
if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0) {
115-
return FALSE;
116-
}
117-
break;
109+
/* Perform platform specific initialization.
110+
* Returns 0 on success, non-0 on failure */
111+
int
112+
NET_PlatformInit()
113+
{
114+
WSADATA wsadata;
118115

119-
case DLL_PROCESS_DETACH:
120-
WSACleanup();
121-
break;
116+
atexit(at_exit_callback);
122117

123-
default:
124-
break;
125-
}
126-
return TRUE;
118+
return WSAStartup(MAKEWORD(2,2), &wsadata);
127119
}
128120

129121
/*

0 commit comments

Comments
 (0)