Skip to content

Commit

Permalink
8346378: Cannot use DllMain in libnet for static builds
Browse files Browse the repository at this point in the history
Reviewed-by: djelinski
  • Loading branch information
magicus committed Dec 18, 2024
1 parent 73b5dba commit 8efc558
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
6 changes: 5 additions & 1 deletion src/java.base/share/native/libnet/net_util.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024, 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 @@ -63,6 +63,10 @@ DEF_JNI_OnLoad(JavaVM *vm, void *reserved)
return JNI_EVERSION; /* JNI version not supported */
}

if (NET_PlatformInit() != 0) {
return JNI_ERR;
}

iCls = (*env)->FindClass(env, "java/lang/Boolean");
CHECK_NULL_RETURN(iCls, JNI_VERSION_1_2);
mid = (*env)->GetStaticMethodID(env, iCls, "getBoolean", "(Ljava/lang/String;)Z");
Expand Down
4 changes: 3 additions & 1 deletion src/java.base/share/native/libnet/net_util.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, 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 @@ -155,6 +155,8 @@ int NET_IsEqual(jbyte* caddr1, jbyte* caddr2);

int NET_IsZeroAddr(jbyte* caddr);

int NET_PlatformInit();

/* Socket operations
*
* These work just like the system calls, except that they may do some
Expand Down
11 changes: 10 additions & 1 deletion src/java.base/unix/native/libnet/net_util_md.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024, 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 @@ -50,6 +50,15 @@
#define IPV6_FLOWINFO_SEND 33
#endif

/* Perform platform specific initialization.
* Returns 0 on success, non-0 on failure */
int
NET_PlatformInit()
{
// Not needed on unix
return 0;
}

void
NET_ThrowByNameWithLastError(JNIEnv *env, const char *name,
const char *defaultDetail) {
Expand Down
30 changes: 11 additions & 19 deletions src/java.base/windows/native/libnet/net_util_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,21 @@ static struct {
{ WSA_OPERATION_ABORTED, 0, "Overlapped operation aborted" },
};

/*
* Initialize Windows Sockets API support
*/
BOOL WINAPI
DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved)
static void at_exit_callback(void)
{
WSADATA wsadata;
WSACleanup();
}

switch (reason) {
case DLL_PROCESS_ATTACH:
if (WSAStartup(MAKEWORD(2,2), &wsadata) != 0) {
return FALSE;
}
break;
/* Perform platform specific initialization.
* Returns 0 on success, non-0 on failure */
int
NET_PlatformInit()
{
WSADATA wsadata;

case DLL_PROCESS_DETACH:
WSACleanup();
break;
atexit(at_exit_callback);

default:
break;
}
return TRUE;
return WSAStartup(MAKEWORD(2,2), &wsadata);
}

/*
Expand Down

0 comments on commit 8efc558

Please sign in to comment.