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