Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk004.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 classfloadhk004 extends DebugeeClass {
36
37
/** Load native library if required. */
38
static {
39
System.loadLibrary("classfloadhk004");
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 classfloadhk004().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 + ".classfloadhk004r";
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 newPath = location + File.separator + "newclass";
90
log.display("Using path to instrumented class: \n\t" + newPath);
91
92
log.display("Reading bytes of instrumented class: \n\t" + TESTED_CLASS_NAME);
93
try {
94
newClassBytes = readBytes(newPath, TESTED_CLASS_NAME);
95
} catch (IOException e) {
96
throw new Failure("IOException in reading instrumented bytecode of tested class file:\n\t" + e);
97
}
98
99
log.display("Sync: debugee ready to load tested class");
100
status = checkStatus(status);
101
102
Class<?> testedClass = null;
103
try {
104
log.display("Loading original tested class: " + TESTED_CLASS_NAME);
105
testedClass = Class.forName(TESTED_CLASS_NAME);
106
} catch (ClassNotFoundException e) {
107
throw new Failure("Original tested class not found: \n\t" + e);
108
}
109
110
log.display("Sync: tested class loaded");
111
status = checkStatus(status);
112
113
log.display("Checking if the insrumented class was actually loaded");
114
try {
115
Method meth = testedClass.getMethod(TESTED_METHOD_NAME, new Class[0]);
116
Object res = meth.invoke(null, new Object[0]);
117
if (!(res instanceof Long)) {
118
log.complain("Tested method of instrumented class retured not Long value: " + res);
119
log.complain("The tested class was instrumented incorrectly!");
120
status = Consts.TEST_FAILED;
121
} else {
122
long value = ((Long)res).longValue();
123
log.display("Tested method of instrumented class returned value: " + value);
124
125
if (value == ORIG_VALUE) {
126
log.complain("Tested method of instrumented class returned original value: " + value);
127
log.complain("The tested class was not instrumented!");
128
status = Consts.TEST_FAILED;
129
} else if (value != NEW_VALUE) {
130
log.complain("Tested method of instrumented class returned unexpected value: " + value);
131
log.complain("The tested class was instrumented incorrectly!");
132
status = Consts.TEST_FAILED;
133
} else {
134
log.display("Tested method of instrumented class returned expected new value: " + value);
135
log.display("The tested class was instrumented correctly!");
136
}
137
}
138
} catch (NoSuchMethodException e) {
139
log.complain("No tested method found in instrumented class:\n\t" + e);
140
status = Consts.TEST_FAILED;
141
} catch (Exception e) {
142
log.complain("Exception in invoking method of instrumented class:\n\t" + e);
143
status = Consts.TEST_FAILED;
144
}
145
146
return status;
147
}
148
149
/** Read classfile for specified path and class name. */
150
public static byte[] readBytes(String path, String classname) throws IOException {
151
String filename = path + File.separator
152
+ classname.replace('.', File.separatorChar) + ".class";
153
FileInputStream in = new FileInputStream(filename);
154
byte[] bytes = new byte[in.available()];
155
in.read(bytes);
156
in.close();
157
return bytes;
158
}
159
}
160
161