1 /*
   2  * Copyright (c) 2014 SAP SE. 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
  26  * @bug 8038048
  27  * @summary assert(null_obj->escape_state() == PointsToNode::NoEscape,etc)
  28  * @modules java.base/jdk.internal.misc
  29  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+DoEscapeAnalysis -XX:-TieredCompilation -Xbatch TestUnsafePutAddressNullObjMustNotEscape
  30  * @author Richard Reingruber richard DOT reingruber AT sap DOT com
  31  */
  32 
  33 import java.lang.reflect.Field;
  34 import jdk.internal.misc.Unsafe;
  35 
  36 public class TestUnsafePutAddressNullObjMustNotEscape {
  37 
  38     public static Unsafe usafe;
  39     public static long mem;
  40     public static long checksum;
  41 
  42     public static void main(String[] args) throws Exception {
  43         System.out.println("EXECUTING test.");
  44 
  45         {
  46             System.out.println("Acquiring jdk.internal.misc.Unsafe.theUnsafe using reflection.");
  47             getUnsafe();
  48             System.out.println("Allocating raw memory.");
  49             mem = (usafe.allocateMemory(1024) + 8L) & ~7L;
  50             System.out.println("Triggering JIT compilation of the test method");
  51             triggerJitCompilationOfTestMethod();
  52         }
  53 
  54         System.out.println("SUCCESSFULLY passed test.");
  55     }
  56 
  57     public static void triggerJitCompilationOfTestMethod() {
  58         long sum = 0;
  59         for (int ii = 50000; ii >= 0; ii--) {
  60             sum = testMethod();
  61         }
  62         checksum = sum;
  63     }
  64 
  65     public static class IDGen {
  66         private static long id;
  67         public long nextId() {
  68             return id++;
  69         }
  70     }
  71 
  72     public static long testMethod() {
  73         // dummy alloc to trigger escape analysis
  74         IDGen gen = new IDGen();
  75         // StoreP of null_obj to raw mem triggers assertion in escape analysis
  76         usafe.putAddress(mem, 0L);
  77         return gen.nextId();
  78     }
  79 
  80     private static void getUnsafe() throws Exception {
  81         Field field = jdk.internal.misc.Unsafe.class.getDeclaredField("theUnsafe");
  82         field.setAccessible(true);
  83         usafe = (jdk.internal.misc.Unsafe) field.get(null);
  84     }
  85 }