diff --git a/compiler/src/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/StandardGraphBuilderPlugins.java b/compiler/src/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/StandardGraphBuilderPlugins.java index ece66e436219..8a020e627576 100644 --- a/compiler/src/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/StandardGraphBuilderPlugins.java +++ b/compiler/src/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/StandardGraphBuilderPlugins.java @@ -177,7 +177,6 @@ import jdk.vm.ci.meta.SpeculationLog; import jdk.vm.ci.meta.SpeculationLog.Speculation; import jdk.vm.ci.meta.SpeculationLog.SpeculationReason; -import sun.misc.Unsafe; /** * Provides non-runtime specific {@link InvocationPlugin}s. @@ -449,48 +448,60 @@ private static Class getJavaClass(JavaKind kind) { return kind == JavaKind.Object ? Object.class : kind.toJavaClass(); } - private static String getKindName(boolean isSunMiscUnsafe, JavaKind kind) { - return (kind == JavaKind.Object && !isSunMiscUnsafe && !(JavaVersionUtil.JAVA_SPEC == 11)) ? "Reference" : kind.name(); + private static String[] getKindNames(boolean isSunMiscUnsafe, JavaKind kind) { + if (kind == JavaKind.Object && !isSunMiscUnsafe && JavaVersionUtil.JAVA_SPEC >= 17) { + /* + * JDK 17 renamed all Object-type-related methods in jdk.internal.misc.Unsafe from + * "Object" to "Reference", but kept the "Object" version as deprecated. We want to + * intrinsify both variants, to avoid problems with Uninterruptible methods in Native + * Image. + */ + return new String[]{"Object", "Reference"}; + } else { + return new String[]{kind.name()}; + } } private static void registerUnsafePlugins(InvocationPlugins plugins, Replacements replacements, boolean explicitUnsafeNullChecks) { - registerUnsafePlugins0(new Registration(plugins, Unsafe.class), true, explicitUnsafeNullChecks); + Registration sunMiscUnsafe = new Registration(plugins, "sun.misc.Unsafe"); + registerUnsafePlugins0(sunMiscUnsafe, true, explicitUnsafeNullChecks); JavaKind[] supportedJavaKinds = {JavaKind.Int, JavaKind.Long, JavaKind.Object}; - registerUnsafeGetAndOpPlugins(new Registration(plugins, Unsafe.class), explicitUnsafeNullChecks, supportedJavaKinds, "Object"); - registerUnsafeAtomicsPlugins(new Registration(plugins, Unsafe.class), true, explicitUnsafeNullChecks, "compareAndSwap", new String[]{""}, + registerUnsafeGetAndOpPlugins(sunMiscUnsafe, true, explicitUnsafeNullChecks, supportedJavaKinds); + registerUnsafeAtomicsPlugins(sunMiscUnsafe, true, explicitUnsafeNullChecks, "compareAndSwap", new String[]{""}, supportedJavaKinds); - Registration r = new Registration(plugins, "jdk.internal.misc.Unsafe", replacements); + Registration jdkInternalMiscUnsafe = new Registration(plugins, "jdk.internal.misc.Unsafe", replacements); - registerUnsafePlugins0(r, false, explicitUnsafeNullChecks); - registerUnsafeUnalignedPlugins(r, explicitUnsafeNullChecks); + registerUnsafePlugins0(jdkInternalMiscUnsafe, false, explicitUnsafeNullChecks); + registerUnsafeUnalignedPlugins(jdkInternalMiscUnsafe, explicitUnsafeNullChecks); supportedJavaKinds = new JavaKind[]{JavaKind.Boolean, JavaKind.Byte, JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long, JavaKind.Object}; - registerUnsafeGetAndOpPlugins(r, explicitUnsafeNullChecks, supportedJavaKinds, JavaVersionUtil.JAVA_SPEC > 11 ? "Reference" : "Object"); - registerUnsafeAtomicsPlugins(r, false, explicitUnsafeNullChecks, "weakCompareAndSet", new String[]{"", "Acquire", "Release", "Plain"}, supportedJavaKinds); - registerUnsafeAtomicsPlugins(r, false, explicitUnsafeNullChecks, "compareAndExchange", new String[]{"Acquire", "Release"}, supportedJavaKinds); + registerUnsafeGetAndOpPlugins(jdkInternalMiscUnsafe, false, explicitUnsafeNullChecks, supportedJavaKinds); + registerUnsafeAtomicsPlugins(jdkInternalMiscUnsafe, false, explicitUnsafeNullChecks, "weakCompareAndSet", new String[]{"", "Acquire", "Release", "Plain"}, supportedJavaKinds); + registerUnsafeAtomicsPlugins(jdkInternalMiscUnsafe, false, explicitUnsafeNullChecks, "compareAndExchange", new String[]{"Acquire", "Release"}, supportedJavaKinds); supportedJavaKinds = new JavaKind[]{JavaKind.Boolean, JavaKind.Byte, JavaKind.Char, JavaKind.Short, JavaKind.Int, JavaKind.Long, JavaKind.Float, JavaKind.Double, JavaKind.Object}; - registerUnsafeAtomicsPlugins(r, false, explicitUnsafeNullChecks, "compareAndSet", new String[]{""}, supportedJavaKinds); - registerUnsafeAtomicsPlugins(r, false, explicitUnsafeNullChecks, "compareAndExchange", new String[]{""}, supportedJavaKinds); + registerUnsafeAtomicsPlugins(jdkInternalMiscUnsafe, false, explicitUnsafeNullChecks, "compareAndSet", new String[]{""}, supportedJavaKinds); + registerUnsafeAtomicsPlugins(jdkInternalMiscUnsafe, false, explicitUnsafeNullChecks, "compareAndExchange", new String[]{""}, supportedJavaKinds); } private static void registerUnsafeAtomicsPlugins(Registration r, boolean isSunMiscUnsafe, boolean explicitUnsafeNullChecks, String casPrefix, String[] memoryOrders, JavaKind[] supportedJavaKinds) { for (JavaKind kind : supportedJavaKinds) { Class javaClass = getJavaClass(kind); - String kindName = getKindName(isSunMiscUnsafe, kind); - boolean isLogic = true; - JavaKind returnKind = JavaKind.Boolean.getStackKind(); - if (casPrefix.startsWith("compareAndExchange")) { - isLogic = false; - returnKind = kind.isNumericInteger() ? kind.getStackKind() : kind; - } - for (String memoryOrderString : memoryOrders) { - MemoryOrderMode memoryOrder = memoryOrderString.equals("") ? MemoryOrderMode.VOLATILE : MemoryOrderMode.valueOf(memoryOrderString.toUpperCase()); - r.register(new UnsafeCompareAndSwapPlugin(returnKind, kind, memoryOrder, isLogic, explicitUnsafeNullChecks, - casPrefix + kindName + memoryOrderString, Receiver.class, Object.class, long.class, javaClass, javaClass)); + for (String kindName : getKindNames(isSunMiscUnsafe, kind)) { + boolean isLogic = true; + JavaKind returnKind = JavaKind.Boolean.getStackKind(); + if (casPrefix.startsWith("compareAndExchange")) { + isLogic = false; + returnKind = kind.isNumericInteger() ? kind.getStackKind() : kind; + } + for (String memoryOrderString : memoryOrders) { + MemoryOrderMode memoryOrder = memoryOrderString.equals("") ? MemoryOrderMode.VOLATILE : MemoryOrderMode.valueOf(memoryOrderString.toUpperCase()); + r.register(new UnsafeCompareAndSwapPlugin(returnKind, kind, memoryOrder, isLogic, explicitUnsafeNullChecks, + casPrefix + kindName + memoryOrderString, Receiver.class, Object.class, long.class, javaClass, javaClass)); + } } } } @@ -513,30 +524,31 @@ public boolean isOptional() { } } - private static void registerUnsafeGetAndOpPlugins(Registration r, boolean explicitUnsafeNullChecks, JavaKind[] unsafeJavaKinds, String objectKindName) { + private static void registerUnsafeGetAndOpPlugins(Registration r, boolean isSunMiscUnsafe, boolean explicitUnsafeNullChecks, JavaKind[] unsafeJavaKinds) { for (JavaKind kind : unsafeJavaKinds) { Class javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); - String kindName = kind == JavaKind.Object ? objectKindName : kind.name(); - r.register(new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks, "getAndSet" + kindName, Receiver.class, Object.class, long.class, javaClass) { - @Override - public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) { - // Emits a null-check for the otherwise unused receiver - unsafe.get(); - createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndWriteNode(obj, offset, value, kind, loc)); - return true; - } - }); - - if (kind != JavaKind.Boolean && kind.isNumericInteger()) { - r.register(new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks, "getAndAdd" + kindName, Receiver.class, Object.class, long.class, javaClass) { + for (String kindName : getKindNames(isSunMiscUnsafe, kind)) { + r.register(new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks, "getAndSet" + kindName, Receiver.class, Object.class, long.class, javaClass) { @Override - public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) { + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode value) { // Emits a null-check for the otherwise unused receiver unsafe.get(); - createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndAddNode(obj, offset, delta, kind, loc)); + createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndWriteNode(obj, offset, value, kind, loc)); return true; } }); + + if (kind != JavaKind.Boolean && kind.isNumericInteger()) { + r.register(new UnsafeAccessPlugin(kind, explicitUnsafeNullChecks, "getAndAdd" + kindName, Receiver.class, Object.class, long.class, javaClass) { + @Override + public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver unsafe, ValueNode object, ValueNode offset, ValueNode delta) { + // Emits a null-check for the otherwise unused receiver + unsafe.get(); + createUnsafeAccess(object, b, (obj, loc) -> new AtomicReadAndAddNode(obj, offset, delta, kind, loc)); + return true; + } + }); + } } } } @@ -545,35 +557,36 @@ private static void registerUnsafePlugins0(Registration r, boolean sunMiscUnsafe for (JavaKind kind : JavaKind.values()) { if ((kind.isPrimitive() && kind != JavaKind.Void) || kind == JavaKind.Object) { Class javaClass = kind == JavaKind.Object ? Object.class : kind.toJavaClass(); - String kindName = getKindName(sunMiscUnsafe, kind); - String getName = "get" + kindName; - String putName = "put" + kindName; - // Object-based accesses - r.register(new UnsafeGetPlugin(kind, explicitUnsafeNullChecks, getName, Receiver.class, Object.class, long.class)); - r.register(new UnsafePutPlugin(kind, explicitUnsafeNullChecks, putName, Receiver.class, Object.class, long.class, javaClass)); - // Volatile object-based accesses - r.register(new UnsafeGetPlugin(kind, MemoryOrderMode.VOLATILE, explicitUnsafeNullChecks, getName + "Volatile", Receiver.class, Object.class, long.class)); - r.register(new UnsafePutPlugin(kind, MemoryOrderMode.VOLATILE, explicitUnsafeNullChecks, putName + "Volatile", Receiver.class, Object.class, long.class, javaClass)); - // Ordered object-based accesses - if (sunMiscUnsafe) { - if (kind == JavaKind.Int || kind == JavaKind.Long || kind == JavaKind.Object) { + for (String kindName : getKindNames(sunMiscUnsafe, kind)) { + String getName = "get" + kindName; + String putName = "put" + kindName; + // Object-based accesses + r.register(new UnsafeGetPlugin(kind, explicitUnsafeNullChecks, getName, Receiver.class, Object.class, long.class)); + r.register(new UnsafePutPlugin(kind, explicitUnsafeNullChecks, putName, Receiver.class, Object.class, long.class, javaClass)); + // Volatile object-based accesses + r.register(new UnsafeGetPlugin(kind, MemoryOrderMode.VOLATILE, explicitUnsafeNullChecks, getName + "Volatile", Receiver.class, Object.class, long.class)); + r.register(new UnsafePutPlugin(kind, MemoryOrderMode.VOLATILE, explicitUnsafeNullChecks, putName + "Volatile", Receiver.class, Object.class, long.class, javaClass)); + // Ordered object-based accesses + if (sunMiscUnsafe) { + if (kind == JavaKind.Int || kind == JavaKind.Long || kind == JavaKind.Object) { + r.register(new UnsafePutPlugin(kind, MemoryOrderMode.RELEASE, explicitUnsafeNullChecks, + "putOrdered" + kindName, Receiver.class, Object.class, long.class, javaClass)); + } + } else { r.register(new UnsafePutPlugin(kind, MemoryOrderMode.RELEASE, explicitUnsafeNullChecks, - "putOrdered" + kindName, Receiver.class, Object.class, long.class, javaClass)); + "put" + kindName + "Release", Receiver.class, Object.class, long.class, javaClass)); + r.register(new UnsafeGetPlugin(kind, MemoryOrderMode.ACQUIRE, explicitUnsafeNullChecks, + "get" + kindName + "Acquire", Receiver.class, Object.class, long.class)); + r.register(new UnsafePutPlugin(kind, MemoryOrderMode.OPAQUE, explicitUnsafeNullChecks, + "put" + kindName + "Opaque", Receiver.class, Object.class, long.class, javaClass)); + r.register(new UnsafeGetPlugin(kind, MemoryOrderMode.OPAQUE, explicitUnsafeNullChecks, + "get" + kindName + "Opaque", Receiver.class, Object.class, long.class)); + } + if (kind != JavaKind.Boolean && kind != JavaKind.Object) { + // Raw accesses to memory addresses + r.register(new UnsafeGetPlugin(kind, explicitUnsafeNullChecks, getName, Receiver.class, long.class)); + r.register(new UnsafePutPlugin(kind, explicitUnsafeNullChecks, putName, Receiver.class, long.class, kind.toJavaClass())); } - } else { - r.register(new UnsafePutPlugin(kind, MemoryOrderMode.RELEASE, explicitUnsafeNullChecks, - "put" + kindName + "Release", Receiver.class, Object.class, long.class, javaClass)); - r.register(new UnsafeGetPlugin(kind, MemoryOrderMode.ACQUIRE, explicitUnsafeNullChecks, - "get" + kindName + "Acquire", Receiver.class, Object.class, long.class)); - r.register(new UnsafePutPlugin(kind, MemoryOrderMode.OPAQUE, explicitUnsafeNullChecks, - "put" + kindName + "Opaque", Receiver.class, Object.class, long.class, javaClass)); - r.register(new UnsafeGetPlugin(kind, MemoryOrderMode.OPAQUE, explicitUnsafeNullChecks, - "get" + kindName + "Opaque", Receiver.class, Object.class, long.class)); - } - if (kind != JavaKind.Boolean && kind != JavaKind.Object) { - // Raw accesses to memory addresses - r.register(new UnsafeGetPlugin(kind, explicitUnsafeNullChecks, getName, Receiver.class, long.class)); - r.register(new UnsafePutPlugin(kind, explicitUnsafeNullChecks, putName, Receiver.class, long.class, kind.toJavaClass())); } } } diff --git a/compiler/src/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalUnsafeAccess.java b/compiler/src/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalUnsafeAccess.java index ef6cf9d0c5df..3658842638c3 100644 --- a/compiler/src/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalUnsafeAccess.java +++ b/compiler/src/org.graalvm.compiler.serviceprovider/src/org/graalvm/compiler/serviceprovider/GraalUnsafeAccess.java @@ -67,14 +67,4 @@ public static Unsafe getUnsafe() { } return UNSAFE; } - - @SuppressWarnings("deprecation") // deprecated since JDK 15 - public static boolean shouldBeInitialized(Class c) { - return UNSAFE.shouldBeInitialized(c); - } - - @SuppressWarnings("deprecation") // deprecated since JDK 15 - public static void ensureClassInitialized(Class c) { - UNSAFE.ensureClassInitialized(c); - } } diff --git a/compiler/src/org.graalvm.compiler.word/src/org/graalvm/compiler/word/Word.java b/compiler/src/org.graalvm.compiler.word/src/org/graalvm/compiler/word/Word.java index b30bbefc8411..5ccdec011370 100644 --- a/compiler/src/org.graalvm.compiler.word/src/org/graalvm/compiler/word/Word.java +++ b/compiler/src/org.graalvm.compiler.word/src/org/graalvm/compiler/word/Word.java @@ -24,8 +24,6 @@ */ package org.graalvm.compiler.word; -import static org.graalvm.compiler.serviceprovider.GraalUnsafeAccess.getUnsafe; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -50,6 +48,7 @@ import org.graalvm.compiler.nodes.calc.XorNode; import org.graalvm.compiler.nodes.memory.OnHeapMemoryAccess.BarrierType; import org.graalvm.compiler.nodes.memory.address.AddressNode.Address; +import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.word.ComparableWord; import org.graalvm.word.LocationIdentity; import org.graalvm.word.Pointer; @@ -63,7 +62,7 @@ public abstract class Word implements SignedWord, UnsignedWord, Pointer { - private static final Unsafe UNSAFE = getUnsafe(); + private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); static { BoxFactoryImpl.initialize(); diff --git a/substratevm/mx.substratevm/suite.py b/substratevm/mx.substratevm/suite.py index 5ebeceeda955..f35a483e8e35 100644 --- a/substratevm/mx.substratevm/suite.py +++ b/substratevm/mx.substratevm/suite.py @@ -424,10 +424,10 @@ "java.xml.crypto", "jdk.jfr", "jdk.management", - "jdk.unsupported", ], "requiresConcealed" : { "java.base" : [ + "jdk.internal.misc", "jdk.internal.org.objectweb.asm", "sun.reflect.annotation", "sun.security.jca", @@ -604,9 +604,11 @@ "dependencies": [ "com.oracle.svm.hosted", ], - "requires" : [ - "jdk.unsupported", - ], + "requiresConcealed" : { + "java.base" : [ + "jdk.internal.misc", + ], + }, "checkstyle": "com.oracle.svm.core", "workingSets": "SVM", "annotationProcessors": [ @@ -665,12 +667,12 @@ "SVM", ], "requires": [ - "jdk.unsupported", "java.compiler", "jdk.jfr", ], - "requiresConcealed": { - "java.base": [ + "requiresConcealed" : { + "java.base" : [ + "jdk.internal.misc", "sun.security.jca", ], }, @@ -769,11 +771,9 @@ "dependencies" : [ "compiler:GRAAL" ], - "requires" : [ - "jdk.unsupported", - ], "requiresConcealed" : { "java.base" : [ + "jdk.internal.misc", "jdk.internal.ref", "sun.nio.ch", ], @@ -792,10 +792,10 @@ "com.oracle.svm.hosted", "truffle:TRUFFLE_API", ], - "requires" : [ - "jdk.unsupported", - ], "requiresConcealed" : { + "java.base" : [ + "jdk.internal.misc", + ], "jdk.internal.vm.ci" : [ "jdk.vm.ci.runtime", ], @@ -862,9 +862,11 @@ "dependencies": [ "com.oracle.svm.graal", ], - "requires" : [ - "jdk.unsupported", - ], + "requiresConcealed" : { + "java.base" : [ + "jdk.internal.misc", + ], + }, "checkstyle": "com.oracle.svm.hosted", "javaCompliance": "11+", "annotationProcessors": [ @@ -963,9 +965,11 @@ "com.oracle.svm.graal", "compiler:GRAAL" ], - "requires" : [ - "jdk.unsupported", - ], + "requiresConcealed" : { + "java.base" : [ + "jdk.internal.misc", + ], + }, "checkstyle" : "com.oracle.svm.hosted", "javaCompliance": "11+", "annotationProcessors": [ @@ -1276,7 +1280,6 @@ "com.oracle.objectfile.macho", ], - "requires" : ["jdk.unsupported"], "requiresConcealed" : { "java.base" : [ "sun.nio.ch", diff --git a/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java b/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java index 0b8fbf7edafb..81ad124cbd12 100644 --- a/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java +++ b/substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java @@ -25,8 +25,6 @@ package com.oracle.objectfile; import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.FileChannel; @@ -53,7 +51,6 @@ import com.oracle.objectfile.macho.MachOObjectFile; import com.oracle.objectfile.pecoff.PECoffObjectFile; -import sun.misc.Unsafe; import sun.nio.ch.DirectBuffer; /** @@ -1275,46 +1272,14 @@ public final void write(FileChannel outputChannel) { try { writeBuffer(sortedObjectFileElements, buffer); } finally { - cleanBuffer(buffer); // unmap immediately + // unmap immediately + ((DirectBuffer) buffer).cleaner().clean(); } - } catch (IOException | ReflectiveOperationException e) { + } catch (IOException e) { throw new RuntimeException(e); } } - private static void cleanBuffer(ByteBuffer buffer) throws ReflectiveOperationException { - try { - /* - * Trying to use sun.misc.Unsafe.invokeCleaner as the first approach restores forward - * compatibility to Java > 8. If it fails we know we are on Java 8 where we can use a - * non-forward compatible way of forcing to clean the ByteBuffer. - */ - Method invokeCleanerMethod = Unsafe.class.getMethod("invokeCleaner", ByteBuffer.class); - invokeCleanerMethod.invoke(UNSAFE, buffer); - } catch (NoSuchMethodException e) { - /* On Java 8 we have to use the non-forward compatible approach. */ - ((DirectBuffer) buffer).cleaner().clean(); - } - } - - private static final Unsafe UNSAFE = initUnsafe(); - - @SuppressWarnings("restriction") - private static Unsafe initUnsafe() { - try { - return Unsafe.getUnsafe(); - } catch (SecurityException se) { - Field theUnsafe = null; - try { - theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); - theUnsafe.setAccessible(true); - return (Unsafe) theUnsafe.get(null); - } catch (ReflectiveOperationException e) { - throw new RuntimeException("exception while trying to get Unsafe", e); - } - } - } - /* * We keep track of what build dependencies have been created, so that the factory in * BuildDependency can query for duplicates. This logic is package-access: it is not needed by diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java index 46cb7fbf2dda..b195673ca765 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SubstrateOptions.java @@ -45,7 +45,6 @@ import org.graalvm.compiler.options.OptionStability; import org.graalvm.compiler.options.OptionType; import org.graalvm.compiler.options.OptionValues; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.ImageInfo; import org.graalvm.nativeimage.ImageSingletons; @@ -61,6 +60,8 @@ import com.oracle.svm.core.thread.VMOperationControl; import com.oracle.svm.core.util.UserError; +import jdk.internal.misc.Unsafe; + public class SubstrateOptions { @Option(help = "When true, compiler graphs are parsed only once before static analysis. When false, compiler graphs are parsed for static analysis and again for AOT compilation.")// @@ -694,21 +695,20 @@ public ReportingSupport(String reportingPath) { public static int getPageSize() { int value = PageSize.getValue(); if (value == 0) { - return hostPageSize; + try { + /* + * On JDK 17 and later, this is just a final field access that can never fail. But + * on JDK 11, it is a native method call with some corner cases that can throw an + * exception. + */ + return Unsafe.getUnsafe().pageSize(); + } catch (IllegalArgumentException e) { + return 4096; + } } return value; } - private static int hostPageSize = getHostPageSize(); - - private static int getHostPageSize() { - try { - return GraalUnsafeAccess.getUnsafe().pageSize(); - } catch (IllegalArgumentException e) { - return 4096; - } - } - @Option(help = "Specifies how many details are printed for certain diagnostic thunks, e.g.: 'DumpThreads:1,DumpRegisters:2'. " + "A value of 1 will result in the maximum amount of information, higher values will print less information. " + "By default, the most detailed output is enabled for all diagnostic thunks. Wildcards (*) are supported in the name of the diagnostic thunk.", type = Expert)// diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/classinitialization/ClassInitializationInfo.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/classinitialization/ClassInitializationInfo.java index f83090fc3283..c09927544068 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/classinitialization/ClassInitializationInfo.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/classinitialization/ClassInitializationInfo.java @@ -27,7 +27,6 @@ import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.Platforms; import org.graalvm.nativeimage.c.function.CFunctionPointer; @@ -39,7 +38,7 @@ import com.oracle.svm.core.snippets.SubstrateForeignCallTarget; import com.oracle.svm.core.util.VMError; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * Information about the runtime class initialization state of a {@link DynamicHub class}, and @@ -53,8 +52,6 @@ @InternalVMMethod public final class ClassInitializationInfo { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - /** * Singleton for classes that are already initialized during image building and do not need * class initialization at runtime, but have {@code } methods. @@ -344,7 +341,7 @@ private void setInitializationStateAndNotify(InitState state) { this.initState = state; this.initThread = null; /* Make sure previous stores are all done, notably the initState. */ - UNSAFE.storeFence(); + Unsafe.getUnsafe().storeFence(); if (initCondition != null) { initCondition.signalAll(); diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/deopt/Deoptimizer.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/deopt/Deoptimizer.java index dcfbec9aaf8f..95528763ec5d 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/deopt/Deoptimizer.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/deopt/Deoptimizer.java @@ -34,7 +34,6 @@ import org.graalvm.compiler.core.common.util.TypeConversion; import org.graalvm.compiler.options.Option; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.compiler.word.BarrieredAccess; import org.graalvm.compiler.word.Word; import org.graalvm.nativeimage.CurrentIsolate; @@ -85,13 +84,13 @@ import com.oracle.svm.core.util.RingBuffer; import com.oracle.svm.core.util.VMError; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.code.InstalledCode; import jdk.vm.ci.meta.DeoptimizationAction; import jdk.vm.ci.meta.DeoptimizationReason; import jdk.vm.ci.meta.JavaConstant; import jdk.vm.ci.meta.JavaKind; import jdk.vm.ci.meta.SpeculationLog.SpeculationReason; -import sun.misc.Unsafe; /** * Performs deoptimization. The method to deoptimize (= the source method) is either a specialized @@ -149,8 +148,6 @@ */ public final class Deoptimizer { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - private static final RingBuffer recentDeoptimizationEvents = new RingBuffer<>(); private static final int actionShift = 0; @@ -962,7 +959,7 @@ private Object materializeObject(int virtualObjectId, FrameInfoQueryResult sourc curIdx = 2; } else { try { - obj = UNSAFE.allocateInstance(DynamicHub.toClass(hub)); + obj = Unsafe.getUnsafe().allocateInstance(DynamicHub.toClass(hub)); } catch (InstantiationException ex) { throw VMError.shouldNotReachHere(ex); } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/jdk/SubstrateObjectCloneSnippets.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/jdk/SubstrateObjectCloneSnippets.java index 850dad950d1c..360b22e160b4 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/jdk/SubstrateObjectCloneSnippets.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/jdk/SubstrateObjectCloneSnippets.java @@ -46,7 +46,6 @@ import org.graalvm.compiler.replacements.SnippetTemplate.Arguments; import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo; import org.graalvm.compiler.replacements.Snippets; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.compiler.word.BarrieredAccess; import org.graalvm.word.LocationIdentity; import org.graalvm.word.WordFactory; @@ -67,6 +66,8 @@ import com.oracle.svm.core.snippets.SubstrateForeignCallTarget; import com.oracle.svm.core.util.NonmovableByteArrayReader; +import jdk.internal.misc.Unsafe; + public final class SubstrateObjectCloneSnippets extends SubstrateTemplates implements Snippets { private static final SubstrateForeignCallDescriptor CLONE = SnippetRuntime.findForeignCall(SubstrateObjectCloneSnippets.class, "doClone", true, LocationIdentity.any()); private static final SubstrateForeignCallDescriptor[] FOREIGN_CALLS = new SubstrateForeignCallDescriptor[]{CLONE}; @@ -95,8 +96,7 @@ private static Object doClone(Object original) throws CloneNotSupportedException } return newArray; } else { - sun.misc.Unsafe unsafe = GraalUnsafeAccess.getUnsafe(); - Object result = unsafe.allocateInstance(DynamicHub.toClass(hub)); + Object result = Unsafe.getUnsafe().allocateInstance(DynamicHub.toClass(hub)); int firstFieldOffset = ConfigurationValues.getObjectLayout().getFirstFieldOffset(); int curOffset = firstFieldOffset; diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CEntryPointSnippets.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CEntryPointSnippets.java index b779168e7481..486c32146ca6 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CEntryPointSnippets.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/graal/snippets/CEntryPointSnippets.java @@ -50,7 +50,6 @@ import org.graalvm.compiler.replacements.SnippetTemplate.Arguments; import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo; import org.graalvm.compiler.replacements.Snippets; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.compiler.word.Word; import org.graalvm.nativeimage.CurrentIsolate; import org.graalvm.nativeimage.ImageSingletons; @@ -104,7 +103,7 @@ import com.oracle.svm.core.thread.VMThreads.SafepointBehavior; import com.oracle.svm.core.util.VMError; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * Snippets for calling from C to Java. See {@link CEntryPointActions} and @@ -255,14 +254,13 @@ private static int initializeIsolate(CEntryPointCreateIsolateParameters paramete boolean firstIsolate = false; final long initStateAddr = FIRST_ISOLATE_INIT_STATE.get().rawValue(); - final Unsafe unsafe = GraalUnsafeAccess.getUnsafe(); - int state = unsafe.getInt(initStateAddr); + int state = Unsafe.getUnsafe().getInt(initStateAddr); if (state != FirstIsolateInitStates.SUCCESSFUL) { - firstIsolate = unsafe.compareAndSwapInt(null, initStateAddr, FirstIsolateInitStates.UNINITIALIZED, FirstIsolateInitStates.IN_PROGRESS); + firstIsolate = Unsafe.getUnsafe().compareAndSetInt(null, initStateAddr, FirstIsolateInitStates.UNINITIALIZED, FirstIsolateInitStates.IN_PROGRESS); if (!firstIsolate) { while (state == FirstIsolateInitStates.IN_PROGRESS) { // spin-wait for first isolate PauseNode.pause(); - state = unsafe.getIntVolatile(null, initStateAddr); + state = Unsafe.getUnsafe().getIntVolatile(null, initStateAddr); } if (state == FirstIsolateInitStates.FAILED) { return CEntryPointErrors.ISOLATE_INITIALIZATION_FAILED; @@ -304,7 +302,7 @@ private static int initializeIsolate(CEntryPointCreateIsolateParameters paramete boolean success = PlatformNativeLibrarySupport.singleton().initializeBuiltinLibraries(); if (firstIsolate) { // let other isolates (if any) initialize now state = success ? FirstIsolateInitStates.SUCCESSFUL : FirstIsolateInitStates.FAILED; - unsafe.putIntVolatile(null, initStateAddr, state); + Unsafe.getUnsafe().putIntVolatile(null, initStateAddr, state); } if (!success) { diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/handles/ObjectHandlesImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/handles/ObjectHandlesImpl.java index 9cf9706e3af7..eb22f645ea6c 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/handles/ObjectHandlesImpl.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/handles/ObjectHandlesImpl.java @@ -26,14 +26,13 @@ import java.lang.ref.WeakReference; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.ObjectHandle; import org.graalvm.nativeimage.ObjectHandles; import org.graalvm.word.SignedWord; import org.graalvm.word.WordBase; import org.graalvm.word.WordFactory; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * This class implements {@link ObjectHandle word}-sized integer handles that refer to Java objects. @@ -53,7 +52,6 @@ * significant role in how indexing is implemented. */ public final class ObjectHandlesImpl implements ObjectHandles { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); /** Private subclass to distinguish from regular handles to {@link WeakReference} objects. */ private static final class HandleWeakReference extends WeakReference { @@ -127,14 +125,14 @@ private static int getIndexInBucket(long index) { } private static long getObjectArrayByteOffset(int index) { - return UNSAFE.arrayBaseOffset(Object[].class) + index * UNSAFE.arrayIndexScale(Object[].class); + return Unsafe.getUnsafe().arrayBaseOffset(Object[].class) + index * Unsafe.getUnsafe().arrayIndexScale(Object[].class); } private Object[] getBucket(int bucketIndex) { // buckets[i] is changed only once from null to its final value: try without volatile first Object[] bucket = buckets[bucketIndex]; if (bucket == null) { - bucket = (Object[]) UNSAFE.getObjectVolatile(buckets, getObjectArrayByteOffset(bucketIndex)); + bucket = (Object[]) Unsafe.getUnsafe().getObjectVolatile(buckets, getObjectArrayByteOffset(bucketIndex)); } return bucket; } @@ -163,7 +161,7 @@ public ObjectHandle create(Object obj) { for (;;) { while (indexInBucket < bucket.length) { if (bucket[indexInBucket] == null) { - if (UNSAFE.compareAndSwapObject(bucket, getObjectArrayByteOffset(indexInBucket), null, obj)) { + if (Unsafe.getUnsafe().compareAndSetObject(bucket, getObjectArrayByteOffset(indexInBucket), null, obj)) { int newSearchIndexInBucket = (indexInBucket + 1 < bucket.length) ? (indexInBucket + 1) : indexInBucket; unusedHandleSearchIndex = toIndex(bucketIndex, newSearchIndexInBucket); // (if the next index is in another bucket, we let the next create() @@ -190,8 +188,8 @@ public ObjectHandle create(Object obj) { newBucketCapacity = getIndexInBucket(maxIndex) + 1; } Object[] newBucket = new Object[newBucketCapacity]; - UNSAFE.putObjectVolatile(newBucket, getObjectArrayByteOffset(0), obj); - if (UNSAFE.compareAndSwapObject(buckets, getObjectArrayByteOffset(newBucketIndex), null, newBucket)) { + Unsafe.getUnsafe().putObjectVolatile(newBucket, getObjectArrayByteOffset(0), obj); + if (Unsafe.getUnsafe().compareAndSetObject(buckets, getObjectArrayByteOffset(newBucketIndex), null, newBucket)) { unusedHandleSearchIndex = toIndex(newBucketIndex, 1); return toHandle(newBucketIndex, 0); } @@ -239,7 +237,7 @@ private Object doGet(ObjectHandle handle) { throw new IllegalArgumentException("Invalid handle"); } int indexInBucket = getIndexInBucket(index); - return UNSAFE.getObjectVolatile(bucket, getObjectArrayByteOffset(indexInBucket)); + return Unsafe.getUnsafe().getObjectVolatile(bucket, getObjectArrayByteOffset(indexInBucket)); } public boolean isWeak(ObjectHandle handle) { @@ -260,7 +258,7 @@ public void destroy(ObjectHandle handle) { throw new IllegalArgumentException("Invalid handle"); } int indexInBucket = getIndexInBucket(index); - UNSAFE.putOrderedObject(bucket, getObjectArrayByteOffset(indexInBucket), null); + Unsafe.getUnsafe().putObjectRelease(bucket, getObjectArrayByteOffset(indexInBucket), null); } public void destroyWeak(ObjectHandle handle) { diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/identityhashcode/IdentityHashCodeSupport.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/identityhashcode/IdentityHashCodeSupport.java index a485c74e8b89..d2d2a573a466 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/identityhashcode/IdentityHashCodeSupport.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/identityhashcode/IdentityHashCodeSupport.java @@ -30,7 +30,6 @@ import org.graalvm.compiler.options.OptionValues; import org.graalvm.compiler.phases.util.Providers; import org.graalvm.compiler.replacements.IdentityHashCodeSnippets; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.compiler.word.ObjectAccess; import org.graalvm.word.LocationIdentity; @@ -40,6 +39,8 @@ import com.oracle.svm.core.threadlocal.FastThreadLocalObject; import com.oracle.svm.core.util.VMError; +import jdk.internal.misc.Unsafe; + public final class IdentityHashCodeSupport { public static final LocationIdentity IDENTITY_HASHCODE_LOCATION = NamedLocationIdentity.mutable("identityHashCode"); @@ -62,7 +63,7 @@ public static int generateIdentityHashCode(Object obj) { // generate a new hashcode and try to store it into the object int newHashCode = generateHashCode(); - if (!GraalUnsafeAccess.getUnsafe().compareAndSwapInt(obj, ConfigurationValues.getObjectLayout().getIdentityHashCodeOffset(), 0, newHashCode)) { + if (!Unsafe.getUnsafe().compareAndSetInt(obj, ConfigurationValues.getObjectLayout().getIdentityHashCodeOffset(), 0, newHashCode)) { newHashCode = ObjectAccess.readInt(obj, ConfigurationValues.getObjectLayout().getIdentityHashCodeOffset(), IDENTITY_HASHCODE_LOCATION); } VMError.guarantee(newHashCode != 0, "Missing identity hash code"); diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RecomputedFields.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RecomputedFields.java index c59e8bfffff5..4d7a5f28fbe7 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RecomputedFields.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/RecomputedFields.java @@ -44,7 +44,6 @@ import java.util.concurrent.locks.ReentrantLock; import java.util.function.Consumer; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.hosted.Feature; import org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport; @@ -61,6 +60,8 @@ import com.oracle.svm.core.util.VMError; import com.oracle.svm.util.ReflectionUtil; +import jdk.internal.misc.Unsafe; + /* * This file contains JDK fields that need to be intercepted because their value in the hosted environment is not * suitable for the Substrate VM. The list is derived from the intercepted fields of the Maxine VM. @@ -116,7 +117,7 @@ final class Target_java_util_concurrent_atomic_AtomicReferenceFieldUpdater_Atomi this.cclass = tclass; this.tclass = tclass; this.vclass = vclass; - this.offset = GraalUnsafeAccess.getUnsafe().objectFieldOffset(field); + this.offset = Unsafe.getUnsafe().objectFieldOffset(field); } } @@ -153,7 +154,7 @@ final class Target_java_util_concurrent_atomic_AtomicIntegerFieldUpdater_AtomicI // access checks are disabled this.cclass = tclass; this.tclass = tclass; - this.offset = GraalUnsafeAccess.getUnsafe().objectFieldOffset(field); + this.offset = Unsafe.getUnsafe().objectFieldOffset(field); } } @@ -190,7 +191,7 @@ final class Target_java_util_concurrent_atomic_AtomicLongFieldUpdater_CASUpdater // access checks are disabled this.cclass = tclass; this.tclass = tclass; - this.offset = GraalUnsafeAccess.getUnsafe().objectFieldOffset(field); + this.offset = Unsafe.getUnsafe().objectFieldOffset(field); } } @@ -228,7 +229,7 @@ final class Target_java_util_concurrent_atomic_AtomicLongFieldUpdater_LockedUpda // access checks are disabled this.cclass = tclass; this.tclass = tclass; - this.offset = GraalUnsafeAccess.getUnsafe().objectFieldOffset(field); + this.offset = Unsafe.getUnsafe().objectFieldOffset(field); } } @@ -281,7 +282,7 @@ private void processFieldUpdater(Object updater) { // search the declared fields for a field with a matching offset for (Field f : tclass.getDeclaredFields()) { if (!Modifier.isStatic(f.getModifiers())) { - long fieldOffset = GraalUnsafeAccess.getUnsafe().objectFieldOffset(f); + long fieldOffset = Unsafe.getUnsafe().objectFieldOffset(f); if (fieldOffset == searchOffset) { markAsUnsafeAccessed.accept(f); return; diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_misc_VM.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_misc_VM.java index 7891ef498b24..1edc96b02493 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_misc_VM.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_jdk_internal_misc_VM.java @@ -26,7 +26,6 @@ import java.util.Map; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.ImageSingletons; import com.oracle.svm.core.SubstrateOptions; @@ -40,6 +39,8 @@ import com.oracle.svm.core.annotate.TargetClass; import com.oracle.svm.core.snippets.KnownIntrinsics; +import jdk.internal.misc.Unsafe; + @TargetClass(className = "jdk.internal.misc.VM") public final class Target_jdk_internal_misc_VM { /** Ensure that we do not leak the full set of properties from the image generator. */ @@ -125,7 +126,7 @@ private static void initialize() { pageAlignDirectMemory = Boolean.getBoolean("sun.nio.PageAlignDirectMemory"); /* Ensure values are published to other threads before marking fields as initialized. */ - GraalUnsafeAccess.getUnsafe().storeFence(); + Unsafe.getUnsafe().storeFence(); initialized = true; } } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/UninterruptibleUtils.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/UninterruptibleUtils.java index ebb2ee7a1b5e..60a4361d2250 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/UninterruptibleUtils.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/UninterruptibleUtils.java @@ -24,7 +24,6 @@ */ package com.oracle.svm.core.jdk; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.word.Pointer; import org.graalvm.word.PointerBase; import org.graalvm.word.UnsignedWord; @@ -34,7 +33,7 @@ import com.oracle.svm.core.annotate.Uninterruptible; import com.oracle.svm.core.util.VMError; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * Annotated replacements to be called from uninterruptible code for methods whose source I do not @@ -47,7 +46,7 @@ public class UninterruptibleUtils { public static class AtomicInteger { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); private static final long VALUE_OFFSET; static { @@ -91,13 +90,13 @@ public int decrementAndGet() { @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) public boolean compareAndSet(int expected, int update) { - return UNSAFE.compareAndSwapInt(this, VALUE_OFFSET, expected, update); + return UNSAFE.compareAndSetInt(this, VALUE_OFFSET, expected, update); } } public static class AtomicLong { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); private static final long VALUE_OFFSET; static { @@ -161,7 +160,7 @@ public long getAndDecrement() { @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) public boolean compareAndSet(long expected, long update) { - return UNSAFE.compareAndSwapLong(this, VALUE_OFFSET, expected, update); + return UNSAFE.compareAndSetLong(this, VALUE_OFFSET, expected, update); } } @@ -288,7 +287,7 @@ public final UnsignedWord subtractAndGet(UnsignedWord delta) { public static class AtomicPointer { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); private static final long VALUE_OFFSET; static { @@ -313,13 +312,13 @@ public void set(T newValue) { @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) public boolean compareAndSet(T expected, T update) { - return UNSAFE.compareAndSwapLong(this, VALUE_OFFSET, expected.rawValue(), update.rawValue()); + return UNSAFE.compareAndSetLong(this, VALUE_OFFSET, expected.rawValue(), update.rawValue()); } } public static class AtomicReference { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); private static final long VALUE_OFFSET; static { @@ -351,7 +350,7 @@ public void set(T newValue) { @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) public boolean compareAndSet(T expected, T update) { - return UNSAFE.compareAndSwapObject(this, VALUE_OFFSET, expected, update); + return UNSAFE.compareAndSetObject(this, VALUE_OFFSET, expected, update); } @Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/VarHandleFeature.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/VarHandleFeature.java index 0286a78b2651..55e050f13acf 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/VarHandleFeature.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/VarHandleFeature.java @@ -35,7 +35,6 @@ import java.util.function.Consumer; import java.util.function.Function; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.compiler.serviceprovider.JavaVersionUtil; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.hosted.Feature; @@ -53,9 +52,9 @@ import com.oracle.svm.core.util.VMError; import com.oracle.svm.util.ReflectionUtil; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.MetaAccessProvider; import jdk.vm.ci.meta.ResolvedJavaField; -import sun.misc.Unsafe; /** * This file contains most of the code necessary for supporting VarHandle in native images. The @@ -97,7 +96,6 @@ */ @AutomaticFeature public class VarHandleFeature implements Feature { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); /** The JDK 11 class VarHandleObjects got renamed to VarHandleReferences. */ static final String OBJECT_SUFFIX = JavaVersionUtil.JAVA_SPEC <= 11 ? "Objects" : "References"; @@ -144,7 +142,7 @@ Field findVarHandleField(Object varHandle) { /* Search the declared fields for a field with a matching offset. */ for (Field field : cur.getDeclaredFields()) { if (Modifier.isStatic(field.getModifiers()) == info.isStatic) { - long fieldOffset = info.isStatic ? UNSAFE.staticFieldOffset(field) : UNSAFE.objectFieldOffset(field); + long fieldOffset = info.isStatic ? Unsafe.getUnsafe().staticFieldOffset(field) : Unsafe.getUnsafe().objectFieldOffset(field); if (fieldOffset == originalFieldOffset) { return field; } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrEventWriterAccess.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrEventWriterAccess.java index ee71feda9171..6a23f51e502d 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrEventWriterAccess.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrEventWriterAccess.java @@ -26,21 +26,20 @@ import java.lang.reflect.Field; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.Platforms; import com.oracle.svm.core.annotate.Uninterruptible; import com.oracle.svm.util.ReflectionUtil; +import jdk.internal.misc.Unsafe; import jdk.jfr.internal.EventWriter; -import sun.misc.Unsafe; /** * Used to access the Java event writer class, see {@link jdk.jfr.internal.EventWriter}. */ public final class JfrEventWriterAccess { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); private static final Field startPosition = ReflectionUtil.lookupField(EventWriter.class, "startPosition"); private static final Field startPositionAddress = ReflectionUtil.lookupField(EventWriter.class, "startPositionAddress"); diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/monitor/MultiThreadedMonitorSupport.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/monitor/MultiThreadedMonitorSupport.java index 9d750ca84ac9..6dbfa0bb10ba 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/monitor/MultiThreadedMonitorSupport.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/monitor/MultiThreadedMonitorSupport.java @@ -38,7 +38,6 @@ import java.util.concurrent.locks.ReentrantLock; import org.graalvm.compiler.core.common.SuppressFBWarnings; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.compiler.serviceprovider.JavaVersionUtil; import org.graalvm.compiler.word.BarrieredAccess; import org.graalvm.nativeimage.Platform; @@ -61,7 +60,7 @@ import com.oracle.svm.core.thread.VMOperationControl; import com.oracle.svm.core.util.VMError; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * Implementation of synchronized-related operations. @@ -86,7 +85,7 @@ */ public class MultiThreadedMonitorSupport extends MonitorSupport { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); /** * Types that are used to implement the secondary storage for monitor slots cannot themselves @@ -334,7 +333,7 @@ public void doRelockObject(Object obj, Object lockData) { int newState = oldState + 1; VMError.guarantee(newState > 0, "Maximum lock count exceeded"); - boolean success = UNSAFE.compareAndSwapInt(qSync, SYNC_STATE_FIELD_OFFSET, oldState, newState); + boolean success = UNSAFE.compareAndSetInt(qSync, SYNC_STATE_FIELD_OFFSET, oldState, newState); VMError.guarantee(success, "Could not re-lock object during deoptimization"); aSync.exclusiveOwnerThread = currentThread; } @@ -429,7 +428,7 @@ protected ReentrantLock getOrCreateMonitorFromObject(Object obj, boolean createI } /* Atomically put a new lock in place of the null at the monitorOffset. */ ReentrantLock newMonitor = newMonitorLock(); - if (UNSAFE.compareAndSwapObject(obj, monitorOffset, null, newMonitor)) { + if (UNSAFE.compareAndSetObject(obj, monitorOffset, null, newMonitor)) { return newMonitor; } /* We lost the race, use the lock some other thread installed. */ @@ -519,7 +518,7 @@ protected ConditionObject getOrCreateCondition(ReentrantLock monitorLock, boolea return existingCondition; } ConditionObject newCondition = (ConditionObject) monitorLock.newCondition(); - if (!UNSAFE.compareAndSwapObject(sync, SYNC_MONITOR_CONDITION_FIELD_OFFSET, MONITOR_WITHOUT_CONDITION, newCondition)) { + if (!UNSAFE.compareAndSetObject(sync, SYNC_MONITOR_CONDITION_FIELD_OFFSET, MONITOR_WITHOUT_CONDITION, newCondition)) { newCondition = SubstrateUtil.cast(sync.objectMonitorCondition, ConditionObject.class); assert isMonitorCondition(newCondition) : "race winner must have installed valid condition"; } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/LoomSupport.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/LoomSupport.java index 52c4d0412cbb..e93898fcbc44 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/LoomSupport.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/LoomSupport.java @@ -27,7 +27,6 @@ import java.lang.reflect.Field; import org.graalvm.compiler.api.replacements.Fold; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.CurrentIsolate; import org.graalvm.nativeimage.ImageInfo; import org.graalvm.nativeimage.IsolateThread; @@ -40,7 +39,7 @@ import com.oracle.svm.core.stack.JavaFrameAnchors; import com.oracle.svm.util.ReflectionUtil; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; public final class LoomSupport { public static final int YIELD_SUCCESS = 0; @@ -111,7 +110,6 @@ public static Target_java_lang_Continuation getContinuation(Target_java_lang_Thr } public static class CompatibilityUtil { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); private static final Field FIELDHOLDER_STATUS_FIELD = (ImageInfo.inImageCode() && isEnabled()) ? ReflectionUtil.lookupField(Target_java_lang_Thread_FieldHolder.class, "threadStatus") : null; private static final Field THREAD_STATUS_FIELD = (ImageInfo.inImageCode() && !isEnabled()) ? ReflectionUtil.lookupField(Target_java_lang_Thread.class, "threadStatus") : null; @@ -133,9 +131,9 @@ static void setThreadStatus(Target_java_lang_Thread tjlt, int threadStatus) { static boolean compareAndSetThreadStatus(Target_java_lang_Thread tjlt, int expectedStatus, int newStatus) { if (isEnabled()) { - return UNSAFE.compareAndSwapInt(tjlt.holder, UNSAFE.objectFieldOffset(FIELDHOLDER_STATUS_FIELD), expectedStatus, newStatus); + return Unsafe.getUnsafe().compareAndSetInt(tjlt.holder, Unsafe.getUnsafe().objectFieldOffset(FIELDHOLDER_STATUS_FIELD), expectedStatus, newStatus); } else { - return UNSAFE.compareAndSwapInt(tjlt, UNSAFE.objectFieldOffset(THREAD_STATUS_FIELD), expectedStatus, newStatus); + return Unsafe.getUnsafe().compareAndSetInt(tjlt, Unsafe.getUnsafe().objectFieldOffset(THREAD_STATUS_FIELD), expectedStatus, newStatus); } } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/PlatformThreads.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/PlatformThreads.java index 977511ab0351..f84a02c86912 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/PlatformThreads.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/PlatformThreads.java @@ -40,7 +40,6 @@ import org.graalvm.compiler.api.replacements.Fold; import org.graalvm.compiler.core.common.SuppressFBWarnings; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.CurrentIsolate; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.Isolate; @@ -79,7 +78,7 @@ import com.oracle.svm.core.util.TimeUtils; import com.oracle.svm.core.util.VMError; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * Implements operations on platform threads, which are typical {@link Thread Java threads} which @@ -93,8 +92,6 @@ public static PlatformThreads singleton() { return ImageSingletons.lookup(PlatformThreads.class); } - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - /** The platform {@link java.lang.Thread} for the {@link IsolateThread}. */ static final FastThreadLocalObject currentThread = FastThreadLocalFactory.createObject(Thread.class, "PlatformThreads.currentThread").setMaxOffset(FastThreadLocal.BYTE_OFFSET); @@ -816,7 +813,7 @@ private static void sleep0(long delayNanos) { * *before* the interrupted check because if not, the interrupt code will not assign one and * the wakeup will be lost. */ - UNSAFE.fullFence(); + Unsafe.getUnsafe().fullFence(); if (JavaThreads.isInterrupted(thread)) { return; // likely leaves a stale unpark which will be reset before the next sleep() diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/SpinLockUtils.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/SpinLockUtils.java index 391338aa10fa..b27e765b2e33 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/SpinLockUtils.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/SpinLockUtils.java @@ -25,11 +25,10 @@ package com.oracle.svm.core.thread; import org.graalvm.compiler.nodes.PauseNode; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import com.oracle.svm.core.annotate.Uninterruptible; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * Spin locks may only be used in places where the critical section contains only a few instructions @@ -37,12 +36,12 @@ * is crucial that really all code within the critical section is uninterruptible. */ public class SpinLockUtils { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); @Uninterruptible(reason = "This method does not do a transition, so the whole critical section must be uninterruptible.", callerMustBe = true) public static void lockNoTransition(Object obj, long intFieldOffset) { // Fast-path. - if (UNSAFE.compareAndSwapInt(obj, intFieldOffset, 0, 1)) { + if (UNSAFE.compareAndSetInt(obj, intFieldOffset, 0, 1)) { return; } @@ -67,7 +66,7 @@ public static void lockNoTransition(Object obj, long intFieldOffset) { } } - if (UNSAFE.compareAndSwapInt(obj, intFieldOffset, 0, 1)) { + if (UNSAFE.compareAndSetInt(obj, intFieldOffset, 0, 1)) { return; } } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/SubstrateVirtualThread.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/SubstrateVirtualThread.java index babe811f111e..c0dd9314d23e 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/SubstrateVirtualThread.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/SubstrateVirtualThread.java @@ -24,8 +24,6 @@ */ package com.oracle.svm.core.thread; -// Checkstyle: stop - import static java.util.concurrent.TimeUnit.NANOSECONDS; import java.util.Locale; @@ -37,7 +35,6 @@ import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.IsolateThread; import com.oracle.svm.core.annotate.Alias; @@ -49,10 +46,7 @@ import com.oracle.svm.core.stack.JavaFrameAnchors; import com.oracle.svm.core.util.VMError; -import sun.misc.Unsafe; -// Checkstyle: resume - -// Checkstyle: allow synchronization +import jdk.internal.misc.Unsafe; /** * Implementation of {@link Thread} that does not correspond to an {@linkplain IsolateThread OS @@ -62,7 +56,6 @@ * This class is based on Project Loom's {@code java.lang.VirtualThread}. */ final class SubstrateVirtualThread extends Thread { - private static final Unsafe U = GraalUnsafeAccess.getUnsafe(); private static final ScheduledExecutorService UNPARKER = createDelayedTaskScheduler(); private static final long STATE; @@ -70,9 +63,9 @@ final class SubstrateVirtualThread extends Thread { private static final long TERMINATION; static { try { - STATE = U.objectFieldOffset(SubstrateVirtualThread.class.getDeclaredField("state")); - PARK_PERMIT = U.objectFieldOffset(SubstrateVirtualThread.class.getDeclaredField("parkPermit")); - TERMINATION = U.objectFieldOffset(SubstrateVirtualThread.class.getDeclaredField("termination")); + STATE = Unsafe.getUnsafe().objectFieldOffset(SubstrateVirtualThread.class.getDeclaredField("state")); + PARK_PERMIT = Unsafe.getUnsafe().objectFieldOffset(SubstrateVirtualThread.class.getDeclaredField("parkPermit")); + TERMINATION = Unsafe.getUnsafe().objectFieldOffset(SubstrateVirtualThread.class.getDeclaredField("termination")); } catch (ReflectiveOperationException e) { throw VMError.shouldNotReachHere(e); } @@ -260,9 +253,9 @@ private void parkOnCarrierThread(boolean timed, long nanos) { try { if (!parkPermit()) { if (!timed) { - U.park(false, 0); + Unsafe.getUnsafe().park(false, 0); } else if (nanos > 0) { - U.park(false, nanos); + Unsafe.getUnsafe().park(false, nanos); } } } finally { @@ -395,7 +388,7 @@ void unpark() { try { Thread carrier = carrierThread; if (carrier != null && state() == PINNED) { - U.unpark(carrier); + Unsafe.getUnsafe().unpark(carrier); } } finally { releaseInterruptLockAndSwitchBack(token); @@ -630,7 +623,7 @@ private CountDownLatch getTermination() { CountDownLatch termination = this.termination; if (termination == null) { termination = new CountDownLatch(1); - if (!U.compareAndSwapObject(this, TERMINATION, null, termination)) { + if (!Unsafe.getUnsafe().compareAndSetObject(this, TERMINATION, null, termination)) { termination = this.termination; } } @@ -646,7 +639,7 @@ private void setState(int newValue) { } private boolean compareAndSetState(int expectedValue, int newValue) { - return U.compareAndSwapInt(this, STATE, expectedValue, newValue); + return Unsafe.getUnsafe().compareAndSetInt(this, STATE, expectedValue, newValue); } private boolean parkPermit() { @@ -663,7 +656,7 @@ private void setParkPermit(boolean newValue) { private boolean getAndSetParkPermit(boolean newValue) { int v = newValue ? 1 : 0; if (parkPermit != v) { - return U.getAndSetInt(this, PARK_PERMIT, v) != 0; + return Unsafe.getUnsafe().getAndSetInt(this, PARK_PERMIT, v) != 0; } else { return newValue; } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/ThreadData.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/ThreadData.java index 80b6edbb4724..80a00f6e8189 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/ThreadData.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/ThreadData.java @@ -24,12 +24,10 @@ */ package com.oracle.svm.core.thread; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; - import com.oracle.svm.core.annotate.Uninterruptible; import com.oracle.svm.core.util.VMError; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * This class holds thread-specific data that must be freed when the thread detaches. However, it @@ -41,7 +39,7 @@ * unexpectedly. */ public final class ThreadData extends UnacquiredThreadData { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); + private static final Unsafe UNSAFE = Unsafe.getUnsafe(); private static final long LOCK_OFFSET; private static final long UNSAFE_PARK_EVENT_OFFSET; private static final long SLEEP_PARK_EVENT_OFFSET; @@ -75,10 +73,9 @@ public ParkEvent getSleepParkEvent() { } /** - * Returns the {@link ParkEvent} for {@link sun.misc.Unsafe#park} and - * {@link sun.misc.Unsafe#unpark}. If this method is called to access the {@link ParkEvent} of - * another thread, then the returned value must not be used after {@link #release() releasing} - * the {@link ThreadData}. + * Returns the {@link ParkEvent} for {@link Unsafe#park} and {@link Unsafe#unpark}. If this + * method is called to access the {@link ParkEvent} of another thread, then the returned value + * must not be used after {@link #release() releasing} the {@link ThreadData}. */ public ParkEvent ensureUnsafeParkEvent() { assert isForCurrentThread() || refCount > 0; diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/ThreadStatus.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/ThreadStatus.java index ac3a85581814..451d6ba75450 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/ThreadStatus.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/thread/ThreadStatus.java @@ -76,13 +76,13 @@ public class ThreadStatus { JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT + JVMTI_THREAD_STATE_IN_OBJECT_WAIT; - /** {@link sun.misc.Unsafe#park}. */ + /** {@link jdk.internal.misc.Unsafe#park}. */ public static final int PARKED = JVMTI_THREAD_STATE_ALIVE + JVMTI_THREAD_STATE_WAITING + JVMTI_THREAD_STATE_WAITING_INDEFINITELY + JVMTI_THREAD_STATE_PARKED; - /** {@link sun.misc.Unsafe#park}. */ + /** {@link jdk.internal.misc.Unsafe#park}. */ public static final int PARKED_TIMED = JVMTI_THREAD_STATE_ALIVE + JVMTI_THREAD_STATE_WAITING + JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT + diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/LazyFinalReference.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/LazyFinalReference.java index e7829739c912..5f2a049f9eb2 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/LazyFinalReference.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/LazyFinalReference.java @@ -26,9 +26,7 @@ import java.util.function.Supplier; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; - -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; /** * An object reference that is set lazily to the reference returned by the provided {@link Supplier} @@ -38,12 +36,10 @@ public final class LazyFinalReference { private static final Object UNINITIALIZED = new Object(); - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - private static final long VALUE_OFFSET; static { try { - VALUE_OFFSET = UNSAFE.objectFieldOffset(LazyFinalReference.class.getDeclaredField("value")); + VALUE_OFFSET = Unsafe.getUnsafe().objectFieldOffset(LazyFinalReference.class.getDeclaredField("value")); } catch (Throwable ex) { throw VMError.shouldNotReachHere(ex); } @@ -62,7 +58,7 @@ public LazyFinalReference(Supplier supplier) { } public boolean isPresent() { - return value != UNINITIALIZED || UNSAFE.getObjectVolatile(this, VALUE_OFFSET) != UNINITIALIZED; + return value != UNINITIALIZED || Unsafe.getUnsafe().getObjectVolatile(this, VALUE_OFFSET) != UNINITIALIZED; } @SuppressWarnings("unchecked") @@ -70,14 +66,14 @@ public T get() { T v = value; if (v == UNINITIALIZED) { // Try volatile read first in case of memory inconsistency to avoid Supplier call - v = (T) UNSAFE.getObjectVolatile(this, VALUE_OFFSET); + v = (T) Unsafe.getUnsafe().getObjectVolatile(this, VALUE_OFFSET); if (v == UNINITIALIZED) { T obj = supplier.get(); - if (UNSAFE.compareAndSwapObject(this, VALUE_OFFSET, UNINITIALIZED, obj)) { + if (Unsafe.getUnsafe().compareAndSetObject(this, VALUE_OFFSET, UNINITIALIZED, obj)) { v = obj; } else { - v = (T) UNSAFE.getObjectVolatile(this, VALUE_OFFSET); + v = (T) Unsafe.getUnsafe().getObjectVolatile(this, VALUE_OFFSET); } assert v != UNINITIALIZED; } diff --git a/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalEntryPoints.java b/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalEntryPoints.java index cb450f20153e..219e3cd65055 100644 --- a/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalEntryPoints.java +++ b/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalEntryPoints.java @@ -25,14 +25,12 @@ package com.oracle.svm.graal.hotspot.libgraal; import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime; -import static sun.misc.Unsafe.ARRAY_BYTE_BASE_OFFSET; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.util.Arrays; import java.util.Map; -import com.oracle.svm.core.heap.Heap; import org.graalvm.collections.EconomicMap; import org.graalvm.compiler.debug.GlobalMetrics; import org.graalvm.compiler.hotspot.CompilationContext; @@ -44,7 +42,6 @@ import org.graalvm.compiler.options.OptionKey; import org.graalvm.compiler.options.OptionValues; import org.graalvm.compiler.options.OptionsParser; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.compiler.serviceprovider.IsolateUtil; import org.graalvm.libgraal.LibGraal; import org.graalvm.libgraal.LibGraalScope; @@ -61,14 +58,15 @@ import com.oracle.svm.core.c.CGlobalData; import com.oracle.svm.core.c.CGlobalDataFactory; +import com.oracle.svm.core.heap.Heap; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.code.InstalledCode; import jdk.vm.ci.hotspot.HotSpotCompilationRequest; import jdk.vm.ci.hotspot.HotSpotInstalledCode; import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; import jdk.vm.ci.runtime.JVMCICompiler; -import sun.misc.Unsafe; /** * Entry points in libgraal corresponding to native methods in {@link LibGraalScope} and @@ -76,8 +74,6 @@ */ public final class LibGraalEntryPoints { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - /** * @see org.graalvm.compiler.hotspot.HotSpotTTYStreamProvider#execute */ @@ -126,7 +122,7 @@ private static OptionValues decodeOptions(long address, int size, int hash) { CachedOptions options = cachedOptions.get(); if (options == null || options.hash != hash) { byte[] buffer = new byte[size]; - UNSAFE.copyMemory(null, address, buffer, Unsafe.ARRAY_BYTE_BASE_OFFSET, size); + Unsafe.getUnsafe().copyMemory(null, address, buffer, Unsafe.ARRAY_BYTE_BASE_OFFSET, size); int actualHash = Arrays.hashCode(buffer); if (actualHash != hash) { throw new IllegalArgumentException(actualHash + " != " + hash); @@ -248,8 +244,8 @@ private static long compileMethod(PointerBase jniEnv, t.printStackTrace(new PrintStream(baos)); byte[] stackTrace = baos.toByteArray(); int length = Math.min(stackTraceCapacity - Integer.BYTES, stackTrace.length); - UNSAFE.putInt(stackTraceAddress, length); - UNSAFE.copyMemory(stackTrace, ARRAY_BYTE_BASE_OFFSET, null, stackTraceAddress + Integer.BYTES, length); + Unsafe.getUnsafe().putInt(stackTraceAddress, length); + Unsafe.getUnsafe().copyMemory(stackTrace, Unsafe.ARRAY_BYTE_BASE_OFFSET, null, stackTraceAddress + Integer.BYTES, length); return 0L; } finally { /* diff --git a/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/FieldsOffsetsFeature.java b/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/FieldsOffsetsFeature.java index 6a5cc479223d..92d41816da29 100644 --- a/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/FieldsOffsetsFeature.java +++ b/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/hosted/FieldsOffsetsFeature.java @@ -40,7 +40,6 @@ import org.graalvm.compiler.lir.CompositeValueClass; import org.graalvm.compiler.lir.LIRInstruction; import org.graalvm.compiler.lir.LIRInstructionClass; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.hosted.Feature; @@ -58,9 +57,9 @@ import com.oracle.svm.hosted.meta.HostedMetaAccess; import com.oracle.svm.util.UnsafePartitionKind; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.MetaAccessProvider; import jdk.vm.ci.meta.ResolvedJavaField; -import sun.misc.Unsafe; /** * Graal uses unsafe memory accesses to access {@link Node}s and {@link LIRInstruction}s. The @@ -71,8 +70,6 @@ */ public class FieldsOffsetsFeature implements Feature { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - abstract static class IterationMaskRecomputation implements RecomputeFieldValue.CustomFieldValueComputer { @Override public RecomputeFieldValue.ValueAvailability valueAvailability() { @@ -243,7 +240,7 @@ public void beforeCompilation(BeforeCompilationAccess a) { long[] newOffsets = new long[fields.getCount()]; for (int i = 0; i < newOffsets.length; i++) { Field field = findField(fields, i); - assert UNSAFE.objectFieldOffset(field) == fields.getOffsets()[i]; + assert Unsafe.getUnsafe().objectFieldOffset(field) == fields.getOffsets()[i]; newOffsets[i] = hMetaAccess.lookupJavaField(field).getLocation(); } replacement.newOffsets = newOffsets; diff --git a/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/meta/SubstrateType.java b/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/meta/SubstrateType.java index 2258abc670aa..3127d85b48a8 100644 --- a/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/meta/SubstrateType.java +++ b/substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/meta/SubstrateType.java @@ -31,7 +31,6 @@ import java.util.NoSuchElementException; import java.util.function.Predicate; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.Platforms; import org.graalvm.word.WordBase; @@ -47,6 +46,7 @@ import com.oracle.truffle.api.nodes.NodeClass; import com.oracle.truffle.api.nodes.NodeCloneable; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.Assumptions.AssumptionResult; import jdk.vm.ci.meta.JavaConstant; import jdk.vm.ci.meta.JavaKind; @@ -54,12 +54,9 @@ import jdk.vm.ci.meta.ResolvedJavaField; import jdk.vm.ci.meta.ResolvedJavaMethod; import jdk.vm.ci.meta.ResolvedJavaType; -import sun.misc.Unsafe; public class SubstrateType extends NodeClass implements SharedType { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - protected static final SubstrateType[] EMPTY_ARRAY = new SubstrateType[0]; private final JavaKind kind; @@ -564,14 +561,14 @@ public void putFieldObject(Object field, Node receiver, Object value) { assert !getFieldType(field).isPrimitive(); assert value == null || getFieldType(field).isInstance(value); long offset = SubstrateNodeFieldAccessor.makeOffset((SubstrateField) field); - UNSAFE.putObject(receiver, offset, value); + Unsafe.getUnsafe().putObject(receiver, offset, value); } @Override public Object getFieldObject(Object field, Node receiver) { assert !getFieldType(field).isPrimitive(); long offset = SubstrateNodeFieldAccessor.makeOffset((SubstrateField) field); - return UNSAFE.getObject(receiver, offset); + return Unsafe.getUnsafe().getObject(receiver, offset); } @Override @@ -579,23 +576,23 @@ public Object getFieldValue(Object field, Node node) { Class fieldType = getFieldType(field); long offset = SubstrateNodeFieldAccessor.makeOffset((SubstrateField) field); if (fieldType == boolean.class) { - return UNSAFE.getBoolean(node, offset); + return Unsafe.getUnsafe().getBoolean(node, offset); } else if (fieldType == byte.class) { - return UNSAFE.getByte(node, offset); + return Unsafe.getUnsafe().getByte(node, offset); } else if (fieldType == short.class) { - return UNSAFE.getShort(node, offset); + return Unsafe.getUnsafe().getShort(node, offset); } else if (fieldType == char.class) { - return UNSAFE.getChar(node, offset); + return Unsafe.getUnsafe().getChar(node, offset); } else if (fieldType == int.class) { - return UNSAFE.getInt(node, offset); + return Unsafe.getUnsafe().getInt(node, offset); } else if (fieldType == long.class) { - return UNSAFE.getLong(node, offset); + return Unsafe.getUnsafe().getLong(node, offset); } else if (fieldType == float.class) { - return UNSAFE.getFloat(node, offset); + return Unsafe.getUnsafe().getFloat(node, offset); } else if (fieldType == double.class) { - return UNSAFE.getDouble(node, offset); + return Unsafe.getUnsafe().getDouble(node, offset); } else { - return UNSAFE.getObject(node, offset); + return Unsafe.getUnsafe().getObject(node, offset); } } @@ -741,8 +738,6 @@ static RuntimeException noFieldsError(SubstrateType type) { class SubstrateNodeIterator implements Iterator { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - private final Node node; private final SubstrateType type; @@ -790,13 +785,13 @@ private void computeNext() { private boolean computeNextFromField(SubstrateField field) { if (SubstrateNodeFieldAccessor.isChildField(field)) { long offset = field.getLocation(); - next = (Node) UNSAFE.getObject(node, offset); + next = (Node) Unsafe.getUnsafe().getObject(node, offset); if (next != null) { return true; } } else if (SubstrateNodeFieldAccessor.isChildrenField(field)) { long offset = field.getLocation(); - children = (Object[]) UNSAFE.getObject(node, offset); + children = (Object[]) Unsafe.getUnsafe().getObject(node, offset); nextChildInChildren = 0; return computeNextFromChildren(); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ameta/AnalysisConstantReflectionProvider.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ameta/AnalysisConstantReflectionProvider.java index 416fa891f5c7..c801b91ea99d 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ameta/AnalysisConstantReflectionProvider.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ameta/AnalysisConstantReflectionProvider.java @@ -27,7 +27,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.compiler.word.Word; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.Platforms; @@ -50,6 +49,7 @@ import com.oracle.svm.hosted.classinitialization.ClassInitializationSupport; import com.oracle.svm.hosted.meta.HostedMetaAccess; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.Constant; import jdk.vm.ci.meta.ConstantReflectionProvider; import jdk.vm.ci.meta.JavaConstant; @@ -189,31 +189,31 @@ public JavaConstant readUninitializedStaticValue(AnalysisField field) { if (reflectionField != null) { assert kind == JavaKind.fromJavaClass(reflectionField.getType()); - Object reflectionFieldBase = GraalUnsafeAccess.getUnsafe().staticFieldBase(reflectionField); - long reflectionFieldOffset = GraalUnsafeAccess.getUnsafe().staticFieldOffset(reflectionField); + Object reflectionFieldBase = Unsafe.getUnsafe().staticFieldBase(reflectionField); + long reflectionFieldOffset = Unsafe.getUnsafe().staticFieldOffset(reflectionField); AnalysisError.guarantee(reflectionFieldBase == base && reflectionFieldOffset == offset); } switch (kind) { case Boolean: - return JavaConstant.forBoolean(GraalUnsafeAccess.getUnsafe().getBoolean(base, offset)); + return JavaConstant.forBoolean(Unsafe.getUnsafe().getBoolean(base, offset)); case Byte: - return JavaConstant.forByte(GraalUnsafeAccess.getUnsafe().getByte(base, offset)); + return JavaConstant.forByte(Unsafe.getUnsafe().getByte(base, offset)); case Char: - return JavaConstant.forChar(GraalUnsafeAccess.getUnsafe().getChar(base, offset)); + return JavaConstant.forChar(Unsafe.getUnsafe().getChar(base, offset)); case Short: - return JavaConstant.forShort(GraalUnsafeAccess.getUnsafe().getShort(base, offset)); + return JavaConstant.forShort(Unsafe.getUnsafe().getShort(base, offset)); case Int: - return JavaConstant.forInt(GraalUnsafeAccess.getUnsafe().getInt(base, offset)); + return JavaConstant.forInt(Unsafe.getUnsafe().getInt(base, offset)); case Long: - return JavaConstant.forLong(GraalUnsafeAccess.getUnsafe().getLong(base, offset)); + return JavaConstant.forLong(Unsafe.getUnsafe().getLong(base, offset)); case Float: - return JavaConstant.forFloat(GraalUnsafeAccess.getUnsafe().getFloat(base, offset)); + return JavaConstant.forFloat(Unsafe.getUnsafe().getFloat(base, offset)); case Double: - return JavaConstant.forDouble(GraalUnsafeAccess.getUnsafe().getDouble(base, offset)); + return JavaConstant.forDouble(Unsafe.getUnsafe().getDouble(base, offset)); case Object: - Object value = GraalUnsafeAccess.getUnsafe().getObject(base, offset); + Object value = Unsafe.getUnsafe().getObject(base, offset); assert value == null || value instanceof String : "String is currently the only specified object type for the ConstantValue class file attribute"; return SubstrateObjectConstant.forObject(value); default: diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/classinitialization/ConfigurableClassInitialization.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/classinitialization/ConfigurableClassInitialization.java index 96d6211b6298..7555c61554cf 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/classinitialization/ConfigurableClassInitialization.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/classinitialization/ConfigurableClassInitialization.java @@ -41,8 +41,6 @@ import java.util.concurrent.ConcurrentMap; import java.util.stream.Collectors; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; - import com.oracle.graal.pointsto.constraints.UnsupportedFeatures; import com.oracle.graal.pointsto.infrastructure.OriginalClassProvider; import com.oracle.graal.pointsto.reports.ReportUtils; @@ -54,6 +52,7 @@ import com.oracle.svm.hosted.ImageClassLoader; import com.oracle.svm.hosted.NativeImageOptions; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.MetaAccessProvider; import jdk.vm.ci.meta.ResolvedJavaType; @@ -175,7 +174,7 @@ public void maybeInitializeHosted(ResolvedJavaType type) { */ private InitKind ensureClassInitialized(Class clazz, boolean allowErrors) { try { - GraalUnsafeAccess.ensureClassInitialized(clazz); + Unsafe.getUnsafe().ensureClassInitialized(clazz); return InitKind.BUILD_TIME; } catch (NoClassDefFoundError ex) { if (NativeImageOptions.AllowIncompleteClasspath.getValue()) { @@ -268,7 +267,7 @@ public void initializeAtRunTime(Class clazz, String reason) { setSubclassesAsRunTime(clazz); checkEagerInitialization(clazz); - if (!GraalUnsafeAccess.shouldBeInitialized(clazz)) { + if (!Unsafe.getUnsafe().shouldBeInitialized(clazz)) { throw UserError.abort("The class %1$s has already been initialized (%2$s); it is too late to register %1$s for build-time initialization. %3$s", clazz.getTypeName(), reason, classInitializationErrorMessage(clazz, "Try avoiding this conflict by avoiding to initialize the class that caused initialization of " + clazz.getTypeName() + @@ -396,7 +395,7 @@ public void rerunInitialization(Class clazz, String reason) { checkEagerInitialization(clazz); try { - GraalUnsafeAccess.ensureClassInitialized(clazz); + Unsafe.getUnsafe().ensureClassInitialized(clazz); } catch (Throwable ex) { throw UserError.abort(ex, "Class initialization failed for %s. The class is requested for re-running (reason: %s)", clazz.getTypeName(), reason); } @@ -522,7 +521,7 @@ public boolean checkDelayedInitialization() { */ Set> illegalyInitialized = new HashSet<>(); for (Map.Entry, InitKind> entry : classInitKinds.entrySet()) { - if (entry.getValue().isRunTime() && !GraalUnsafeAccess.shouldBeInitialized(entry.getKey())) { + if (entry.getValue().isRunTime() && !Unsafe.getUnsafe().shouldBeInitialized(entry.getKey())) { illegalyInitialized.add(entry.getKey()); } } @@ -587,7 +586,7 @@ InitKind computeInitKindAndMaybeInitializeClass(Class clazz, boolean memoize, } /* Well, and enums that got initialized while annotations are parsed. */ - if (clazz.isEnum() && !GraalUnsafeAccess.shouldBeInitialized(clazz)) { + if (clazz.isEnum() && !Unsafe.getUnsafe().shouldBeInitialized(clazz)) { if (memoize) { forceInitializeHosted(clazz, "enums referred in annotations must be initialized", false); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageHeapWriter.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageHeapWriter.java index 9cf3f4582d82..f496056c67c1 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageHeapWriter.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/image/NativeImageHeapWriter.java @@ -34,7 +34,6 @@ import org.graalvm.compiler.core.common.NumUtil; import org.graalvm.compiler.debug.DebugContext; import org.graalvm.compiler.debug.Indent; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.c.function.CFunctionPointer; import org.graalvm.nativeimage.c.function.RelocatedPointer; @@ -58,17 +57,15 @@ import com.oracle.svm.hosted.meta.HostedField; import com.oracle.svm.hosted.meta.MaterializedConstantFields; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.JavaConstant; import jdk.vm.ci.meta.JavaKind; -import sun.misc.Unsafe; /** * Writes the native image heap into one or multiple {@link RelocatableBuffer}s. */ public final class NativeImageHeapWriter { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - private final NativeImageHeap heap; private final ImageHeapLayoutInfo heapLayout; private long sectionOffsetOfARelocatablePointer; @@ -380,9 +377,10 @@ private void writeObject(ObjectInfo info, RelocatableBuffer buffer) { } } else { int elementIndex = info.getIndexInBuffer(objectLayout.getArrayElementOffset(kind, 0)); - int elementTypeSize = UNSAFE.arrayIndexScale(array.getClass()); + int elementTypeSize = Unsafe.getUnsafe().arrayIndexScale(array.getClass()); assert elementTypeSize == kind.getByteCount(); - UNSAFE.copyMemory(array, UNSAFE.arrayBaseOffset(array.getClass()), buffer.getBackingArray(), Unsafe.ARRAY_BYTE_BASE_OFFSET + elementIndex, length * elementTypeSize); + Unsafe.getUnsafe().copyMemory(array, Unsafe.getUnsafe().arrayBaseOffset(array.getClass()), buffer.getBackingArray(), Unsafe.ARRAY_BYTE_BASE_OFFSET + elementIndex, + length * elementTypeSize); } } else { diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jfr/JfrEventSubstitution.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jfr/JfrEventSubstitution.java index 2fdbe3d33490..5590922a3d74 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jfr/JfrEventSubstitution.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jfr/JfrEventSubstitution.java @@ -29,16 +29,16 @@ import java.lang.reflect.Modifier; import java.util.concurrent.ConcurrentHashMap; -import com.oracle.svm.core.jfr.JfrJavaEvents; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.Platforms; import com.oracle.graal.pointsto.infrastructure.OriginalClassProvider; import com.oracle.graal.pointsto.infrastructure.SubstitutionProcessor; import com.oracle.graal.pointsto.util.GraalAccess; +import com.oracle.svm.core.jfr.JfrJavaEvents; import com.oracle.svm.core.util.VMError; +import jdk.internal.misc.Unsafe; import jdk.jfr.Event; import jdk.jfr.internal.EventWriter; import jdk.jfr.internal.JVM; @@ -48,7 +48,6 @@ import jdk.vm.ci.meta.ResolvedJavaMethod; import jdk.vm.ci.meta.ResolvedJavaType; import jdk.vm.ci.meta.Signature; -import sun.misc.Unsafe; /** * This class triggers the class redefinition (see {@link JVM#retransformClasses}) for all @@ -186,10 +185,9 @@ private static void setPublicModifier(ResolvedJavaMethod m) { methodAccessFlagsOffsetF.setAccessible(true); Object hotSpotVMConfig = configM.invoke(null); int methodAccessFlagsOffset = methodAccessFlagsOffsetF.getInt(hotSpotVMConfig); - Unsafe unsafe = GraalUnsafeAccess.getUnsafe(); - int modifiers = unsafe.getInt(metaspaceMethod + methodAccessFlagsOffset); + int modifiers = Unsafe.getUnsafe().getInt(metaspaceMethod + methodAccessFlagsOffset); int newModifiers = modifiers & ~Modifier.PRIVATE | Modifier.PUBLIC; - unsafe.putInt(metaspaceMethod + methodAccessFlagsOffset, newModifiers); + Unsafe.getUnsafe().putInt(metaspaceMethod + methodAccessFlagsOffset, newModifiers); } catch (Exception ex) { throw VMError.shouldNotReachHere(ex); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/SubstrateGraphBuilderPlugins.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/SubstrateGraphBuilderPlugins.java index a31e89facf97..eac59d07f806 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/SubstrateGraphBuilderPlugins.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/SubstrateGraphBuilderPlugins.java @@ -547,7 +547,7 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec } private static void registerUnsafePlugins(MetaAccessProvider metaAccess, InvocationPlugins plugins, SnippetReflectionProvider snippetReflection, ParsingReason reason) { - registerUnsafePlugins(metaAccess, new Registration(plugins, sun.misc.Unsafe.class), snippetReflection, reason, true); + registerUnsafePlugins(metaAccess, new Registration(plugins, "sun.misc.Unsafe"), snippetReflection, reason, true); Registration r = new Registration(plugins, "jdk.internal.misc.Unsafe"); registerUnsafePlugins(metaAccess, r, snippetReflection, reason, false); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/substitute/ComputedValueField.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/substitute/ComputedValueField.java index 9058d0a2659f..2e1aed9d04ad 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/substitute/ComputedValueField.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/substitute/ComputedValueField.java @@ -43,7 +43,6 @@ import org.graalvm.collections.EconomicMap; import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import com.oracle.graal.pointsto.infrastructure.OriginalFieldProvider; import com.oracle.graal.pointsto.meta.AnalysisField; @@ -63,6 +62,7 @@ import com.oracle.svm.util.ReflectionUtil; import com.oracle.svm.util.ReflectionUtil.ReflectionUtilError; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.common.NativeImageReinitialize; import jdk.vm.ci.meta.JavaConstant; import jdk.vm.ci.meta.JavaKind; @@ -70,7 +70,6 @@ import jdk.vm.ci.meta.MetaAccessProvider; import jdk.vm.ci.meta.ResolvedJavaField; import jdk.vm.ci.meta.ResolvedJavaType; -import sun.misc.Unsafe; /** * Wraps a field whose value is recomputed when added to an image. @@ -80,7 +79,6 @@ */ public class ComputedValueField implements ReadableJavaField, OriginalFieldProvider, ComputedValue { - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); private static final EnumSet offsetComputationKinds = EnumSet.of(FieldOffset, TranslateFieldOffset, AtomicFieldUpdaterOffset); private final ResolvedJavaField original; private final ResolvedJavaField annotated; @@ -422,7 +420,7 @@ private JavaConstant translateFieldOffset(MetaAccessProvider metaAccess, JavaCon // search the declared fields for a field with a matching offset for (Field f : tclass.getDeclaredFields()) { if (!Modifier.isStatic(f.getModifiers())) { - long fieldOffset = UNSAFE.objectFieldOffset(f); + long fieldOffset = Unsafe.getUnsafe().objectFieldOffset(f); if (fieldOffset == searchOffset) { HostedField sf = (HostedField) metaAccess.lookupJavaField(f); guarantee(sf.isAccessed() && sf.getLocation() > 0, "Field not marked as accessed: " + sf.format("%H.%n")); diff --git a/substratevm/src/com.oracle.svm.jni/src/com/oracle/svm/jni/JNIGeneratedMethodSupport.java b/substratevm/src/com.oracle.svm.jni/src/com/oracle/svm/jni/JNIGeneratedMethodSupport.java index 08de0c1a5722..d8e2d5090e69 100644 --- a/substratevm/src/com.oracle.svm.jni/src/com/oracle/svm/jni/JNIGeneratedMethodSupport.java +++ b/substratevm/src/com.oracle.svm.jni/src/com/oracle/svm/jni/JNIGeneratedMethodSupport.java @@ -26,7 +26,6 @@ import java.lang.reflect.Array; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.c.type.CCharPointer; import org.graalvm.word.PointerBase; import org.graalvm.word.WordBase; @@ -40,8 +39,8 @@ import com.oracle.svm.jni.nativeapi.JNIFieldId; import com.oracle.svm.jni.nativeapi.JNIObjectHandle; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.JavaKind; -import sun.misc.Unsafe; /** * Helper code that is used in generated JNI code via {@code JNIGraphKit}. @@ -49,8 +48,6 @@ public final class JNIGeneratedMethodSupport { // Careful around here -- these methods are invoked by generated methods. - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - static PointerBase nativeCallAddress(JNINativeLinkage linkage) { return linkage.getOrFindEntryPoint(); } @@ -125,7 +122,7 @@ static void getPrimitiveArrayRegion(JavaKind elementKind, Object array, int star if (count > 0) { long offset = ConfigurationValues.getObjectLayout().getArrayElementOffset(elementKind, start); int elementSize = ConfigurationValues.getObjectLayout().sizeInBytes(elementKind); - UNSAFE.copyMemory(array, offset, null, buffer.rawValue(), count * elementSize); + Unsafe.getUnsafe().copyMemory(array, offset, null, buffer.rawValue(), count * elementSize); } } @@ -136,7 +133,7 @@ static void setPrimitiveArrayRegion(JavaKind elementKind, Object array, int star if (count > 0) { long offset = ConfigurationValues.getObjectLayout().getArrayElementOffset(elementKind, start); int elementSize = ConfigurationValues.getObjectLayout().sizeInBytes(elementKind); - UNSAFE.copyMemory(null, buffer.rawValue(), array, offset, count * elementSize); + Unsafe.getUnsafe().copyMemory(null, buffer.rawValue(), array, offset, count * elementSize); } } } diff --git a/substratevm/src/com.oracle.svm.jni/src/com/oracle/svm/jni/functions/JNIFunctions.java b/substratevm/src/com.oracle.svm.jni/src/com/oracle/svm/jni/functions/JNIFunctions.java index 34612de8b71b..18092b92c8d3 100644 --- a/substratevm/src/com.oracle.svm.jni/src/com/oracle/svm/jni/functions/JNIFunctions.java +++ b/substratevm/src/com.oracle.svm.jni/src/com/oracle/svm/jni/functions/JNIFunctions.java @@ -37,7 +37,6 @@ import java.util.Arrays; import org.graalvm.compiler.nodes.java.ArrayLengthNode; -import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.IsolateThread; import org.graalvm.nativeimage.LogHandler; @@ -114,8 +113,8 @@ import com.oracle.svm.jni.nativeapi.JNIObjectRefType; import com.oracle.svm.jni.nativeapi.JNIVersion; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.MetaUtil; -import sun.misc.Unsafe; /** * Implementations of the functions defined by the Java Native Interface. @@ -149,8 +148,6 @@ public final class JNIFunctions { * jint GetVersion(JNIEnv *env); */ - private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe(); - @CEntryPoint(include = CEntryPoint.NotIncludedAutomatically.class) @CEntryPointOptions(prologue = CEntryPointOptions.NoPrologue.class, epilogue = CEntryPointOptions.NoEpilogue.class, publishAs = Publish.NotPublished) @Uninterruptible(reason = "No need to enter the isolate and also no way to report errors if unable to.") @@ -449,7 +446,7 @@ static JNIObjectHandle AllocObject(JNIEnvironment env, JNIObjectHandle classHand Class clazz = JNIObjectHandles.getObject(classHandle); Object instance; try { - instance = UNSAFE.allocateInstance(clazz); + instance = Unsafe.getUnsafe().allocateInstance(clazz); } catch (InstantiationException e) { instance = null; } diff --git a/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/clinit/TestClassInitializationMustBeSafeEarly.java b/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/clinit/TestClassInitializationMustBeSafeEarly.java index a72879668098..4485b2d597b7 100644 --- a/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/clinit/TestClassInitializationMustBeSafeEarly.java +++ b/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/clinit/TestClassInitializationMustBeSafeEarly.java @@ -36,7 +36,7 @@ import org.graalvm.nativeimage.hosted.Feature; import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; -import sun.misc.Unsafe; +import jdk.internal.misc.Unsafe; class PureMustBeSafeEarly { static int v; diff --git a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java index f247e55127af..22af88086e4b 100644 --- a/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java +++ b/substratevm/src/com.oracle.svm.truffle/src/com/oracle/svm/truffle/TruffleBaseFeature.java @@ -42,7 +42,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.BooleanSupplier; -import com.oracle.svm.core.configure.ResourcesRegistry; import org.graalvm.collections.Pair; import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; import org.graalvm.compiler.nodes.ConstantNode; @@ -58,6 +57,7 @@ import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; import org.graalvm.nativeimage.hosted.RuntimeReflection; +import org.graalvm.nativeimage.impl.ConfigurationCondition; import com.oracle.graal.pointsto.infrastructure.OriginalClassProvider; import com.oracle.graal.pointsto.meta.AnalysisMetaAccess; @@ -73,6 +73,7 @@ import com.oracle.svm.core.annotate.Substitute; import com.oracle.svm.core.annotate.TargetClass; import com.oracle.svm.core.config.ConfigurationValues; +import com.oracle.svm.core.configure.ResourcesRegistry; import com.oracle.svm.core.hub.DynamicHub; import com.oracle.svm.core.util.UserError; import com.oracle.svm.core.util.VMError; @@ -107,12 +108,11 @@ import com.oracle.truffle.api.staticobject.StaticProperty; import com.oracle.truffle.api.staticobject.StaticShape; +import jdk.internal.misc.Unsafe; import jdk.vm.ci.meta.JavaKind; import jdk.vm.ci.meta.MetaAccessProvider; import jdk.vm.ci.meta.ResolvedJavaField; import jdk.vm.ci.meta.ResolvedJavaMethod; -import org.graalvm.nativeimage.impl.ConfigurationCondition; -import sun.misc.Unsafe; /** * Base feature for using Truffle in the SVM. If only this feature is used (not included through