Skip to content

Commit acd93df

Browse files
committed
8355080: java.base/jdk.internal.foreign.SystemLookup.find() doesn't work on static JDK
Reviewed-by: mcimadamore, jvernee
1 parent 2f84480 commit acd93df

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

make/modules/java.base/Lib.gmk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ endif
158158

159159
$(eval $(call SetupJdkLibrary, BUILD_LIBSYSLOOKUP, \
160160
NAME := syslookup, \
161+
EXTRA_HEADER_DIRS := java.base:libjava, \
161162
LD_SET_ORIGIN := false, \
162163
LDFLAGS_linux := -Wl$(COMMA)--no-as-needed, \
163164
LDFLAGS_aix := -brtl -bexpfull, \

src/java.base/aix/native/libsyslookup/syslookup.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
33
* Copyright (c) 2023, IBM Corp.
44
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
55
*
@@ -29,6 +29,10 @@
2929
#include <string.h>
3030
#include <math.h>
3131

32+
#include "jni_util.h"
33+
34+
DEF_STATIC_JNI_OnLoad
35+
3236
// Addresses of functions to be referenced using static linking.
3337
void* funcs[] = {
3438
//string.h

src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package jdk.internal.foreign;
2727

28+
import jdk.internal.loader.NativeLibraries;
2829
import jdk.internal.loader.NativeLibrary;
2930
import jdk.internal.loader.RawNativeLibraries;
3031
import jdk.internal.util.OperatingSystem;
@@ -66,7 +67,7 @@ private static SymbolLookup makeSystemLookup() {
6667
if (OperatingSystem.isWindows()) {
6768
return makeWindowsLookup();
6869
} else {
69-
return libLookup(libs -> libs.load(jdkLibraryPath("syslookup")));
70+
return sysLookup();
7071
}
7172
} catch (Throwable ex) {
7273
// This can happen in the event of a library loading failure - e.g. if one of the libraries the
@@ -84,13 +85,12 @@ private static SymbolLookup makeWindowsLookup() {
8485

8586
boolean useUCRT = Files.exists(ucrtbase);
8687
Path stdLib = useUCRT ? ucrtbase : msvcrt;
87-
SymbolLookup lookup = libLookup(libs -> libs.load(stdLib));
88+
SymbolLookup lookup = stdLibLookup(libs -> libs.load(stdLib));
8889

8990
if (useUCRT) {
9091
// use a fallback lookup to look up inline functions from fallback lib
9192

92-
SymbolLookup fallbackLibLookup =
93-
libLookup(libs -> libs.load(jdkLibraryPath("syslookup")));
93+
SymbolLookup fallbackLibLookup = sysLookup();
9494

9595
@SuppressWarnings("restricted")
9696
MemorySegment funcs = fallbackLibLookup.findOrThrow("funcs")
@@ -110,8 +110,7 @@ private static SymbolLookup makeWindowsLookup() {
110110
return lookup;
111111
}
112112

113-
private static SymbolLookup libLookup(Function<RawNativeLibraries, NativeLibrary> loader) {
114-
NativeLibrary lib = loader.apply(RawNativeLibraries.newInstance(MethodHandles.lookup()));
113+
private static SymbolLookup lookup(NativeLibrary lib) {
115114
return name -> {
116115
Objects.requireNonNull(name);
117116
if (Utils.containsNullChars(name)) return Optional.empty();
@@ -126,16 +125,17 @@ private static SymbolLookup libLookup(Function<RawNativeLibraries, NativeLibrary
126125
};
127126
}
128127

129-
/*
130-
* Returns the path of the given library name from JDK
131-
*/
132-
private static Path jdkLibraryPath(String name) {
133-
Path javahome = Path.of(StaticProperty.javaHome());
134-
String lib = OperatingSystem.isWindows() ? "bin" : "lib";
135-
String libname = System.mapLibraryName(name);
136-
return javahome.resolve(lib).resolve(libname);
128+
private static SymbolLookup stdLibLookup(Function<RawNativeLibraries, NativeLibrary> loader) {
129+
NativeLibrary lib = loader.apply(RawNativeLibraries.newInstance(MethodHandles.lookup()));
130+
return lookup(lib);
137131
}
138132

133+
@SuppressWarnings("restricted")
134+
private static SymbolLookup sysLookup() {
135+
NativeLibraries libs = NativeLibraries.newInstance(null);
136+
NativeLibrary lib = libs.loadLibrary(SymbolLookup.class, "syslookup");
137+
return lookup(lib);
138+
}
139139

140140
public static SystemLookup getInstance() {
141141
return INSTANCE;

src/java.base/share/native/libsyslookup/syslookup.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 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
@@ -27,6 +27,10 @@
2727
// Adding at least one #include removes unwanted warnings on some platforms.
2828
#include <stdlib.h>
2929

30+
#include "jni_util.h"
31+
32+
DEF_STATIC_JNI_OnLoad
33+
3034
// Simple dummy function so this library appears as a normal library to tooling.
3135
char* syslookup() {
3236
return "syslookup";

src/java.base/windows/native/libsyslookup/syslookup.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 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
@@ -26,6 +26,10 @@
2626
#include <stdio.h>
2727
#include <time.h>
2828

29+
#include "jni_util.h"
30+
31+
DEF_STATIC_JNI_OnLoad
32+
2933
// Forces generation of inline code on Windows
3034
__declspec(dllexport) void* funcs[] = {
3135
// stdio.h

0 commit comments

Comments
 (0)