-
Notifications
You must be signed in to change notification settings - Fork 7
/
vmStructs.h
689 lines (574 loc) · 19.7 KB
/
vmStructs.h
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
* Copyright 2017 Andrei Pangin
* Copyright 2022, 2023 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.
*/
#ifndef _VMSTRUCTS_H
#define _VMSTRUCTS_H
#include <jvmti.h>
#include <stdint.h>
#include <string.h>
#include "codeCache.h"
#include "jniHelper.h"
#include "jvmHeap.h"
#include "safeAccess.h"
#include "threadState.h"
#include "vmEntry.h"
class HeapUsage;
class VMStructs {
protected:
static CodeCache* _libjvm;
static CodeCache _unsafe_to_walk;
static bool _has_class_names;
static bool _has_method_structs;
static bool _has_compiler_structs;
static bool _has_stack_structs;
static bool _has_class_loader_data;
static bool _has_native_thread_id;
static bool _has_perm_gen;
static int _klass_name_offset;
static int _symbol_length_offset;
static int _symbol_length_and_refcount_offset;
static int _symbol_body_offset;
static int _class_loader_data_offset;
static int _class_loader_data_next_offset;
static int _methods_offset;
static int _jmethod_ids_offset;
static int _thread_osthread_offset;
static int _thread_anchor_offset;
static int _thread_state_offset;
static int _thread_vframe_offset;
static int _thread_exception_offset;
static int _osthread_id_offset;
static int _osthread_state_offset;
static int _comp_env_offset;
static int _comp_task_offset;
static int _comp_method_offset;
static int _anchor_sp_offset;
static int _anchor_pc_offset;
static int _anchor_fp_offset;
static int _frame_size_offset;
static int _frame_complete_offset;
static int _code_begin_offset;
static int _scopes_begin_offset;
static int _nmethod_name_offset;
static int _nmethod_method_offset;
static int _nmethod_entry_offset;
static int _nmethod_state_offset;
static int _nmethod_level_offset;
static int _nmethod_metadata_offset;
static int _nmethod_pcs_begin_offset;
static int _nmethod_pcs_end_offset;
static int _method_constmethod_offset;
static int _method_code_offset;
static int _constmethod_constants_offset;
static int _constmethod_idnum_offset;
static int _constmethod_size;
static int _pool_holder_offset;
static int _array_data_offset;
static int _code_heap_memory_offset;
static int _code_heap_segmap_offset;
static int _code_heap_segment_shift;
static int _vs_low_bound_offset;
static int _vs_high_bound_offset;
static int _vs_low_offset;
static int _vs_high_offset;
static int _flag_name_offset;
static int _flag_addr_offset;
static const char* _flags_addr;
static int _flag_count;
static int _flag_size;
static char* _code_heap[3];
static const void* _code_heap_low;
static const void* _code_heap_high;
static char** _code_heap_addr;
static const void** _code_heap_low_addr;
static const void** _code_heap_high_addr;
static int* _klass_offset_addr;
static char** _narrow_klass_base_addr;
static char* _narrow_klass_base;
static int* _narrow_klass_shift_addr;
static int _narrow_klass_shift;
static char** _collected_heap_addr;
static char* _collected_heap;
static int _collected_heap_reserved_offset;
static int _region_start_offset;
static int _region_size_offset;
static int _markword_klass_shift;
static int _markword_monitor_value;
static int _interpreter_frame_bcp_offset;
static unsigned char _unsigned5_base;
static const void** _call_stub_return_addr;
static const void* _call_stub_return;
static const void* _interpreted_frame_valid_start;
static const void* _interpreted_frame_valid_end;
static jfieldID _eetop;
static jfieldID _tid;
static jfieldID _klass;
static int _tls_index;
static intptr_t _env_offset;
typedef void (*LockFunc)(void*);
static LockFunc _lock_func;
static LockFunc _unlock_func;
typedef HeapUsage (*HeapUsageFunc)(const void*);
static HeapUsageFunc _heap_usage_func;
typedef void* (*MemoryUsageFunc)(void*, void*, bool);
static MemoryUsageFunc _memory_usage_func;
typedef GCHeapSummary (*GCHeapSummaryFunc)(void*);
static GCHeapSummaryFunc _gc_heap_summary_func;
typedef void* (*FindFlagFunc)(const char*, size_t, bool, bool);
static FindFlagFunc _find_flag_func;
typedef bool (*IsValidMethodFunc)(void*);
static IsValidMethodFunc _is_valid_method_func;
static uintptr_t readSymbol(const char* symbol_name);
static void initOffsets();
static void resolveOffsets();
static void patchSafeFetch();
static void initJvmFunctions();
static void initTLS(void* vm_thread);
static void initThreadBridge(JNIEnv* env);
static void initLogging(JNIEnv* env);
static void initUnsafeFunctions();
static void initMemoryUsage(JNIEnv* env);
const char* at(int offset) {
return (const char*)this + offset;
}
static bool aligned(const void* ptr) {
return ((uintptr_t)ptr & (sizeof(uintptr_t) - 1)) == 0;
}
static void checkNativeBinding(jvmtiEnv *jvmti, JNIEnv *jni, jmethodID method, void *address);
private:
static const void* findHeapUsageFunc();
static bool isFlagTrue(const char* name);
public:
static void init(CodeCache* libjvm);
static void ready();
static CodeCache* libjvm() {
return _libjvm;
}
static bool hasClassNames() {
return _has_class_names;
}
static bool hasMethodStructs() {
return _has_method_structs;
}
static bool hasCompilerStructs() {
return _has_compiler_structs;
}
static bool hasStackStructs() {
return _has_stack_structs;
}
static bool hasClassLoaderData() {
return _has_class_loader_data;
}
static bool hasJavaThreadId() {
return _tid != NULL;
}
static bool isInterpretedFrameValidFunc(const void* pc) {
return pc >= _interpreted_frame_valid_start && pc < _interpreted_frame_valid_end;
}
typedef jvmtiError (*GetStackTraceFunc)(void* self, void* thread,
jint start_depth, jint max_frame_count,
jvmtiFrameInfo* frame_buffer, jint* count_ptr);
static GetStackTraceFunc _get_stack_trace;
static bool hasDebugSymbols() {
return _get_stack_trace != NULL;
}
static bool isSafeToWalk(uintptr_t pc);
static void JNICALL NativeMethodBind(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread, jmethodID method, void *address, void **new_address_ptr);
};
class MethodList {
public:
enum { SIZE = 8 };
private:
intptr_t _method[SIZE];
int _ptr;
MethodList* _next;
int _padding;
public:
MethodList(MethodList* next) : _ptr(0), _next(next), _padding(0) {
for (int i = 0; i < SIZE; i++) {
_method[i] = 0x37;
}
}
};
class NMethod;
class VMSymbol : VMStructs {
public:
unsigned short length() {
if (_symbol_length_offset >= 0) {
return *(unsigned short*) at(_symbol_length_offset);
} else {
return *(unsigned int*) at(_symbol_length_and_refcount_offset) >> 16;
}
}
const char* body() {
return at(_symbol_body_offset);
}
};
class ClassLoaderData : VMStructs {
private:
void* mutex() {
return *(void**) at(sizeof(uintptr_t) * 3);
}
public:
void lock() {
_lock_func(mutex());
}
void unlock() {
_unlock_func(mutex());
}
MethodList** methodList() {
return (MethodList**) at(sizeof(uintptr_t) * 6 + 8);
}
};
class VMKlass : VMStructs {
public:
static VMKlass* fromJavaClass(JNIEnv* env, jclass cls) {
if (_has_perm_gen) {
jobject klassOop = env->GetObjectField(cls, _klass);
return (VMKlass*)(*(uintptr_t**)klassOop + 2);
} else if (sizeof(VMKlass*) == 8) {
return (VMKlass*)(uintptr_t)env->GetLongField(cls, _klass);
} else {
return (VMKlass*)(uintptr_t)env->GetIntField(cls, _klass);
}
}
static VMKlass* fromHandle(uintptr_t handle) {
if (_has_perm_gen) {
// On JDK 7 KlassHandle is a pointer to klassOop, hence one more indirection
return (VMKlass*)(*(uintptr_t**)handle + 2);
} else {
return (VMKlass*)handle;
}
}
VMSymbol* name() {
return *(VMSymbol**) at(_klass_name_offset);
}
ClassLoaderData* classLoaderData() {
return *(ClassLoaderData**) at(_class_loader_data_offset);
}
int methodCount() {
int* methods = *(int**) at(_methods_offset);
return methods == NULL ? 0 : *methods & 0xffff;
}
jmethodID* jmethodIDs() {
return __atomic_load_n((jmethodID**) at(_jmethod_ids_offset), __ATOMIC_ACQUIRE);
}
};
class VMMethod : VMStructs {
private:
static bool check_jmethodID_J9(jmethodID id);
static bool check_jmethodID_hotspot(jmethodID id);
public:
static VMMethod* fromMethodID(jmethodID id) {
return *(VMMethod**)id;
}
jmethodID id();
static bool check_jmethodID(jmethodID id);
const char* bytecode() {
return *(const char**) at(_method_constmethod_offset) + _constmethod_size;
}
NMethod* code() {
return *(NMethod**) at(_method_code_offset);
}
};
class NMethod : VMStructs {
public:
int frameSize() {
return *(int*) at(_frame_size_offset);
}
int frameCompleteOffset() {
return *(int*) at(_frame_complete_offset);
}
void setFrameCompleteOffset(int offset) {
*(int*) at(_frame_complete_offset) = offset;
}
const char* code() {
if (_code_begin_offset >= 0) {
return *(const char**) at(_code_begin_offset);
} else {
return at(*(int*) at(-_code_begin_offset));
}
}
const char* scopes() {
if (_scopes_begin_offset >= 0) {
return *(const char**) at(_scopes_begin_offset);
} else {
return at(*(int*) at(-_scopes_begin_offset));
}
}
bool isFrameCompleteAt(const void* pc) {
return pc >= code() + frameCompleteOffset();
}
bool isEntryFrame(const void* pc) {
return pc == _call_stub_return;
}
const char* name() {
return *(const char**) at(_nmethod_name_offset);
}
bool isNMethod() {
const char* n = name();
return n != NULL && (strcmp(n, "nmethod") == 0 || strcmp(n, "native nmethod") == 0);
}
bool isInterpreter() {
const char* n = name();
return n != NULL && strcmp(n, "Interpreter") == 0;
}
VMMethod* method() {
return *(VMMethod**) at(_nmethod_method_offset);
}
void* entry() {
return *(void**) at(_nmethod_entry_offset);
}
char state() {
return *at(_nmethod_state_offset);
}
bool isAlive() {
return state() >= 0 && state() <= 1;
}
int level() {
return _nmethod_level_offset >= 0 ? *(signed char*) at(_nmethod_level_offset) : 0;
}
VMMethod** metadata() {
return (VMMethod**) at(*(int*) at(_nmethod_metadata_offset));
}
int findScopeOffset(const void* pc);
};
class VMThread : VMStructs {
public:
static VMThread* current();
static int key() {
return _tls_index;
}
static VMThread* fromJavaThread(JNIEnv* env, jthread thread) {
return _eetop != NULL && thread != NULL ? (VMThread*)(uintptr_t)env->GetLongField(thread, _eetop) : NULL;
}
static VMThread* fromEnv(JNIEnv* env) {
return (VMThread*)((intptr_t)env - _env_offset);
}
static jlong javaThreadId(JNIEnv* env, jthread thread) {
return env->GetLongField(thread, _tid);
}
static int nativeThreadId(JNIEnv* jni, jthread thread);
int osThreadId() {
const char* osthread = *(const char**) at(_thread_osthread_offset);
return *(int*)(osthread + _osthread_id_offset);
}
int state() {
return _thread_state_offset >= 0 ? *(int*) at(_thread_state_offset) : 0;
}
bool inJava() {
return state() == 8;
}
bool inDeopt() {
return *(void**) at(_thread_vframe_offset) != NULL;
}
void*& exception() {
return *(void**) at(_thread_exception_offset);
}
ThreadState osThreadState() {
if (_thread_osthread_offset >= 0 && _osthread_state_offset >= 0) {
const char* osthread = *(const char**) at(_thread_osthread_offset);
if (osthread != nullptr) {
return static_cast<ThreadState>(*(int *) (osthread + _osthread_state_offset));
}
}
return ThreadState::UNKNOWN;
}
uintptr_t& lastJavaSP() {
return *(uintptr_t*) (at(_thread_anchor_offset) + _anchor_sp_offset);
}
uintptr_t& lastJavaPC() {
return *(uintptr_t*) (at(_thread_anchor_offset) + _anchor_pc_offset);
}
uintptr_t& lastJavaFP() {
return *(uintptr_t*) (at(_thread_anchor_offset) + _anchor_fp_offset);
}
VMMethod* compiledMethod() {
const char* env = *(const char**) at(_comp_env_offset);
if (env != NULL) {
const char* task = *(const char**) (env + _comp_task_offset);
if (task != NULL) {
return *(VMMethod**) (task + _comp_method_offset);
}
}
return NULL;
}
};
class CodeHeap : VMStructs {
private:
static bool contains(char* heap, const void* pc) {
return heap != NULL &&
pc >= *(const void**)(heap + _code_heap_memory_offset + _vs_low_offset) &&
pc < *(const void**)(heap + _code_heap_memory_offset + _vs_high_offset);
}
static NMethod* findNMethod(char* heap, const void* pc);
public:
static bool available() {
return _code_heap_addr != NULL;
}
static bool contains(const void* pc) {
return _code_heap_low <= pc && pc < _code_heap_high;
}
static void updateBounds(const void* start, const void* end) {
for (const void* low = _code_heap_low;
start < low && !__sync_bool_compare_and_swap(&_code_heap_low, low, start);
low = _code_heap_low);
for (const void* high = _code_heap_high;
end > high && !__sync_bool_compare_and_swap(&_code_heap_high, high, end);
high = _code_heap_high);
}
static NMethod* findNMethod(const void* pc) {
if (contains(_code_heap[0], pc)) return findNMethod(_code_heap[0], pc);
if (contains(_code_heap[1], pc)) return findNMethod(_code_heap[1], pc);
if (contains(_code_heap[2], pc)) return findNMethod(_code_heap[2], pc);
return NULL;
}
};
class JVMFlag : VMStructs {
public:
static void* find(const char* name);
const char* name() {
return *(const char**) at(_flag_name_offset);
}
void* addr() {
return *(void**) at(_flag_addr_offset);
}
};
class HeapUsage : VMStructs {
public:
size_t _initSize = -1;
size_t _used = -1;
size_t _committed = -1;
size_t _maxSize = -1;
size_t _used_at_last_gc = -1;
static bool isLastGCUsageSupported() {
// only supported for JDK 17+
// the CollectedHeap structure is vastly different in JDK 11 and earlier so we can't support it
return _collected_heap_addr != NULL && _heap_usage_func != NULL;
}
static bool needsNativeBindingInterception() {
return _collected_heap_addr == NULL || (_heap_usage_func == NULL && _gc_heap_summary_func == NULL);
}
static jlong getMaxHeap(JNIEnv* env) {
static jclass _rt;
static jmethodID _get_rt;
static jmethodID _max_memory;
if (!(_rt = env->FindClass("java/lang/Runtime"))) {
jniExceptionCheck(env);
return -1;
}
if (!(_get_rt = env->GetStaticMethodID(_rt, "getRuntime", "()Ljava/lang/Runtime;"))) {
jniExceptionCheck(env);
return -1;
}
if (!(_max_memory = env->GetMethodID(_rt, "maxMemory", "()J"))) {
jniExceptionCheck(env);
return -1;
}
jobject rt = (jobject)env->CallStaticObjectMethod(_rt, _get_rt);
jlong ret = (jlong)env->CallLongMethod(rt, _max_memory);
if (jniExceptionCheck(env)) {
return -1;
}
return ret;
}
static HeapUsage get() {
return get(true);
}
static HeapUsage get(bool allow_jmx) {
HeapUsage usage;
if (_collected_heap_addr != NULL) {
if (_heap_usage_func != NULL) {
// this is the JDK 17+ path
usage = _heap_usage_func(*_collected_heap_addr);
usage._used_at_last_gc = ((CollectedHeap*)*_collected_heap_addr)->_used_at_last_gc;
} else if (_gc_heap_summary_func != NULL) {
// this is the JDK 11 path
// we need to collect GCHeapSummary information first
GCHeapSummary summary = _gc_heap_summary_func(*_collected_heap_addr);
usage._initSize = -1;
usage._used = summary.used();
usage._committed = -1;
usage._maxSize = summary.maxSize();
}
}
if (usage._maxSize == -1 && _memory_usage_func != NULL && allow_jmx) {
// this path is for non-hotspot JVMs
// we need to patch the native method binding for JMX GetMemoryUsage to capture the native method pointer first
// also, it requires JMX and allocating new objects so it really should not be used in a GC callback
JNIEnv* env = VM::jni();
if (env == NULL) {
return usage;
}
jobject m_usage = (jobject) _memory_usage_func(env, (jobject) NULL, (jboolean) true);
jclass cls = env->GetObjectClass(m_usage);
jfieldID init_fid = env->GetFieldID(cls, "init", "J");
jfieldID max_fid = env->GetFieldID(cls, "max", "J");
jfieldID used_fid = env->GetFieldID(cls, "used", "J");
jfieldID committed_fid = env->GetFieldID(cls, "committed", "J");
if (init_fid == NULL || max_fid == NULL || used_fid == NULL || committed_fid == NULL) {
return usage;
}
usage._initSize = env->GetLongField(m_usage, init_fid);
usage._maxSize = env->GetLongField(m_usage, max_fid);
usage._used = env->GetLongField(m_usage, used_fid);
usage._committed = env->GetLongField(m_usage, committed_fid);
}
return usage;
}
};
class PcDesc {
public:
int _pc;
int _scope_offset;
int _obj_offset;
int _flags;
};
class ScopeDesc : VMStructs {
private:
NMethod* _nm;
const unsigned char* _stream;
int _method_offset;
int _bci;
int readInt();
public:
ScopeDesc(NMethod* nm) : _nm(nm) {
}
int decode(int offset) {
_stream = (const unsigned char*)_nm->scopes() + offset;
int sender_offset = readInt();
_method_offset = readInt();
_bci = readInt() - 1;
return sender_offset;
}
VMMethod* method() {
return _method_offset > 0 ? _nm->metadata()[_method_offset - 1] : NULL;
}
int bci() {
return _bci;
}
};
class InterpreterFrame : VMStructs {
public:
enum {
sender_sp_offset = -1,
method_offset = -3
};
static int bcp_offset() {
return _interpreter_frame_bcp_offset;
}
};
#endif // _VMSTRUCTS_H