8355080: java.base/jdk.internal.foreign.SystemLookup.find() doesn't work on static JDK

Reviewed-by: mcimadamore, jvernee
This commit is contained in:
Jiangli Zhou 2025-04-28 18:55:26 +00:00
parent 2f8448034f
commit acd93df612
5 changed files with 30 additions and 17 deletions

View File

@ -158,6 +158,7 @@ endif
$(eval $(call SetupJdkLibrary, BUILD_LIBSYSLOOKUP, \
NAME := syslookup, \
EXTRA_HEADER_DIRS := java.base:libjava, \
LD_SET_ORIGIN := false, \
LDFLAGS_linux := -Wl$(COMMA)--no-as-needed, \
LDFLAGS_aix := -brtl -bexpfull, \

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023, IBM Corp.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@ -29,6 +29,10 @@
#include <string.h>
#include <math.h>
#include "jni_util.h"
DEF_STATIC_JNI_OnLoad
// Addresses of functions to be referenced using static linking.
void* funcs[] = {
//string.h

View File

@ -25,6 +25,7 @@
package jdk.internal.foreign;
import jdk.internal.loader.NativeLibraries;
import jdk.internal.loader.NativeLibrary;
import jdk.internal.loader.RawNativeLibraries;
import jdk.internal.util.OperatingSystem;
@ -66,7 +67,7 @@ public final class SystemLookup implements SymbolLookup {
if (OperatingSystem.isWindows()) {
return makeWindowsLookup();
} else {
return libLookup(libs -> libs.load(jdkLibraryPath("syslookup")));
return sysLookup();
}
} catch (Throwable ex) {
// This can happen in the event of a library loading failure - e.g. if one of the libraries the
@ -84,13 +85,12 @@ public final class SystemLookup implements SymbolLookup {
boolean useUCRT = Files.exists(ucrtbase);
Path stdLib = useUCRT ? ucrtbase : msvcrt;
SymbolLookup lookup = libLookup(libs -> libs.load(stdLib));
SymbolLookup lookup = stdLibLookup(libs -> libs.load(stdLib));
if (useUCRT) {
// use a fallback lookup to look up inline functions from fallback lib
SymbolLookup fallbackLibLookup =
libLookup(libs -> libs.load(jdkLibraryPath("syslookup")));
SymbolLookup fallbackLibLookup = sysLookup();
@SuppressWarnings("restricted")
MemorySegment funcs = fallbackLibLookup.findOrThrow("funcs")
@ -110,8 +110,7 @@ public final class SystemLookup implements SymbolLookup {
return lookup;
}
private static SymbolLookup libLookup(Function<RawNativeLibraries, NativeLibrary> loader) {
NativeLibrary lib = loader.apply(RawNativeLibraries.newInstance(MethodHandles.lookup()));
private static SymbolLookup lookup(NativeLibrary lib) {
return name -> {
Objects.requireNonNull(name);
if (Utils.containsNullChars(name)) return Optional.empty();
@ -126,16 +125,17 @@ public final class SystemLookup implements SymbolLookup {
};
}
/*
* Returns the path of the given library name from JDK
*/
private static Path jdkLibraryPath(String name) {
Path javahome = Path.of(StaticProperty.javaHome());
String lib = OperatingSystem.isWindows() ? "bin" : "lib";
String libname = System.mapLibraryName(name);
return javahome.resolve(lib).resolve(libname);
private static SymbolLookup stdLibLookup(Function<RawNativeLibraries, NativeLibrary> loader) {
NativeLibrary lib = loader.apply(RawNativeLibraries.newInstance(MethodHandles.lookup()));
return lookup(lib);
}
@SuppressWarnings("restricted")
private static SymbolLookup sysLookup() {
NativeLibraries libs = NativeLibraries.newInstance(null);
NativeLibrary lib = libs.loadLibrary(SymbolLookup.class, "syslookup");
return lookup(lib);
}
public static SystemLookup getInstance() {
return INSTANCE;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 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
@ -27,6 +27,10 @@
// Adding at least one #include removes unwanted warnings on some platforms.
#include <stdlib.h>
#include "jni_util.h"
DEF_STATIC_JNI_OnLoad
// Simple dummy function so this library appears as a normal library to tooling.
char* syslookup() {
return "syslookup";

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 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
@ -26,6 +26,10 @@
#include <stdio.h>
#include <time.h>
#include "jni_util.h"
DEF_STATIC_JNI_OnLoad
// Forces generation of inline code on Windows
__declspec(dllexport) void* funcs[] = {
// stdio.h