Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk005.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 classfloadhk005 extends DebugeeClass {
36
37
/** Load native library if required. */
38
static {
39
System.loadLibrary("classfloadhk005");
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 classfloadhk005().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 + ".classfloadhk005r";
60
61
/* scaffold objects */
62
ArgumentHandler argHandler = null;
63
Log log = null;
64
long timeout = 0;
65
int status = Consts.TEST_PASSED;
66
67
/* new bytecode of tested class */
68
public static byte newClassBytes[] = null;
69
70
/* tested method name */
71
public static final String TESTED_METHOD_NAME = "testedStaticMethod";
72
73
/* possible values returned by tested method */
74
public static final long ORIG_VALUE = 20;
75
public static final long NEW_VALUE = 2200;
76
77
/** Run debuggee code. */
78
public int runIt(String argv[], PrintStream out) {
79
argHandler = new ArgumentHandler(argv);
80
log = new Log(out, argHandler);
81
timeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds
82
83
String args[] = argHandler.getArguments();
84
if (args.length <= 0) {
85
throw new Failure("Path for tested class file to load not specified");
86
}
87
88
String location = args[0];
89
String origPath = location + File.separator + "loadclass";
90
log.display("Using path to original class: \n\t" + origPath);
91
String newPath = location + File.separator + "newclass";
92
log.display("Using path to instrumented class: \n\t" + newPath);
93
94
classfloadhk005ClassLoader classLoader = new classfloadhk005ClassLoader(origPath);
95
96
log.display("Reading instrumented bytecode of tested class: \n\t" + TESTED_CLASS_NAME);
97
try {
98
newClassBytes = classLoader.readBytes(newPath, TESTED_CLASS_NAME);
99
} catch (IOException e) {
100
throw new Failure("IOException in reading instrumented bytecode of tested class file:\n\t" + e);
101
}
102
103
log.display("Sync: debugee ready to load tested class");
104
status = checkStatus(status);
105
106
Class<?> testedClass = null;
107
try {
108
log.display("Loading original tested class: " + TESTED_CLASS_NAME);
109
testedClass = Class.forName(TESTED_CLASS_NAME, true, classLoader);
110
} catch (ClassNotFoundException e) {
111
throw new Failure("Tested class not found: \n\t" + e);
112
}
113
114
log.display("Sync: tested class loaded");
115
status = checkStatus(status);
116
117
log.display("Checking if the insrumented class was actually loaded");
118
try {
119
Method meth = testedClass.getMethod(TESTED_METHOD_NAME, new Class[0]);
120
Object res = meth.invoke(null, new Object[0]);
121
if (!(res instanceof Long)) {
122
log.complain("Tested method of instrumented class retured not Long value: " + res);
123
log.complain("The tested class was instrumented incorrectly!");
124
status = Consts.TEST_FAILED;
125
} else {
126
long value = ((Long)res).longValue();
127
log.display("Tested method of instrumented class returned value: " + value);
128
129
if (value == ORIG_VALUE) {
130
log.complain("Tested method of instrumented class returned original value: " + value);
131
log.complain("The tested class was not instrumented!");
132
status = Consts.TEST_FAILED;
133
} else if (value != NEW_VALUE) {
134
log.complain("Tested method of instrumented class returned unexpected value: " + value);
135
log.complain("The tested class was instrumented incorrectly!");
136
status = Consts.TEST_FAILED;
137
} else {
138
log.display("Tested method of instrumented class returned expected new value: " + value);
139
log.display("The tested class was instrumented correctly!");
140
}
141
}
142
} catch (NoSuchMethodException e) {
143
log.complain("No tested method found in instrumented class:\n\t" + e);
144
status = Consts.TEST_FAILED;
145
} catch (Exception e) {
146
log.complain("Exception in invoking method of instrumented class:\n\t" + e);
147
status = Consts.TEST_FAILED;
148
}
149
150
return status;
151
}
152
}
153
154
/* =================================================================== */
155
156
/** Classloader for tested class. */
157
class classfloadhk005ClassLoader extends ClassLoader {
158
private String path;
159
160
/** Make classloader providing path to class files. */
161
public classfloadhk005ClassLoader(String path) {
162
this.path = path;
163
}
164
165
/** Load specified class. */
166
protected Class<?> findClass(String name) throws ClassNotFoundException {
167
try {
168
byte[] bytes = readBytes(path, name);
169
return defineClass(name, bytes, 0, bytes.length);
170
} catch (IOException e) {
171
throw new ClassNotFoundException("IOException in loading class: " + name, e);
172
}
173
}
174
175
/** Read classfile for specified path and class name. */
176
public static byte[] readBytes(String path, String classname) throws IOException {
177
String filename = path + File.separator
178
+ classname.replace('.', File.separatorChar) + ".class";
179
FileInputStream in = new FileInputStream(filename);
180
byte[] bytes = new byte[in.available()];
181
in.read(bytes);
182
in.close();
183
return bytes;
184
}
185
}
186
187