1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.replacements.jdk12.test; 26 27 import org.graalvm.compiler.api.test.Graal; 28 import org.graalvm.compiler.replacements.test.MethodSubstitutionTest; 29 import org.graalvm.compiler.runtime.RuntimeProvider; 30 import org.graalvm.compiler.test.AddExports; 31 import org.junit.Test; 32 33 import jdk.vm.ci.aarch64.AArch64; 34 import jdk.vm.ci.amd64.AMD64; 35 import jdk.vm.ci.code.TargetDescription; 36 37 /** 38 * As of JDK 12 {@code jdk.internal.misc.Unsafe::.*Object()} methods were renamed to 39 * {@code .*Reference()}. 40 * 41 * @see "https://bugs.openjdk.java.net/browse/JDK-8207146" 42 */ 43 @AddExports("java.base/jdk.internal.misc") 44 public class UnsafeObjectReplacementsTest extends MethodSubstitutionTest { 45 46 static class Container { 47 public volatile Object objectField = dummyValue; 48 } 49 50 static jdk.internal.misc.Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); 51 static Container dummyValue = new Container(); 52 static Container newDummyValue = new Container(); 53 static long objectOffset; 54 55 static { 56 try { 57 objectOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("objectField")); 58 } catch (NoSuchFieldException e) { 59 throw new RuntimeException(e); 60 } 61 } 62 63 public static Object unsafeCompareAndExchangeReference() { 64 Container container = new Container(); 65 return unsafe.compareAndExchangeReference(container, objectOffset, dummyValue, newDummyValue); 66 } 67 68 @Test 69 public void testCompareAndSet() { 70 TargetDescription target = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getTarget(); 71 if (target.arch instanceof AMD64) { 72 testGraph("unsafeCompareAndExchangeReference"); 73 } 74 test("unsafeCompareAndExchangeReference"); 75 } 76 77 public static Object unsafeGetAndSetReference() { 78 Container container = new Container(); 79 container.objectField = null; 80 Container other = new Container(); 81 return unsafe.getAndSetReference(container, objectOffset, other); 82 } 83 84 @Test 85 public void testGetAndSet() { 86 TargetDescription target = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getTarget(); 87 if (target.arch instanceof AMD64 || target.arch instanceof AArch64) { 88 testGraph("unsafeGetAndSetReference"); 89 } 90 test("unsafeGetAndSetReference"); 91 } 92 93 public static Object unsafeGetPutReference() { 94 Container container = new Container(); 95 unsafe.putReference(container, objectOffset, "Hello there"); 96 return unsafe.getReference(container, objectOffset); 97 } 98 99 public static Object unsafeGetPutReferenceOpaque() { 100 Container container = new Container(); 101 unsafe.putReferenceOpaque(container, objectOffset, "Hello there"); 102 return unsafe.getReferenceOpaque(container, objectOffset); 103 } 104 105 public static Object unsafeGetPutReferenceRA() { 106 Container container = new Container(); 107 unsafe.putReferenceRelease(container, objectOffset, "Hello there"); 108 return unsafe.getReferenceAcquire(container, objectOffset); 109 } 110 111 public static Object unsafeGetPutReferenceVolatile() { 112 Container container = new Container(); 113 unsafe.putReferenceVolatile(container, objectOffset, "Hello there"); 114 return unsafe.getReferenceVolatile(container, objectOffset); 115 } 116 117 @Test 118 public void testUnsafeGetPutPlain() { 119 testGraph("unsafeGetPutReference"); 120 test("unsafeGetPutReference"); 121 } 122 123 @Test 124 public void testUnsafeGetPutOpaque() { 125 testGraph("unsafeGetPutReferenceOpaque"); 126 test("unsafeGetPutReferenceOpaque"); 127 } 128 129 @Test 130 public void testUnsafeGetPutReleaseAcquire() { 131 testGraph("unsafeGetPutReferenceRA"); 132 test("unsafeGetPutReferenceRA"); 133 } 134 135 @Test 136 public void testUnsafeGetPutVolatile() { 137 testGraph("unsafeGetPutReferenceVolatile"); 138 test("unsafeGetPutReferenceVolatile"); 139 } 140 }