Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk009.java
40948 views
1
/*
2
* Copyright (c) 2003, 2018, 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
package nsk.jvmti.ClassFileLoadHook;
25
26
import java.io.*;
27
import java.lang.reflect.*;
28
29
import nsk.share.*;
30
import nsk.share.jvmti.*;
31
32
/**
33
* Debuggee class of JVMTI test.
34
*/
35
public class classfloadhk009 extends DebugeeClass {
36
37
/** Load native library if required. */
38
static {
39
System.loadLibrary("classfloadhk009");
40
}
41
42
/** Run test from command line. */
43
public static void main(String argv[]) {
44
argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
45
46
// JCK-compatible exit
47
System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
48
}
49
50
/** Run test from JCK-compatible environment. */
51
public static int run(String argv[], PrintStream out) {
52
return new classfloadhk009().runIt(argv, out);
53
}
54
55
/* =================================================================== */
56
57
/* constant names */
58
public static final String PACKAGE_NAME = "nsk.jvmti.ClassFileLoadHook";
59
public static final String TESTED_CLASS_NAME = PACKAGE_NAME + ".classfloadhk009r";
60
61
/* scaffold objects */
62
ArgumentHandler argHandler = null;
63
Log log = null;
64
long timeout = 0;
65
int status = Consts.TEST_PASSED;
66
67
/* bytecode of redefined class */
68
public static byte redefClassBytes[] = null;
69
/* bytecode of instrumented class */
70
public static byte newClassBytes[] = null;
71
72
/* tested class to de redefined */
73
public static Class<?> testedClass = null;
74
75
/* tested method name */
76
public static final String TESTED_METHOD_NAME = "testedStaticMethod";
77
78
/* possible values returned by tested method */
79
public static final long ORIG_VALUE = 20;
80
public static final long REDEF_VALUE = 12;
81
public static final long NEW_VALUE = 58;
82
83
/** Run debuggee code. */
84
public int runIt(String argv[], PrintStream out) {
85
argHandler = new ArgumentHandler(argv);
86
log = new Log(out, argHandler);
87
timeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds
88
89
String args[] = argHandler.getArguments();
90
if (args.length <= 0) {
91
throw new Failure("Path for tested class file to load not specified");
92
}
93
94
String location = args[0];
95
String origPath = location + File.separator + "loadclass";
96
log.display("Using path to original class: \n\t" + origPath);
97
String redefPath = location + File.separator + "newclass";
98
log.display("Using path to redefined class: \n\t" + redefPath);
99
String newPath = location + File.separator + "newclass01";
100
log.display("Using path to instrumented class: \n\t" + newPath);
101
102
classfloadhk009ClassLoader classLoader = new classfloadhk009ClassLoader(origPath);
103
104
log.display("Reading redefined bytecode of tested class: \n\t" + TESTED_CLASS_NAME);
105
try {
106
redefClassBytes = classLoader.readBytes(redefPath, TESTED_CLASS_NAME);
107
} catch (IOException e) {
108
throw new Failure("IOException in reading redefined bytecode of tested class file:\n\t" + e);
109
}
110
111
log.display("Reading instrumented bytecode of tested class: \n\t" + TESTED_CLASS_NAME);
112
try {
113
newClassBytes = classLoader.readBytes(newPath, TESTED_CLASS_NAME);
114
} catch (IOException e) {
115
throw new Failure("IOException in reading instrumented bytecode of tested class file:\n\t" + e);
116
}
117
118
try {
119
log.display("Loading original tested class: " + TESTED_CLASS_NAME);
120
testedClass = Class.forName(TESTED_CLASS_NAME, true, classLoader);
121
} catch (ClassNotFoundException e) {
122
throw new Failure("Tested class not found: \n\t" + e);
123
}
124
125
log.display("Sync: tested class loaded");
126
status = checkStatus(status);
127
128
log.display("Checking if the tested class was actually redefined");
129
try {
130
Method meth = testedClass.getMethod(TESTED_METHOD_NAME, new Class[0]);
131
Object res = meth.invoke(null, new Object[0]);
132
if (!(res instanceof Long)) {
133
log.complain("Tested method of redefined and instrumented class retured not Long value: " + res);
134
log.complain("The tested class was redefined and instrumented incorrectly!");
135
status = Consts.TEST_FAILED;
136
} else {
137
long value = ((Long)res).longValue();
138
log.display("Tested method of redefined and instrumented class returned value: " + value);
139
140
if (value == ORIG_VALUE) {
141
log.complain("Tested method of redefined and instrumented class returned original value: " + value);
142
log.complain("The tested class was not redefined nor instrumented!");
143
status = Consts.TEST_FAILED;
144
} else if (value == REDEF_VALUE) {
145
log.complain("Tested method of redefined and instrumented class returned redefined value: " + value);
146
log.complain("The tested class was redefined but not instrumented!");
147
status = Consts.TEST_FAILED;
148
} else if (value != NEW_VALUE) {
149
log.complain("Tested method of redefined and instrumented class returned unexpected value: " + value);
150
log.complain("The tested class was redefined and instrumented incorrectly!");
151
status = Consts.TEST_FAILED;
152
} else {
153
log.display("Tested method of redefined and instrumented class returned expected new value: " + value);
154
log.display("The tested class was redefined and instrumented correctly!");
155
}
156
}
157
} catch (NoSuchMethodException e) {
158
log.complain("No tested method found in redefined and instrumented class:\n\t" + e);
159
status = Consts.TEST_FAILED;
160
} catch (Exception e) {
161
log.complain("Exception in invoking method of redefined and instrumented class:\n\t" + e);
162
status = Consts.TEST_FAILED;
163
}
164
165
return status;
166
}
167
}
168
169
/* =================================================================== */
170
171
/** Classloader for tested class. */
172
class classfloadhk009ClassLoader extends ClassLoader {
173
private String path;
174
175
/** Make classloader providing path to class files. */
176
public classfloadhk009ClassLoader(String path) {
177
this.path = path;
178
}
179
180
/** Load specified class. */
181
protected Class<?> findClass(String name) throws ClassNotFoundException {
182
try {
183
byte[] bytes = readBytes(path, name);
184
return defineClass(name, bytes, 0, bytes.length);
185
} catch (IOException e) {
186
throw new ClassNotFoundException("IOException in loading class: " + name, e);
187
}
188
}
189
190
/** Read classfile for specified path and class name. */
191
public static byte[] readBytes(String path, String classname) throws IOException {
192
String filename = path + File.separator
193
+ classname.replace('.', File.separatorChar) + ".class";
194
FileInputStream in = new FileInputStream(filename);
195
byte[] bytes = new byte[in.available()];
196
in.read(bytes);
197
in.close();
198
return bytes;
199
}
200
}
201
202