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