Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/FieldModification/fieldmod001/fieldmod001.cpp
40955 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 <inttypes.h>
27
#include "jvmti.h"
28
#include "agent_common.h"
29
#include "JVMTITools.h"
30
31
extern "C" {
32
33
34
#define PASSED 0
35
#define STATUS_FAILED 2
36
37
typedef struct {
38
jfieldID fid;
39
char *m_cls;
40
char *m_name;
41
char *m_sig;
42
jlocation loc;
43
char *f_cls;
44
char *f_name;
45
char *f_sig;
46
jboolean is_static;
47
jvalue val;
48
} writable_watch_info;
49
50
typedef struct {
51
jfieldID fid;
52
const char *m_cls;
53
const char *m_name;
54
const char *m_sig;
55
jlocation loc;
56
const char *f_cls;
57
const char *f_name;
58
const char *f_sig;
59
const jboolean is_static;
60
jvalue val;
61
} watch_info;
62
63
static jvmtiEnv *jvmti;
64
static jvmtiEventCallbacks callbacks;
65
static jvmtiCapabilities caps;
66
static jint result = PASSED;
67
static jboolean printdump = JNI_FALSE;
68
static int eventsExpected = 0;
69
static int eventsCount = 0;
70
static watch_info watches[] = {
71
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 1,
72
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticBoolean", "Z", JNI_TRUE, {} },
73
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 5,
74
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticByte", "B", JNI_TRUE, {} },
75
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 9,
76
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticShort", "S", JNI_TRUE, {} },
77
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 13,
78
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticInt", "I", JNI_TRUE, {} },
79
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 19,
80
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticLong", "J", JNI_TRUE, {} },
81
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 24,
82
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticFloat", "F", JNI_TRUE, {} },
83
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 30,
84
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticDouble", "D", JNI_TRUE, {} },
85
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 35,
86
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticChar", "C", JNI_TRUE, {} },
87
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 41,
88
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticObject", "Ljava/lang/Object;", JNI_TRUE, {} },
89
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 47,
90
"Lnsk/jvmti/FieldModification/fieldmod001a;", "staticArrInt", "[I", JNI_TRUE, {} },
91
92
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 52,
93
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceBoolean", "Z", JNI_FALSE, {} },
94
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 58,
95
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceByte", "B", JNI_FALSE, {} },
96
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 64,
97
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceShort", "S", JNI_FALSE, {} },
98
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 70,
99
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceInt", "I", JNI_FALSE, {} },
100
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 77,
101
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceLong", "J", JNI_FALSE, {} },
102
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 83,
103
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceFloat", "F", JNI_FALSE, {} },
104
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 90,
105
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceDouble", "D", JNI_FALSE, {} },
106
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 96,
107
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceChar", "C", JNI_FALSE, {} },
108
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 103,
109
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceObject", "Ljava/lang/Object;", JNI_FALSE, {} },
110
{ NULL, "Lnsk/jvmti/FieldModification/fieldmod001a;", "run", "()V", 110,
111
"Lnsk/jvmti/FieldModification/fieldmod001a;", "instanceArrInt", "[I", JNI_FALSE, {} }
112
};
113
114
void printValue(jvalue val, char *sig) {
115
switch (*sig) {
116
case 'J':
117
printf("0x%x%08x", (jint)(val.j >> 32), (jint)val.j);
118
break;
119
case 'F':
120
printf("%.3f", (double)val.f);
121
break;
122
case 'D':
123
printf("%f", (double)val.d);
124
break;
125
case 'L':
126
case '[':
127
printf("0x%p", val.l);
128
break;
129
case 'Z':
130
printf("0x%x", val.z);
131
break;
132
case 'B':
133
printf("%d", val.b);
134
break;
135
case 'S':
136
printf("%d", val.s);
137
break;
138
case 'C':
139
printf("0x%x", val.c);
140
break;
141
case 'I':
142
printf("%d", val.i);
143
break;
144
default:
145
printf("0x%x%08x", (jint)(val.j >> 32), (jint)val.j);
146
break;
147
}
148
}
149
150
int isEqual(JNIEnv *env, char *sig, jvalue v1, jvalue v2) {
151
switch (*sig) {
152
case 'J':
153
return (v1.j == v2.j);
154
case 'F':
155
return (v1.f == v2.f);
156
case 'D':
157
return (v1.d == v2.d);
158
case 'L':
159
case '[':
160
return env->IsSameObject(v1.l, v2.l);
161
case 'Z':
162
return (v1.z == v2.z);
163
case 'B':
164
return (v1.b == v2.b);
165
case 'S':
166
return (v1.s == v2.s);
167
case 'C':
168
return (v1.c == v2.c);
169
case 'I':
170
return (v1.i == v2.i);
171
default:
172
return (1);
173
}
174
}
175
176
void JNICALL FieldModification(jvmtiEnv *jvmti_env, JNIEnv *env,
177
jthread thr, jmethodID method, jlocation location,
178
jclass field_klass, jobject obj,
179
jfieldID field, char sig, jvalue new_value) {
180
jvmtiError err;
181
jclass cls;
182
writable_watch_info watch;
183
char *generic;
184
size_t i;
185
186
eventsCount++;
187
if (printdump == JNI_TRUE) {
188
printf(">>> retrieving modification watch info ...\n");
189
}
190
watch.fid = field;
191
watch.loc = location;
192
watch.val = new_value;
193
watch.is_static = (obj == NULL) ? JNI_TRUE : JNI_FALSE;
194
err = jvmti_env->GetMethodDeclaringClass(method, &cls);
195
if (err != JVMTI_ERROR_NONE) {
196
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
197
TranslateError(err), err);
198
result = STATUS_FAILED;
199
return;
200
}
201
err = jvmti_env->GetClassSignature(cls,
202
&watch.m_cls, &generic);
203
if (err != JVMTI_ERROR_NONE) {
204
printf("(GetClassSignature) unexpected error: %s (%d)\n",
205
TranslateError(err), err);
206
result = STATUS_FAILED;
207
return;
208
}
209
err = jvmti_env->GetMethodName(method,
210
&watch.m_name, &watch.m_sig, &generic);
211
if (err != JVMTI_ERROR_NONE) {
212
printf("(GetMethodName) unexpected error: %s (%d)\n",
213
TranslateError(err), err);
214
result = STATUS_FAILED;
215
return;
216
}
217
err = jvmti_env->GetClassSignature(field_klass,
218
&watch.f_cls, &generic);
219
if (err != JVMTI_ERROR_NONE) {
220
printf("(GetClassSignature) unexpected error: %s (%d)\n",
221
TranslateError(err), err);
222
result = STATUS_FAILED;
223
return;
224
}
225
err = jvmti_env->GetFieldName(field_klass, field,
226
&watch.f_name, &watch.f_sig, &generic);
227
if (err != JVMTI_ERROR_NONE) {
228
printf("(GetFieldName) unexpected error: %s (%d)\n",
229
TranslateError(err), err);
230
result = STATUS_FAILED;
231
return;
232
}
233
if (printdump == JNI_TRUE) {
234
printf(">>> class: \"%s\"\n", watch.m_cls);
235
printf(">>> method: \"%s%s\"\n", watch.m_name, watch.m_sig);
236
printf(">>> location: 0x%x%08x\n",
237
(jint)(watch.loc >> 32), (jint)watch.loc);
238
printf(">>> field cls: \"%s\"\n", watch.f_cls);
239
printf(">>> field: \"%s:%s\"\n", watch.f_name, watch.f_sig);
240
printf(">>> object: 0x%p\n", obj);
241
printf(">>> new value: ");
242
printValue(watch.val, watch.f_sig);
243
printf("\n");
244
}
245
for (i = 0; i < sizeof(watches)/sizeof(watch_info); i++) {
246
if (watch.fid == watches[i].fid) {
247
if (watch.m_cls == NULL ||
248
strcmp(watch.m_cls, watches[i].m_cls) != 0) {
249
printf("(watch#%" PRIuPTR ") wrong class: \"%s\", expected: \"%s\"\n",
250
i, watch.m_cls, watches[i].m_cls);
251
result = STATUS_FAILED;
252
}
253
if (watch.m_name == NULL ||
254
strcmp(watch.m_name, watches[i].m_name) != 0) {
255
printf("(watch#%" PRIuPTR ") wrong method name: \"%s\"",
256
i, watch.m_name);
257
printf(", expected: \"%s\"\n", watches[i].m_name);
258
result = STATUS_FAILED;
259
}
260
if (watch.m_sig == NULL ||
261
strcmp(watch.m_sig, watches[i].m_sig) != 0) {
262
printf("(watch#%" PRIuPTR ") wrong method sig: \"%s\"",
263
i, watch.m_sig);
264
printf(", expected: \"%s\"\n", watches[i].m_sig);
265
result = STATUS_FAILED;
266
}
267
if (watch.loc != watches[i].loc) {
268
printf("(watch#%" PRIuPTR ") wrong location: 0x%x%08x",
269
i, (jint)(watch.loc >> 32), (jint)watch.loc);
270
printf(", expected: 0x%x%08x\n",
271
(jint)(watches[i].loc >> 32), (jint)watches[i].loc);
272
result = STATUS_FAILED;
273
}
274
if (watch.f_name == NULL ||
275
strcmp(watch.f_name, watches[i].f_name) != 0) {
276
printf("(watch#%" PRIuPTR ") wrong field name: \"%s\"",
277
i, watch.f_name);
278
printf(", expected: \"%s\"\n", watches[i].f_name);
279
result = STATUS_FAILED;
280
}
281
if (watch.f_sig == NULL ||
282
strcmp(watch.f_sig, watches[i].f_sig) != 0) {
283
printf("(watch#%" PRIuPTR ") wrong field sig: \"%s\"",
284
i, watch.f_sig);
285
printf(", expected: \"%s\"\n", watches[i].f_sig);
286
result = STATUS_FAILED;
287
}
288
if (watch.is_static != watches[i].is_static) {
289
printf("(watch#%" PRIuPTR ") wrong field type: %s", i,
290
(watch.is_static == JNI_TRUE) ? "static" : "instance");
291
printf(", expected: %s\n",
292
(watches[i].is_static == JNI_TRUE) ? "static" : "instance");
293
result = STATUS_FAILED;
294
}
295
if (!isEqual((JNIEnv *)env, watch.f_sig, watch.val, watches[i].val)) {
296
printf("(watch#%" PRIuPTR ") wrong new value: ", i);
297
printValue(watch.val, watch.f_sig);
298
printf(", expected: ");
299
printValue(watches[i].val, watch.f_sig);
300
printf("\n");
301
result = STATUS_FAILED;
302
}
303
return;
304
}
305
}
306
printf("Unexpected field modification catched: 0x%p\n", watch.fid);
307
result = STATUS_FAILED;
308
}
309
310
#ifdef STATIC_BUILD
311
JNIEXPORT jint JNICALL Agent_OnLoad_fieldmod001(JavaVM *jvm, char *options, void *reserved) {
312
return Agent_Initialize(jvm, options, reserved);
313
}
314
JNIEXPORT jint JNICALL Agent_OnAttach_fieldmod001(JavaVM *jvm, char *options, void *reserved) {
315
return Agent_Initialize(jvm, options, reserved);
316
}
317
JNIEXPORT jint JNI_OnLoad_fieldmod001(JavaVM *jvm, char *options, void *reserved) {
318
return JNI_VERSION_1_8;
319
}
320
#endif
321
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
322
jvmtiError err;
323
jint res;
324
325
if (options != NULL && strcmp(options, "printdump") == 0) {
326
printdump = JNI_TRUE;
327
}
328
329
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
330
if (res != JNI_OK || jvmti == NULL) {
331
printf("Wrong result of a valid call to GetEnv!\n");
332
return JNI_ERR;
333
}
334
335
err = jvmti->GetPotentialCapabilities(&caps);
336
if (err != JVMTI_ERROR_NONE) {
337
printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
338
TranslateError(err), err);
339
return JNI_ERR;
340
}
341
342
err = jvmti->AddCapabilities(&caps);
343
if (err != JVMTI_ERROR_NONE) {
344
printf("(AddCapabilities) unexpected error: %s (%d)\n",
345
TranslateError(err), err);
346
return JNI_ERR;
347
}
348
349
err = jvmti->GetCapabilities(&caps);
350
if (err != JVMTI_ERROR_NONE) {
351
printf("(GetCapabilities) unexpected error: %s (%d)\n",
352
TranslateError(err), err);
353
return JNI_ERR;
354
}
355
356
if (caps.can_generate_field_modification_events) {
357
callbacks.FieldModification = &FieldModification;
358
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
359
if (err != JVMTI_ERROR_NONE) {
360
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
361
TranslateError(err), err);
362
return JNI_ERR;
363
}
364
365
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
366
JVMTI_EVENT_FIELD_MODIFICATION, NULL);
367
if (err != JVMTI_ERROR_NONE) {
368
printf("Failed to enable JVMTI_EVENT_FIELD_MODIFICATION: %s (%d)\n",
369
TranslateError(err), err);
370
return JNI_ERR;
371
}
372
} else {
373
printf("Warning: FieldModification watch is not implemented\n");
374
}
375
376
return JNI_OK;
377
}
378
379
JNIEXPORT void JNICALL
380
Java_nsk_jvmti_FieldModification_fieldmod001_getReady(JNIEnv *env, jclass klass,
381
jobject obj1, jobject obj2, jobject arr1, jobject arr2) {
382
jvmtiError err;
383
jclass cls;
384
size_t i;
385
386
if (!caps.can_generate_field_modification_events) {
387
return;
388
}
389
390
if (printdump == JNI_TRUE) {
391
printf(">>> setting field modification watches ...\n");
392
}
393
cls = env->FindClass("nsk/jvmti/FieldModification/fieldmod001a");
394
if (cls == NULL) {
395
printf("Cannot find fieldmod001a class!\n");
396
result = STATUS_FAILED;
397
return;
398
}
399
for (i = 0; i < sizeof(watches)/sizeof(watch_info); i++) {
400
if (watches[i].is_static == JNI_TRUE) {
401
watches[i].fid = env->GetStaticFieldID(
402
cls, watches[i].f_name, watches[i].f_sig);
403
} else {
404
watches[i].fid = env->GetFieldID(
405
cls, watches[i].f_name, watches[i].f_sig);
406
}
407
if (watches[i].fid == NULL) {
408
printf("Cannot get field ID for \"%s:%s\"\n",
409
watches[i].f_name, watches[i].f_sig);
410
result = STATUS_FAILED;
411
return;
412
}
413
err = jvmti->SetFieldModificationWatch(cls, watches[i].fid);
414
if (err == JVMTI_ERROR_NONE) {
415
eventsExpected++;
416
} else {
417
printf("(SetFieldModificationWatch#%" PRIuPTR ") unexpected error: %s (%d)\n",
418
i, TranslateError(err), err);
419
result = STATUS_FAILED;
420
}
421
}
422
423
watches[0].val.z = JNI_TRUE;
424
watches[1].val.b = 1;
425
watches[2].val.s = 2;
426
watches[3].val.i = 3;
427
watches[4].val.j = 4;
428
watches[5].val.f = 0.5F;
429
watches[6].val.d = 0.6;
430
watches[7].val.c = 0x61;
431
watches[8].val.l = env->NewGlobalRef(obj1);
432
watches[9].val.l = env->NewGlobalRef(arr1);
433
434
watches[10].val.z = JNI_FALSE;
435
watches[11].val.b = 10;
436
watches[12].val.s = 20;
437
watches[13].val.i = 30;
438
watches[14].val.j = 40;
439
watches[15].val.f = 0.05F;
440
watches[16].val.d = 0.06;
441
watches[17].val.c = 0x7a;
442
watches[18].val.l = env->NewGlobalRef(obj2);
443
watches[19].val.l = env->NewGlobalRef(arr2);
444
445
if (printdump == JNI_TRUE) {
446
printf(">>> ... done\n");
447
}
448
}
449
450
JNIEXPORT jint JNICALL
451
Java_nsk_jvmti_FieldModification_fieldmod001_check(JNIEnv *env, jclass cls) {
452
if (eventsCount != eventsExpected) {
453
printf("Wrong number of field modification events: %d, expected: %d\n",
454
eventsCount, eventsExpected);
455
result = STATUS_FAILED;
456
}
457
return result;
458
}
459
460
}
461
462