Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/FieldAccess/fieldacc002/fieldacc002.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 <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
} writable_watch_info;
48
49
typedef struct {
50
jfieldID fid;
51
const char *m_cls;
52
const char *m_name;
53
const char *m_sig;
54
jlocation loc;
55
const char *f_cls;
56
const char *f_name;
57
const char *f_sig;
58
jboolean is_static;
59
} watch_info;
60
61
static jvmtiEnv *jvmti;
62
static jvmtiEventCallbacks callbacks;
63
static jvmtiCapabilities caps;
64
static jint result = PASSED;
65
static jboolean printdump = JNI_FALSE;
66
static int eventsExpected = 0;
67
static int eventsCount = 0;
68
static watch_info watches[] = {
69
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
70
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticBoolean", "Z", JNI_TRUE },
71
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
72
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticByte", "B", JNI_TRUE },
73
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
74
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticShort", "S", JNI_TRUE },
75
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
76
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticInt", "I", JNI_TRUE },
77
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
78
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticLong", "J", JNI_TRUE },
79
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
80
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticFloat", "F", JNI_TRUE },
81
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
82
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticDouble", "D", JNI_TRUE },
83
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
84
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticChar", "C", JNI_TRUE },
85
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
86
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticObject", "Ljava/lang/Object;", JNI_TRUE },
87
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
88
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "staticArrInt", "[I", JNI_TRUE },
89
90
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
91
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceBoolean", "Z", JNI_FALSE },
92
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
93
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceByte", "B", JNI_FALSE },
94
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
95
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceShort", "S", JNI_FALSE },
96
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
97
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceInt", "I", JNI_FALSE },
98
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
99
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceLong", "J", JNI_FALSE },
100
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
101
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceFloat", "F", JNI_FALSE },
102
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
103
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceDouble", "D", JNI_FALSE },
104
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
105
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceChar", "C", JNI_FALSE },
106
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
107
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceObject", "Ljava/lang/Object;", JNI_FALSE },
108
{ NULL, "Lnsk/jvmti/FieldAccess/fieldacc002;", "check", "(Ljava/lang/Object;)I", 0,
109
"Lnsk/jvmti/FieldAccess/fieldacc002a;", "instanceArrInt", "[I", JNI_FALSE }
110
};
111
112
void JNICALL FieldAccess(jvmtiEnv *jvmti_env, JNIEnv *env,
113
jthread thr, jmethodID method,
114
jlocation location, jclass field_klass, jobject obj, jfieldID field) {
115
jvmtiError err;
116
jclass cls;
117
writable_watch_info watch;
118
char *generic;
119
size_t i;
120
121
eventsCount++;
122
if (printdump == JNI_TRUE) {
123
printf(">>> retrieving access watch info ...\n");
124
}
125
watch.fid = field;
126
watch.loc = location;
127
watch.is_static = (obj == NULL) ? JNI_TRUE : JNI_FALSE;
128
err = jvmti_env->GetMethodDeclaringClass(method, &cls);
129
if (err != JVMTI_ERROR_NONE) {
130
printf("(GetMethodDeclaringClass) unexpected error: %s (%d)\n",
131
TranslateError(err), err);
132
result = STATUS_FAILED;
133
return;
134
}
135
err = jvmti_env->GetClassSignature(cls,
136
&watch.m_cls, &generic);
137
if (err != JVMTI_ERROR_NONE) {
138
printf("(GetClassSignature) unexpected error: %s (%d)\n",
139
TranslateError(err), err);
140
result = STATUS_FAILED;
141
return;
142
}
143
err = jvmti_env->GetMethodName(method,
144
&watch.m_name, &watch.m_sig, &generic);
145
if (err != JVMTI_ERROR_NONE) {
146
printf("(GetMethodName) unexpected error: %s (%d)\n",
147
TranslateError(err), err);
148
result = STATUS_FAILED;
149
return;
150
}
151
err = jvmti_env->GetClassSignature(field_klass,
152
&watch.f_cls, &generic);
153
if (err != JVMTI_ERROR_NONE) {
154
printf("(GetClassSignature) unexpected error: %s (%d)\n",
155
TranslateError(err), err);
156
result = STATUS_FAILED;
157
return;
158
}
159
err = jvmti_env->GetFieldName(field_klass, field,
160
&watch.f_name, &watch.f_sig, &generic);
161
if (err != JVMTI_ERROR_NONE) {
162
printf("(GetFieldName) unexpected error: %s (%d)\n",
163
TranslateError(err), err);
164
result = STATUS_FAILED;
165
return;
166
}
167
if (printdump == JNI_TRUE) {
168
printf(">>> class: \"%s\"\n", watch.m_cls);
169
printf(">>> method: \"%s%s\"\n", watch.m_name, watch.m_sig);
170
printf(">>> location: 0x%x%08x\n",
171
(jint)(watch.loc >> 32), (jint)watch.loc);
172
printf(">>> field cls: \"%s\"\n", watch.f_cls);
173
printf(">>> field: \"%s:%s\"\n", watch.f_name, watch.f_sig);
174
printf(">>> object: 0x%p\n", obj);
175
}
176
for (i = 0; i < sizeof(watches)/sizeof(watch_info); i++) {
177
if (watch.fid == watches[i].fid) {
178
if (watch.m_cls == NULL ||
179
strcmp(watch.m_cls, watches[i].m_cls) != 0) {
180
printf("(watch#%" PRIuPTR ") wrong class: \"%s\", expected: \"%s\"\n",
181
i, watch.m_cls, watches[i].m_cls);
182
result = STATUS_FAILED;
183
}
184
if (watch.m_name == NULL ||
185
strcmp(watch.m_name, watches[i].m_name) != 0) {
186
printf("(watch#%" PRIuPTR ") wrong method name: \"%s\"",
187
i, watch.m_name);
188
printf(", expected: \"%s\"\n", watches[i].m_name);
189
result = STATUS_FAILED;
190
}
191
if (watch.m_sig == NULL ||
192
strcmp(watch.m_sig, watches[i].m_sig) != 0) {
193
printf("(watch#%" PRIuPTR ") wrong method sig: \"%s\"",
194
i, watch.m_sig);
195
printf(", expected: \"%s\"\n", watches[i].m_sig);
196
result = STATUS_FAILED;
197
}
198
if (watch.loc != watches[i].loc) {
199
printf("(watch#%" PRIuPTR ") wrong location: 0x%x%08x",
200
i, (jint)(watch.loc >> 32), (jint)watch.loc);
201
printf(", expected: 0x%x%08x\n",
202
(jint)(watches[i].loc >> 32), (jint)watches[i].loc);
203
result = STATUS_FAILED;
204
}
205
if (watch.f_name == NULL ||
206
strcmp(watch.f_name, watches[i].f_name) != 0) {
207
printf("(watch#%" PRIuPTR ") wrong field name: \"%s\"",
208
i, watch.f_name);
209
printf(", expected: \"%s\"\n", watches[i].f_name);
210
result = STATUS_FAILED;
211
}
212
if (watch.f_sig == NULL ||
213
strcmp(watch.f_sig, watches[i].f_sig) != 0) {
214
printf("(watch#%" PRIuPTR ") wrong field sig: \"%s\"",
215
i, watch.f_sig);
216
printf(", expected: \"%s\"\n", watches[i].f_sig);
217
result = STATUS_FAILED;
218
}
219
if (watch.is_static != watches[i].is_static) {
220
printf("(watch#%" PRIuPTR ") wrong field type: %s", i,
221
(watch.is_static == JNI_TRUE) ? "static" : "instance");
222
printf(", expected: %s\n",
223
(watches[i].is_static == JNI_TRUE) ? "static" : "instance");
224
result = STATUS_FAILED;
225
}
226
return;
227
}
228
}
229
printf("Unexpected field access catched: 0x%p\n", watch.fid);
230
result = STATUS_FAILED;
231
}
232
233
#ifdef STATIC_BUILD
234
JNIEXPORT jint JNICALL Agent_OnLoad_fieldacc002(JavaVM *jvm, char *options, void *reserved) {
235
return Agent_Initialize(jvm, options, reserved);
236
}
237
JNIEXPORT jint JNICALL Agent_OnAttach_fieldacc002(JavaVM *jvm, char *options, void *reserved) {
238
return Agent_Initialize(jvm, options, reserved);
239
}
240
JNIEXPORT jint JNI_OnLoad_fieldacc002(JavaVM *jvm, char *options, void *reserved) {
241
return JNI_VERSION_1_8;
242
}
243
#endif
244
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
245
jvmtiError err;
246
jint res;
247
248
if (options != NULL && strcmp(options, "printdump") == 0) {
249
printdump = JNI_TRUE;
250
}
251
252
res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
253
if (res != JNI_OK || jvmti == NULL) {
254
printf("Wrong result of a valid call to GetEnv!\n");
255
return JNI_ERR;
256
}
257
258
err = jvmti->GetPotentialCapabilities(&caps);
259
if (err != JVMTI_ERROR_NONE) {
260
printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
261
TranslateError(err), err);
262
return JNI_ERR;
263
}
264
265
err = jvmti->AddCapabilities(&caps);
266
if (err != JVMTI_ERROR_NONE) {
267
printf("(AddCapabilities) unexpected error: %s (%d)\n",
268
TranslateError(err), err);
269
return JNI_ERR;
270
}
271
272
err = jvmti->GetCapabilities(&caps);
273
if (err != JVMTI_ERROR_NONE) {
274
printf("(GetCapabilities) unexpected error: %s (%d)\n",
275
TranslateError(err), err);
276
return JNI_ERR;
277
}
278
279
if (caps.can_generate_field_access_events) {
280
callbacks.FieldAccess = &FieldAccess;
281
err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
282
if (err != JVMTI_ERROR_NONE) {
283
printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
284
TranslateError(err), err);
285
return JNI_ERR;
286
}
287
288
err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
289
JVMTI_EVENT_FIELD_ACCESS, NULL);
290
if (err != JVMTI_ERROR_NONE) {
291
printf("Failed to enable JVMTI_EVENT_FIELD_ACCESS: %s (%d)\n",
292
TranslateError(err), err);
293
return JNI_ERR;
294
}
295
} else {
296
printf("Warning: FieldAccess watch is not implemented\n");
297
}
298
299
return JNI_OK;
300
}
301
302
JNIEXPORT void JNICALL Java_nsk_jvmti_FieldAccess_fieldacc002_getReady(JNIEnv *env, jclass clz) {
303
jvmtiError err;
304
jclass cls;
305
size_t i;
306
307
if (!caps.can_generate_field_access_events) {
308
return;
309
}
310
311
if (printdump == JNI_TRUE) {
312
printf(">>> setting field access watches ...\n");
313
}
314
315
cls = env->FindClass("nsk/jvmti/FieldAccess/fieldacc002a");
316
if (cls == NULL) {
317
printf("Cannot find fieldacc002a class!\n");
318
result = STATUS_FAILED;
319
return;
320
}
321
for (i = 0; i < sizeof(watches)/sizeof(watch_info); i++) {
322
if (watches[i].is_static == JNI_TRUE) {
323
watches[i].fid = env->GetStaticFieldID(
324
cls, watches[i].f_name, watches[i].f_sig);
325
} else {
326
watches[i].fid = env->GetFieldID(
327
cls, watches[i].f_name, watches[i].f_sig);
328
}
329
if (watches[i].fid == NULL) {
330
printf("Cannot find field \"%s\"!\n", watches[i].f_name);
331
result = STATUS_FAILED;
332
return;
333
}
334
err = jvmti->SetFieldAccessWatch(cls, watches[i].fid);
335
if (err == JVMTI_ERROR_NONE) {
336
eventsExpected++;
337
} else {
338
printf("(SetFieldAccessWatch#%" PRIuPTR ") unexpected error: %s (%d)\n",
339
i, TranslateError(err), err);
340
result = STATUS_FAILED;
341
}
342
}
343
344
if (printdump == JNI_TRUE) {
345
printf(">>> ... done\n");
346
}
347
}
348
349
JNIEXPORT jint JNICALL
350
Java_nsk_jvmti_FieldAccess_fieldacc002_check(JNIEnv *env, jclass clz, jobject obj) {
351
jclass cls;
352
353
if (!caps.can_generate_field_access_events) {
354
return result;
355
}
356
357
if (printdump == JNI_TRUE) {
358
printf(">>> accessing fields ...\n");
359
}
360
361
cls = env->FindClass("nsk/jvmti/FieldAccess/fieldacc002a");
362
if (cls == NULL) {
363
printf("Cannot find fieldacc002a class!\n");
364
return STATUS_FAILED;
365
}
366
367
env->GetStaticBooleanField(cls, watches[0].fid);
368
env->GetStaticByteField(cls, watches[1].fid);
369
env->GetStaticShortField(cls, watches[2].fid);
370
env->GetStaticIntField(cls, watches[3].fid);
371
env->GetStaticLongField(cls, watches[4].fid);
372
env->GetStaticFloatField(cls, watches[5].fid);
373
env->GetStaticDoubleField(cls, watches[6].fid);
374
env->GetStaticCharField(cls, watches[7].fid);
375
env->GetStaticObjectField(cls, watches[8].fid);
376
env->GetStaticObjectField(cls, watches[9].fid);
377
378
env->GetBooleanField(obj, watches[10].fid);
379
env->GetByteField(obj, watches[11].fid);
380
env->GetShortField(obj, watches[12].fid);
381
env->GetIntField(obj, watches[13].fid);
382
env->GetLongField(obj, watches[14].fid);
383
env->GetFloatField(obj, watches[15].fid);
384
env->GetDoubleField(obj, watches[16].fid);
385
env->GetCharField(obj, watches[17].fid);
386
env->GetObjectField(obj, watches[18].fid);
387
env->GetObjectField(obj, watches[19].fid);
388
389
if (printdump == JNI_TRUE) {
390
printf(">>> ... done\n");
391
}
392
393
if (eventsCount != eventsExpected) {
394
printf("Wrong number of field access events: %d, expected: %d\n",
395
eventsCount, eventsExpected);
396
result = STATUS_FAILED;
397
}
398
return result;
399
}
400
401
}
402
403