53
54 static jint newClassSize = 0;
55 static unsigned char* newClassBytes = NULL;
56
57 static volatile int eventsCount = 0;
58
59 /* ============================================================================= */
60
61 /** Get classfile bytecode from a static field of given class. */
62 static int getBytecode(jvmtiEnv* jvmti, JNIEnv* jni, jclass cls,
63 const char fieldName[], const char fieldSig[],
64 jint* size, unsigned char* *bytes) {
65
66 jfieldID fieldID = NULL;
67 jbyteArray array = NULL;
68 jbyte* elements;
69 int i;
70
71 NSK_DISPLAY1("Find static field: %s\n", fieldName);
72 if (!NSK_JNI_VERIFY(jni, (fieldID =
73 NSK_CPP_STUB4(GetStaticFieldID, jni, cls, fieldName, fieldSig)) != NULL)) {
74 nsk_jvmti_setFailStatus();
75 return NSK_FALSE;
76 }
77 NSK_DISPLAY1(" ... got fieldID: 0x%p\n", (void*)fieldID);
78
79 NSK_DISPLAY1("Get classfile bytes array from static field: %s\n", fieldName);
80 if (!NSK_JNI_VERIFY(jni, (array = (jbyteArray)
81 NSK_CPP_STUB3(GetStaticObjectField, jni, cls, fieldID)) != NULL)) {
82 nsk_jvmti_setFailStatus();
83 return NSK_FALSE;
84 }
85 NSK_DISPLAY1(" ... got array object: 0x%p\n", (void*)array);
86
87 if (!NSK_JNI_VERIFY(jni, (*size =
88 NSK_CPP_STUB2(GetArrayLength, jni, array)) > 0)) {
89 nsk_jvmti_setFailStatus();
90 return NSK_FALSE;
91 }
92 NSK_DISPLAY1(" ... got array size: %d bytes\n", (int)*size);
93
94 {
95 jboolean isCopy;
96 if (!NSK_JNI_VERIFY(jni, (elements =
97 NSK_CPP_STUB3(GetByteArrayElements, jni, array,
98 &isCopy)) != NULL)) {
99 nsk_jvmti_setFailStatus();
100 return NSK_FALSE;
101 }
102 }
103 NSK_DISPLAY1(" ... got elements list: 0x%p\n", (void*)elements);
104
105 if (!NSK_JVMTI_VERIFY(
106 NSK_CPP_STUB3(Allocate, jvmti, *size, bytes))) {
107 nsk_jvmti_setFailStatus();
108 return NSK_FALSE;
109 }
110 NSK_DISPLAY1(" ... created bytes array: 0x%p\n", (void*)*bytes);
111
112 for (i = 0; i < *size; i++) {
113 (*bytes)[i] = (unsigned char)elements[i];
114 }
115 NSK_DISPLAY1(" ... copied bytecode: %d bytes\n", (int)*size);
116
117 NSK_DISPLAY1("Release elements list: 0x%p\n", (void*)elements);
118 NSK_TRACE(NSK_CPP_STUB4(ReleaseByteArrayElements, jni, array, elements, JNI_ABORT));
119 NSK_DISPLAY0(" ... released\n");
120
121 return NSK_TRUE;
122 }
123
124 /** Get global reference to object from a static field of given class. */
125 static jobject getObject(jvmtiEnv* jvmti, JNIEnv* jni, jclass cls,
126 const char fieldName[], const char fieldSig[]) {
127
128 jfieldID fieldID = NULL;
129 jobject obj = NULL;
130
131 NSK_DISPLAY1("Find static field: %s\n", fieldName);
132 if (!NSK_JNI_VERIFY(jni, (fieldID =
133 NSK_CPP_STUB4(GetStaticFieldID, jni, cls, fieldName, fieldSig)) != NULL)) {
134 nsk_jvmti_setFailStatus();
135 return NULL;
136 }
137 NSK_DISPLAY1(" ... got fieldID: 0x%p\n", (void*)fieldID);
138
139 NSK_DISPLAY1("Get object from static field: %s\n", fieldName);
140 if (!NSK_JNI_VERIFY(jni, (obj =
141 NSK_CPP_STUB3(GetStaticObjectField, jni, cls, fieldID)) != NULL)) {
142 nsk_jvmti_setFailStatus();
143 return NULL;
144 }
145 NSK_DISPLAY1(" ... got object: 0x%p\n", (void*)obj);
146
147 NSK_DISPLAY1("Make global reference to object: 0x%p\n", obj);
148 if (!NSK_JNI_VERIFY(jni, (obj =
149 NSK_CPP_STUB2(NewGlobalRef, jni, obj)) != NULL)) {
150 nsk_jvmti_setFailStatus();
151 return NULL;
152 }
153 NSK_DISPLAY1(" ... got global ref: 0x%p\n", (void*)obj);
154
155 return obj;
156 }
157
158 /** Redefine given class with new bytecode. */
159 static int redefineClass(jvmtiEnv* jvmti, jclass klass, const char className[],
160 jint size, unsigned char bytes[]) {
161 jvmtiClassDefinition classDef;
162
163 classDef.klass = klass;
164 classDef.class_byte_count = size;
165 classDef.class_bytes = bytes;
166
167 NSK_DISPLAY1("Redefine class: %s\n", className);
168 if (!NSK_JVMTI_VERIFY(
169 NSK_CPP_STUB3(RedefineClasses, jvmti, 1, &classDef))) {
170 nsk_jvmti_setFailStatus();
171 return NSK_FALSE;
172 }
173 NSK_DISPLAY1(" ... redefined with bytecode: %d bytes\n", (int)size);
174
175 return NSK_TRUE;
176 }
177
178 /* ============================================================================= */
179
180 /** Agent algorithm. */
181 static void JNICALL
182 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
183 NSK_DISPLAY0("Wait for debuggee to load original class\n");
184 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
185 return;
186
187 /* perform testing */
188 {
189 {
190 jclass debugeeClass = NULL;
191
192 NSK_DISPLAY0(">>> Obtain debuggee class\n");
193 NSK_DISPLAY1("Find debugee class: %s\n", DEBUGEE_CLASS_NAME);
194 if (!NSK_JNI_VERIFY(jni, (debugeeClass =
195 NSK_CPP_STUB2(FindClass, jni, DEBUGEE_CLASS_NAME)) != NULL)) {
196 nsk_jvmti_setFailStatus();
197 return;
198 }
199 NSK_DISPLAY1(" ... found class: 0x%p\n", (void*)debugeeClass);
200
201 NSK_DISPLAY0(">>> Obtain tested class object\n");
202 if (!NSK_VERIFY((testedClass = (jclass)
203 getObject(jvmti, jni, debugeeClass, TESTED_CLASS_FIELD_NAME,
204 TESTED_CLASS_FIELD_SIG)) != NULL))
205 return;
206
207 NSK_DISPLAY0(">>> Obtain redefined bytecode of tested class\n");
208 if (!NSK_VERIFY(getBytecode(jvmti, jni, debugeeClass,
209 REDEF_BYTECODE_FIELD_NAME,
210 BYTECODE_FIELD_SIG,
211 &redefClassSize, &redefClassBytes)))
212 return;
213
214 NSK_DISPLAY0(">>> Obtain new instrumented bytecode of tested class\n");
215 if (!NSK_VERIFY(getBytecode(jvmti, jni, debugeeClass,
241 NSK_DISPLAY1("Disable event: %s\n", "CLASS_FILE_LOAD_HOOK");
242 if (NSK_VERIFY(nsk_jvmti_enableEvents(JVMTI_DISABLE, 1, &event, NULL))) {
243 NSK_DISPLAY0(" ... event disabled\n");
244 }
245
246 NSK_DISPLAY1("Check if event was received: %s\n", "CLASS_FILE_LOAD_HOOK");
247 if (eventsCount != 1) {
248 NSK_COMPLAIN3("Unexpected number of %s events received for tested class:\n"
249 "# received: %d events\n"
250 "# expected: %d events\n",
251 "CLASS_FILE_LOAD_HOOK", eventsCount, 1);
252 nsk_jvmti_setFailStatus();
253 } else {
254 NSK_DISPLAY1(" ... received: %d events\n", eventsCount);
255 }
256 }
257
258 NSK_DISPLAY0(">>> Clean used data\n");
259 {
260 NSK_DISPLAY1("Delete global reference to tested class object: 0x%p\n", (void*)testedClass);
261 NSK_CPP_STUB2(DeleteGlobalRef, jni, testedClass);
262
263 NSK_DISPLAY1("Deallocate redefined bytecode array: 0x%p\n", (void*)redefClassBytes);
264 if (!NSK_JVMTI_VERIFY(
265 NSK_CPP_STUB2(Deallocate, jvmti, redefClassBytes))) {
266 nsk_jvmti_setFailStatus();
267 }
268 }
269 }
270
271 NSK_DISPLAY0("Let debugee to finish\n");
272 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
273 return;
274 }
275
276 /* ============================================================================= */
277
278 /** Callback for CLASS_FILE_LOAD_HOOK event **/
279 static void JNICALL
280 callbackClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv *jni,
281 jclass class_being_redefined,
282 jobject loader, const char* name, jobject protection_domain,
283 jint class_data_len, const unsigned char* class_data,
284 jint *new_class_data_len, unsigned char** new_class_data) {
285
342 jvmtiEnv* jvmti = NULL;
343
344 /* init framework and parse options */
345 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
346 return JNI_ERR;
347
348 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
349
350 /* create JVMTI environment */
351 if (!NSK_VERIFY((jvmti =
352 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
353 return JNI_ERR;
354
355 NSK_DISPLAY1("Add required capabilities: %s\n", "can_generate_eraly_class_hook_events, can_redefine_classes");
356 {
357 jvmtiCapabilities caps;
358
359 memset(&caps, 0, sizeof(caps));
360 caps.can_generate_all_class_hook_events = 1;
361 caps.can_redefine_classes = 1;
362 if (!NSK_JVMTI_VERIFY(
363 NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
364 return JNI_ERR;
365 }
366 }
367 NSK_DISPLAY0(" ... added\n");
368
369 NSK_DISPLAY1("Set callback for event: %s\n", "CLASS_FILE_LOAD_HOOK");
370 {
371 jvmtiEventCallbacks callbacks;
372 jint size = (jint)sizeof(callbacks);
373
374 memset(&callbacks, 0, sizeof(callbacks));
375 callbacks.ClassFileLoadHook = callbackClassFileLoadHook;
376 if (!NSK_JVMTI_VERIFY(
377 NSK_CPP_STUB3(SetEventCallbacks, jvmti, &callbacks, size))) {
378 return JNI_ERR;
379 }
380 }
381 NSK_DISPLAY0(" ... set\n");
382
383 /* register agent proc and arg */
384 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
385 return JNI_ERR;
386
387 return JNI_OK;
388 }
389
390 /* ============================================================================= */
391
392 }
|
53
54 static jint newClassSize = 0;
55 static unsigned char* newClassBytes = NULL;
56
57 static volatile int eventsCount = 0;
58
59 /* ============================================================================= */
60
61 /** Get classfile bytecode from a static field of given class. */
62 static int getBytecode(jvmtiEnv* jvmti, JNIEnv* jni, jclass cls,
63 const char fieldName[], const char fieldSig[],
64 jint* size, unsigned char* *bytes) {
65
66 jfieldID fieldID = NULL;
67 jbyteArray array = NULL;
68 jbyte* elements;
69 int i;
70
71 NSK_DISPLAY1("Find static field: %s\n", fieldName);
72 if (!NSK_JNI_VERIFY(jni, (fieldID =
73 jni->GetStaticFieldID(cls, fieldName, fieldSig)) != NULL)) {
74 nsk_jvmti_setFailStatus();
75 return NSK_FALSE;
76 }
77 NSK_DISPLAY1(" ... got fieldID: 0x%p\n", (void*)fieldID);
78
79 NSK_DISPLAY1("Get classfile bytes array from static field: %s\n", fieldName);
80 if (!NSK_JNI_VERIFY(jni, (array = (jbyteArray)
81 jni->GetStaticObjectField(cls, fieldID)) != NULL)) {
82 nsk_jvmti_setFailStatus();
83 return NSK_FALSE;
84 }
85 NSK_DISPLAY1(" ... got array object: 0x%p\n", (void*)array);
86
87 if (!NSK_JNI_VERIFY(jni, (*size = jni->GetArrayLength(array)) > 0)) {
88 nsk_jvmti_setFailStatus();
89 return NSK_FALSE;
90 }
91 NSK_DISPLAY1(" ... got array size: %d bytes\n", (int)*size);
92
93 {
94 jboolean isCopy;
95 if (!NSK_JNI_VERIFY(jni, (elements = jni->GetByteArrayElements(array, &isCopy)) != NULL)) {
96 nsk_jvmti_setFailStatus();
97 return NSK_FALSE;
98 }
99 }
100 NSK_DISPLAY1(" ... got elements list: 0x%p\n", (void*)elements);
101
102 if (!NSK_JVMTI_VERIFY(jvmti->Allocate(*size, bytes))) {
103 nsk_jvmti_setFailStatus();
104 return NSK_FALSE;
105 }
106 NSK_DISPLAY1(" ... created bytes array: 0x%p\n", (void*)*bytes);
107
108 for (i = 0; i < *size; i++) {
109 (*bytes)[i] = (unsigned char)elements[i];
110 }
111 NSK_DISPLAY1(" ... copied bytecode: %d bytes\n", (int)*size);
112
113 NSK_DISPLAY1("Release elements list: 0x%p\n", (void*)elements);
114 NSK_TRACE(jni->ReleaseByteArrayElements(array, elements, JNI_ABORT));
115 NSK_DISPLAY0(" ... released\n");
116
117 return NSK_TRUE;
118 }
119
120 /** Get global reference to object from a static field of given class. */
121 static jobject getObject(jvmtiEnv* jvmti, JNIEnv* jni, jclass cls,
122 const char fieldName[], const char fieldSig[]) {
123
124 jfieldID fieldID = NULL;
125 jobject obj = NULL;
126
127 NSK_DISPLAY1("Find static field: %s\n", fieldName);
128 if (!NSK_JNI_VERIFY(jni, (fieldID =
129 jni->GetStaticFieldID(cls, fieldName, fieldSig)) != NULL)) {
130 nsk_jvmti_setFailStatus();
131 return NULL;
132 }
133 NSK_DISPLAY1(" ... got fieldID: 0x%p\n", (void*)fieldID);
134
135 NSK_DISPLAY1("Get object from static field: %s\n", fieldName);
136 if (!NSK_JNI_VERIFY(jni, (obj = jni->GetStaticObjectField(cls, fieldID)) != NULL)) {
137 nsk_jvmti_setFailStatus();
138 return NULL;
139 }
140 NSK_DISPLAY1(" ... got object: 0x%p\n", (void*)obj);
141
142 NSK_DISPLAY1("Make global reference to object: 0x%p\n", obj);
143 if (!NSK_JNI_VERIFY(jni, (obj = jni->NewGlobalRef(obj)) != NULL)) {
144 nsk_jvmti_setFailStatus();
145 return NULL;
146 }
147 NSK_DISPLAY1(" ... got global ref: 0x%p\n", (void*)obj);
148
149 return obj;
150 }
151
152 /** Redefine given class with new bytecode. */
153 static int redefineClass(jvmtiEnv* jvmti, jclass klass, const char className[],
154 jint size, unsigned char bytes[]) {
155 jvmtiClassDefinition classDef;
156
157 classDef.klass = klass;
158 classDef.class_byte_count = size;
159 classDef.class_bytes = bytes;
160
161 NSK_DISPLAY1("Redefine class: %s\n", className);
162 if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(1, &classDef))) {
163 nsk_jvmti_setFailStatus();
164 return NSK_FALSE;
165 }
166 NSK_DISPLAY1(" ... redefined with bytecode: %d bytes\n", (int)size);
167
168 return NSK_TRUE;
169 }
170
171 /* ============================================================================= */
172
173 /** Agent algorithm. */
174 static void JNICALL
175 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
176 NSK_DISPLAY0("Wait for debuggee to load original class\n");
177 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
178 return;
179
180 /* perform testing */
181 {
182 {
183 jclass debugeeClass = NULL;
184
185 NSK_DISPLAY0(">>> Obtain debuggee class\n");
186 NSK_DISPLAY1("Find debugee class: %s\n", DEBUGEE_CLASS_NAME);
187 if (!NSK_JNI_VERIFY(jni, (debugeeClass =
188 jni->FindClass(DEBUGEE_CLASS_NAME)) != NULL)) {
189 nsk_jvmti_setFailStatus();
190 return;
191 }
192 NSK_DISPLAY1(" ... found class: 0x%p\n", (void*)debugeeClass);
193
194 NSK_DISPLAY0(">>> Obtain tested class object\n");
195 if (!NSK_VERIFY((testedClass = (jclass)
196 getObject(jvmti, jni, debugeeClass, TESTED_CLASS_FIELD_NAME,
197 TESTED_CLASS_FIELD_SIG)) != NULL))
198 return;
199
200 NSK_DISPLAY0(">>> Obtain redefined bytecode of tested class\n");
201 if (!NSK_VERIFY(getBytecode(jvmti, jni, debugeeClass,
202 REDEF_BYTECODE_FIELD_NAME,
203 BYTECODE_FIELD_SIG,
204 &redefClassSize, &redefClassBytes)))
205 return;
206
207 NSK_DISPLAY0(">>> Obtain new instrumented bytecode of tested class\n");
208 if (!NSK_VERIFY(getBytecode(jvmti, jni, debugeeClass,
234 NSK_DISPLAY1("Disable event: %s\n", "CLASS_FILE_LOAD_HOOK");
235 if (NSK_VERIFY(nsk_jvmti_enableEvents(JVMTI_DISABLE, 1, &event, NULL))) {
236 NSK_DISPLAY0(" ... event disabled\n");
237 }
238
239 NSK_DISPLAY1("Check if event was received: %s\n", "CLASS_FILE_LOAD_HOOK");
240 if (eventsCount != 1) {
241 NSK_COMPLAIN3("Unexpected number of %s events received for tested class:\n"
242 "# received: %d events\n"
243 "# expected: %d events\n",
244 "CLASS_FILE_LOAD_HOOK", eventsCount, 1);
245 nsk_jvmti_setFailStatus();
246 } else {
247 NSK_DISPLAY1(" ... received: %d events\n", eventsCount);
248 }
249 }
250
251 NSK_DISPLAY0(">>> Clean used data\n");
252 {
253 NSK_DISPLAY1("Delete global reference to tested class object: 0x%p\n", (void*)testedClass);
254 jni->DeleteGlobalRef(testedClass);
255
256 NSK_DISPLAY1("Deallocate redefined bytecode array: 0x%p\n", (void*)redefClassBytes);
257 if (!NSK_JVMTI_VERIFY(jvmti->Deallocate(redefClassBytes))) {
258 nsk_jvmti_setFailStatus();
259 }
260 }
261 }
262
263 NSK_DISPLAY0("Let debugee to finish\n");
264 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
265 return;
266 }
267
268 /* ============================================================================= */
269
270 /** Callback for CLASS_FILE_LOAD_HOOK event **/
271 static void JNICALL
272 callbackClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv *jni,
273 jclass class_being_redefined,
274 jobject loader, const char* name, jobject protection_domain,
275 jint class_data_len, const unsigned char* class_data,
276 jint *new_class_data_len, unsigned char** new_class_data) {
277
334 jvmtiEnv* jvmti = NULL;
335
336 /* init framework and parse options */
337 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
338 return JNI_ERR;
339
340 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
341
342 /* create JVMTI environment */
343 if (!NSK_VERIFY((jvmti =
344 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
345 return JNI_ERR;
346
347 NSK_DISPLAY1("Add required capabilities: %s\n", "can_generate_eraly_class_hook_events, can_redefine_classes");
348 {
349 jvmtiCapabilities caps;
350
351 memset(&caps, 0, sizeof(caps));
352 caps.can_generate_all_class_hook_events = 1;
353 caps.can_redefine_classes = 1;
354 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
355 return JNI_ERR;
356 }
357 }
358 NSK_DISPLAY0(" ... added\n");
359
360 NSK_DISPLAY1("Set callback for event: %s\n", "CLASS_FILE_LOAD_HOOK");
361 {
362 jvmtiEventCallbacks callbacks;
363 jint size = (jint)sizeof(callbacks);
364
365 memset(&callbacks, 0, sizeof(callbacks));
366 callbacks.ClassFileLoadHook = callbackClassFileLoadHook;
367 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, size))) {
368 return JNI_ERR;
369 }
370 }
371 NSK_DISPLAY0(" ... set\n");
372
373 /* register agent proc and arg */
374 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
375 return JNI_ERR;
376
377 return JNI_OK;
378 }
379
380 /* ============================================================================= */
381
382 }
|