|
1 | 1 | /* |
2 | | - * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2023, 2025, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
29 | 29 | * cause, even if we can't create the ExceptionInInitializerError |
30 | 30 | * @comment This test could easily be perturbed so don't allow flag settings. |
31 | 31 | * @requires vm.flagless |
| 32 | + * @library /test/lib |
32 | 33 | * @comment Run with the smallest stack possible to limit the execution time. |
33 | 34 | * This is the smallest stack that is supported by all platforms. |
34 | | - * @run main/othervm -Xss384K -Xint TestStackOverflowDuringInit |
| 35 | + * @build jdk.test.whitebox.WhiteBox |
| 36 | + * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox |
| 37 | + * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI TestStackOverflowDuringInit |
35 | 38 | */ |
36 | 39 |
|
37 | 40 | import java.io.ByteArrayOutputStream; |
38 | 41 | import java.io.PrintStream; |
| 42 | +import jdk.test.lib.process.OutputAnalyzer; |
| 43 | +import jdk.test.lib.process.ProcessTools; |
| 44 | +import jdk.test.whitebox.WhiteBox; |
39 | 45 |
|
40 | 46 | public class TestStackOverflowDuringInit { |
41 | 47 |
|
| 48 | + static String expected = "java.lang.NoClassDefFoundError: Could not initialize class TestStackOverflowDuringInit$LongCache"; |
| 49 | + static String cause = "Caused by: java.lang.StackOverflowError"; |
| 50 | + |
42 | 51 | // The setup for this is somewhat intricate. We need to trigger a |
43 | 52 | // StackOverflowError during execution of the static initializer |
44 | 53 | // for a class, but we need there to be insufficient stack left |
@@ -88,34 +97,47 @@ static void recurse() { |
88 | 97 | } |
89 | 98 | } |
90 | 99 |
|
91 | | - public static void main(String[] args) throws Exception { |
92 | | - String expected = "java.lang.NoClassDefFoundError: Could not initialize class TestStackOverflowDuringInit$LongCache"; |
93 | | - String cause = "Caused by: java.lang.StackOverflowError"; |
| 100 | + static class Launcher { |
| 101 | + public static void main(String[] args) throws Exception { |
94 | 102 |
|
95 | | - // Pre-load, but not initialize, LongCache, else we will |
96 | | - // hit SOE during class loading. |
97 | | - System.out.println("Pre-loading ..."); |
98 | | - Class<?> c = Class.forName("TestStackOverflowDuringInit$LongCache", |
99 | | - false, |
100 | | - TestStackOverflowDuringInit.class.getClassLoader()); |
101 | | - try { |
102 | | - recurse(); |
103 | | - } catch (Throwable ex) { |
104 | | - // ex.printStackTrace(); |
105 | | - verify_stack(ex, expected, cause); |
| 103 | + // Pre-load, but not initialize, LongCache, else we will |
| 104 | + // hit SOE during class loading. |
| 105 | + System.out.println("Pre-loading ..."); |
| 106 | + Class<?> c = Class.forName("TestStackOverflowDuringInit$LongCache", |
| 107 | + false, |
| 108 | + TestStackOverflowDuringInit.class.getClassLoader()); |
| 109 | + try { |
| 110 | + recurse(); |
| 111 | + } catch (Throwable ex) { |
| 112 | + // ex.printStackTrace(); |
| 113 | + verify_stack(ex, expected, cause); |
| 114 | + } |
106 | 115 | } |
107 | | - } |
108 | 116 |
|
109 | | - private static void verify_stack(Throwable e, String expected, String cause) throws Exception { |
110 | | - ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); |
111 | | - try (PrintStream printStream = new PrintStream(byteOS)) { |
112 | | - e.printStackTrace(printStream); |
113 | | - } |
114 | | - String stackTrace = byteOS.toString("ASCII"); |
115 | | - System.out.println(stackTrace); |
116 | | - if (!stackTrace.contains(expected) || |
117 | | - (cause != null && !stackTrace.contains(cause))) { |
118 | | - throw new RuntimeException(expected + " and/or " + cause + " missing from stacktrace"); |
| 117 | + private static void verify_stack(Throwable e, String expected, String cause) throws Exception { |
| 118 | + ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); |
| 119 | + try (PrintStream printStream = new PrintStream(byteOS)) { |
| 120 | + e.printStackTrace(printStream); |
| 121 | + } |
| 122 | + String stackTrace = byteOS.toString("ASCII"); |
| 123 | + System.out.println(stackTrace); |
| 124 | + if (!stackTrace.contains(expected) || |
| 125 | + (cause != null && !stackTrace.contains(cause))) { |
| 126 | + throw new RuntimeException(expected + " and/or " + cause + " missing from stacktrace"); |
| 127 | + } |
119 | 128 | } |
120 | 129 | } |
| 130 | + |
| 131 | + public static void main(String[] args) throws Exception { |
| 132 | + WhiteBox wb = WhiteBox.getWhiteBox(); |
| 133 | + long minimumJavaStackSize = wb.getMinimumJavaStackSize(); |
| 134 | + ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder( |
| 135 | + "-Xss" + Long.toString(minimumJavaStackSize), "-Xint", |
| 136 | + Launcher.class.getName()); |
| 137 | + |
| 138 | + OutputAnalyzer analyzer = new OutputAnalyzer(pb.start()); |
| 139 | + analyzer.shouldHaveExitValue(0); |
| 140 | + analyzer.shouldContain(expected); |
| 141 | + analyzer.shouldContain(cause); |
| 142 | + } |
121 | 143 | } |
0 commit comments