1 /*
2 * Copyright (c) 2026, 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 Tests that JVM_IHashCode does not cache when a SOE occurs.
27 * @comment This test runs with the interpreter such that the value is always
28 * buffered, meaning that the identity hash code computed will be
29 * saved in the markWord.
30 * @enablePreview
31 * @requires vm.flagless
32 * @compile HashOverflowTest.java
33 * @run main/othervm -Xint -Xss256K
34 * runtime.valhalla.inlinetypes.HashOverflowTest
35 */
36
37 package runtime.valhalla.inlinetypes;
38
39 public class HashOverflowTest {
40 private static final int N_ELEMS = 1000;
41
42 public static void main(String[] args) {
43 Cons list = makeLargeDataStructure();
44 try {
45 System.identityHashCode(list);
46 throw new RuntimeException("expected to stack overflow when computing identity hash");
47 } catch (StackOverflowError expected) {
48 // Expected, continue execution.
49 }
50 try {
51 System.identityHashCode(list);
52 throw new RuntimeException("expected subsequent identity hash calls to also overflow");
53 } catch (StackOverflowError expected) {
54 // Expected, test passes!
55 }
56 }
57
58 private static Cons makeLargeDataStructure() {
59 Cons prev = new Cons(N_ELEMS - 1, null);
60 for (int i = N_ELEMS - 2; i >= 0; i--) {
61 Cons curr = new Cons(i, prev);
62 prev = curr;
63 }
64 return prev;
65 }
66
67 public static value record Cons(int x, Cons y) {}
68 }