1 /*
 2  * Copyright (c) 2022, Oracle and/or its affiliates. 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  * @summary Test that oop fields of value/primitive classes are preserved over safepoints at returns.
27  * @compile -XDenablePrimitiveClasses TestSafepointAtPollReturn.java
28  * @run main/othervm -XX:+EnableValhalla -XX:+EnablePrimitiveClasses
29  *                   -XX:CompileCommand=dontinline,TestSafepointAtPollReturn::test* -XX:+UnlockDiagnosticVMOptions
30  *                   -XX:+SafepointALot -XX:-TieredCompilation TestSafepointAtPollReturn
31  */
32 
33 public class TestSafepointAtPollReturn {
34     static Integer INT_VAL = 0;
35 
36     static value class MyValue {
37         Integer val = INT_VAL;
38     }
39 
40     static primitive class MyPrimitive {
41         Integer val = INT_VAL;
42     }
43 
44     static public MyValue testValueCallee(boolean b) {
45         return b ? null : new MyValue();
46     }
47 
48     static public MyValue testValue(boolean b) {
49         return testValueCallee(b);
50     }
51 
52     static public MyPrimitive testPrimitiveCallee() {
53         return new MyPrimitive();
54     }
55 
56     static public MyPrimitive testPrimitive() {
57         return testPrimitiveCallee();
58     }
59 
60     public static void main(String[] args) {
61         for (int i = 0; i < 1_000_000_000; ++i) {
62             INT_VAL = i;
63             boolean b = (i % 2) == 0;
64             MyValue val = testValue(b);
65             if (b) {
66                 if (val != null) {
67                     throw new RuntimeException("testValue failed: result should be null");
68                 }
69             } else {
70                 int res = val.val;
71                 if (res != i) {
72                     throw new RuntimeException("testValue failed: " + res + " != " + i);
73                 }
74             }
75             int res = testPrimitive().val;
76             if (res != i) {
77                 throw new RuntimeException("testPrimitive failed: " + res + " != " + i);
78             }
79         }
80     }
81 }