1 /*
2 * Copyright (c) 2004, 2021, 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 <stdlib.h>
25 #include <string.h>
26 #include <atomic>
27 #include "jni_tools.h"
28 #include "jvmti_tools.h"
29 #include "Injector.h"
30 #include "agent_common.h"
31
32 #define PASSED 0
33
34 extern "C" {
35
36 /* ========================================================================== */
37
38 #define DEFAULT_MAX_NUMBER_OF_CLASSES 100
39 #define DEFAULT_NUMBER_OF_SAMPLES 10
40 #define DEFAULT_SAMPLING_INTERVAL 100
41 #define DEFAULT_PACKAGE_NAME "nsk/jvmti/scenarios/hotswap"
42 #define PROFILE_CLASS_NAME "nsk/share/jvmti/ProfileCollector"
43
44 enum {
45 VM_MODE_COMPILED = 0,
46 VM_MODE_INTERPRETED = 1,
47 VM_MODE_MIXED = 2
48 };
49
50 /* scaffold objects */
51 static jlong timeout = 0;
52
53 /* test options */
54 static int number_of_samples;
55 static jlong sampling_interval;
56 static const char* package_name;
57 static size_t package_name_length;
58 static int vm_mode = VM_MODE_COMPILED;
59 static int bci_mode = BCI_MODE_EMCP;
60 static int sync_freq = 0;
61
62 static jclass profile_klass = NULL;
63 static jfieldID count_field = NULL;
64
65 /* test objects */
66 static int max_classes;
67 static char** names = NULL;
68 static jvmtiClassDefinition* old_class_def = NULL;
69 static jvmtiClassDefinition* new_class_def = NULL;
70 static int classCount = 0;
71 /* lock to access classCount */
72 static jrawMonitorID classLoadLock = NULL;
73 static int newFlag = NSK_FALSE;
74
75 /* ========================================================================== */
76
redefine(jvmtiEnv * jvmti,jvmtiClassDefinition * class_def)77 static int redefine(jvmtiEnv* jvmti, jvmtiClassDefinition* class_def) {
78
79 if (!NSK_VERIFY(classCount != 0))
80 return NSK_FALSE;
81
82 NSK_DISPLAY1("Redefining %d classes...\n", classCount);
83
84 if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(classCount, class_def)))
85 return NSK_FALSE;
86
87 return NSK_TRUE;
88 }
89
90 /* ========================================================================== */
91
92 /** callback functions **/
93
94 static void JNICALL
ClassFileLoadHook(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jclass class_being_redefined,jobject loader,const char * name,jobject protection_domain,jint class_data_len,const unsigned char * class_data,jint * new_class_data_len,unsigned char ** new_class_data)95 ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
96 jclass class_being_redefined, jobject loader,
97 const char* name, jobject protection_domain,
98 jint class_data_len, const unsigned char* class_data,
99 jint *new_class_data_len, unsigned char** new_class_data) {
100 jint name_len;
101
102 if (name != NULL &&
103 class_being_redefined == NULL &&
104 (strcmp(name, PROFILE_CLASS_NAME) != 0) &&
105 (strncmp(name, package_name, package_name_length) == 0)) {
106 if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(classLoadLock))) {
107 nsk_jvmti_setFailStatus();
108 return;
109 }
110 // use while instead of if to exit the block on error
111 while (classCount < max_classes) {
112 NSK_DISPLAY1("ClassFileLoadHook: %s\n", name);
113 name_len = (jint)strlen(name) + 1;
114 if (!NSK_JVMTI_VERIFY(jvmti_env->Allocate(name_len, (unsigned char**)& names[classCount]))) {
115 nsk_jvmti_setFailStatus();
116 break;
117 }
118 memcpy(names[classCount], name, name_len);
119 if (!NSK_JVMTI_VERIFY(jvmti_env->Allocate(class_data_len, (unsigned char**)
120 & old_class_def[classCount].class_bytes))) {
121 nsk_jvmti_setFailStatus();
122 break;
123 }
124 memcpy((unsigned char*)old_class_def[classCount].class_bytes,
125 class_data, class_data_len);
126 old_class_def[classCount].class_byte_count = class_data_len;
127 classCount++;
128 break;
129 }
130 if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(classLoadLock))) {
131 nsk_jvmti_setFailStatus();
132 }
133 }
134 }
135
136 static std::atomic<int> CompiledMethodLoadEventsCount(0);
137
138 static void JNICALL
CompiledMethodLoad(jvmtiEnv * jvmti_env,jmethodID method,jint code_size,const void * code_addr,jint map_length,const jvmtiAddrLocationMap * map,const void * compile_info)139 CompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method,
140 jint code_size, const void* code_addr, jint map_length,
141 const jvmtiAddrLocationMap* map, const void* compile_info) {
142 char *name = NULL;
143 char *signature = NULL;
144
145 CompiledMethodLoadEventsCount++;
146
147 if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(method, &name, &signature, NULL))) {
148 nsk_jvmti_setFailStatus();
149 return;
150 }
151 NSK_DISPLAY3("CompiledMethodLoad event: %s%s (0x%p)\n",
152 name, signature, code_addr);
153 if (name != NULL)
154 jvmti_env->Deallocate((unsigned char*)name);
155 if (signature != NULL)
156 jvmti_env->Deallocate((unsigned char*)signature);
157 }
158
159 static std::atomic<int> SingleStepEventsCount(0);
160
161 static void JNICALL
SingleStep(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location)162 SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
163 jmethodID method, jlocation location) {
164
165 SingleStepEventsCount++;
166 }
167
168 static std::atomic<int> ExceptionEventsCount(0);
169
170 static void JNICALL
Exception(jvmtiEnv * jvmti_env,JNIEnv * jni_env,jthread thread,jmethodID method,jlocation location,jobject exception,jmethodID catch_method,jlocation catch_location)171 Exception(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread,
172 jmethodID method, jlocation location, jobject exception,
173 jmethodID catch_method, jlocation catch_location) {
174
175 if (sync_freq && ((ExceptionEventsCount % sync_freq) == 0)) {
176
177 if (nsk_getVerboseMode()) {
178 jclass klass = NULL;
179 char *signature = NULL;
180
181 if (!NSK_JNI_VERIFY(jni_env, (klass = jni_env->GetObjectClass(exception)) != NULL)) {
182 nsk_jvmti_setFailStatus();
183 return;
184 }
185 if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &signature, NULL))) {
186 nsk_jvmti_setFailStatus();
187 return;
188 }
189 NSK_DISPLAY2("Exception event %d: %s\n",
190 ExceptionEventsCount.load(), signature);
191 if (signature != NULL)
192 jvmti_env->Deallocate((unsigned char*)signature);
193 }
194
195 if (!redefine(jvmti_env, (bci_mode != BCI_MODE_EMCP && newFlag) ?
196 new_class_def : old_class_def))
197 nsk_jvmti_setFailStatus();
198
199 NSK_DISPLAY1("SingleStepEventsCount: %d\n", SingleStepEventsCount.load());
200 if (vm_mode == VM_MODE_MIXED) {
201 if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(
202 ((newFlag) ? JVMTI_DISABLE : JVMTI_ENABLE),
203 JVMTI_EVENT_SINGLE_STEP, NULL)))
204 nsk_jvmti_setFailStatus();
205 }
206
207 if (nsk_getVerboseMode() && bci_mode != BCI_MODE_EMCP) {
208 jint profileCount = jni_env->GetStaticIntField(profile_klass, count_field);
209 NSK_DISPLAY1("profileCount: %d\n", profileCount);
210 }
211
212 newFlag = (newFlag) ? NSK_FALSE : NSK_TRUE;
213 }
214
215 ExceptionEventsCount++;
216 }
217
218 /* ========================================================================== */
219
220 static jrawMonitorID waitLock = NULL;
221
prepare(jvmtiEnv * jvmti,JNIEnv * jni)222 static int prepare(jvmtiEnv* jvmti, JNIEnv* jni) {
223 int i;
224
225 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
226 return NSK_FALSE;
227
228 if (vm_mode != VM_MODE_COMPILED) {
229 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, NULL)))
230 return NSK_FALSE;
231 }
232
233 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("waitLock", &waitLock)))
234 return NSK_FALSE;
235
236 for (i = 0; i < classCount; i++) {
237 NSK_DISPLAY1("Find class: %s\n", names[i]);
238 if (!NSK_JNI_VERIFY(jni, (old_class_def[i].klass = jni->FindClass(names[i])) != NULL))
239 return NSK_FALSE;
240
241 if (!NSK_JNI_VERIFY(jni, (old_class_def[i].klass = (jclass)
242 jni->NewGlobalRef(old_class_def[i].klass)) != NULL))
243 return NSK_FALSE;
244 }
245
246 if (bci_mode != BCI_MODE_EMCP) {
247 NSK_DISPLAY1("Find class: %s\n", PROFILE_CLASS_NAME);
248 if (!NSK_JNI_VERIFY(jni, (profile_klass = jni->FindClass(PROFILE_CLASS_NAME)) != NULL))
249 return NSK_FALSE;
250
251 if (!NSK_JNI_VERIFY(jni, (profile_klass = (jclass)
252 jni->NewGlobalRef(profile_klass)) != NULL))
253 return NSK_FALSE;
254
255 if (!NSK_JNI_VERIFY(jni, (count_field =
256 jni->GetStaticFieldID(profile_klass,
257 (bci_mode == BCI_MODE_CALL) ? "callCount" : "allocCount",
258 "I")) != NULL))
259 return NSK_FALSE;
260
261 if (!NSK_JVMTI_VERIFY(jvmti->Allocate(classCount * sizeof(jvmtiClassDefinition),
262 (unsigned char**) &new_class_def)))
263 return NSK_FALSE;
264
265 for (i = 0; i < classCount; i++) {
266 new_class_def[i].klass = old_class_def[i].klass;
267 if (!Inject(old_class_def[i].class_bytes,
268 old_class_def[i].class_byte_count,
269 (unsigned char**) &new_class_def[i].class_bytes,
270 &new_class_def[i].class_byte_count, bci_mode))
271 return NSK_FALSE;
272 }
273 }
274
275 if (sync_freq) {
276 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL)))
277 return NSK_FALSE;
278 }
279
280 return NSK_TRUE;
281 }
282
283 /* ========================================================================== */
284
wait_for(jvmtiEnv * jvmti,jlong millis)285 static int wait_for(jvmtiEnv* jvmti, jlong millis) {
286
287 if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(waitLock)))
288 return NSK_FALSE;
289
290 if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorWait(waitLock, millis)))
291 nsk_jvmti_setFailStatus();
292
293 if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(waitLock)))
294 return NSK_FALSE;
295
296 return NSK_TRUE;
297 }
298
299 /* ========================================================================== */
300
301 /** Agent algorithm. */
302 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * jni,void * arg)303 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
304 int i;
305
306 if (!nsk_jvmti_waitForSync(timeout))
307 return;
308
309 if (!prepare(jvmti, jni)) {
310 nsk_jvmti_setFailStatus();
311 return;
312 }
313
314 /* resume debugee and wait for sync */
315 if (!nsk_jvmti_resumeSync())
316 return;
317 if (!nsk_jvmti_waitForSync(timeout))
318 return;
319
320 if (sync_freq) {
321 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_EXCEPTION, NULL)))
322 nsk_jvmti_setFailStatus();
323 } else {
324
325 for (i = 0; i < number_of_samples && !nsk_jvmti_isFailStatus(); i++) {
326 wait_for(jvmti, sampling_interval);
327
328 if (!redefine(jvmti, (bci_mode != BCI_MODE_EMCP && newFlag) ?
329 new_class_def : old_class_def))
330 nsk_jvmti_setFailStatus();
331
332 NSK_DISPLAY1("SingleStepEventsCount: %d\n", SingleStepEventsCount.load());
333 if (vm_mode == VM_MODE_MIXED) {
334 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(
335 (((i % 2) == 0) ? JVMTI_DISABLE : JVMTI_ENABLE),
336 JVMTI_EVENT_SINGLE_STEP, NULL)))
337 nsk_jvmti_setFailStatus();
338 }
339
340 if (nsk_getVerboseMode() && bci_mode != BCI_MODE_EMCP) {
341 jint profileCount = jni->GetStaticIntField(profile_klass, count_field);
342 NSK_DISPLAY1("profileCount: %d\n", profileCount);
343 }
344
345 newFlag = (newFlag) ? NSK_FALSE : NSK_TRUE;
346 }
347
348 }
349
350 if (vm_mode != VM_MODE_COMPILED) {
351 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_SINGLE_STEP, NULL)))
352 nsk_jvmti_setFailStatus();
353 }
354
355 if (!nsk_jvmti_resumeSync())
356 return;
357 }
358
359 /* ========================================================================== */
360
361 /** Agent library initialization. */
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)362 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
363 jvmtiEnv* jvmti = NULL;
364 jvmtiCapabilities caps;
365 jvmtiEventCallbacks callbacks;
366 const char* optValue;
367
368 NSK_DISPLAY0("Agent_OnLoad\n");
369
370 /* init framework and parse options */
371 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
372 return JNI_ERR;
373
374 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
375
376 /* get options */
377 number_of_samples = nsk_jvmti_findOptionIntValue("samples",
378 DEFAULT_NUMBER_OF_SAMPLES);
379 if (!NSK_VERIFY(number_of_samples > 0))
380 return JNI_ERR;
381 NSK_DISPLAY1("samples: %d\n", number_of_samples);
382
383 sampling_interval = nsk_jvmti_findOptionIntValue("interval",
384 DEFAULT_SAMPLING_INTERVAL);
385 if (!NSK_VERIFY(sampling_interval > 0))
386 return JNI_ERR;
387 NSK_DISPLAY1("interval: %d\n", sampling_interval);
388
389 package_name = nsk_jvmti_findOptionStringValue("package",
390 DEFAULT_PACKAGE_NAME);
391 if (!NSK_VERIFY(package_name != NULL))
392 return JNI_ERR;
393 NSK_DISPLAY1("package: %s\n", package_name);
394
395 package_name_length = strlen(package_name);
396 if (!NSK_VERIFY(package_name_length > 0))
397 return JNI_ERR;
398
399 max_classes = nsk_jvmti_findOptionIntValue("classes",
400 DEFAULT_MAX_NUMBER_OF_CLASSES);
401 if (!NSK_VERIFY(max_classes > 0))
402 return JNI_ERR;
403 NSK_DISPLAY1("classes: %d\n", max_classes);
404
405 optValue = nsk_jvmti_findOptionValue("mode");
406 if (optValue != NULL) {
407 if (strcmp(optValue, "compiled") == 0)
408 vm_mode = VM_MODE_COMPILED;
409 else if (strcmp(optValue, "interpreted") == 0)
410 vm_mode = VM_MODE_INTERPRETED;
411 else if (strcmp(optValue, "mixed") == 0)
412 vm_mode = VM_MODE_MIXED;
413 else {
414 NSK_COMPLAIN1("Unknown option value: mode=%s\n", optValue);
415 return JNI_ERR;
416 }
417 }
418
419 optValue = nsk_jvmti_findOptionValue("bci");
420 if (optValue != NULL) {
421 if (strcmp(optValue, "emcp") == 0)
422 bci_mode = BCI_MODE_EMCP;
423 else if (strcmp(optValue, "call") == 0)
424 bci_mode = BCI_MODE_CALL;
425 else if (strcmp(optValue, "alloc") == 0)
426 bci_mode = BCI_MODE_ALLOC;
427 else {
428 NSK_COMPLAIN1("Unknown option value: bci=%s\n", optValue);
429 return JNI_ERR;
430 }
431 }
432
433 sync_freq = nsk_jvmti_findOptionIntValue("sync", 0);
434 if (!NSK_VERIFY(sync_freq >= 0))
435 return JNI_ERR;
436 NSK_DISPLAY1("sync: %d\n", sync_freq);
437
438 /* create JVMTI environment */
439 if (!NSK_VERIFY((jvmti =
440 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
441 return JNI_ERR;
442
443 /* allocate tables for classes */
444 if (!NSK_JVMTI_VERIFY(jvmti->Allocate(max_classes * sizeof(char*), (unsigned char**) &names)))
445 return JNI_ERR;
446
447 if (!NSK_JVMTI_VERIFY(jvmti->Allocate(max_classes * sizeof(jvmtiClassDefinition),
448 (unsigned char**) &old_class_def)))
449 return JNI_ERR;
450
451 if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("classLoadLock", &classLoadLock)))
452 return JNI_ERR;
453
454 /* add capabilities */
455 memset(&caps, 0, sizeof(caps));
456 caps.can_redefine_classes = 1;
457 caps.can_generate_compiled_method_load_events = 1;
458 if (vm_mode != VM_MODE_COMPILED) {
459 caps.can_generate_single_step_events = 1;
460 }
461 if (sync_freq) {
462 caps.can_generate_exception_events = 1;
463 }
464 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
465 return JNI_ERR;
466
467 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
468 return JNI_ERR;
469
470 /* set event callbacks */
471 memset(&callbacks, 0, sizeof(callbacks));
472 callbacks.ClassFileLoadHook = &ClassFileLoadHook;
473 callbacks.CompiledMethodLoad = &CompiledMethodLoad;
474 if (vm_mode != VM_MODE_COMPILED) {
475 callbacks.SingleStep = &SingleStep;
476 }
477 if (sync_freq) {
478 callbacks.Exception = &Exception;
479 }
480 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
481 return JNI_ERR;
482
483 /* enable events */
484 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
485 return JNI_ERR;
486 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL)))
487 return JNI_ERR;
488
489 return JNI_OK;
490 }
491
492 /* ========================================================================== */
493
494 }
495