1 /*
 2  * Copyright (c) 2022, 2024, 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 classes are preserved over safepoints at returns.
27  * @enablePreview
28  * @run main/othervm -XX:CompileCommand=dontinline,TestSafepointAtPollReturn::test* -XX:+UnlockDiagnosticVMOptions
29  *                   -XX:+SafepointALot -XX:-TieredCompilation TestSafepointAtPollReturn
30  */
31 
32 public class TestSafepointAtPollReturn {
33     static Integer INT_VAL = 0;
34 
35     static value class MyValue {
36         Integer val = INT_VAL;
37     }
38 
39     static public MyValue testValueCallee(boolean b) {
40         return b ? null : new MyValue();
41     }
42 
43     static public MyValue testValue(boolean b) {
44         return testValueCallee(b);
45     }
46 
47     public static void main(String[] args) {
48         for (int i = 0; i < 1_000_000_000; ++i) {
49             INT_VAL = i;
50             boolean b = (i % 2) == 0;
51             MyValue val = testValue(b);
52             if (b) {
53                 if (val != null) {
54                     throw new RuntimeException("testValue failed: result should be null");
55                 }
56             } else {
57                 int res = val.val;
58                 if (res != i) {
59                     throw new RuntimeException("testValue failed: " + res + " != " + i);
60                 }
61             }
62         }
63     }
64 }