Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions make/modules/java.base/Lib.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
6 changes: 5 additions & 1 deletion src/java.base/aix/native/libsyslookup/syslookup.c
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +67,7 @@ private static SymbolLookup makeSystemLookup() {
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
Expand All @@ -84,13 +85,12 @@ private static SymbolLookup makeWindowsLookup() {

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")
Expand All @@ -110,8 +110,7 @@ private static SymbolLookup makeWindowsLookup() {
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();
Expand All @@ -126,16 +125,17 @@ private static SymbolLookup libLookup(Function<RawNativeLibraries, NativeLibrary
};
}

/*
* 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);
Copy link
Contributor

@slowhog slowhog Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consult @mcimadamore or @JornVernee look at this for the native function call permission requirements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcimadamore or @JornVernee Can you help take a look of this? Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I believe both of them are on vacation, yet they have the required expertise to review this PR. You might need to wait a bit, sorry!

NativeLibrary lib = libs.loadLibrary(SymbolLookup.class, "syslookup");
return lookup(lib);
}

public static SystemLookup getInstance() {
return INSTANCE;
Expand Down
6 changes: 5 additions & 1 deletion src/java.base/share/native/libsyslookup/syslookup.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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";
Expand Down
6 changes: 5 additions & 1 deletion src/java.base/windows/native/libsyslookup/syslookup.c
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down