Path: blob/master/test/hotspot/jtreg/gc/shenandoah/compiler/TestMaybeNullUnsafeAccess.java
41153 views
/*1* Copyright (c) 2017, 2018, 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*/2223/**24* @test TestMaybeNullUnsafeAccess25* @summary cast before unsafe access moved in dominating null check null path causes crash26* @requires vm.gc.Shenandoah27* @modules java.base/jdk.internal.misc:+open28*29* @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:-TieredCompilation30* TestMaybeNullUnsafeAccess31*32* @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:-TieredCompilation33* -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC34* TestMaybeNullUnsafeAccess35*36*/3738import jdk.internal.misc.Unsafe;3940import java.lang.reflect.Field;4142public class TestMaybeNullUnsafeAccess {4344static final jdk.internal.misc.Unsafe UNSAFE = Unsafe.getUnsafe();45static final long F_OFFSET;4647static class A {48int f;49}5051static {52try {53Field fField = A.class.getDeclaredField("f");54F_OFFSET = UNSAFE.objectFieldOffset(fField);55} catch (Exception e) {56throw new RuntimeException(e);57}58}5960static A test_helper(Object o) {61return (A) o;62}6364static int test(Object o) {65int f = 0;66for (int i = 0; i < 100; i++) {67A a = test_helper(o);68f = UNSAFE.getInt(a, F_OFFSET);69}70return f;71}7273static public void main(String[] args) {74A a = new A();75for (int i = 0; i < 20000; i++) {76test_helper(null);77test_helper(a);78test(a);79}80}8182}838485