mirror of
https://github.com/openjdk/jdk.git
synced 2026-01-28 12:09:14 +00:00
8355080: java.base/jdk.internal.foreign.SystemLookup.find() doesn't work on static JDK
Reviewed-by: mcimadamore, jvernee
This commit is contained in:
parent
2f8448034f
commit
acd93df612
@ -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, \
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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";
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user