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.jdk9_11.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 @AddExports("java.base/jdk.internal.misc") 38 public class UnsafeObjectReplacementsTest extends MethodSubstitutionTest { 39 40 static class Container { 41 public volatile Object objectField = dummyValue; 42 } 43 44 static jdk.internal.misc.Unsafe unsafe = jdk.internal.misc.Unsafe.getUnsafe(); 45 static Container dummyValue = new Container(); 46 static Container newDummyValue = new Container(); 47 static long objectOffset; 48 49 static { 50 try { 51 objectOffset = unsafe.objectFieldOffset(Container.class.getDeclaredField("objectField")); 52 } catch (NoSuchFieldException e) { 53 throw new RuntimeException(e); 54 } 55 } 56 57 public static Object unsafeCompareAndExchangeObject() { 58 Container container = new Container(); 59 return unsafe.compareAndExchangeObject(container, objectOffset, dummyValue, newDummyValue); 60 } 61 62 @Test 63 public void testCompareAndSet() { 64 TargetDescription target = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getTarget(); 65 if (target.arch instanceof AMD64) { 66 testGraph("unsafeCompareAndExchangeObject"); 67 } 68 test("unsafeCompareAndExchangeObject"); 69 } 70 71 public static Object unsafeGetAndSetObject() { 72 Container container = new Container(); 73 container.objectField = null; 74 Container other = new Container(); 75 return unsafe.getAndSetObject(container, objectOffset, other); 76 } 77 78 @Test 79 public void testGetAndSet() { 80 TargetDescription target = Graal.getRequiredCapability(RuntimeProvider.class).getHostBackend().getTarget(); 81 if (target.arch instanceof AMD64 || target.arch instanceof AArch64) { 82 testGraph("unsafeGetAndSetObject"); 83 } 84 test("unsafeGetAndSetObject"); 85 } 86 87 public static Object unsafeGetPutObject() { 88 Container container = new Container(); 89 unsafe.putObject(container, objectOffset, "Hello there"); 90 return unsafe.getObject(container, objectOffset); 91 } 92 93 public static Object unsafeGetPutObjectOpaque() { 94 Container container = new Container(); 95 unsafe.putObjectOpaque(container, objectOffset, "Hello there"); 96 return unsafe.getObjectOpaque(container, objectOffset); 97 } 98 99 public static Object unsafeGetPutObjectRA() { 100 Container container = new Container(); 101 unsafe.putObjectRelease(container, objectOffset, "Hello there"); 102 return unsafe.getObjectAcquire(container, objectOffset); 103 } 104 105 public static Object unsafeGetPutObjectVolatile() { 106 Container container = new Container(); 107 unsafe.putObjectVolatile(container, objectOffset, "Hello there"); 108 return unsafe.getObjectVolatile(container, objectOffset); 109 } 110 111 @Test 112 public void testUnsafeGetPutPlain() { 113 testGraph("unsafeGetPutObject"); 114 test("unsafeGetPutObject"); 115 } 116 117 @Test 118 public void testUnsafeGetPutOpaque() { 119 testGraph("unsafeGetPutObjectOpaque"); 120 test("unsafeGetPutObjectOpaque"); 121 } 122 123 @Test 124 public void testUnsafeGetPutReleaseAcquire() { 125 testGraph("unsafeGetPutObjectRA"); 126 test("unsafeGetPutObjectRA"); 127 } 128 129 @Test 130 public void testUnsafeGetPutVolatile() { 131 testGraph("unsafeGetPutObjectVolatile"); 132 test("unsafeGetPutObjectVolatile"); 133 } 134 }