Path: blob/master/test/hotspot/jtreg/compiler/c1/UnsafeVolatileGuardTest.java
41152 views
/*1* Copyright (c) 2017, Red Hat Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.reflect.Field;2425/**26* @test27* @bug 817588728* @summary C1 value numbering handling of Unsafe.get*Volatile is incorrect29* @modules java.base/jdk.internal.misc30* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:TieredStopAtLevel=1 UnsafeVolatileGuardTest31*/32public class UnsafeVolatileGuardTest {33volatile static private int a;34static private int b;3536static final jdk.internal.misc.Unsafe UNSAFE = jdk.internal.misc.Unsafe.getUnsafe();3738static final Object BASE;39static final long OFFSET;4041static {42try {43Field f = UnsafeVolatileGuardTest.class.getDeclaredField("a");44BASE = UNSAFE.staticFieldBase(f);45OFFSET = UNSAFE.staticFieldOffset(f);46} catch (Exception e) {47throw new RuntimeException(e);48}49}5051static void test() {52int tt = b; // makes the JVM CSE the value of b5354while (UNSAFE.getIntVolatile(BASE, OFFSET) == 0) {} // burn55if (b == 0) {56System.err.println("wrong value of b");57System.exit(1); // fail hard to report the error58}59}6061public static void main(String [] args) throws Exception {62for (int i = 0; i < 10; i++) {63new Thread(UnsafeVolatileGuardTest::test).start();64}65b = 1;66a = 1;67}68}697071