-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvmEntry.cpp
575 lines (506 loc) · 18.6 KB
/
vmEntry.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
* Copyright 2016 Andrei Pangin
* Copyright 2021, 2024 Datadog, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common.h"
#include "vmEntry.h"
#include "arguments.h"
#include "context.h"
#include "j9Ext.h"
#include "jniHelper.h"
#include "libraries.h"
#include "log.h"
#include "os.h"
#include "profiler.h"
#include "safeAccess.h"
#include "vmStructs.h"
#include <dlfcn.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
// JVM TI agent return codes
const int ARGUMENTS_ERROR = 100;
const int COMMAND_ERROR = 200;
static Arguments _agent_args(true);
JavaVM *VM::_vm;
jvmtiEnv *VM::_jvmti = NULL;
int VM::_java_version = 0;
int VM::_java_update_version = 0;
int VM::_hotspot_version = 0;
bool VM::_openj9 = false;
bool VM::_hotspot = false;
bool VM::_zing = false;
bool VM::_can_sample_objects = false;
bool VM::_can_intercept_binding = false;
bool VM::_is_adaptive_gc_boundary_flag_set = false;
jvmtiError(JNICALL *VM::_orig_RedefineClasses)(jvmtiEnv *, jint,
const jvmtiClassDefinition *);
jvmtiError(JNICALL *VM::_orig_RetransformClasses)(jvmtiEnv *, jint,
const jclass *classes);
void *VM::_libjvm;
void *VM::_libjava;
AsyncGetCallTrace VM::_asyncGetCallTrace;
JVM_GetManagement VM::_getManagement;
static void wakeupHandler(int signo) {
// Dummy handler for interrupting syscalls
}
static bool isZeroInterpreterMethod(const char *blob_name) {
return strncmp(blob_name, "_ZN15ZeroInterpreter", 20) == 0 ||
strncmp(blob_name, "_ZN19BytecodeInterpreter3run", 28) == 0;
}
static bool isOpenJ9InterpreterMethod(const char *blob_name) {
return strncmp(blob_name, "_ZN32VM_BytecodeInterpreter", 27) == 0 ||
strncmp(blob_name, "_ZN26VM_BytecodeInterpreter", 27) == 0 ||
strncmp(blob_name, "bytecodeLoop", 12) == 0 ||
strcmp(blob_name, "cInterpreter") == 0;
}
static bool isOpenJ9JitStub(const char *blob_name) {
if (strncmp(blob_name, "jit", 3) == 0) {
blob_name += 3;
return strcmp(blob_name, "NewObject") == 0 ||
strcmp(blob_name, "NewArray") == 0 ||
strcmp(blob_name, "ANewArray") == 0;
}
return false;
}
static void *resolveMethodId(void **mid) {
return mid == NULL || *mid < (void *)4096 ? NULL : *mid;
}
static void resolveMethodIdEnd() {}
JavaFullVersion JavaVersionAccess::get_java_version(char* prop_value) {
JavaFullVersion version = {8, 362}; // initial value is 8u362; an arbitrary java version
TEST_LOG("version property: %s", prop_value);
if (strncmp(prop_value, "1.8.0_", 6) == 0) {
version.major = 8;
version.update = atoi(prop_value + 6);
} else if (strncmp(prop_value, "8.0.", 4) == 0) {
version.major = 8;
version.update = atoi(prop_value + 4);
} else if (strncmp(prop_value, "25.", 3) == 0 && prop_value[3] != '0') {
// Java 8 encoded in java.vm.version system property looks like 25.352-b08
// The upcoming Java 25 version will have a form of '25.0.<update>' instead
version.major = 8;
version.update = atoi(prop_value + 3);
} else if (strncmp(prop_value, "JRE 1.8.0", 9) == 0) {
// IBM JDK 8 does not report the 'real' version in any property accessible
// from JVMTI The only piece of info we can use has the following format
// `JRE 1.8.0 <some text> 20230313_47323 <some more text>`
// Considering that JDK 8.0.361 is the only release in 2023 we can use
// that part of the version string to pretend anything after year 2023
// inclusive is 8.0.361. Not perfect, but this is the only thing we have.
version.major = 8;
char *idx = strstr(prop_value, " 202");
if (idx != NULL) {
int year = atol(idx + 1);
if (year >= 2023) {
version.update = 361;
} else {
version.update = 351;
}
}
} else {
version.major = atoi(prop_value);
if (version.major < 9) {
version.major = 9;
}
// format is 11.0.17+8
// this shortcut for parsing the update version should hold till Java 99
version.update = atoi(prop_value + 5);
}
return version;
}
int JavaVersionAccess::get_hotspot_version(char* prop_value) {
int hs_version = 0;
if (strncmp(prop_value, "25.", 3) == 0 && prop_value[3] > '0') {
hs_version = 8;
} else if (strncmp(prop_value, "24.", 3) == 0 && prop_value[3] > '0') {
hs_version = 7;
} else if (strncmp(prop_value, "20.", 3) == 0 && prop_value[3] > '0') {
hs_version = 6;
} else if ((hs_version = atoi(prop_value)) < 9) {
hs_version = 9;
}
return hs_version;
}
CodeCache* VM::openJvmLibrary() {
if ((void*)_asyncGetCallTrace == nullptr) {
return nullptr;
}
Libraries* libraries = Libraries::instance();
CodeCache *lib =
isOpenJ9()
? libraries->findJvmLibrary("libj9vm")
: libraries->findLibraryByAddress((const void *)_asyncGetCallTrace);
return lib;
}
bool VM::initShared(JavaVM* vm) {
if (_jvmti != NULL)
return true;
_vm = vm;
if (_vm->GetEnv((void **)&_jvmti, JVMTI_VERSION_1_0) != 0) {
return false;
}
#ifdef __APPLE__
Dl_info dl_info;
if (dladdr((const void *)wakeupHandler, &dl_info) &&
dl_info.dli_fname != NULL) {
// Make sure java-profiler DSO cannot be unloaded, since it contains JVM
// callbacks. On Linux, we use 'nodelete' linker option.
dlopen(dl_info.dli_fname, RTLD_LAZY | RTLD_NODELETE);
}
#endif
bool is_zero_vm = false;
char *prop;
if (_jvmti->GetSystemProperty("java.vm.name", &prop) == 0) {
_hotspot = strstr(prop, "OpenJDK") != NULL ||
strstr(prop, "HotSpot") != NULL ||
strstr(prop, "GraalVM") != NULL ||
strstr(prop, "Dynamic Code Evolution") != NULL;
is_zero_vm = strstr(prop, "Zero") != NULL;
_zing = !_hotspot && strstr(prop, "Zing") != NULL;
_openj9 = !_hotspot && strstr(prop, "OpenJ9") != NULL;
_jvmti->Deallocate((unsigned char *)prop);
prop = NULL;
}
_libjvm = getLibraryHandle("libjvm.so");
_asyncGetCallTrace = (AsyncGetCallTrace)dlsym(_libjvm, "AsyncGetCallTrace");
_getManagement = (JVM_GetManagement)dlsym(_libjvm, "JVM_GetManagement");
Libraries *libraries = Libraries::instance();
libraries->updateSymbols(false);
_openj9 = !_hotspot && J9Ext::initialize(
_jvmti, libraries->resolveSymbol("j9thread_self*"));
if (_openj9) {
if (_jvmti->GetSystemProperty("jdk.extensions.version", &prop) == 0) {
// OpenJ9 Semeru will report the version here
// insert debug output here
} else {
if (prop != NULL) {
_jvmti->Deallocate((unsigned char *)prop);
prop = NULL;
}
if (_jvmti->GetSystemProperty("java.fullversion", &prop) == 0) {
// IBM JDK 8 will report something here
// The reported string contains JRE 1.8.0 and then later year and
// (possibly) hash code Although not very precise, this is best we can
// get :/ insert debug output here
} else {
if (prop != NULL) {
_jvmti->Deallocate((unsigned char *)prop);
prop = NULL;
}
}
}
}
if (prop == NULL) {
if (_jvmti->GetSystemProperty("java.runtime.version", &prop) == 0) {
// insert debug output here
} else {
if (prop != NULL) {
_jvmti->Deallocate((unsigned char *)prop);
prop = NULL;
}
}
}
TEST_LOG("java.runtime.version: %s", prop);
if (prop != NULL) {
JavaFullVersion version = JavaVersionAccess::get_java_version(prop);
_java_version = version.major;
_java_update_version = version.update;
_jvmti->Deallocate((unsigned char *)prop);
prop = NULL;
}
if (_jvmti->GetSystemProperty("java.vm.version", &prop) == 0) {
TEST_LOG("java.vm.version: %s", prop);
if (_java_version == 0) {
JavaFullVersion version = JavaVersionAccess::get_java_version(prop);
_java_version = version.major;
_java_update_version = version.update;
}
_hotspot_version = JavaVersionAccess::get_hotspot_version(prop);
_jvmti->Deallocate((unsigned char *)prop);
prop = NULL;
}
if (prop != NULL) {
_jvmti->Deallocate((unsigned char *)prop);
}
if (_java_version == 0 && _hotspot_version > 0) {
// sanity fallback:
// - if we failed to resolve the _java_version but have _hotspot_version, let's use the hotspot version as java version
_java_version = _hotspot_version;
}
TEST_LOG("jvm_version#%d.%d.%d", _java_version, 0, _java_update_version);
CodeCache *lib = openJvmLibrary();
if (lib == nullptr) {
return false;
}
VMStructs::init(lib);
if (is_zero_vm) {
lib->mark(isZeroInterpreterMethod);
} else if (isOpenJ9()) {
lib->mark(isOpenJ9InterpreterMethod);
CodeCache *libjit = libraries->findJvmLibrary("libj9jit");
if (libjit != NULL) {
libjit->mark(isOpenJ9JitStub);
}
}
return true;
}
bool VM::initLibrary(JavaVM *vm) {
TEST_LOG("VM::initLibrary");
if (!initShared(vm)) {
return false;
}
ready(jvmti(), jni());
return true;
}
bool VM::initProfilerBridge(JavaVM *vm, bool attach) {
TEST_LOG("VM::initProfilerBridge");
if (!initShared(vm)) {
return false;
}
CodeCache *lib = openJvmLibrary();
if (lib == nullptr) {
return false;
}
if (!attach && hotspot_version() == 8 && OS::isLinux()) {
// Workaround for JDK-8185348
char *func = (char *)lib->findSymbol(
"_ZN6Method26checked_resolve_jmethod_idEP10_jmethodID");
if (func != NULL) {
applyPatch(func, (const char *)resolveMethodId,
(const char *)resolveMethodIdEnd);
}
}
jvmtiCapabilities potential_capabilities = {0};
_jvmti->GetPotentialCapabilities(&potential_capabilities);
_can_sample_objects =
potential_capabilities.can_generate_sampled_object_alloc_events &&
(!_hotspot || hotspot_version() >= 11);
_can_intercept_binding =
potential_capabilities.can_generate_native_method_bind_events &&
HeapUsage::needsNativeBindingInterception();
jvmtiCapabilities capabilities = {0};
capabilities.can_generate_all_class_hook_events = 1;
capabilities.can_retransform_classes = 1;
capabilities.can_retransform_any_class = isOpenJ9() ? 0 : 1;
// capabilities.can_generate_vm_object_alloc_events = isOpenJ9() ? 1 : 0;
capabilities.can_generate_sampled_object_alloc_events =
_can_sample_objects ? 1 : 0;
capabilities.can_generate_native_method_bind_events =
_can_intercept_binding ? 1 : 0;
capabilities.can_generate_garbage_collection_events = 1;
capabilities.can_get_bytecodes = 1;
capabilities.can_get_constant_pool = 1;
capabilities.can_get_source_file_name = 1;
capabilities.can_get_line_numbers = 1;
capabilities.can_generate_compiled_method_load_events = 1;
capabilities.can_generate_monitor_events = 1;
capabilities.can_tag_objects = 1;
_jvmti->AddCapabilities(&capabilities);
jvmtiEventCallbacks callbacks = {0};
callbacks.VMInit = VMInit;
callbacks.VMDeath = VMDeath;
callbacks.ClassLoad = ClassLoad;
callbacks.ClassPrepare = ClassPrepare;
callbacks.CompiledMethodLoad = Profiler::CompiledMethodLoad;
callbacks.DynamicCodeGenerated = Profiler::DynamicCodeGenerated;
callbacks.ThreadStart = Profiler::ThreadStart;
callbacks.ThreadEnd = Profiler::ThreadEnd;
callbacks.SampledObjectAlloc = ObjectSampler::SampledObjectAlloc;
callbacks.GarbageCollectionFinish = LivenessTracker::GarbageCollectionFinish;
callbacks.NativeMethodBind = VMStructs::NativeMethodBind;
_jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL);
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL);
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE,
NULL);
_jvmti->SetEventNotificationMode(JVMTI_ENABLE,
JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_NATIVE_METHOD_BIND,
NULL);
if (hotspot_version() == 0 || !CodeHeap::available()) {
// Workaround for JDK-8173361: avoid CompiledMethodLoad events when possible
_jvmti->SetEventNotificationMode(JVMTI_ENABLE,
JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
} else {
// DebugNonSafepoints is automatically enabled with CompiledMethodLoad,
// otherwise we set the flag manually
char *flag_addr = (char *)JVMFlag::find("DebugNonSafepoints", {JVMFlag::Type::Bool});
if (flag_addr != NULL) {
*flag_addr = 1;
}
}
char *flag_addr = (char *)JVMFlag::find("KeepJNIIDs", {JVMFlag::Type::Bool});
if (flag_addr != NULL) {
*flag_addr = 1;
}
// if the user sets -XX:+UseAdaptiveGCBoundary we will just disable the
// profiler to avoid the risk of crashing flag was made obsolete (inert) in 15
// (see JDK-8228991) and removed in 16 (see JDK-8231560)
if (hotspot_version() < 15) {
char *flag_addr = (char *)JVMFlag::find("UseAdaptiveGCBoundary", {JVMFlag::Type::Bool});
_is_adaptive_gc_boundary_flag_set = flag_addr != NULL && *flag_addr == 1;
}
// Make sure we reload method IDs upon class retransformation
JVMTIFunctions *functions = *(JVMTIFunctions **)_jvmti;
_orig_RedefineClasses = functions->RedefineClasses;
_orig_RetransformClasses = functions->RetransformClasses;
functions->RedefineClasses = RedefineClassesHook;
functions->RetransformClasses = RetransformClassesHook;
if (attach) {
loadAllMethodIDs(_jvmti, jni());
_jvmti->GenerateEvents(JVMTI_EVENT_DYNAMIC_CODE_GENERATED);
_jvmti->GenerateEvents(JVMTI_EVENT_COMPILED_METHOD_LOAD);
} else {
_jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
}
OS::installSignalHandler(WAKEUP_SIGNAL, NULL, wakeupHandler);
return true;
}
// Run late initialization when JVM is ready
void VM::ready(jvmtiEnv *jvmti, JNIEnv *jni) {
Profiler::setupSignalHandlers();
{
JitWriteProtection jit(true);
VMStructs::ready();
}
_libjava = getLibraryHandle("libjava.so");
}
void VM::applyPatch(char *func, const char *patch, const char *end_patch) {
size_t size = end_patch - patch;
uintptr_t start_page = (uintptr_t)func & ~OS::page_mask;
uintptr_t end_page =
((uintptr_t)func + size + OS::page_mask) & ~OS::page_mask;
if (mprotect((void *)start_page, end_page - start_page,
PROT_READ | PROT_WRITE | PROT_EXEC) == 0) {
memcpy(func, patch, size);
__builtin___clear_cache(func, func + size);
mprotect((void *)start_page, end_page - start_page, PROT_READ | PROT_EXEC);
}
}
void *VM::getLibraryHandle(const char *name) {
if (OS::isLinux()) {
void *handle = dlopen(name, RTLD_LAZY);
if (handle != NULL) {
return handle;
}
Log::warn("Failed to load %s: %s", name, dlerror());
}
// JVM symbols are globally visible on macOS
return RTLD_DEFAULT;
}
void VM::loadMethodIDs(jvmtiEnv *jvmti, JNIEnv *jni, jclass klass) {
bool needs_patch = VM::hotspot_version() == 8;
if (needs_patch) {
// Workaround for JVM bug https://bugs.openjdk.org/browse/JDK-8062116
// Preallocate space for jmethodIDs at the beginning of the list (rather than at the end)
// This is relevant only for JDK 8 - later versions do not have this bug
if (VMStructs::hasClassLoaderData()) {
VMKlass *vmklass = VMKlass::fromJavaClass(jni, klass);
int method_count = vmklass->methodCount();
if (method_count > 0) {
ClassLoaderData *cld = vmklass->classLoaderData();
cld->lock();
for (int i = 0; i < method_count; i += MethodList::SIZE) {
*cld->methodList() = new MethodList(*cld->methodList());
}
cld->unlock();
}
}
}
jint method_count;
jmethodID *methods;
if (jvmti->GetClassMethods(klass, &method_count, &methods) == 0) {
jvmti->Deallocate((unsigned char *)methods);
}
}
void VM::loadAllMethodIDs(jvmtiEnv *jvmti, JNIEnv *jni) {
jint class_count;
jclass *classes;
if (jvmti->GetLoadedClasses(&class_count, &classes) == 0) {
for (int i = 0; i < class_count; i++) {
loadMethodIDs(jvmti, jni, classes[i]);
}
jvmti->Deallocate((unsigned char *)classes);
}
}
void JNICALL VM::VMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
ready(jvmti, jni);
loadAllMethodIDs(jvmti, jni);
// Delayed start of profiler if agent has been loaded at VM bootstrap
Error error = Profiler::instance()->run(_agent_args);
if (error) {
Log::error("%s", error.message());
}
}
void JNICALL VM::VMDeath(jvmtiEnv *jvmti, JNIEnv *jni) {
Profiler::instance()->shutdown(_agent_args);
}
jvmtiError
VM::RedefineClassesHook(jvmtiEnv *jvmti, jint class_count,
const jvmtiClassDefinition *class_definitions) {
jvmtiError result =
_orig_RedefineClasses(jvmti, class_count, class_definitions);
if (result == 0) {
// jmethodIDs are invalidated after RedefineClasses
JNIEnv *env = jni();
for (int i = 0; i < class_count; i++) {
if (class_definitions[i].klass != NULL) {
loadMethodIDs(jvmti, env, class_definitions[i].klass);
}
}
}
return result;
}
jvmtiError VM::RetransformClassesHook(jvmtiEnv *jvmti, jint class_count,
const jclass *classes) {
jvmtiError result = _orig_RetransformClasses(jvmti, class_count, classes);
if (result == 0) {
// jmethodIDs are invalidated after RetransformClasses
JNIEnv *env = jni();
for (int i = 0; i < class_count; i++) {
if (classes[i] != NULL) {
loadMethodIDs(jvmti, env, classes[i]);
}
}
}
return result;
}
extern "C" DLLEXPORT jint JNICALL
Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
Error error = _agent_args.parse(options);
Log::open(_agent_args);
if (error) {
Log::error("%s", error.message());
return ARGUMENTS_ERROR;
}
if (!VM::initProfilerBridge(vm, false)) {
Log::error("JVM does not support Tool Interface");
return COMMAND_ERROR;
}
return 0;
}
extern "C" DLLEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
if (!VM::initLibrary(vm)) {
return 0;
}
return JNI_VERSION_1_6;
}
extern "C" DLLEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
Profiler *profiler = Profiler::instance();
if (profiler != NULL) {
profiler->stop();
}
}