Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdwp/VirtualMachine/RedefineClasses/redefinecls001.java
41161 views
1
/*
2
* Copyright (c) 2001, 2020, 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.jdwp.VirtualMachine.RedefineClasses;
25
26
import java.io.*;
27
28
import nsk.share.*;
29
import nsk.share.jpda.*;
30
import nsk.share.jdwp.*;
31
32
/**
33
* Test for JDWP command: VirtualMachine.RedefineClasses.
34
*
35
* See redefinecls001.README for description of test execution.
36
*
37
* This class represents debugger part of the test.
38
* Test is executed by invoking method runIt().
39
* JDWP command is tested in the method testCommand().
40
*
41
* @see #runIt()
42
* @see #testCommand()
43
*/
44
public class redefinecls001 {
45
46
// exit status constants
47
static final int JCK_STATUS_BASE = 95;
48
static final int PASSED = 0;
49
static final int FAILED = 2;
50
51
// VM capability constatnts
52
static final int VM_CAPABILITY_NUMBER = JDWP.Capability.CAN_REDEFINE_CLASSES;
53
static final String VM_CAPABILITY_NAME = "canRedefineClasses";
54
55
// package and classes names
56
static final String PACKAGE_NAME = "nsk.jdwp.VirtualMachine.RedefineClasses";
57
static final String TEST_CLASS_NAME = PACKAGE_NAME + "." + "redefinecls001";
58
static final String DEBUGEE_CLASS_NAME = TEST_CLASS_NAME + "a";
59
60
// tested JDWP command
61
static final String JDWP_COMMAND_NAME = "VirtualMachine.RedefineClasses";
62
static final int JDWP_COMMAND_ID = JDWP.Command.VirtualMachine.RedefineClasses;
63
64
// tested class name and signature
65
static final String TESTED_CLASS_NAME = TEST_CLASS_NAME + "b";
66
static final String TESTED_CLASS_SIGNATURE = "L" + TESTED_CLASS_NAME.replace('.', '/') + ";";
67
68
// field and method names
69
static final String CONSTRUCTOR_FIELD_NAME = "constructorInvoked";
70
static final String STATIC_METHOD_FIELD_NAME = "staticMethodInvoked";
71
static final String OBJECT_METHOD_FIELD_NAME = "objectMethodInvoked";
72
static final String BREAKPOINT_METHOD_NAME = "runIt";
73
static final int BREAKPOINT_LINE_BEFORE = redefinecls001a.BREAKPOINT_LINE_BEFORE;
74
static final int BREAKPOINT_LINE_AFTER = redefinecls001a.BREAKPOINT_LINE_AFTER;
75
76
// filename for redefined class
77
static final String REDEFINED_CLASS_FILE_NAME = "bin" + File.separator + "newclass"
78
+ File.separator + PACKAGE_NAME.replace('.',File.separatorChar)
79
+ File.separator + "redefinecls001b.class";
80
81
// usual scaffold objects
82
ArgumentHandler argumentHandler = null;
83
Log log = null;
84
Binder binder = null;
85
Debugee debugee = null;
86
Transport transport = null;
87
int waitTime = 0; // minutes
88
long timeout = 0; // milliseconds
89
String testDir = null;
90
boolean dead = false;
91
boolean success = true;
92
93
// data obtained from debuggee
94
long debugeeClassID = 0;
95
long testedClassID = 0;
96
long breakpointMethodID = 0;
97
ByteBuffer redefinedClassBytes = null;
98
99
// -------------------------------------------------------------------
100
101
/**
102
* Start test from command line.
103
*/
104
public static void main (String argv[]) {
105
System.exit(run(argv,System.out) + JCK_STATUS_BASE);
106
}
107
108
/**
109
* Start JCK-compilant test.
110
*/
111
public static int run(String argv[], PrintStream out) {
112
return new redefinecls001().runIt(argv, out);
113
}
114
115
// -------------------------------------------------------------------
116
117
/**
118
* Perform test execution.
119
*/
120
public int runIt(String argv[], PrintStream out) {
121
122
// make log for debugger messages
123
argumentHandler = new ArgumentHandler(argv);
124
log = new Log(out, argumentHandler);
125
waitTime = argumentHandler.getWaitTime(); // minutes
126
timeout = waitTime * 60 * 1000; // milliseconds
127
128
// get testDir as first positional parameter
129
String args[] = argumentHandler.getArguments();
130
if (args.length < 1) {
131
log.complain("Test dir required as the first positional argument");
132
return FAILED;
133
}
134
testDir = args[0];
135
136
// execute test and display results
137
try {
138
log.display("\n>>> Loading redefined class \n");
139
140
// load class file for redefined class
141
log.display("Loading bytecode of redefined class from file: " +
142
REDEFINED_CLASS_FILE_NAME);
143
redefinedClassBytes = loadClassBytes(REDEFINED_CLASS_FILE_NAME, testDir);
144
log.display(" ... loaded bytes: " + redefinedClassBytes.length());
145
146
log.display("\n>>> Starting debugee \n");
147
148
// launch debuggee
149
binder = new Binder(argumentHandler, log);
150
log.display("Launching debugee VM");
151
debugee = binder.bindToDebugee(DEBUGEE_CLASS_NAME);
152
transport = debugee.getTransport();
153
log.display(" ... debuggee launched");
154
155
// set timeout for debuggee responces
156
log.display("Setting timeout for debuggee responces: " + waitTime + " minute(s)");
157
transport.setReadTimeout(timeout);
158
log.display(" ... timeout set");
159
160
// wait for VM_INIT event
161
log.display("Waiting for VM_INIT event");
162
debugee.waitForVMInit();
163
log.display(" ... VM_INIT event received");
164
165
// query debugee for VM-dependent ID sizes
166
log.display("Querying for IDSizes");
167
debugee.queryForIDSizes();
168
log.display(" ... size of VM-dependent types adjusted");
169
170
// check for VM capability
171
log.display("\n>>> Checking VM capability \n");
172
log.display("Getting new VM capability: " + VM_CAPABILITY_NAME);
173
boolean capable = debugee.getNewCapability(VM_CAPABILITY_NUMBER, VM_CAPABILITY_NAME);
174
log.display(" ... got VM capability: " + capable);
175
176
// exit as PASSED if this capability is not supported
177
if (!capable) {
178
out.println("TEST PASSED: unsupported VM capability: "
179
+ VM_CAPABILITY_NAME);
180
return PASSED;
181
}
182
183
// prepare debuggee for testing and obtain required data
184
log.display("\n>>> Getting prepared for testing \n");
185
prepareForTest();
186
187
// test JDWP command
188
log.display("\n>> Testing JDWP command \n");
189
testCommand();
190
191
// check command results
192
if (success) {
193
log.display("\n>>> Checking result of tested command \n");
194
checkResult();
195
}
196
197
// finish debuggee
198
log.display("\n>> Finishing debuggee \n");
199
200
// resume debuggee after testing command
201
log.display("Resuming debuggee");
202
debugee.resume();
203
log.display(" ... debuggee resumed");
204
205
// wait for VM_DEATH event
206
log.display("Waiting for VM_DEATH event");
207
debugee.waitForVMDeath();
208
log.display(" ... VM_DEATH event received");
209
dead = true;
210
211
} catch (Failure e) {
212
log.complain("TEST FAILED: " + e.getMessage());
213
success = false;
214
} catch (Exception e) {
215
e.printStackTrace(out);
216
log.complain("Caught unexpected exception while running the test:\n\t" + e);
217
success = false;
218
} finally {
219
log.display("\n>>> Finishing test \n");
220
221
// disconnect debugee and wait for its exit
222
if (debugee != null) {
223
quitDebugee();
224
}
225
}
226
227
// check result
228
if (!success) {
229
log.complain("TEST FAILED");
230
return FAILED;
231
}
232
out.println("TEST PASSED");
233
return PASSED;
234
}
235
236
/**
237
* Get debuggee prepared for testing and obtain required data.
238
*/
239
void prepareForTest() {
240
// wait for debuggee and tested classes loaded on debuggee startup
241
log.display("Waiting for classes loaded:"
242
+ "\n\t" + DEBUGEE_CLASS_NAME
243
+ "\n\t" + TESTED_CLASS_NAME);
244
String classNames[] = {DEBUGEE_CLASS_NAME, TESTED_CLASS_NAME};
245
long classIDs[] = debugee.waitForClassesLoaded(classNames,
246
JDWP.SuspendPolicy.ALL);
247
debugeeClassID = classIDs[0];
248
log.display(" ... debuggee class loaded with classID: " + debugeeClassID);
249
testedClassID = classIDs[1];
250
log.display(" ... tested class loaded with classID: " + testedClassID);
251
log.display("");
252
253
// set breakpoint before redefinition and wait for debugee reached it
254
log.display("Waiting for breakpoint before redefiniton reached at: "
255
+ BREAKPOINT_METHOD_NAME + ":" + BREAKPOINT_LINE_BEFORE);
256
long threadID = debugee.waitForBreakpointReached(debugeeClassID,
257
BREAKPOINT_METHOD_NAME,
258
BREAKPOINT_LINE_BEFORE,
259
JDWP.SuspendPolicy.ALL);
260
log.display(" ... breakpoint before redefinition reached with threadID: " + threadID);
261
log.display("");
262
}
263
264
/**
265
* Perform testing JDWP command.
266
*/
267
void testCommand() {
268
int length = redefinedClassBytes.length();
269
// create command packet and fill requred out data
270
log.display("Create command packet:");
271
log.display("Command: " + JDWP_COMMAND_NAME);
272
CommandPacket command = new CommandPacket(JDWP_COMMAND_ID);
273
log.display(" classes: " + 1);
274
command.addInt(1);
275
log.display(" refTypeID: " + testedClassID);
276
command.addReferenceTypeID(testedClassID);
277
log.display(" classfile: " + length + " bytes");
278
command.addInt(length);
279
log.display(" classbytes:\n" + redefinedClassBytes);
280
command.addBytes(redefinedClassBytes.getBytes(), 0, length);
281
command.setLength();
282
283
// send command packet to debugee
284
try {
285
log.display("Sending command packet:\n" + command);
286
transport.write(command);
287
} catch (IOException e) {
288
log.complain("Unable to send command packet:\n\t" + e);
289
success = false;
290
return;
291
}
292
293
// receive reply packet from debugee
294
ReplyPacket reply = new ReplyPacket();
295
try {
296
log.display("Waiting for reply packet");
297
transport.read(reply);
298
log.display(" ... reply packet received:\n" + reply);
299
} catch (IOException e) {
300
log.complain("Unable to read reply packet for tested command:\n\t" + e);
301
success = false;
302
return;
303
}
304
305
// check reply packet header
306
try{
307
log.display("Checking header of reply packet");
308
reply.checkHeader(command.getPacketID());
309
log.display(" ... packet header is correct");
310
} catch (BoundException e) {
311
log.complain("Wrong header of reply packet for tested command:\n\t"
312
+ e.getMessage());
313
success = false;
314
return;
315
}
316
317
// start parsing reply packet data
318
log.display("Parsing reply packet data:");
319
reply.resetPosition();
320
321
// no reply data to parse
322
log.display(" no reply data");
323
324
// check for extra data in reply packet
325
if (!reply.isParsed()) {
326
log.complain("Extra trailing bytes found in reply packet at: "
327
+ reply.offsetString());
328
success = false;
329
}
330
331
log.display(" ... packed data parsed");
332
}
333
334
/**
335
* Check result of the tested JDWP command.
336
*/
337
void checkResult() {
338
// set breakpoint after redefinition and wait for debugee reached it
339
log.display("Waiting for breakpoint after redefiniton reached at: "
340
+ BREAKPOINT_METHOD_NAME + ":" + BREAKPOINT_LINE_AFTER);
341
long threadID = debugee.waitForBreakpointReached(debugeeClassID,
342
BREAKPOINT_METHOD_NAME,
343
BREAKPOINT_LINE_AFTER,
344
JDWP.SuspendPolicy.ALL);
345
log.display(" ... breakpoint after redefinition reached with threadID: " + threadID);
346
log.display("");
347
348
// check invoked methods
349
log.display("Getting value of static field: " + CONSTRUCTOR_FIELD_NAME);
350
JDWP.Value value = debugee.getStaticFieldValue(testedClassID,
351
CONSTRUCTOR_FIELD_NAME, JDWP.Tag.INT);
352
int constructorInvoked = ((Integer)value.getValue()).intValue();
353
log.display(" ... got constructorInvoked: " + methodKind(constructorInvoked));
354
355
if (constructorInvoked != redefinecls001b.NOT_REDEFINED_METHOD_INVOKED) {
356
log.complain("Constructor has been invoked after class redefinition");
357
success = false;
358
}
359
360
log.display("Getting value of static field: " + STATIC_METHOD_FIELD_NAME);
361
value = debugee.getStaticFieldValue(testedClassID,
362
STATIC_METHOD_FIELD_NAME, JDWP.Tag.INT);
363
int staticMethodInvoked = ((Integer)value.getValue()).intValue();
364
log.display(" ... got staticMethodInvoked: " + methodKind(staticMethodInvoked));
365
366
if (staticMethodInvoked != redefinecls001b.REDEFINED_METHOD_INVOKED) {
367
log.complain("Not redefined static method is invoked after class redefinition");
368
success = false;
369
}
370
371
log.display("Getting value of static field: " + OBJECT_METHOD_FIELD_NAME);
372
value = debugee.getStaticFieldValue(testedClassID,
373
OBJECT_METHOD_FIELD_NAME, JDWP.Tag.INT);
374
int objectMethodInvoked = ((Integer)value.getValue()).intValue();
375
log.display(" ... got objectMethodInvoked: " + methodKind(objectMethodInvoked));
376
377
if (objectMethodInvoked != redefinecls001b.REDEFINED_METHOD_INVOKED) {
378
log.complain("Not redefined object method is invoked after class redefinition");
379
success = false;
380
}
381
}
382
383
/**
384
* Load class bytes form the given file.
385
*/
386
ByteBuffer loadClassBytes(String fileName, String dirName) {
387
String fileSep = System.getProperty("file.separator");
388
String filePath = dirName + fileSep + fileName;
389
390
String error = "Unable to read bytes from class file:\n\t" + filePath;
391
392
int length = 0;
393
byte bytes[] = null;
394
try {
395
File file = new File(filePath);
396
length = (int)file.length();
397
FileInputStream is = new FileInputStream(file);
398
bytes = new byte[length];
399
int number = is.read(bytes);
400
if (number < 0) {
401
log.complain("EOF reached while reading bytes from file");
402
throw new Failure(error);
403
} else if (number != length) {
404
log.complain("Unexpected number of bytes red from file: " + number
405
+ " (expected: " + length + ")");
406
throw new Failure(error);
407
}
408
is.close();
409
} catch ( IOException e ) {
410
log.complain("Caught IOException while reading bytes from file:\n\t" + e);
411
throw new Failure(error);
412
}
413
ByteBuffer byteBuffer = new ByteBuffer(length, 0);
414
byteBuffer.addBytes(bytes, 0, length);
415
return byteBuffer;
416
}
417
418
/**
419
* Disconnect debuggee and wait for it exited.
420
*/
421
void quitDebugee() {
422
if (debugee == null)
423
return;
424
425
// disconnect debugee
426
if (!dead) {
427
try {
428
log.display("Disconnecting debuggee");
429
debugee.dispose();
430
log.display(" ... debuggee disconnected");
431
} catch (Failure e) {
432
log.display("Failed to finally disconnect debuggee:\n\t"
433
+ e.getMessage());
434
}
435
}
436
437
// wait for debugee exited
438
log.display("Waiting for debuggee exit");
439
int code = debugee.waitFor();
440
log.display(" ... debuggee exited with exit code: " + code);
441
442
// analize debugee exit status code
443
if (code != JCK_STATUS_BASE + PASSED) {
444
log.complain("Debuggee FAILED with exit code: " + code);
445
success = false;
446
}
447
}
448
449
// return string representation of kind of invoked method
450
private static String methodKind(int kind) {
451
switch (kind) {
452
case redefinecls001b.METHOD_NOT_INVOKED:
453
return "METHOD_NOT_INVOKED";
454
case redefinecls001b.REDEFINED_METHOD_INVOKED:
455
return "REDEFINED_METHOD_INVOKED";
456
case redefinecls001b.NOT_REDEFINED_METHOD_INVOKED:
457
return "NOT_REDEFINED_METHOD_INVOKED";
458
default:
459
return "UNKNOWN_METHOD_KIND";
460
}
461
}
462
}
463
464