1 /* 2 * Copyright (c) 2020, 2024, 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 package compiler.valhalla.inlinetypes; 25 26 import java.lang.invoke.*; 27 import java.lang.reflect.Field; 28 import java.lang.reflect.Method; 29 30 import jdk.test.lib.Asserts; 31 import jdk.internal.misc.Unsafe; 32 33 import jdk.internal.value.ValueClass; 34 import jdk.internal.vm.annotation.ImplicitlyConstructible; 35 import jdk.internal.vm.annotation.LooselyConsistentValue; 36 import jdk.internal.vm.annotation.NullRestricted; 37 38 /** 39 * @test TestBufferTearing 40 * @key randomness 41 * @summary Detect tearing on value class buffer writes due to missing barriers. 42 * @library /testlibrary /test/lib /compiler/whitebox / 43 * @enablePreview 44 * @modules java.base/jdk.internal.misc 45 * java.base/jdk.internal.value 46 * java.base/jdk.internal.vm.annotation 47 * @run main/othervm -XX:InlineFieldMaxFlatSize=0 -XX:FlatArrayElementMaxSize=0 48 * -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:+StressLCM 49 * compiler.valhalla.inlinetypes.TestBufferTearing 50 * @run main/othervm -XX:InlineFieldMaxFlatSize=0 -XX:FlatArrayElementMaxSize=0 51 * -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:+StressLCM 52 * -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline 53 * compiler.valhalla.inlinetypes.TestBufferTearing 54 * @run main/othervm -XX:InlineFieldMaxFlatSize=0 -XX:FlatArrayElementMaxSize=0 55 * -XX:CompileCommand=dontinline,*::incrementAndCheck* 56 * -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:+StressLCM 57 * compiler.valhalla.inlinetypes.TestBufferTearing 58 * @run main/othervm -XX:InlineFieldMaxFlatSize=0 -XX:FlatArrayElementMaxSize=0 59 * -XX:CompileCommand=dontinline,*::incrementAndCheck* 60 * -XX:+UnlockDiagnosticVMOptions -XX:+StressGCM -XX:+StressLCM 61 * -XX:+IgnoreUnrecognizedVMOptions -XX:+AlwaysIncrementalInline 62 * compiler.valhalla.inlinetypes.TestBufferTearing 63 */ 64 65 @ImplicitlyConstructible 66 @LooselyConsistentValue 67 value class MyValue { 68 int x; 69 int y; 70 71 private static final Unsafe U = Unsafe.getUnsafe(); 72 private static final long X_OFFSET; 73 private static final long Y_OFFSET; 74 static { 75 try { 76 Field xField = MyValue.class.getDeclaredField("x"); 77 X_OFFSET = U.objectFieldOffset(xField); 78 Field yField = MyValue.class.getDeclaredField("y"); 79 Y_OFFSET = U.objectFieldOffset(yField); 80 } catch (Exception e) { 81 throw new RuntimeException(e); 82 } 83 } 84 85 MyValue(int x, int y) { 86 this.x = x; 87 this.y = y; 88 } 89 90 MyValue incrementAndCheck() { 91 Asserts.assertEQ(x, y, "Inconsistent field values"); 92 return new MyValue(x + 1, y + 1); 93 } 94 95 MyValue incrementAndCheckUnsafe() { 96 Asserts.assertEQ(x, y, "Inconsistent field values"); 97 MyValue vt = U.makePrivateBuffer(this); 98 U.putInt(vt, X_OFFSET, x + 1); 99 U.putInt(vt, Y_OFFSET, y + 1); 100 return U.finishPrivateBuffer(vt); 101 } 102 } 103 104 public class TestBufferTearing { 105 @NullRestricted 106 static MyValue vtField1; 107 @NullRestricted 108 MyValue vtField2; 109 MyValue[] vtField3 = (MyValue[])ValueClass.newNullRestrictedArray(MyValue.class, 1); 110 111 static final MethodHandle incrementAndCheck_mh; 112 113 static { 114 try { 115 Class<?> clazz = MyValue.class; 116 MethodHandles.Lookup lookup = MethodHandles.lookup(); 117 118 MethodType mt = MethodType.methodType(MyValue.class); 119 incrementAndCheck_mh = lookup.findVirtual(clazz, "incrementAndCheck", mt); 120 } catch (NoSuchMethodException | IllegalAccessException e) { 121 e.printStackTrace(); 122 throw new RuntimeException("Method handle lookup failed"); 123 } 124 } 125 126 static class Runner extends Thread { 127 TestBufferTearing test; 128 129 public Runner(TestBufferTearing test) { 130 this.test = test; 131 } 132 133 public void run() { 134 for (int i = 0; i < 1_000_000; ++i) { 135 test.vtField1 = test.vtField1.incrementAndCheck(); 136 test.vtField2 = test.vtField2.incrementAndCheck(); 137 test.vtField3[0] = test.vtField3[0].incrementAndCheck(); 138 139 test.vtField1 = test.vtField1.incrementAndCheckUnsafe(); 140 test.vtField2 = test.vtField2.incrementAndCheckUnsafe(); 141 test.vtField3[0] = test.vtField3[0].incrementAndCheckUnsafe(); 142 143 try { 144 test.vtField1 = (MyValue)incrementAndCheck_mh.invokeExact(test.vtField1); 145 test.vtField2 = (MyValue)incrementAndCheck_mh.invokeExact(test.vtField2); 146 test.vtField3[0] = (MyValue)incrementAndCheck_mh.invokeExact(test.vtField3[0]); 147 } catch (Throwable t) { 148 throw new RuntimeException("Invoke failed", t); 149 } 150 } 151 } 152 } 153 154 public static void main(String[] args) throws Exception { 155 // Create threads that concurrently update some value class (array) fields 156 // and check the fields of the value classes for consistency to detect tearing. 157 TestBufferTearing test = new TestBufferTearing(); 158 Thread runner = null; 159 for (int i = 0; i < 10; ++i) { 160 runner = new Runner(test); 161 runner.start(); 162 } 163 runner.join(); 164 } 165 }