1 /* 2 * Copyright (c) 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 /** 25 * @test ValueCopyingTest 26 * @summary Verify that interpreter doesn't tear up primitive fields when copying a value 27 * @library /test/lib 28 * @modules java.base/jdk.internal.vm.annotation 29 * @enablePreview 30 * @compile ValueCopyingTest.java 31 * @run main/othervm -Xint -XX:+UnlockDiagnosticVMOptions -XX:+PrintInlineLayout runtime.valhalla.inlinetypes.ValueCopyingTest 32 */ 33 34 package runtime.valhalla.inlinetypes; 35 36 import jdk.internal.vm.annotation.ImplicitlyConstructible; 37 import jdk.internal.vm.annotation.LooselyConsistentValue; 38 import jdk.internal.vm.annotation.NullRestricted; 39 import jdk.test.lib.Asserts; 40 41 public class ValueCopyingTest { 42 43 static final int NUM_WORKERS = 16; 44 45 static ValueCopyingTest target = new ValueCopyingTest(); 46 47 @ImplicitlyConstructible 48 @LooselyConsistentValue 49 static value class TestValue { 50 int i; 51 byte b; 52 TestValue(int i0) { 53 i = i0; 54 b = 0; 55 } 56 } 57 58 @NullRestricted 59 TestValue tv; 60 61 static class Worker implements Runnable { 62 int i; 63 TestValue v; 64 Worker(byte b) { 65 i = b | (b << 8) | (b << 16) | (b << 24); 66 v = new TestValue(i); 67 } 68 69 static void checkValue(int i) { 70 byte b = (byte)(i & 0xFF); 71 Asserts.assertTrue(((i >> 8) & 0xFF) == b, "Tearing detected"); 72 Asserts.assertTrue(((i >> 16) & 0xFF) == b, "Tearing detected"); 73 Asserts.assertTrue(((i >> 24) & 0xFF) == b, "Tearing detected"); 74 } 75 76 public void run() { 77 for (int n = 0; n < 10000000; n++) { 78 ValueCopyingTest.target.tv = v; 79 int ri = ValueCopyingTest.target.tv.i; 80 checkValue(ri); 81 } 82 } 83 } 84 85 static public void main(String[] args) throws InterruptedException { 86 Thread[] workers = new Thread[NUM_WORKERS]; 87 for (int i = 0; i < NUM_WORKERS; i++) { 88 workers[i] = new Thread(new Worker((byte)i)); 89 } 90 for (int i = 0; i < NUM_WORKERS; i++) { 91 workers[i].start(); 92 } 93 try { 94 for (int i = 0; i < NUM_WORKERS; i++) { 95 workers[i].join(); 96 } 97 } catch(InterruptedException e) { 98 e.printStackTrace(); 99 throw e; 100 } 101 } 102 }