1 /*
   2  * Copyright (c) 2017, 2018, Red Hat, Inc. 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 TestMaybeNullUnsafeAccess
  26  * @summary cast on before unsafe access moved in dominating null check null path causes crash
  27  * @key gc
  28  * @library /testlibrary
  29  *
  30  * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:-TieredCompilation
  31  *                   TestMaybeNullUnsafeAccess
  32  *
  33  * @run main/othervm -XX:-UseOnStackReplacement -XX:-BackgroundCompilation -XX:-TieredCompilation
  34  *                   -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC
  35  *                   TestMaybeNullUnsafeAccess
  36  *
  37  */
  38 
  39 import sun.misc.Unsafe;
  40 
  41 import java.lang.reflect.Field;
  42 
  43 import com.oracle.java.testlibrary.*;
  44 
  45 public class TestMaybeNullUnsafeAccess {
  46 
  47     static final sun.misc.Unsafe UNSAFE = Utils.getUnsafe();
  48     static final long F_OFFSET;
  49 
  50     static class A {
  51         int f;
  52     }
  53 
  54     static {
  55         try {
  56             Field fField = A.class.getDeclaredField("f");
  57             F_OFFSET = UNSAFE.objectFieldOffset(fField);
  58         } catch (Exception e) {
  59             throw new RuntimeException(e);
  60         }
  61     }
  62 
  63     static A test_helper(Object o) {
  64         return (A) o;
  65     }
  66 
  67     static int test(Object o) {
  68         int f = 0;
  69         for (int i = 0; i < 100; i++) {
  70             A a = test_helper(o);
  71             f = UNSAFE.getInt(a, F_OFFSET);
  72         }
  73         return f;
  74     }
  75 
  76     static public void main(String[] args) {
  77         A a = new A();
  78         for (int i = 0; i < 20000; i++) {
  79             test_helper(null);
  80             test_helper(a);
  81             test(a);
  82         }
  83     }
  84 
  85 }