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