From bd3fec3075829efc0afe7a99d7a684cf81cc5bbb Mon Sep 17 00:00:00 2001 From: Per Minborg Date: Wed, 13 Nov 2024 16:42:48 +0000 Subject: [PATCH] 8344086: Remove security manager dependency in FFM Reviewed-by: mcimadamore, rriggs, jvernee --- .../classes/jdk/internal/foreign/CABI.java | 6 ++--- .../foreign/SegmentBulkOperations.java | 3 +-- .../jdk/internal/foreign/StringSupport.java | 3 +-- .../jdk/internal/foreign/SystemLookup.java | 26 +++++------------- .../classes/jdk/internal/foreign/Utils.java | 4 --- .../foreign/abi/BindingSpecializer.java | 10 +++---- .../foreign/abi/CallingSequenceBuilder.java | 6 ++--- .../internal/foreign/abi/CapturableState.java | 9 ++++--- .../internal/foreign/abi/DowncallLinker.java | 4 +-- .../internal/foreign/abi/UpcallLinker.java | 8 +++--- .../foreign/abi/fallback/LibFallback.java | 27 ++++++++----------- 11 files changed, 39 insertions(+), 67 deletions(-) diff --git a/src/java.base/share/classes/jdk/internal/foreign/CABI.java b/src/java.base/share/classes/jdk/internal/foreign/CABI.java index 500e312b2f3..216a9fd874c 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/CABI.java +++ b/src/java.base/share/classes/jdk/internal/foreign/CABI.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023, 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 @@ -23,6 +23,7 @@ * questions. * */ + package jdk.internal.foreign; import jdk.internal.foreign.abi.fallback.FallbackLinker; @@ -31,7 +32,6 @@ import jdk.internal.util.OperatingSystem; import jdk.internal.util.StaticProperty; import static java.lang.foreign.ValueLayout.ADDRESS; -import static sun.security.action.GetPropertyAction.privilegedGetProperty; public enum CABI { SYS_V, @@ -50,7 +50,7 @@ public enum CABI { private static final CABI CURRENT = computeCurrent(); private static CABI computeCurrent() { - String abi = privilegedGetProperty("jdk.internal.foreign.CABI"); + String abi = System.getProperty("jdk.internal.foreign.CABI"); if (abi != null) { return CABI.valueOf(abi); } diff --git a/src/java.base/share/classes/jdk/internal/foreign/SegmentBulkOperations.java b/src/java.base/share/classes/jdk/internal/foreign/SegmentBulkOperations.java index d5a5954fa0e..773b8e57440 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/SegmentBulkOperations.java +++ b/src/java.base/share/classes/jdk/internal/foreign/SegmentBulkOperations.java @@ -29,7 +29,6 @@ import jdk.internal.misc.ScopedMemoryAccess; import jdk.internal.util.Architecture; import jdk.internal.util.ArraysSupport; import jdk.internal.vm.annotation.ForceInline; -import sun.security.action.GetIntegerAction; import java.lang.foreign.MemorySegment; @@ -293,7 +292,7 @@ public final class SegmentBulkOperations { // The returned value is in the interval [0, 2^30] static int powerOfPropertyOr(String name, int defaultPower) { - final int power = GetIntegerAction.privilegedGetProperty(PROPERTY_PATH + name, defaultPower); + final int power = Integer.getInteger(PROPERTY_PATH + name, defaultPower); return 1 << Math.clamp(power, 0, Integer.SIZE - 2); } diff --git a/src/java.base/share/classes/jdk/internal/foreign/StringSupport.java b/src/java.base/share/classes/jdk/internal/foreign/StringSupport.java index b74d09be0dd..78550c56136 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/StringSupport.java +++ b/src/java.base/share/classes/jdk/internal/foreign/StringSupport.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 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 @@ -29,7 +29,6 @@ import jdk.internal.access.JavaLangAccess; import jdk.internal.access.SharedSecrets; import jdk.internal.foreign.abi.SharedUtils; import jdk.internal.util.ArraysSupport; -import sun.security.action.GetPropertyAction; import java.lang.foreign.MemorySegment; import java.nio.charset.Charset; diff --git a/src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java b/src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java index 98e02140af6..7194813aff7 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java +++ b/src/java.base/share/classes/jdk/internal/foreign/SystemLookup.java @@ -29,14 +29,12 @@ import java.lang.foreign.*; import java.lang.invoke.MethodHandles; import java.nio.file.Files; import java.nio.file.Path; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.Objects; import java.util.Optional; import java.util.function.Function; import jdk.internal.loader.NativeLibrary; import jdk.internal.loader.RawNativeLibraries; -import sun.security.action.GetPropertyAction; +import jdk.internal.util.OperatingSystem; import static java.lang.foreign.ValueLayout.ADDRESS; @@ -60,7 +58,7 @@ public final class SystemLookup implements SymbolLookup { private static SymbolLookup makeSystemLookup() { try { - if (Utils.IS_WINDOWS) { + if (OperatingSystem.isWindows()) { return makeWindowsLookup(); } else { return libLookup(libs -> libs.load(jdkLibraryPath("syslookup"))); @@ -74,24 +72,12 @@ public final class SystemLookup implements SymbolLookup { } private static SymbolLookup makeWindowsLookup() { - @SuppressWarnings("removal") - String systemRoot = AccessController.doPrivileged(new PrivilegedAction<>() { - @Override - public String run() { - return System.getenv("SystemRoot"); - } - }); + String systemRoot = System.getenv("SystemRoot"); Path system32 = Path.of(systemRoot, "System32"); Path ucrtbase = system32.resolve("ucrtbase.dll"); Path msvcrt = system32.resolve("msvcrt.dll"); - @SuppressWarnings("removal") - boolean useUCRT = AccessController.doPrivileged(new PrivilegedAction<>() { - @Override - public Boolean run() { - return Files.exists(ucrtbase); - } - }); + boolean useUCRT = Files.exists(ucrtbase); Path stdLib = useUCRT ? ucrtbase : msvcrt; SymbolLookup lookup = libLookup(libs -> libs.load(stdLib)); @@ -139,8 +125,8 @@ 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(GetPropertyAction.privilegedGetProperty("java.home")); - String lib = Utils.IS_WINDOWS ? "bin" : "lib"; + Path javahome = Path.of(System.getProperty("java.home")); + String lib = OperatingSystem.isWindows() ? "bin" : "lib"; String libname = System.mapLibraryName(name); return javahome.resolve(lib).resolve(libname); } diff --git a/src/java.base/share/classes/jdk/internal/foreign/Utils.java b/src/java.base/share/classes/jdk/internal/foreign/Utils.java index f214d3fb806..3d252dcf233 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/Utils.java +++ b/src/java.base/share/classes/jdk/internal/foreign/Utils.java @@ -48,15 +48,11 @@ import jdk.internal.misc.Unsafe; import jdk.internal.vm.annotation.ForceInline; import sun.invoke.util.Wrapper; -import static sun.security.action.GetPropertyAction.privilegedGetProperty; - /** * This class contains misc helper functions to support creation of memory segments. */ public final class Utils { - public static final boolean IS_WINDOWS = privilegedGetProperty("os.name").startsWith("Windows"); - // Suppresses default constructor, ensuring non-instantiability. private Utils() {} diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/BindingSpecializer.java b/src/java.base/share/classes/jdk/internal/foreign/abi/BindingSpecializer.java index 41d7e3339d5..144af9dac79 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/abi/BindingSpecializer.java +++ b/src/java.base/share/classes/jdk/internal/foreign/abi/BindingSpecializer.java @@ -22,6 +22,7 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + package jdk.internal.foreign.abi; import java.lang.classfile.Annotation; @@ -48,9 +49,6 @@ import jdk.internal.foreign.abi.Binding.ShiftRight; import jdk.internal.foreign.abi.Binding.VMLoad; import jdk.internal.foreign.abi.Binding.VMStore; import jdk.internal.vm.annotation.ForceInline; -import sun.security.action.GetBooleanAction; -import sun.security.action.GetIntegerAction; -import sun.security.action.GetPropertyAction; import java.io.IOException; import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute; @@ -78,11 +76,11 @@ import static jdk.internal.constant.ConstantUtils.*; public class BindingSpecializer { private static final String DUMP_CLASSES_DIR - = GetPropertyAction.privilegedGetProperty("jdk.internal.foreign.abi.Specializer.DUMP_CLASSES_DIR"); + = System.getProperty("jdk.internal.foreign.abi.Specializer.DUMP_CLASSES_DIR"); private static final boolean PERFORM_VERIFICATION - = GetBooleanAction.privilegedGetProperty("jdk.internal.foreign.abi.Specializer.PERFORM_VERIFICATION"); + = Boolean.getBoolean("jdk.internal.foreign.abi.Specializer.PERFORM_VERIFICATION"); private static final int SCOPE_DEDUP_DEPTH - = GetIntegerAction.privilegedGetProperty("jdk.internal.foreign.abi.Specializer.SCOPE_DEDUP_DEPTH", 2); + = Integer.getInteger("jdk.internal.foreign.abi.Specializer.SCOPE_DEDUP_DEPTH", 2); // Bunch of helper constants private static final int CLASSFILE_VERSION = ClassFileFormatVersion.latest().major(); diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/CallingSequenceBuilder.java b/src/java.base/share/classes/jdk/internal/foreign/abi/CallingSequenceBuilder.java index a439df3eb1d..f24de62c807 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/abi/CallingSequenceBuilder.java +++ b/src/java.base/share/classes/jdk/internal/foreign/abi/CallingSequenceBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023, 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 @@ -22,6 +22,7 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + package jdk.internal.foreign.abi; import jdk.internal.foreign.Utils; @@ -38,7 +39,6 @@ import jdk.internal.foreign.abi.Binding.ShiftLeft; import jdk.internal.foreign.abi.Binding.ShiftRight; import jdk.internal.foreign.abi.Binding.VMLoad; import jdk.internal.foreign.abi.Binding.VMStore; -import sun.security.action.GetPropertyAction; import java.lang.foreign.FunctionDescriptor; import java.lang.foreign.MemoryLayout; @@ -54,7 +54,7 @@ import static java.lang.invoke.MethodType.methodType; public class CallingSequenceBuilder { private static final boolean VERIFY_BINDINGS = Boolean.parseBoolean( - GetPropertyAction.privilegedGetProperty("java.lang.foreign.VERIFY_BINDINGS", "true")); + System.getProperty("java.lang.foreign.VERIFY_BINDINGS", "true")); private final ABIDescriptor abi; private final LinkerOptions linkerOptions; diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/CapturableState.java b/src/java.base/share/classes/jdk/internal/foreign/abi/CapturableState.java index 8689751a3c8..7fb62c56bf1 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/abi/CapturableState.java +++ b/src/java.base/share/classes/jdk/internal/foreign/abi/CapturableState.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 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 @@ -22,9 +22,10 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + package jdk.internal.foreign.abi; -import jdk.internal.foreign.Utils; +import jdk.internal.util.OperatingSystem; import java.lang.foreign.MemoryLayout; import java.lang.foreign.StructLayout; @@ -35,8 +36,8 @@ import java.util.stream.Stream; import static java.lang.foreign.ValueLayout.JAVA_INT; public enum CapturableState { - GET_LAST_ERROR ("GetLastError", JAVA_INT, 1 << 0, Utils.IS_WINDOWS), - WSA_GET_LAST_ERROR("WSAGetLastError", JAVA_INT, 1 << 1, Utils.IS_WINDOWS), + GET_LAST_ERROR ("GetLastError", JAVA_INT, 1 << 0, OperatingSystem.isWindows()), + WSA_GET_LAST_ERROR("WSAGetLastError", JAVA_INT, 1 << 1, OperatingSystem.isWindows()), ERRNO ("errno", JAVA_INT, 1 << 2, true); public static final StructLayout LAYOUT = MemoryLayout.structLayout( diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/DowncallLinker.java b/src/java.base/share/classes/jdk/internal/foreign/abi/DowncallLinker.java index c56cca30912..627de9f0765 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/abi/DowncallLinker.java +++ b/src/java.base/share/classes/jdk/internal/foreign/abi/DowncallLinker.java @@ -22,12 +22,12 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ + package jdk.internal.foreign.abi; import jdk.internal.access.JavaLangInvokeAccess; import jdk.internal.access.SharedSecrets; import jdk.internal.invoke.MhUtil; -import sun.security.action.GetPropertyAction; import java.lang.foreign.AddressLayout; import java.lang.foreign.Arena; @@ -52,7 +52,7 @@ import static java.lang.invoke.MethodType.methodType; public class DowncallLinker { private static final boolean USE_SPEC = Boolean.parseBoolean( - GetPropertyAction.privilegedGetProperty("jdk.internal.foreign.DowncallLinker.USE_SPEC", "true")); + System.getProperty("jdk.internal.foreign.DowncallLinker.USE_SPEC", "true")); private static final JavaLangInvokeAccess JLIA = SharedSecrets.getJavaLangInvokeAccess(); diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/UpcallLinker.java b/src/java.base/share/classes/jdk/internal/foreign/abi/UpcallLinker.java index 272cfc3ca4e..486ee8b57c5 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/abi/UpcallLinker.java +++ b/src/java.base/share/classes/jdk/internal/foreign/abi/UpcallLinker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023, 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 @@ -26,7 +26,6 @@ package jdk.internal.foreign.abi; import jdk.internal.foreign.abi.AbstractLinker.UpcallStubFactory; -import sun.security.action.GetPropertyAction; import java.lang.foreign.Arena; import java.lang.foreign.MemorySegment; @@ -43,13 +42,12 @@ import static java.lang.invoke.MethodHandles.exactInvoker; import static java.lang.invoke.MethodHandles.insertArguments; import static java.lang.invoke.MethodHandles.lookup; import static java.lang.invoke.MethodType.methodType; -import static sun.security.action.GetBooleanAction.privilegedGetProperty; public class UpcallLinker { private static final boolean DEBUG = - privilegedGetProperty("jdk.internal.foreign.UpcallLinker.DEBUG"); + Boolean.getBoolean("jdk.internal.foreign.UpcallLinker.DEBUG"); private static final boolean USE_SPEC = Boolean.parseBoolean( - GetPropertyAction.privilegedGetProperty("jdk.internal.foreign.UpcallLinker.USE_SPEC", "true")); + System.getProperty("jdk.internal.foreign.UpcallLinker.USE_SPEC", "true")); private static final MethodHandle MH_invokeInterpBindings; diff --git a/src/java.base/share/classes/jdk/internal/foreign/abi/fallback/LibFallback.java b/src/java.base/share/classes/jdk/internal/foreign/abi/fallback/LibFallback.java index 19682d6c43f..b68b1ed4bad 100644 --- a/src/java.base/share/classes/jdk/internal/foreign/abi/fallback/LibFallback.java +++ b/src/java.base/share/classes/jdk/internal/foreign/abi/fallback/LibFallback.java @@ -36,23 +36,18 @@ final class LibFallback { static final boolean SUPPORTED = tryLoadLibrary(); - @SuppressWarnings({"removal", "restricted"}) + @SuppressWarnings({"restricted"}) private static boolean tryLoadLibrary() { - return java.security.AccessController.doPrivileged( - new java.security.PrivilegedAction<>() { - public Boolean run() { - try { - System.loadLibrary("fallbackLinker"); - } catch (UnsatisfiedLinkError ule) { - return false; - } - if (!init()) { - // library failed to initialize. Do not silently mark as unsupported - throw new ExceptionInInitializerError("Fallback library failed to initialize"); - } - return true; - } - }); + try { + System.loadLibrary("fallbackLinker"); + } catch (UnsatisfiedLinkError ule) { + return false; + } + if (!init()) { + // library failed to initialize. Do not silently mark as unsupported + throw new ExceptionInInitializerError("Fallback library failed to initialize"); + } + return true; } static int defaultABI() { return NativeConstants.DEFAULT_ABI; }