Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/runtime/BootstrapMethod/BSMCalledTwice.java
64474 views
1
/*
2
* Copyright (c) 2017, 2021, 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
* @bug 8174954
27
* @library /test/lib
28
* @modules java.base/jdk.internal.org.objectweb.asm
29
* @compile -XDignore.symbol.file BSMCalledTwice.java
30
* @run main BSMCalledTwice
31
*/
32
33
/*
34
* @test
35
* @bug 8262134
36
* @library /test/lib
37
* @requires vm.debug
38
* @modules java.base/jdk.internal.org.objectweb.asm
39
* @compile -XDignore.symbol.file BSMCalledTwice.java
40
* @run main/othervm -Xcomp -XX:CompileCommand=compileonly,TestC::* -XX:+DeoptimizeALot -XX:+VerifyStack BSMCalledTwice
41
*/
42
43
import java.io.File;
44
import java.io.FileOutputStream;
45
import java.util.*;
46
import static jdk.internal.org.objectweb.asm.Opcodes.*;
47
import jdk.internal.org.objectweb.asm.*;
48
import jdk.test.lib.process.ProcessTools;
49
import jdk.test.lib.process.OutputAnalyzer;
50
51
// BSMCalledTwice generates a class file named "TestC.class" that contains
52
// bytecodes that represent the following program
53
//
54
// public class TestC {
55
// public static void main(java.lang.String[] arg) {
56
// for (int i=0; i < 2; i++) {
57
// try {
58
// String f = "friend";
59
//
60
// // The "hello " + f in the following statement produces an
61
// // invokedynamic with a BSM of
62
// // StringConcatFactory.java/makeConcatWithConstants.
63
// // The ASM below erroneously puts 2 static arguments, "hello "
64
// // and "goodbye" on the stack for the BSM. Causing a exception to
65
// // be thrown when creatingthe CallSite object.
66
// System.out.println("hello " + f); <--------------- invokedynamic
67
//
68
// } catch (Error e) {
69
// System.out.println("Caught Error:");
70
// System.out.println(e.getMessage());
71
// e.printStackTrace();
72
// }
73
// }
74
// }
75
// }
76
//
77
public class BSMCalledTwice implements Opcodes {
78
static final String classTestCName = "TestC";
79
80
public static int count_makeSite(String text) {
81
int count = 0;
82
String text_ptr = text;
83
while (text_ptr.indexOf("makeSite") != -1) {
84
text_ptr = text_ptr.substring(text_ptr.indexOf("makeSite") + 1);
85
count++;
86
}
87
return count;
88
}
89
90
public static void main(String[] args) throws Exception {
91
ClassLoader cl = new ClassLoader() {
92
public Class<?> loadClass(String name) throws ClassNotFoundException {
93
if (findLoadedClass(name) != null) {
94
return findLoadedClass(name);
95
}
96
97
if (classTestCName.equals(name)) {
98
byte[] classFile = null;
99
try {
100
classFile = dumpTestC();
101
} catch (Exception e) {
102
}
103
return defineClass(classTestCName, classFile, 0, classFile.length);
104
}
105
return super.loadClass(name);
106
}
107
};
108
109
cl.loadClass(classTestCName);
110
ProcessBuilder pb = ProcessTools.createTestJvm("-cp", ".", classTestCName);
111
OutputAnalyzer output = new OutputAnalyzer(pb.start());
112
String test_output = output.getOutput();
113
if (test_output == null) {
114
throw new RuntimeException("Test failed, null test output");
115
}
116
// "makeSite" is currently listed twice in the exception stacks for each
117
// failing call to the BootstrapMethod. So more that two calls means
118
// that the BootstrapMethod was called more than once.
119
int count = count_makeSite(test_output);
120
if (count < 1 || count > 2) {
121
throw new RuntimeException("Test failed, bad number of calls to BootstrapMethod");
122
}
123
output.shouldHaveExitValue(0);
124
}
125
126
public static byte[] dumpTestC () throws Exception {
127
ClassWriter cw = new ClassWriter(0);
128
FieldVisitor fv;
129
MethodVisitor mv;
130
AnnotationVisitor av0;
131
132
cw.visit(53, ACC_PUBLIC + ACC_SUPER, classTestCName, null, "java/lang/Object", null);
133
134
cw.visitInnerClass("java/lang/invoke/MethodHandles$Lookup",
135
"java/lang/invoke/MethodHandles", "Lookup",
136
ACC_PUBLIC + ACC_FINAL + ACC_STATIC);
137
138
{
139
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
140
mv.visitCode();
141
mv.visitVarInsn(ALOAD, 0);
142
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
143
mv.visitInsn(RETURN);
144
mv.visitMaxs(1, 1);
145
mv.visitEnd();
146
}
147
{
148
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
149
mv.visitCode();
150
Label l0 = new Label();
151
Label l1 = new Label();
152
Label l2 = new Label();
153
mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Error");
154
mv.visitInsn(ICONST_0);
155
mv.visitVarInsn(ISTORE, 1);
156
Label l3 = new Label();
157
mv.visitLabel(l3);
158
mv.visitFrame(Opcodes.F_APPEND,1, new Object[] {Opcodes.INTEGER}, 0, null);
159
mv.visitVarInsn(ILOAD, 1);
160
mv.visitInsn(ICONST_2);
161
Label l4 = new Label();
162
mv.visitJumpInsn(IF_ICMPGE, l4);
163
mv.visitLabel(l0);
164
mv.visitLdcInsn("friend");
165
mv.visitVarInsn(ASTORE, 2);
166
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
167
mv.visitVarInsn(ALOAD, 2);
168
mv.visitInvokeDynamicInsn("makeConcatWithConstants",
169
"(Ljava/lang/String;)Ljava/lang/String;",
170
new Handle(Opcodes.H_INVOKESTATIC,
171
"java/lang/invoke/StringConcatFactory",
172
"makeConcatWithConstants",
173
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;"),
174
new Object[]{"hello \u0001", "goodbye"});
175
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
176
mv.visitLabel(l1);
177
Label l5 = new Label();
178
mv.visitJumpInsn(GOTO, l5);
179
mv.visitLabel(l2);
180
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"java/lang/Error"});
181
mv.visitVarInsn(ASTORE, 2);
182
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
183
mv.visitLdcInsn("Caught Error:");
184
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
185
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
186
mv.visitVarInsn(ALOAD, 2);
187
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Error", "getMessage", "()Ljava/lang/String;", false);
188
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
189
mv.visitVarInsn(ALOAD, 2);
190
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Error", "printStackTrace", "()V", false);
191
mv.visitLabel(l5);
192
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
193
mv.visitIincInsn(1, 1);
194
mv.visitJumpInsn(GOTO, l3);
195
mv.visitLabel(l4);
196
mv.visitFrame(Opcodes.F_CHOP,1, null, 0, null);
197
mv.visitInsn(RETURN);
198
mv.visitMaxs(2, 3);
199
mv.visitEnd();
200
}
201
cw.visitEnd();
202
203
try(FileOutputStream fos = new FileOutputStream(new File("TestC.class"))) {
204
fos.write(cw.toByteArray());
205
}
206
return cw.toByteArray();
207
}
208
}
209
210