Skip to content

Commit 6dd5553

Browse files
toxaartAfshin Zafari
authored andcommitted
8354329: Rewrite runtime/ClassFile/JsrRewriting.java and OomWhileParsingRepeatedJsr.java tests
Reviewed-by: matsaave, coleenp
1 parent 8511220 commit 6dd5553

File tree

6 files changed

+286
-36
lines changed

6 files changed

+286
-36
lines changed

test/hotspot/jtreg/runtime/ClassFile/JsrRewriting.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -34,50 +34,42 @@
3434
* @bug 7149464
3535
* @requires vm.flagless
3636
* @library /test/lib
37+
* @library /testlibrary/asm
3738
* @modules java.base/jdk.internal.misc
3839
* java.desktop
3940
* java.management
4041
* @run driver JsrRewriting
4142
*/
4243

43-
import jdk.test.lib.JDKToolFinder;
44-
import jdk.test.lib.Platform;
45-
import jdk.test.lib.process.ProcessTools;
46-
import jdk.test.lib.process.OutputAnalyzer;
4744
import java.io.File;
45+
import java.nio.file.Files;
46+
import jdk.test.lib.process.OutputAnalyzer;
47+
import jdk.test.lib.process.ProcessTools;
4848

4949
public class JsrRewriting {
5050

5151
public static void main(String[] args) throws Exception {
5252

53-
// ======= Configure the test
54-
String jarFile = System.getProperty("test.src") +
55-
File.separator + "JsrRewritingTestCase.jar";
53+
// create a file in the scratch dir
5654
String className = "OOMCrashClass4000_1";
55+
File classFile = new File(className + ".class");
56+
classFile.createNewFile();
5757

58-
// limit is 768MB in native words
59-
int mallocMaxTestWords = (1024 * 1024 * 768 / 4);
60-
if (Platform.is64bit())
61-
mallocMaxTestWords = (mallocMaxTestWords / 2);
62-
63-
// ======= extract the test class
64-
ProcessBuilder pb = new ProcessBuilder(new String[] {
65-
JDKToolFinder.getJDKTool("jar"),
66-
"xvf", jarFile } );
67-
OutputAnalyzer output = new OutputAnalyzer(pb.start());
68-
output.shouldHaveExitValue(0);
58+
// fill it with the binary data of the class file
59+
byte[] bytes = OOMCrashClass4000_1.dump();
60+
Files.write(classFile.toPath(), bytes);
6961

7062
// ======= execute the test
7163
// We run the test with MallocLimit set to 768m in oom mode,
7264
// in order to trigger and observe a fake os::malloc oom. This needs NMT.
73-
pb = ProcessTools.createLimitedTestJavaProcessBuilder(
65+
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
7466
"-cp", ".",
7567
"-XX:+UnlockDiagnosticVMOptions",
7668
"-XX:NativeMemoryTracking=summary",
7769
"-XX:MallocLimit=768m:oom",
7870
className);
7971

80-
output = new OutputAnalyzer(pb.start());
72+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
8173
output.shouldNotHaveExitValue(0);
8274
String[] expectedMsgs = {
8375
"java.lang.LinkageError",
Binary file not shown.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2025, 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+
import org.objectweb.asm.ClassWriter;
25+
import org.objectweb.asm.Label;
26+
import org.objectweb.asm.MethodVisitor;
27+
import org.objectweb.asm.Opcodes;
28+
29+
/*
30+
This is class is a dumper for the original OOMCrashClass1960_2.class, i.e. its dump() method
31+
produces a byte array with the original class file data.
32+
33+
To get this source code, one needs to run the following command:
34+
java jdk.internal.org.objectweb.asm.util.ASMifier OOMCrashClass1960_2.class >> OOMCrashClass1960_2.java
35+
36+
The resulting java source code is large (>2 mb), so certain refactoring is applied.
37+
*/
38+
39+
public class OOMCrashClass1960_2 implements Opcodes {
40+
41+
public static byte[] dump() throws Exception {
42+
43+
ClassWriter classWriter = new ClassWriter(0);
44+
MethodVisitor methodVisitor;
45+
46+
classWriter.visit(V1_1, ACC_PUBLIC | ACC_SUPER, "OOMCrashClass1960_2", null, "java/lang/Object", null);
47+
48+
classWriter.visitSource("<generated>", null);
49+
50+
{
51+
methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "main0", "([Ljava/lang/String;)V", null, null);
52+
methodVisitor.visitCode();
53+
Label label0 = new Label();
54+
methodVisitor.visitLabel(label0);
55+
methodVisitor.visitVarInsn(ALOAD, 0);
56+
methodVisitor.visitInsn(ICONST_0);
57+
methodVisitor.visitInsn(AALOAD);
58+
methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I", false);
59+
methodVisitor.visitVarInsn(ISTORE, 1);
60+
Label label1 = new Label();
61+
methodVisitor.visitJumpInsn(GOTO, label1);
62+
Label label2 = new Label();
63+
methodVisitor.visitLabel(label2);
64+
methodVisitor.visitInsn(RETURN);
65+
66+
// This line overflows the jump target for jsr
67+
68+
Label prevLabel = label2;
69+
for (int i = 0; i < 1959; ++i) {
70+
Label curLabel = getCurLabel(methodVisitor, prevLabel);
71+
prevLabel = curLabel;
72+
}
73+
74+
Label label5877 = prevLabel;
75+
76+
methodVisitor.visitLabel(label1);
77+
methodVisitor.visitVarInsn(ILOAD, 1);
78+
methodVisitor.visitJumpInsn(IFEQ, label5877);
79+
Label label5880 = new Label();
80+
methodVisitor.visitJumpInsn(JSR, label5880);
81+
Label label5881 = new Label();
82+
methodVisitor.visitJumpInsn(GOTO, label5881);
83+
methodVisitor.visitLabel(label5880);
84+
methodVisitor.visitInsn(RETURN);
85+
methodVisitor.visitLabel(label5881);
86+
methodVisitor.visitInsn(ACONST_NULL);
87+
methodVisitor.visitJumpInsn(GOTO, label1);
88+
Label label5882 = new Label();
89+
methodVisitor.visitLabel(label5882);
90+
methodVisitor.visitLocalVariable("argv", "[Ljava/lang/String;", null, label0, label5882, 0);
91+
methodVisitor.visitMaxs(65535, 2);
92+
methodVisitor.visitEnd();
93+
}
94+
{
95+
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
96+
methodVisitor.visitCode();
97+
Label label0 = new Label();
98+
methodVisitor.visitLabel(label0);
99+
methodVisitor.visitVarInsn(ALOAD, 0);
100+
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
101+
methodVisitor.visitInsn(RETURN);
102+
Label label1 = new Label();
103+
methodVisitor.visitLabel(label1);
104+
methodVisitor.visitLocalVariable("this", "LOOMCrashClass1960_2;", null, label0, label1, 0);
105+
methodVisitor.visitMaxs(1, 1);
106+
methodVisitor.visitEnd();
107+
}
108+
classWriter.visitEnd();
109+
110+
return classWriter.toByteArray();
111+
}
112+
113+
public static Label getCurLabel(final MethodVisitor methodVisitor, final Label prevLabel) {
114+
final Label curLabel = new Label();
115+
methodVisitor.visitLabel(curLabel);
116+
methodVisitor.visitVarInsn(ILOAD, 1);
117+
methodVisitor.visitJumpInsn(IFEQ, prevLabel);
118+
Label tmpLabel1 = new Label();
119+
methodVisitor.visitJumpInsn(JSR, tmpLabel1);
120+
Label tmpLabel2 = new Label();
121+
methodVisitor.visitJumpInsn(GOTO, tmpLabel2);
122+
methodVisitor.visitLabel(tmpLabel1);
123+
methodVisitor.visitInsn(RETURN);
124+
methodVisitor.visitLabel(tmpLabel2);
125+
methodVisitor.visitInsn(ACONST_NULL);
126+
methodVisitor.visitJumpInsn(GOTO, curLabel);
127+
return curLabel;
128+
}
129+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2025, 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+
import org.objectweb.asm.ClassWriter;
25+
import org.objectweb.asm.Label;
26+
import org.objectweb.asm.MethodVisitor;
27+
import org.objectweb.asm.Opcodes;
28+
29+
/*
30+
This is class is a dumper for the original OOMCrashClass4000_1.class, i.e. its dump() method
31+
produces a byte array with the original class file data.
32+
33+
To get this source code, one needs to run the following command:
34+
java jdk.internal.org.objectweb.asm.util.ASMifier OOMCrashClass4000_1.class >> OOMCrashClass4000_1.java
35+
36+
The resulting java source code is large (>2 mb), so certain refactoring is applied.
37+
*/
38+
39+
public class OOMCrashClass4000_1 implements Opcodes {
40+
41+
public static byte[] dump() throws Exception {
42+
43+
ClassWriter classWriter = new ClassWriter(0);
44+
MethodVisitor methodVisitor;
45+
46+
classWriter.visit(V1_1, ACC_PUBLIC | ACC_SUPER, "OOMCrashClass4000_1", null, "java/lang/Object", null);
47+
48+
classWriter.visitSource("<generated>", null);
49+
50+
{
51+
methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "main0", "([Ljava/lang/String;)V", null, null);
52+
methodVisitor.visitCode();
53+
Label label0 = new Label();
54+
methodVisitor.visitLabel(label0);
55+
methodVisitor.visitVarInsn(ALOAD, 0);
56+
methodVisitor.visitInsn(ICONST_0);
57+
methodVisitor.visitInsn(AALOAD);
58+
methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "parseInt", "(Ljava/lang/String;)I", false);
59+
methodVisitor.visitVarInsn(ISTORE, 1);
60+
Label label1 = new Label();
61+
methodVisitor.visitJumpInsn(GOTO, label1);
62+
Label label2 = new Label();
63+
methodVisitor.visitLabel(label2);
64+
methodVisitor.visitInsn(RETURN);
65+
66+
// This line overflows the jump target for jsr
67+
68+
Label prevLabel = label2;
69+
for (int i = 0; i < 3999; ++i) {
70+
Label curLabel = getCurLabel(methodVisitor, prevLabel);
71+
prevLabel = curLabel;
72+
}
73+
74+
Label label11997 = prevLabel;
75+
76+
methodVisitor.visitLabel(label1);
77+
methodVisitor.visitVarInsn(ILOAD, 1);
78+
methodVisitor.visitJumpInsn(IFEQ, label11997);
79+
Label label12000 = new Label();
80+
methodVisitor.visitJumpInsn(JSR, label12000);
81+
Label label12001 = new Label();
82+
methodVisitor.visitJumpInsn(GOTO, label12001);
83+
methodVisitor.visitLabel(label12000);
84+
methodVisitor.visitInsn(RETURN);
85+
methodVisitor.visitLabel(label12001);
86+
methodVisitor.visitInsn(ACONST_NULL);
87+
methodVisitor.visitJumpInsn(GOTO, label1);
88+
Label label12002 = new Label();
89+
methodVisitor.visitLabel(label12002);
90+
methodVisitor.visitLocalVariable("argv", "[Ljava/lang/String;", null, label0, label12002, 0);
91+
methodVisitor.visitMaxs(65535, 2);
92+
methodVisitor.visitEnd();
93+
}
94+
{
95+
methodVisitor = classWriter.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
96+
methodVisitor.visitCode();
97+
Label label0 = new Label();
98+
methodVisitor.visitLabel(label0);
99+
methodVisitor.visitVarInsn(ALOAD, 0);
100+
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
101+
methodVisitor.visitInsn(RETURN);
102+
Label label1 = new Label();
103+
methodVisitor.visitLabel(label1);
104+
methodVisitor.visitLocalVariable("this", "LOOMCrashClass4000_1;", null, label0, label1, 0);
105+
methodVisitor.visitMaxs(1, 1);
106+
methodVisitor.visitEnd();
107+
}
108+
classWriter.visitEnd();
109+
110+
return classWriter.toByteArray();
111+
}
112+
113+
public static Label getCurLabel(final MethodVisitor methodVisitor, final Label prevLabel) {
114+
final Label curLabel = new Label();
115+
methodVisitor.visitLabel(curLabel);
116+
methodVisitor.visitVarInsn(ILOAD, 1);
117+
methodVisitor.visitJumpInsn(IFEQ, prevLabel);
118+
Label tmpLabel1 = new Label();
119+
methodVisitor.visitJumpInsn(JSR, tmpLabel1);
120+
Label tmpLabel2 = new Label();
121+
methodVisitor.visitJumpInsn(GOTO, tmpLabel2);
122+
methodVisitor.visitLabel(tmpLabel1);
123+
methodVisitor.visitInsn(RETURN);
124+
methodVisitor.visitLabel(tmpLabel2);
125+
methodVisitor.visitInsn(ACONST_NULL);
126+
methodVisitor.visitJumpInsn(GOTO, curLabel);
127+
return curLabel;
128+
}
129+
}

test/hotspot/jtreg/runtime/ClassFile/OomWhileParsingRepeatedJsr.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,43 +35,43 @@
3535
* @bug 8016029
3636
* @requires vm.flagless
3737
* @library /test/lib
38+
* @library /testlibrary/asm
3839
* @modules java.base/jdk.internal.misc
3940
* java.desktop
4041
* java.management
4142
* @run driver OomWhileParsingRepeatedJsr
4243
*/
4344

44-
import jdk.test.lib.JDKToolFinder;
45-
import jdk.test.lib.Platform;
46-
import jdk.test.lib.process.ProcessTools;
45+
import java.io.File;
46+
import java.nio.file.Files;
4747
import jdk.test.lib.process.OutputAnalyzer;
48+
import jdk.test.lib.process.ProcessTools;
4849

4950
public class OomWhileParsingRepeatedJsr {
5051

5152
public static void main(String[] args) throws Exception {
5253

53-
// ======= Configure the test
54-
String jarFile = System.getProperty("test.src") + "/testcase.jar";
5554
String className = "OOMCrashClass1960_2";
5655

57-
// ======= extract the test class
58-
ProcessBuilder pb = new ProcessBuilder(new String[] {
59-
JDKToolFinder.getJDKTool("jar"),
60-
"xvf", jarFile } );
61-
OutputAnalyzer output = new OutputAnalyzer(pb.start());
62-
output.shouldHaveExitValue(0);
56+
// create a file in the scratch dir
57+
File classFile = new File(className + ".class");
58+
classFile.createNewFile();
59+
60+
// fill it with the binary data of the class file
61+
byte[] bytes = OOMCrashClass1960_2.dump();
62+
Files.write(classFile.toPath(), bytes);
6363

6464
// ======= execute the test
6565
// We run the test with MallocLimit set to 768m in oom mode,
6666
// in order to trigger and observe a fake os::malloc oom. This needs NMT.
67-
pb = ProcessTools.createLimitedTestJavaProcessBuilder(
67+
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
6868
"-cp", ".",
6969
"-XX:+UnlockDiagnosticVMOptions",
7070
"-XX:NativeMemoryTracking=summary",
7171
"-XX:MallocLimit=768m:oom",
72-
className );
72+
className);
7373

74-
output = new OutputAnalyzer(pb.start());
74+
OutputAnalyzer output = new OutputAnalyzer(pb.start());
7575
output.shouldNotHaveExitValue(0);
7676
output.shouldContain("Cannot reserve enough memory");
7777
}
-984 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)