1 /*
 2  * Copyright (c) 2023, Arm Limited. All rights reserved.
 3  * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
 4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 5  *
 6  * This code is free software; you can redistribute it and/or modify it
 7  * under the terms of the GNU General Public License version 2 only, as
 8  * published by the Free Software Foundation.
 9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /**
26  * @test
27  * @bug 8303416
28  * @summary Fix JVM crash at Unsafe_FinishPrivateBuffer
29  * @library /test/lib
30  * @enablePreview
31  * @modules java.base/jdk.internal.misc
32  * @run main/othervm compiler.valhalla.inlinetypes.TestLarvalState
33  */
34 
35 package compiler.valhalla.inlinetypes;
36 
37 import java.lang.reflect.*;
38 import java.util.Random;
39 
40 import jdk.internal.misc.Unsafe;
41 import jdk.test.lib.Asserts;
42 import jdk.test.lib.Utils;
43 
44 public class TestLarvalState {
45     private static int LENGTH = 10000;
46 
47     private static final Random RD = Utils.getRandomInstance();
48 
49     static byte[] arr = new byte[LENGTH];
50 
51     static {
52         for (int i = 0; i < LENGTH; i++) {
53             arr[i] = (byte) RD.nextInt(127);
54         }
55     }
56 
57     public static byte test(byte b) {
58         Value obj = new Value();
59         obj = Unsafe.getUnsafe().makePrivateBuffer(obj);
60         Unsafe.getUnsafe().putByte(obj, obj.offset, b);
61         obj = Unsafe.getUnsafe().finishPrivateBuffer(obj);
62         return Unsafe.getUnsafe().getByte(obj, obj.offset);
63     }
64 
65     public static void main(String[] args) {
66         byte actual = 0;
67         for (int i = 0; i < LENGTH; i++) {
68             actual += test(arr[i]);
69         }
70 
71         byte expected = 0;
72         for (int i = 0; i < LENGTH; i++) {
73             expected += arr[i];
74         }
75         Asserts.assertEquals(expected, actual);
76     }
77 
78     static value class Value {
79         byte field = 0;
80 
81         static long offset = fieldOffset();
82 
83         private static long fieldOffset() {
84             try {
85                 var f = Value.class.getDeclaredField("field");
86                 return Unsafe.getUnsafe().objectFieldOffset(f);
87             } catch (Exception e) {
88                 System.out.println(e);
89             }
90             return -1L;
91         }
92     }
93 }
94