From 55279388faa5b5d9da2fb5fcdd56d5656df683a7 Mon Sep 17 00:00:00 2001 From: Florian Angerer Date: Tue, 1 Oct 2024 09:03:38 +0200 Subject: [PATCH] Provide jdk.internal.loader.NativeLibraries --- .../jdk/Target_java_lang_ClassLoader.java | 19 +------ ...t_jdk_internal_loader_NativeLibraries.java | 53 ++++++------------- 2 files changed, 18 insertions(+), 54 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_ClassLoader.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_ClassLoader.java index 1bf3e489d766..ce0006e988ba 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_ClassLoader.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_java_lang_ClassLoader.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 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 @@ -251,23 +251,6 @@ private static long findNative(@SuppressWarnings("unused") ClassLoader loader, S return NativeLibrarySupport.singleton().findSymbol(entryName).rawValue(); } - @Substitute - @TargetElement(onlyWith = JDKLatest.class) - static long findNative(ClassLoader loader, Class clazz, String entryName, String javaName) { - long addr = NativeLibrarySupport.singleton().findSymbol(entryName).rawValue(); - if (addr != 0 && loader != null) { - Target_jdk_internal_reflect_Reflection.ensureNativeAccess(clazz, clazz, javaName, true); - } - return addr; - } - - /** - * All usages of {@link Target_jdk_internal_loader_NativeLibraries} are substituted currently. - */ - @Delete - @TargetElement(onlyWith = JDKLatest.class) - static native Target_jdk_internal_loader_NativeLibraries nativeLibrariesFor(ClassLoader loader); - @Substitute @SuppressWarnings({"unused", "static-method"}) Class defineClass(byte[] b, int off, int len) throws ClassFormatError { diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_loader_NativeLibraries.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_loader_NativeLibraries.java index 6eb6a3e04ce5..5cc76ce14def 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_loader_NativeLibraries.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_loader_NativeLibraries.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 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 @@ -24,50 +24,31 @@ */ package com.oracle.svm.core.jdk; -import java.util.Map; -import java.util.Set; - import com.oracle.svm.core.annotate.Delete; import com.oracle.svm.core.annotate.Substitute; import com.oracle.svm.core.annotate.TargetClass; -import jdk.internal.loader.NativeLibraries; - +/* + * Substituting the entire class because the original class has fields capturing + * state from the image generator that must not leak into the image heap. + */ +@Substitute @TargetClass(value = jdk.internal.loader.NativeLibraries.class) final class Target_jdk_internal_loader_NativeLibraries { - /** - * The NativeLibraries is only used by the `loadLibrary` methods that are substituted, so we do - * not need an instance for now. - */ - @SuppressWarnings("unused") - @Substitute// - public static NativeLibraries newInstance(ClassLoader loader) { - return null; - } - /* - * Fields capturing state from the image generator that must not leak into the image heap. - */ - - @Delete // - private Map libraries; - @Delete // - private static Set loadedLibraryNames; - - /* - * We are defensive and also handle private native methods by marking them as deleted. If they - * are reachable, the user is certainly doing something wrong. But we do not want to fail with a - * linking error. - */ - - @Delete// - private static native boolean load(Target_jdk_internal_loader_NativeLibraries_NativeLibraryImpl impl, String name, boolean isBuiltin, boolean throwExceptionIfFail); + // needs to be explicitly deleted (GR-58623) + @Delete @SuppressWarnings("unused") static final boolean $assertionsDisabled = false; - @Delete// - private static native void unload(String name, boolean isBuiltin, long handle); + @Substitute + public static Target_jdk_internal_loader_NativeLibraries newInstance(@SuppressWarnings("unused") ClassLoader loader) { + return new Target_jdk_internal_loader_NativeLibraries(); + } - @Delete - private static native String findBuiltinLib(String name); + @Substitute + @SuppressWarnings("static-method") + public long find(String name) { + return NativeLibrarySupport.singleton().findSymbol(name).rawValue(); + } } @TargetClass(value = jdk.internal.loader.NativeLibraries.class, innerClass = "NativeLibraryImpl")