Skip to content

Commit 28c332d

Browse files
committed
[GR-54555] Replace sun.misc.Unsafe with jdk.internal.misc.Unsafe in the compiler.
PullRequest: graal/17949
2 parents 8100e72 + 65eb7a5 commit 28c332d

File tree

43 files changed

+211
-368
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+211
-368
lines changed

compiler/mx.compiler/mx_compiler.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ def apply(self, config):
832832
if _get_XX_option_value(vmArgs, 'TypeProfileWidth', None) is None:
833833
vmArgs.append('-XX:TypeProfileWidth=8')
834834

835+
vmArgs.append('--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED')
835836
# TODO: GR-31197, this should be removed.
836837
vmArgs.append('-Dpolyglot.engine.DynamicCompilationThresholds=false')
837838
vmArgs.append('-Dpolyglot.engine.AllowExperimentalOptions=true')
@@ -1112,8 +1113,14 @@ def collate_metrics(args):
11121113

11131114
def run_java(args, out=None, err=None, addDefaultArgs=True, command_mapper_hooks=None, jdk=None, **kw_args):
11141115
graaljdk = jdk or get_graaljdk()
1116+
all_modules = [jmd.name for jmd in graaljdk.get_modules()]
11151117
vm_args = _parseVmArgs(args, addDefaultArgs=addDefaultArgs)
1116-
args = ['-XX:+UnlockExperimentalVMOptions', '-XX:+EnableJVMCI', '--add-exports=java.base/jdk.internal.misc=jdk.graal.compiler'] + vm_args
1118+
args = ['-XX:+UnlockExperimentalVMOptions', '-XX:+EnableJVMCI']
1119+
if 'com.oracle.graal.graal_enterprise' in all_modules:
1120+
args += ['--add-exports=java.base/jdk.internal.misc=jdk.graal.compiler,com.oracle.graal.graal_enterprise']
1121+
else:
1122+
args += ['--add-exports=java.base/jdk.internal.misc=jdk.graal.compiler']
1123+
args += vm_args
11171124
_check_bootstrap_config(args)
11181125
cmd = get_vm_prefix() + [graaljdk.java] + ['-server'] + args
11191126
map_file = join(graaljdk.home, 'proguard.map')

compiler/mx.compiler/suite.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@
168168
],
169169
"requires" : [
170170
"jdk.internal.vm.ci",
171-
"jdk.unsupported",
172171
"java.logging",
173172
],
174173
"requiresConcealed" : {
@@ -527,9 +526,6 @@
527526
# This distribution defines a module.
528527
"moduleInfo" : {
529528
"name" : "jdk.graal.compiler",
530-
"requires" : [
531-
"jdk.unsupported" # sun.misc.Unsafe
532-
],
533529
"exports" : [
534530
"""* to com.oracle.graal.graal_enterprise,
535531
org.graalvm.nativeimage.pointsto,

compiler/src/jdk.graal.compiler.hotspot.jdk21.test/src/jdk/graal/compiler/hotspot/jdk21/test/PreviewEnabledTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,16 +26,15 @@
2626

2727
import java.io.IOException;
2828

29+
import org.junit.Assume;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
2933
import jdk.graal.compiler.api.test.ModuleSupport;
3034
import jdk.graal.compiler.core.test.SubprocessTest;
3135
import jdk.graal.compiler.hotspot.test.HotSpotGraalCompilerTest;
32-
import jdk.graal.compiler.test.AddExports;
3336
import jdk.graal.compiler.test.SubprocessUtil;
34-
import org.junit.Assume;
35-
import org.junit.Before;
36-
import org.junit.Test;
3737

38-
@AddExports("java.base/jdk.internal.misc")
3938
public class PreviewEnabledTest extends HotSpotGraalCompilerTest {
4039

4140
@Before

compiler/src/jdk.graal.compiler.hotspot.jdk21.test/src/jdk/graal/compiler/hotspot/jdk21/test/ScopedValueCacheTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,9 +27,7 @@
2727
import java.lang.reflect.Method;
2828

2929
import jdk.graal.compiler.hotspot.test.HotSpotGraalCompilerTest;
30-
import jdk.graal.compiler.test.AddExports;
3130

32-
@AddExports("java.base/jdk.internal.misc")
3331
public class ScopedValueCacheTest extends HotSpotGraalCompilerTest {
3432

3533
private static boolean contains(Object[] array, Object value) {

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/amd64/test/AMD64VolatileWriteMembarEliminationTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,13 +26,13 @@
2626

2727
import static org.junit.Assume.assumeTrue;
2828

29+
import org.junit.Before;
30+
import org.junit.Test;
31+
2932
import jdk.graal.compiler.core.common.memory.MemoryOrderMode;
3033
import jdk.graal.compiler.core.test.GraalCompilerTest;
3134
import jdk.graal.compiler.nodes.StructuredGraph;
3235
import jdk.graal.compiler.nodes.memory.WriteNode;
33-
import org.junit.Before;
34-
import org.junit.Test;
35-
3636
import jdk.vm.ci.amd64.AMD64;
3737

3838
public class AMD64VolatileWriteMembarEliminationTest extends GraalCompilerTest {
@@ -55,9 +55,9 @@ public void checkAMD64() {
5555

5656
public static void testMethod1() {
5757
Object[] array = objectArray;
58-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset, array);
59-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset + (1 * objectArrayIndexScale), array);
60-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset + (2 * objectArrayIndexScale), array);
58+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset, array);
59+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset + (1 * objectArrayIndexScale), array);
60+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset + (2 * objectArrayIndexScale), array);
6161
}
6262

6363
static int expectedVolatiles;
@@ -77,10 +77,10 @@ protected void checkLowTierGraph(StructuredGraph graph) {
7777
public static int testMethod2() {
7878
int result = 0;
7979
Object[] array = objectArray;
80-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset, array);
80+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset, array);
8181
result += field1;
82-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset + (1 * objectArrayIndexScale), array);
83-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset + (2 * objectArrayIndexScale), array);
82+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset + (1 * objectArrayIndexScale), array);
83+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset + (2 * objectArrayIndexScale), array);
8484
return result;
8585
}
8686

@@ -93,11 +93,11 @@ public void test2() {
9393
public static int testMethod3() {
9494
int result = 0;
9595
Object[] array = objectArray;
96-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset, array);
96+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset, array);
9797
field1 = 1;
9898
field2 = 2;
99-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset + (1 * objectArrayIndexScale), array);
100-
UNSAFE.putObjectVolatile(array, objectArrayBaseOffset + (2 * objectArrayIndexScale), array);
99+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset + (1 * objectArrayIndexScale), array);
100+
UNSAFE.putReferenceVolatile(array, objectArrayBaseOffset + (2 * objectArrayIndexScale), array);
101101
return result;
102102
}
103103

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/MarkUnsafeAccessTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -36,20 +36,20 @@
3636
import java.nio.file.Files;
3737
import java.nio.file.Path;
3838

39-
import jdk.graal.compiler.nodes.StructuredGraph;
40-
import jdk.graal.compiler.phases.common.inlining.InliningPhase;
41-
import jdk.graal.compiler.phases.common.inlining.policy.InlineEverythingPolicy;
42-
import jdk.graal.compiler.phases.tiers.HighTierContext;
4339
import org.junit.Assert;
4440
import org.junit.Assume;
4541
import org.junit.Test;
4642

43+
import jdk.graal.compiler.nodes.StructuredGraph;
44+
import jdk.graal.compiler.phases.common.inlining.InliningPhase;
45+
import jdk.graal.compiler.phases.common.inlining.policy.InlineEverythingPolicy;
46+
import jdk.graal.compiler.phases.tiers.HighTierContext;
47+
import jdk.internal.misc.Unsafe;
4748
import jdk.vm.ci.code.InstalledCode;
4849
import jdk.vm.ci.code.InvalidInstalledCodeException;
4950
import jdk.vm.ci.meta.Assumptions.AssumptionResult;
5051
import jdk.vm.ci.meta.ResolvedJavaMethod;
5152
import jdk.vm.ci.meta.ResolvedJavaType;
52-
import sun.misc.Unsafe;
5353

5454
public class MarkUnsafeAccessTest extends GraalCompilerTest {
5555

@@ -72,7 +72,7 @@ public void put() {
7272
}
7373

7474
public void cas() {
75-
unsafe.compareAndSwapInt(null, 0, 0, 0);
75+
unsafe.compareAndSetInt(null, 0, 0, 0);
7676
}
7777

7878
public void noAccess() {

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/ReadConstantFoldingTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -37,6 +37,10 @@
3737
import java.util.ArrayList;
3838
import java.util.List;
3939

40+
import org.junit.Assert;
41+
import org.junit.Before;
42+
import org.junit.Test;
43+
4044
import jdk.graal.compiler.api.replacements.SnippetReflectionProvider;
4145
import jdk.graal.compiler.graph.iterators.NodeIterable;
4246
import jdk.graal.compiler.nodes.ConstantNode;
@@ -49,11 +53,6 @@
4953
import jdk.graal.compiler.options.OptionValues;
5054
import jdk.graal.compiler.phases.PhaseSuite;
5155
import jdk.graal.compiler.phases.tiers.HighTierContext;
52-
import jdk.graal.compiler.test.AddExports;
53-
import org.junit.Assert;
54-
import org.junit.Before;
55-
import org.junit.Test;
56-
5756
import jdk.internal.misc.Unsafe;
5857
import jdk.vm.ci.code.InstalledCode;
5958
import jdk.vm.ci.meta.JavaConstant;
@@ -63,7 +62,6 @@
6362
* Exercise the constant folding of {@link RawLoadNode} and it's lowered form to ensure that it
6463
* always succeeds when it's possible.
6564
*/
66-
@AddExports("java.base/jdk.internal.misc")
6765
public class ReadConstantFoldingTest extends GraalCompilerTest {
6866
static final Unsafe U = Unsafe.getUnsafe();
6967
private static final List<Object> StableArrays = new ArrayList<>();

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/SpectreFenceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,10 @@
3232
import static org.junit.Assume.assumeTrue;
3333

3434
import org.graalvm.collections.EconomicMap;
35+
import org.junit.Assert;
36+
import org.junit.Before;
37+
import org.junit.Test;
38+
3539
import jdk.graal.compiler.api.directives.GraalDirectives;
3640
import jdk.graal.compiler.api.test.Graal;
3741
import jdk.graal.compiler.core.common.SpectrePHTMitigations;
@@ -40,14 +44,10 @@
4044
import jdk.graal.compiler.options.OptionKey;
4145
import jdk.graal.compiler.options.OptionValues;
4246
import jdk.graal.compiler.runtime.RuntimeProvider;
43-
import org.junit.Assert;
44-
import org.junit.Before;
45-
import org.junit.Test;
46-
47+
import jdk.internal.misc.Unsafe;
4748
import jdk.vm.ci.aarch64.AArch64;
4849
import jdk.vm.ci.amd64.AMD64;
4950
import jdk.vm.ci.code.Architecture;
50-
import sun.misc.Unsafe;
5151

5252
public class SpectreFenceTest extends GraalCompilerTest {
5353

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/StampMemoryAccessTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void testReadPrimitiveUnaligned() throws Exception {
7373
Object object = this;
7474
JavaConstant objectBase = getSnippetReflection().forObject(object);
7575
Field f = getClass().getDeclaredField("longField1");
76-
long baseDisplacement = getObjectFieldOffset(f);
76+
long baseDisplacement = UNSAFE.objectFieldOffset(f);
7777
for (JavaKind kind : JavaKind.values()) {
7878
if (kind.isPrimitive() && kind != JavaKind.Void && kind.getByteCount() > 1) {
7979
for (long offset = 1; offset < kind.getByteCount(); offset++) {

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/SubWordArrayStoreTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -162,7 +162,7 @@ protected byte[] generateClass(String internalClassName) {
162162
snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
163163
snippet.visitLdcInsn(arrayBaseOffset(kind));
164164
snippet.visitLdcInsn(value);
165-
snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "put" + SubWordTestUtil.getUnsafePutMethodName(kind), "(Ljava/lang/Object;J" + kind.getTypeChar() + ")V", false);
165+
snippet.visitMethodInsn(INVOKEVIRTUAL, "jdk/internal/misc/Unsafe", "put" + SubWordTestUtil.getUnsafePutMethodName(kind), "(Ljava/lang/Object;J" + kind.getTypeChar() + ")V", false);
166166
} else {
167167
snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
168168
snippet.visitInsn(ICONST_0);
@@ -174,7 +174,7 @@ protected byte[] generateClass(String internalClassName) {
174174
SubWordTestUtil.getUnsafe(snippet);
175175
snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
176176
snippet.visitLdcInsn(arrayBaseOffset(kind));
177-
snippet.visitMethodInsn(INVOKEVIRTUAL, "sun/misc/Unsafe", "get" + SubWordTestUtil.getUnsafePutMethodName(kind), "(Ljava/lang/Object;J)" + kind.getTypeChar(), false);
177+
snippet.visitMethodInsn(INVOKEVIRTUAL, "jdk/internal/misc/Unsafe", "get" + SubWordTestUtil.getUnsafePutMethodName(kind), "(Ljava/lang/Object;J)" + kind.getTypeChar(), false);
178178
} else {
179179
snippet.visitFieldInsn(GETSTATIC, internalClassName, fieldName, fieldDescriptor);
180180
snippet.visitInsn(ICONST_0);

0 commit comments

Comments
 (0)