Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetBytecodes/bytecodes003/bytecodes003.cpp
40951 views
1
/*
2
* Copyright (c) 2003, 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
#include <stdio.h>
25
#include <string.h>
26
#include "jvmti.h"
27
#include "agent_common.h"
28
#include "JVMTITools.h"
29
30
extern "C" {
31
32
33
#define PASSED 0
34
#define STATUS_FAILED 2
35
36
typedef enum {
37
opc_iinc = 132,
38
opc_tableswitch = 170,
39
opc_lookupswitch = 171,
40
opc_wide = 196
41
} opcode_type;
42
43
typedef struct {
44
const char *name;
45
unsigned char code;
46
int length;
47
} opcode_info;
48
49
static jvmtiEnv *jvmti = NULL;
50
static jvmtiCapabilities caps;
51
static jvmtiEventCallbacks callbacks;
52
static jint result = PASSED;
53
static jboolean printdump = JNI_FALSE;
54
static int eventsCount = 0;
55
static opcode_info opcodes[] = {
56
{ "nop", 0, 1 },
57
{ "aconst_null", 1, 1 },
58
{ "iconst_m1", 2, 1 },
59
{ "iconst_0", 3, 1 },
60
{ "iconst_1", 4, 1 },
61
{ "iconst_2", 5, 1 },
62
{ "iconst_3", 6, 1 },
63
{ "iconst_4", 7, 1 },
64
{ "iconst_5", 8, 1 },
65
{ "lconst_0", 9, 1 },
66
{ "lconst_1", 10, 1 },
67
{ "fconst_0", 11, 1 },
68
{ "fconst_1", 12, 1 },
69
{ "fconst_2", 13, 1 },
70
{ "dconst_0", 14, 1 },
71
{ "dconst_1", 15, 1 },
72
{ "bipush", 16, 2 },
73
{ "sipush", 17, 3 },
74
{ "ldc", 18, 2 },
75
{ "ldc_w", 19, 3 },
76
{ "ldc2_w", 20, 3 },
77
{ "iload", 21, 2 },
78
{ "lload", 22, 2 },
79
{ "fload", 23, 2 },
80
{ "dload", 24, 2 },
81
{ "aload", 25, 2 },
82
{ "iload_0", 26, 1 },
83
{ "iload_1", 27, 1 },
84
{ "iload_2", 28, 1 },
85
{ "iload_3", 29, 1 },
86
{ "lload_0", 30, 1 },
87
{ "lload_1", 31, 1 },
88
{ "lload_2", 32, 1 },
89
{ "lload_3", 33, 1 },
90
{ "fload_0", 34, 1 },
91
{ "fload_1", 35, 1 },
92
{ "fload_2", 36, 1 },
93
{ "fload_3", 37, 1 },
94
{ "dload_0", 38, 1 },
95
{ "dload_1", 39, 1 },
96
{ "dload_2", 40, 1 },
97
{ "dload_3", 41, 1 },
98
{ "aload_0", 42, 1 },
99
{ "aload_1", 43, 1 },
100
{ "aload_2", 44, 1 },
101
{ "aload_3", 45, 1 },
102
{ "iaload", 46, 1 },
103
{ "laload", 47, 1 },
104
{ "faload", 48, 1 },
105
{ "daload", 49, 1 },
106
{ "aaload", 50, 1 },
107
{ "baload", 51, 1 },
108
{ "caload", 52, 1 },
109
{ "saload", 53, 1 },
110
{ "istore", 54, 2 },
111
{ "lstore", 55, 2 },
112
{ "fstore", 56, 2 },
113
{ "dstore", 57, 2 },
114
{ "astore", 58, 2 },
115
{ "istore_0", 59, 1 },
116
{ "istore_1", 60, 1 },
117
{ "istore_2", 61, 1 },
118
{ "istore_3", 62, 1 },
119
{ "lstore_0", 63, 1 },
120
{ "lstore_1", 64, 1 },
121
{ "lstore_2", 65, 1 },
122
{ "lstore_3", 66, 1 },
123
{ "fstore_0", 67, 1 },
124
{ "fstore_1", 68, 1 },
125
{ "fstore_2", 69, 1 },
126
{ "fstore_3", 70, 1 },
127
{ "dstore_0", 71, 1 },
128
{ "dstore_1", 72, 1 },
129
{ "dstore_2", 73, 1 },
130
{ "dstore_3", 74, 1 },
131
{ "astore_0", 75, 1 },
132
{ "astore_1", 76, 1 },
133
{ "astore_2", 77, 1 },
134
{ "astore_3", 78, 1 },
135
{ "iastore", 79, 1 },
136
{ "lastore", 80, 1 },
137
{ "fastore", 81, 1 },
138
{ "dastore", 82, 1 },
139
{ "aastore", 83, 1 },
140
{ "bastore", 84, 1 },
141
{ "castore", 85, 1 },
142
{ "sastore", 86, 1 },
143
{ "pop", 87, 1 },
144
{ "pop2", 88, 1 },
145
{ "dup", 89, 1 },
146
{ "dup_x1", 90, 1 },
147
{ "dup_x2", 91, 1 },
148
{ "dup2", 92, 1 },
149
{ "dup2_x1", 93, 1 },
150
{ "dup2_x2", 94, 1 },
151
{ "swap", 95, 1 },
152
{ "iadd", 96, 1 },
153
{ "ladd", 97, 1 },
154
{ "fadd", 98, 1 },
155
{ "dadd", 99, 1 },
156
{ "isub", 100, 1 },
157
{ "lsub", 101, 1 },
158
{ "fsub", 102, 1 },
159
{ "dsub", 103, 1 },
160
{ "imul", 104, 1 },
161
{ "lmul", 105, 1 },
162
{ "fmul", 106, 1 },
163
{ "dmul", 107, 1 },
164
{ "idiv", 108, 1 },
165
{ "ldiv", 109, 1 },
166
{ "fdiv", 110, 1 },
167
{ "ddiv", 111, 1 },
168
{ "irem", 112, 1 },
169
{ "lrem", 113, 1 },
170
{ "frem", 114, 1 },
171
{ "drem", 115, 1 },
172
{ "ineg", 116, 1 },
173
{ "lneg", 117, 1 },
174
{ "fneg", 118, 1 },
175
{ "dneg", 119, 1 },
176
{ "ishl", 120, 1 },
177
{ "lshl", 121, 1 },
178
{ "ishr", 122, 1 },
179
{ "lshr", 123, 1 },
180
{ "iushr", 124, 1 },
181
{ "lushr", 125, 1 },
182
{ "iand", 126, 1 },
183
{ "land", 127, 1 },
184
{ "ior", 128, 1 },
185
{ "lor", 129, 1 },
186
{ "ixor", 130, 1 },
187
{ "lxor", 131, 1 },
188
{ "iinc", 132, 3 },
189
{ "i2l", 133, 1 },
190
{ "i2f", 134, 1 },
191
{ "i2d", 135, 1 },
192
{ "l2i", 136, 1 },
193
{ "l2f", 137, 1 },
194
{ "l2d", 138, 1 },
195
{ "f2i", 139, 1 },
196
{ "f2l", 140, 1 },
197
{ "f2d", 141, 1 },
198
{ "d2i", 142, 1 },
199
{ "d2l", 143, 1 },
200
{ "d2f", 144, 1 },
201
{ "i2b", 145, 1 },
202
{ "i2c", 146, 1 },
203
{ "i2s", 147, 1 },
204
{ "lcmp", 148, 1 },
205
{ "fcmpl", 149, 1 },
206
{ "fcmpg", 150, 1 },
207
{ "dcmpl", 151, 1 },
208
{ "dcmpg", 152, 1 },
209
{ "ifeq", 153, 3 },
210
{ "ifne", 154, 3 },
211
{ "iflt", 155, 3 },
212
{ "ifge", 156, 3 },
213
{ "ifgt", 157, 3 },
214
{ "ifle", 158, 3 },
215
{ "if_icmpeq", 159, 3 },
216
{ "if_icmpne", 160, 3 },
217
{ "if_icmplt", 161, 3 },
218
{ "if_icmpge", 162, 3 },
219
{ "if_icmpgt", 163, 3 },
220
{ "if_icmple", 164, 3 },
221
{ "if_acmpeq", 165, 3 },
222
{ "if_acmpne", 166, 3 },
223
{ "goto", 167, 3 },
224
{ "jsr", 168, 3 },
225
{ "ret", 169, 2 },
226
{ "tableswitch", 170, 0 },
227
{ "lookupswitch", 171, 0 },
228
{ "ireturn", 172, 1 },
229
{ "lreturn", 173, 1 },
230
{ "freturn", 174, 1 },
231
{ "dreturn", 175, 1 },
232
{ "areturn", 176, 1 },
233
{ "return", 177, 1 },
234
{ "getstatic", 178, 3 },
235
{ "putstatic", 179, 3 },
236
{ "getfield", 180, 3 },
237
{ "putfield", 181, 3 },
238
{ "invokevirtual", 182, 3 },
239
{ "invokespecial", 183, 3 },
240
{ "invokestatic", 184, 3 },
241
{ "invokeinterface", 185, 5 },
242
{ "invokedynamic", 186, 5 },
243
{ "new", 187, 3 },
244
{ "newarray", 188, 2 },
245
{ "anewarray", 189, 3 },
246
{ "arraylength", 190, 1 },
247
{ "athrow", 191, 1 },
248
{ "checkcast", 192, 3 },
249
{ "instanceof", 193, 3 },
250
{ "monitorenter", 194, 1 },
251
{ "monitorexit", 195, 1 },
252
{ "wide", 196, 0 },
253
{ "multianewarray", 197, 4 },
254
{ "ifnull", 198, 3 },
255
{ "ifnonnull", 199, 3 },
256
{ "goto_w", 200, 5 },
257
{ "jsr_w", 201, 5 },
258
{ "breakpoint", 202, 1 },
259
{ "impdep1", 254, 1 },
260
{ "impdep2", 255, 1 }
261
};
262
263
jint get_u4(unsigned char *p) {
264
return (jint)p[3] | ((jint)p[2]<<8) | ((jint)p[1]<<16) | ((jint)p[0]<<24);
265
}
266
267
jboolean checkCode(jint bytecodeCount, unsigned char *buf) {
268
unsigned char code;
269
jint pc, cur_pc, length;
270
size_t i;
271
272
for (pc = 0; pc >= 0 && pc < bytecodeCount; pc += length) {
273
code = buf[pc];
274
for (i = 0; i < sizeof(opcodes)/sizeof(opcode_info); i++) {
275
if (code == opcodes[i].code) {
276
switch (code) {
277
case opc_wide:
278
length = (buf[pc + 1] == opc_iinc ? 6 : 4);
279
break;
280
case opc_lookupswitch:
281
cur_pc = (pc + 4) & (~3);
282
length = cur_pc - pc + 8;
283
length += get_u4(buf + cur_pc + 4) * 8;
284
break;
285
case opc_tableswitch:
286
cur_pc = (pc + 4) & (~3);
287
length = cur_pc - pc + 12;
288
length += (get_u4(buf + cur_pc + 8) -
289
get_u4(buf + cur_pc + 4) + 1) * 4;
290
break;
291
default:
292
length = opcodes[i].length;
293
break;
294
}
295
if (printdump == JNI_TRUE) {
296
printf(">>> %4d: %s (%d)\n",
297
pc, opcodes[i].name, length);
298
}
299
if (length <= 0) {
300
printf("Invalid length: %d for opcode \"%s\" (%d)\n",
301
length, opcodes[i].name, code);
302
return JNI_FALSE;
303
}
304
break;
305
}
306
}
307
if (i >= sizeof(opcodes)/sizeof(opcode_info)) {
308
/* opcode not found */
309
printf("Non-standard opcode: %d (0x%x)\n", code, code);
310
return JNI_FALSE;
311
}
312
}
313
return JNI_TRUE;
314
}
315
316
void JNICALL ClassPrepare(jvmtiEnv *jvmti_env, JNIEnv *env,
317
jthread thr, jclass cls) {
318
jvmtiError err;
319
char *sig, *name, *msig;
320
jint mcount;
321
jmethodID *methods;
322
jboolean isNative;
323
jint bytecodeCount;
324
unsigned char *bytecodes;
325
jint i;
326
327
sig = NULL;
328
err = jvmti_env->GetClassSignature(cls, &sig, NULL);
329
if (err != JVMTI_ERROR_NONE) {
330
printf("(GetClassSignature#%d) unexpected error: %s (%d)\n",
331
eventsCount, TranslateError(err), err);
332
result = STATUS_FAILED;
333
return;
334
}
335
err = jvmti_env->GetClassMethods(cls, &mcount, &methods);
336
if (err != JVMTI_ERROR_NONE) {
337
printf("(GetClassMethods#%d) unexpected error: %s (%d)\n",
338
eventsCount, TranslateError(err), err);
339
result = STATUS_FAILED;
340
return;
341
}
342
343
if (printdump == JNI_TRUE) {
344
printf(">>> [class prepare event #%d]", eventsCount);
345
printf(" \"%s\"\n", sig);
346
printf(">>> %d methods:\n", mcount);
347
}
348
349
for (i = 0; i < mcount; i++) {
350
if (methods[i] == NULL) {
351
if (printdump == JNI_TRUE) {
352
printf(" null");
353
}
354
} else {
355
name = NULL;
356
msig = NULL;
357
bytecodes = NULL;
358
err = jvmti_env->GetMethodName(methods[i], &name, &msig, NULL);
359
if (err != JVMTI_ERROR_NONE) {
360
printf("(GetMethodName) unexpected error: %s (%d)\n",
361
TranslateError(err), err);
362
printf(" class: \"%s\"\n", sig);
363
result = STATUS_FAILED;
364
return;
365
}
366
isNative = JNI_TRUE;
367
err = jvmti_env->IsMethodNative(methods[i], &isNative);
368
if (err != JVMTI_ERROR_NONE) {
369
printf("(IsMethodNative) unexpected error: %s (%d)\n",
370
TranslateError(err), err);
371
printf(" class: \"%s\"\n", sig);
372
printf(" method = \"%s%s\"\n", name, msig);
373
result = STATUS_FAILED;
374
return;
375
}
376
if (isNative == JNI_TRUE) {
377
if (printdump == JNI_TRUE) {
378
printf(">>> \"%s%s\", native\n", name, msig);
379
}
380
} else {
381
err = jvmti_env->GetBytecodes(methods[i],
382
&bytecodeCount, &bytecodes);
383
if (err != JVMTI_ERROR_NONE) {
384
printf("(GetBytecodes#%d:%d) unexpected error: %s (%d)\n",
385
eventsCount, i, TranslateError(err), err);
386
result = STATUS_FAILED;
387
return;
388
} else {
389
if (printdump == JNI_TRUE) {
390
printf(">>> \"%s%s\", %d bytes\n",
391
name, msig, bytecodeCount);
392
}
393
if (checkCode(bytecodeCount, bytecodes) == JNI_FALSE) {
394
printf(" class: \"%s\"\n", sig);
395
printf(" method = \"%s%s\"\n", name, msig);
396
result = STATUS_FAILED;
397
}
398
}
399
}
400
if (name != NULL) {
401
jvmti_env->Deallocate((unsigned char *)name);
402
}
403
if (msig != NULL) {
404
jvmti_env->Deallocate((unsigned char *)msig);
405
}
406
if (bytecodes != NULL) {
407
jvmti_env->Deallocate(bytecodes);
408
}
409
}
410
}
411
412
if (methods != NULL) {
413
jvmti_env->Deallocate((unsigned char *)methods);
414
}
415
if (sig != NULL) {
416
jvmti_env->Deallocate((unsigned char *)sig);
417
}
418
eventsCount++;
419
}
420
421
#ifdef STATIC_BUILD
422
JNIEXPORT jint JNICALL Agent_OnLoad_bytecodes003(JavaVM *jvm, char *options, void *reserved) {
423
return Agent_Initialize(jvm, options, reserved);
424
}
425
JNIEXPORT jint JNICALL Agent_OnAttach_bytecodes003(JavaVM *jvm, char *options, void *reserved) {
426
return Agent_Initialize(jvm, options, reserved);
427
}
428
JNIEXPORT jint JNI_OnLoad_bytecodes003(JavaVM *jvm, char *options, void *reserved) {
429
return JNI_VERSION_1_8;
430
}
431
#endif
432
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
433
jvmtiError err;
434
jint res;
435
436
if (options != NULL && strcmp(options, "printdump") == 0) {
437
printdump = JNI_TRUE;
438
}
439
440
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
441
if (res != JNI_OK || jvmti == NULL) {
442
printf("Wrong result of a valid call to GetEnv!\n");
443
return JNI_ERR;
444
}
445
446
err = jvmti->GetCapabilities(&caps);
447
if (err != JVMTI_ERROR_NONE) {
448
printf("(GetCapabilities) unexpected error: %s (%d)\n",
449
TranslateError(err), err);
450
return JNI_ERR;
451
}
452
453
err = jvmti->GetCapabilities(&caps);
454
if (err != JVMTI_ERROR_NONE) {
455
printf("(GetCapabilities) unexpected error: %s (%d)\n",
456
TranslateError(err), err);
457
return JNI_ERR;
458
}
459
460
err = jvmti->GetPotentialCapabilities(&caps);
461
if (err != JVMTI_ERROR_NONE) {
462
printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
463
TranslateError(err), err);
464
return JNI_ERR;
465
}
466
467
err = jvmti->AddCapabilities(&caps);
468
if (err != JVMTI_ERROR_NONE) {
469
printf("(AddCapabilities) unexpected error: %s (%d)\n",
470
TranslateError(err), err);
471
return JNI_ERR;
472
}
473
474
err = jvmti->GetCapabilities(&caps);
475
if (err != JVMTI_ERROR_NONE) {
476
printf("(GetCapabilities) unexpected error: %s (%d)\n",
477
TranslateError(err), err);
478
return JNI_ERR;
479
}
480
481
if (caps.can_get_bytecodes) {
482
callbacks.ClassPrepare = &ClassPrepare;
483
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
484
if (err != JVMTI_ERROR_NONE) {
485
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
486
TranslateError(err), err);
487
return JNI_ERR;
488
}
489
490
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
491
JVMTI_EVENT_CLASS_PREPARE, NULL);
492
if (err != JVMTI_ERROR_NONE) {
493
printf("Failed to enable ClassPrepare: %s (%d)\n",
494
TranslateError(err), err);
495
result = STATUS_FAILED;
496
}
497
} else {
498
printf("Warning: GetBytecodes is not implemented\n");
499
}
500
501
return JNI_OK;
502
}
503
504
JNIEXPORT jint JNICALL
505
Java_nsk_jvmti_GetBytecodes_bytecodes003_check(JNIEnv *env, jclass cls) {
506
jvmtiError err;
507
508
if (jvmti == NULL) {
509
printf("JVMTI client was not properly loaded!\n");
510
return STATUS_FAILED;
511
}
512
513
if (caps.can_get_bytecodes) {
514
err = jvmti->SetEventNotificationMode(JVMTI_DISABLE,
515
JVMTI_EVENT_CLASS_PREPARE, NULL);
516
if (err != JVMTI_ERROR_NONE) {
517
printf("Failed to disable JVMTI_EVENT_CLASS_PREPARE: %s (%d)\n",
518
TranslateError(err), err);
519
result = STATUS_FAILED;
520
}
521
}
522
523
if (printdump == JNI_TRUE) {
524
printf("Total number of class prepare events: %d\n", eventsCount);
525
}
526
527
return result;
528
}
529
530
}
531
532