-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvmStructs.h
More file actions
1126 lines (951 loc) · 44.4 KB
/
Copy pathvmStructs.h
File metadata and controls
1126 lines (951 loc) · 44.4 KB
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright The async-profiler authors
* Copyright 2026 Datadog, Inc
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _VMSTRUCTS_H
#define _VMSTRUCTS_H
#include <initializer_list>
#include <jvmti.h>
#include <stdint.h>
#include <type_traits>
#include "codeCache.h"
#include "counters.h"
#include "safeAccess.h"
#include "thread.h"
#include "threadState.h"
#include "vmEntry.h"
class GCHeapSummary;
class HeapUsage;
class VMNMethod;
// During stack walking in the profiler's signal handler, GC or class unloading
// on another thread can free VMNMethod/VMMethod memory concurrently, making
// pointers stale between the readability check and the actual dereference.
// In release builds the setjmp/longjmp crash protection in walkVM catches the
// resulting SIGSEGV. In debug builds the assert(isReadable) fires first,
// sending SIGABRT which is uncatchable by crash protection.
// When crash protection is active the assert is redundant — any bad read will
// be caught by the SIGSEGV handler and recovered via longjmp — so we skip it.
inline bool crashProtectionActive() {
ProfiledThread* pt = ProfiledThread::currentSignalSafe();
return pt != nullptr && pt->isCrashProtectionActive();
}
template <typename T>
inline T* cast_to(const void* ptr) {
assert(T::type_size() > 0); // Ensure type size has been initialized
assert(crashProtectionActive() || ptr == nullptr || SafeAccess::isReadableRange(ptr, T::type_size()));
return reinterpret_cast<T*>(const_cast<void*>(ptr));
}
#define TYPE_SIZE_NAME(name) _##name##_size
// MATCH_SYMBOLS macro expands into a string list, that is consumed by matchAny() method
#define MATCH_SYMBOLS(...) std::initializer_list<const char*>{ __VA_ARGS__ }
/**
* This macro defines a counterpart of a JVM class, e.g. VMKlass -> Klass.
* By the convention, we prefix the class name with 'VM' to avoid namespace collision
* with JVM inside a debug session. E.g.
* gdb > p this
* gdb > (VMKlass*)0x123456
* gdb > p (Klass*)this
* ....
*
* The macro implicitly defines three static functions:
* - type_size() Return class size defined in JVM.
* - cast() It performs memory readability check before casts a void* pointer to this type.
* It ensures the memory range [ptr, ptr + type_size()) is readable.
* - load_then_cast() It loads a pointer from specified location, then does above cast.
*/
#define DECLARE(name) \
class name : VMStructs { \
public: \
static uint64_t type_size() { return TYPE_SIZE_NAME(name); } \
static name * cast(const void* ptr) { return cast_to<name>(ptr); } \
static name * cast_raw(const void* ptr) { return (name *)ptr; } \
static name * load_then_cast(const void* ptr) { \
assert(ptr != nullptr); \
return cast(*(const void**)ptr); }
#define DECLARE_END };
/**
* Defines a type and its matching symbols in vmStructs.
* A type may match multiple names in different JVM versions.
* Macro expansion:
* - Declaration phase
* static uint64_t _TYPE_size;
*
* For example:
* f(VMClassLoaderData) -> static uint64_t _VMClassLoaderData_size;
*
* - Initialization phase
* uint64_t VMStructs::_TYPE_size = 0;
*
* For example:
* f(VMClassLoaderData) -> uint64_t VMStructs::_VMClassLoaderData_size = 0;
*
* - Value population phase
* if (matchAny((char*)[]) { typeName, nullptr}) {
* _TYPE_size = size;
* continue;
* }
*
* For example:
* f(VMClassLoaderData, MATCH_SYMBOLS("ClassLoaderData")) ->
* if (matchAny((char*)[] {"ClassLoaderData", nullptr})) {
* _ClassLoaderData_size = size;
* continue;
* }
*
* - Value verification phase
* assert(_TYPE_size > 0);
*
* For example:
* f(VMClassLoaderData, MATCH_SYMBOLS("ClassLoaderData")) ->
* assert(_VMClassLoaderData_size > 0);
*/
#define DECLARE_TYPES_DO(f) \
f(VMClassLoaderData, MATCH_SYMBOLS("ClassLoaderData")) \
f(VMConstantPool, MATCH_SYMBOLS("ConstantPool")) \
f(VMConstMethod, MATCH_SYMBOLS("ConstMethod")) \
f(VMFlag, MATCH_SYMBOLS("JVMFlag", "Flag")) \
f(VMJavaFrameAnchor, MATCH_SYMBOLS("JavaFrameAnchor")) \
f(VMKlass, MATCH_SYMBOLS("Klass")) \
f(VMMethod, MATCH_SYMBOLS("Method")) \
f(VMNMethod, MATCH_SYMBOLS("nmethod")) \
f(VMSymbol, MATCH_SYMBOLS("Symbol")) \
f(VMThread, MATCH_SYMBOLS("Thread"))
/**
* Following macros define field offsets, addresses or values of JVM classes that are exported by
* vmStructs.
* - type_begin() Start a definition of a type. The type name is not used at this moment, but
* improves readability.
* - field() Define a field of a class, can be either an offset, an address or a value
* - field_with_version A field that only exits in the specified JVM version range
* - type_end() End of a type definition
*/
typedef int offset;
typedef void* address;
#define MIN_VERSION 8
// JDK 128 :-)
#define MAX_VERSION 128
#define DECLARE_TYPE_FIELD_DO(type_begin, field, field_with_version, type_end) \
type_begin(VMMemRegion, MATCH_SYMBOLS("MemRegion")) \
field(_region_start_offset, offset, MATCH_SYMBOLS("_start")) \
field(_region_size_offset, offset, MATCH_SYMBOLS("_word_size")) \
type_end() \
type_begin(VMNMethod, MATCH_SYMBOLS("CompiledMethod", "nmethod")) \
field(_nmethod_method_offset, offset,MATCH_SYMBOLS("_method")) \
field_with_version(_nmethod_entry_offset, offset, 23, MAX_VERSION, MATCH_SYMBOLS("_verified_entry_offset")) \
field_with_version(_nmethod_entry_address, offset, 8, 22, MATCH_SYMBOLS("_verified_entry_point")) \
field(_nmethod_state_offset, offset, MATCH_SYMBOLS("_state")) \
field(_nmethod_level_offset, offset, MATCH_SYMBOLS("_comp_level")) \
field_with_version(_nmethod_metadata_offset, offset, MIN_VERSION, 24, MATCH_SYMBOLS("_metadata_offset")) \
field_with_version(_nmethod_immutable_offset, offset, 23, MAX_VERSION, MATCH_SYMBOLS("_immutable_data")) \
field(_scopes_pcs_offset, offset, MATCH_SYMBOLS("_scopes_pcs_offset")) \
field_with_version(_scopes_data_offset, offset, 23, MAX_VERSION, MATCH_SYMBOLS("_scopes_data_offset")) \
field_with_version(_scopes_data_address, offset, 9, 22, MATCH_SYMBOLS("_scopes_data_begin")) \
type_end() \
type_begin(VMMethod, MATCH_SYMBOLS("Method")) \
field(_method_constmethod_offset, offset, MATCH_SYMBOLS("_constMethod")) \
field(_method_code_offset, offset, MATCH_SYMBOLS("_code")) \
type_end() \
type_begin(VMConstMethod, MATCH_SYMBOLS("ConstMethod")) \
field(_constmethod_constants_offset, offset, MATCH_SYMBOLS("_constants")) \
field(_constmethod_idnum_offset, offset, MATCH_SYMBOLS("_method_idnum")) \
type_end() \
type_begin(VMConstantPool, MATCH_SYMBOLS("ConstantPool")) \
field(_pool_holder_offset, offset, MATCH_SYMBOLS("_pool_holder")) \
type_end() \
type_begin(VMKlass, MATCH_SYMBOLS("Klass", "InstanceKlass")) \
field(_klass_name_offset, offset, MATCH_SYMBOLS("_name")) \
field(_class_loader_data_offset, offset, MATCH_SYMBOLS("_class_loader_data")) \
field(_methods_offset, offset, MATCH_SYMBOLS("_methods")) \
field(_jmethod_ids_offset, offset, MATCH_SYMBOLS("_methods_jmethod_ids")) \
type_end() \
type_begin(VMClassLoaderData, MATCH_SYMBOLS("ClassLoaderData")) \
field(_class_loader_data_next_offset, offset, MATCH_SYMBOLS("_next")) \
type_end() \
type_begin(VMJavaClass, MATCH_SYMBOLS("java_lang_Class")) \
field(_klass_offset_addr, address, MATCH_SYMBOLS("_klass_offset")) \
type_end() \
type_begin(VMSymbol, MATCH_SYMBOLS("Symbol")) \
field(_symbol_length_offset, offset, MATCH_SYMBOLS("_length")) \
field(_symbol_body_offset, offset, MATCH_SYMBOLS("_body")) \
type_end() \
type_begin(VMJavaThread, MATCH_SYMBOLS("JavaThread", "Thread")) \
field(_thread_osthread_offset, offset, MATCH_SYMBOLS("_osthread")) \
field(_thread_anchor_offset, offset, MATCH_SYMBOLS("_anchor")) \
field(_thread_state_offset, offset, MATCH_SYMBOLS("_thread_state")) \
field(_thread_vframe_offset, offset, MATCH_SYMBOLS("_vframe_array_head")) \
type_end() \
type_begin(VMOSThread, MATCH_SYMBOLS("OSThread")) \
field(_osthread_id_offset, offset, MATCH_SYMBOLS("_thread_id")) \
field_with_version(_osthread_state_offset, offset, 10, MAX_VERSION, MATCH_SYMBOLS("_state")) \
type_end() \
type_begin(VMThreadShadow, MATCH_SYMBOLS("ThreadShadow")) \
field(_thread_exception_offset, offset, MATCH_SYMBOLS("_exception_file")) \
type_end() \
type_begin(VMCompilerThread, MATCH_SYMBOLS("CompilerThread")) \
field(_comp_env_offset, offset, MATCH_SYMBOLS("_env")) \
type_end() \
type_begin(VMciEnv, MATCH_SYMBOLS("ciEnv")) \
field(_comp_task_offset, offset, MATCH_SYMBOLS("_task")) \
type_end() \
type_begin(VMCompileTask, MATCH_SYMBOLS("CompileTask")) \
field(_comp_method_offset, offset, MATCH_SYMBOLS("_method")) \
type_end() \
type_begin(VMJavaCallWrapper, MATCH_SYMBOLS("JavaCallWrapper")) \
field(_call_wrapper_anchor_offset, offset, MATCH_SYMBOLS("_anchor")) \
type_end() \
type_begin(VMJavaFrameAnchor, MATCH_SYMBOLS("JavaFrameAnchor")) \
field(_anchor_sp_offset, offset, MATCH_SYMBOLS("_last_Java_sp")) \
field(_anchor_pc_offset, offset, MATCH_SYMBOLS("_last_Java_pc")) \
field(_anchor_fp_offset, offset, MATCH_SYMBOLS("_last_Java_fp")) \
type_end() \
type_begin(VMCodeBlob, MATCH_SYMBOLS("CodeBlob")) \
field(_blob_size_offset, offset, MATCH_SYMBOLS("_size")) \
field(_frame_size_offset, offset, MATCH_SYMBOLS("_frame_size")) \
field(_frame_complete_offset, offset, MATCH_SYMBOLS("_frame_complete_offset")) \
field_with_version(_code_offset, offset, 23, MAX_VERSION, MATCH_SYMBOLS("_code_offset")) \
field_with_version(_code_address, offset, 9, 22, MATCH_SYMBOLS("_code_begin")) \
field(_data_offset, offset, MATCH_SYMBOLS("_data_offset")) \
field_with_version(_mutable_data_offset, offset, 25, MAX_VERSION, MATCH_SYMBOLS("_mutable_data")) \
field_with_version(_relocation_size_offset, offset, 23, MAX_VERSION, MATCH_SYMBOLS("_relocation_size")) \
field(_nmethod_name_offset, offset, MATCH_SYMBOLS("_name")) \
type_end() \
type_begin(VMCodeCache, MATCH_SYMBOLS("CodeCache")) \
field(_code_heap_addr, address, MATCH_SYMBOLS("_heap", "_heaps")) \
field_with_version(_code_heap_low_addr, address, 9, MAX_VERSION, MATCH_SYMBOLS("_low_bound")) \
field_with_version(_code_heap_high_addr, address, 9, MAX_VERSION, MATCH_SYMBOLS("_high_bound")) \
type_end() \
type_begin(VMCodeHeap, MATCH_SYMBOLS("CodeHeap")) \
field(_code_heap_memory_offset, offset, MATCH_SYMBOLS("_memory")) \
field(_code_heap_segmap_offset, offset, MATCH_SYMBOLS("_segmap")) \
field(_code_heap_segment_shift, offset, MATCH_SYMBOLS("_log2_segment_size")) \
type_end() \
type_begin(VMHeapBlock, MATCH_SYMBOLS("HeapBlock::Header")) \
field(_heap_block_used_offset, offset, MATCH_SYMBOLS("_used")) \
type_end() \
type_begin(VMVirtualSpace, MATCH_SYMBOLS("VirtualSpace")) \
field(_vs_low_bound_offset, offset, MATCH_SYMBOLS("_low_boundary")) \
field(_vs_high_bound_offset, offset, MATCH_SYMBOLS("_high_boundary")) \
field(_vs_low_offset, offset, MATCH_SYMBOLS("_low")) \
field(_vs_high_offset, offset, MATCH_SYMBOLS("_high")) \
type_end() \
type_begin(VMStubRoutine, MATCH_SYMBOLS("StubRoutines")) \
field(_call_stub_return_addr, address, MATCH_SYMBOLS("_call_stub_return_address")) \
type_end() \
type_begin(VMGrowableArray, MATCH_SYMBOLS("GrowableArrayBase", "GenericGrowableArray")) \
field(_array_len_offset, offset, MATCH_SYMBOLS("_len")) \
type_end() \
type_begin(VMGrowableArrayInt, MATCH_SYMBOLS("GrowableArray<int>")) \
field(_array_data_offset, offset, MATCH_SYMBOLS("_data")) \
type_end() \
type_begin(VMFlag, MATCH_SYMBOLS("JVMFlag", "Flag")) \
field(_flag_name_offset, offset, MATCH_SYMBOLS("_name", "name")) \
field(_flag_addr_offset, offset, MATCH_SYMBOLS("_addr", "addr")) \
field(_flag_origin_offset, offset, MATCH_SYMBOLS("_flags", "origin")) \
field(_flags_addr, address, MATCH_SYMBOLS("flags")) \
field(_flag_count, address, MATCH_SYMBOLS("numFlags")) \
field(_flag_type_offset, offset, MATCH_SYMBOLS("_type", "type")) \
type_end() \
type_begin(VMOop, MATCH_SYMBOLS("oopDesc")) \
field(_oop_klass_offset, offset, MATCH_SYMBOLS("_metadata._klass")) \
type_end() \
type_begin(VMUniverse, MATCH_SYMBOLS("Universe", "CompressedKlassPointers")) \
field(_narrow_klass_base_addr, address, MATCH_SYMBOLS("_narrow_klass._base", "_base")) \
field(_narrow_klass_shift_addr, address, MATCH_SYMBOLS("_narrow_klass._shift", "_shift")) \
field(_collected_heap_addr, address, MATCH_SYMBOLS("_collectedHeap")) \
type_end()
/**
* The following macros declare JVM constants that are exported by vmStructs
* - constant defines a constant of a class
*/
#define DECLARE_INT_CONSTANTS_DO(constant, constant_with_version) \
constant(frame, entry_frame_call_wrapper_offset)
#define DECLARE_LONG_CONSTANTS_DO(constant, constant_with_version) \
constant_with_version(markWord, klass_shift, 24, MAX_VERSION) \
constant_with_version(markWord, monitor_value, 24, MAX_VERSION)
class VMStructs {
public:
typedef bool (*IsValidMethodFunc)(void *);
protected:
enum { MONITOR_BIT = 2 };
static CodeCache* _libjvm;
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 _can_dereference_jmethod_id;
static bool _compact_object_headers;
static int _narrow_klass_shift;
static char* _code_heap[3];
static const void* _code_heap_low;
static const void* _code_heap_high;
static char* _narrow_klass_base;
static int _interpreter_frame_bcp_offset;
static unsigned char _unsigned5_base;
static const void* _call_stub_return;
static const void* _interpreter_start;
static VMNMethod* _interpreter_nm;
static const void* _interpreted_frame_valid_start;
static const void* _interpreted_frame_valid_end;
// Declare type size variables
#define DECLARE_TYPE_SIZE_VAR(name, names) \
static uint64_t TYPE_SIZE_NAME(name);
DECLARE_TYPES_DO(DECLARE_TYPE_SIZE_VAR)
#undef DECLARE_TYPE_SIZE_VAR
// Declare vmStructs' field offsets and addresses
// Do nothing macro
#define DO_NOTHING(...)
#define DECLARE_TYPE_FIELD(var, field_type, names) \
static field_type var;
#define DECLARE_TYPE_FIELD_WITH_VERSION(var, field_type, min_version, max_version, names) \
static field_type var;
DECLARE_TYPE_FIELD_DO(DO_NOTHING, DECLARE_TYPE_FIELD, DECLARE_TYPE_FIELD_WITH_VERSION, DO_NOTHING)
#undef DECLARE_TYPE_FIELD
#undef DECLARE_TYPE_FIELD_WITH_VERSION
#undef DO_NOTHING
// Declare int constant variables
#define DECLARE_INT_CONSTANT_VAR(type, field, ...) \
static int _##type##_##field;
DECLARE_INT_CONSTANTS_DO(DECLARE_INT_CONSTANT_VAR, DECLARE_INT_CONSTANT_VAR)
#undef DECLARE_INT_CONSTANT_VAR
// Declare long constant variables
#define DECLARE_LONG_CONSTANT_VAR(type, field, ...) \
static long _##type##_##field;
DECLARE_LONG_CONSTANTS_DO(DECLARE_LONG_CONSTANT_VAR, DECLARE_LONG_CONSTANT_VAR)
#undef DECLARE_LONG_CONSTANT_VAR
static jfieldID _eetop;
static jfieldID _tid;
static jfieldID _klass;
static int _tls_index;
static intptr_t _env_offset;
static void* _java_thread_vtbl[6];
typedef void (*LockFunc)(void*);
static LockFunc _lock_func;
static LockFunc _unlock_func;
// Datadog-specific extensions
static CodeCache _unsafe_to_walk;
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;
static IsValidMethodFunc _is_valid_method_func;
static uintptr_t readSymbol(const char* symbol_name);
// Read VM information from vmStructs
static void init_type_sizes();
static void init_offsets_and_addresses();
static void init_constants();
static void initOffsets();
#ifdef DEBUG
static void verify_offsets();
#endif
static void resolveOffsets();
static void patchSafeFetch();
static void initJvmFunctions();
static void initTLS(void* vm_thread);
static void initThreadBridge();
// Datadog-specific private methods
static void initUnsafeFunctions();
static void initCriticalJNINatives();
static void checkNativeBinding(jvmtiEnv *jvmti, JNIEnv *jni, jmethodID method, void *address);
static const void *findHeapUsageFunc();
const char* at(int offset) {
const char* ptr = (const char*)this + offset;
assert(crashProtectionActive() || SafeAccess::isReadable(ptr));
return ptr;
}
static bool goodPtr(const void* ptr) {
return (uintptr_t)ptr >= 0x1000 && ((uintptr_t)ptr & (sizeof(uintptr_t) - 1)) == 0;
}
template<typename T>
static T align(const void* ptr) {
static_assert(std::is_pointer<T>::value, "T must be a pointer type");
return (T)((uintptr_t)ptr & ~(sizeof(T) - 1));
}
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 hasNativeThreadId() {
return _has_native_thread_id;
}
static bool hasJavaThreadId() {
return _tid != NULL;
}
static bool isInterpretedFrameValidFunc(const void* pc) {
return pc >= _interpreted_frame_valid_start && pc < _interpreted_frame_valid_end;
}
// Datadog-specific extensions
static bool isSafeToWalk(uintptr_t pc);
static void JNICALL NativeMethodBind(jvmtiEnv *jvmti, JNIEnv *jni,
jthread thread, jmethodID method,
void *address, void **new_address_ptr);
static int thread_osthread_offset() {
return _thread_osthread_offset;
}
static int osthread_state_offset() {
return _osthread_state_offset;
}
static int osthread_id_offset() {
return _osthread_id_offset;
}
static int thread_state_offset() {
return _thread_state_offset;
}
static int flag_type_offset() {
return _flag_type_offset;
}
static int method_constmethod_offset() {
return _method_constmethod_offset;
}
static int constmethod_constants_offset() {
return _constmethod_constants_offset;
}
static int pool_holder_offset() {
return _pool_holder_offset;
}
static int class_loader_data_offset() {
return _class_loader_data_offset;
}
static IsValidMethodFunc is_valid_method_func() {
return _is_valid_method_func;
}
};
class HeapUsage : VMStructs {
private:
static bool is_jmx_attempted;
static bool is_jmx_supported; // default to not-supported
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 void initJMXUsage(JNIEnv* env);
static bool isJMXSupported() {
initJMXUsage(VM::jni());
return is_jmx_supported;
}
static bool isLastGCUsageSupported();
static bool needsNativeBindingInterception();
static jlong getMaxHeap(JNIEnv *env);
static HeapUsage get();
static HeapUsage get(bool allow_jmx);
};
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 VMNMethod;
class VMMethod;
DECLARE(VMSymbol)
public:
unsigned short length() {
assert(_symbol_length_offset >= 0);
return *(unsigned short*) at(_symbol_length_offset);
}
const char* body() {
assert(_symbol_body_offset >= 0);
return at(_symbol_body_offset);
}
DECLARE_END
DECLARE(VMClassLoaderData)
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);
}
DECLARE_END
DECLARE(VMKlass)
public:
static VMKlass* fromJavaClass(JNIEnv* env, jclass cls) {
if (sizeof(VMKlass*) == 8) {
return VMKlass::cast((const void*)(intptr_t)env->GetLongField(cls, _klass));
} else {
return VMKlass::cast((const void*)(intptr_t)env->GetIntField(cls, _klass));
}
}
static VMKlass* fromHandle(uintptr_t handle) {
return VMKlass::cast((const void*)handle);
}
static VMKlass* fromOop(uintptr_t oop) {
if (_narrow_klass_shift >= 0) {
uintptr_t narrow_klass;
if (_compact_object_headers) {
uintptr_t mark = *(uintptr_t*)oop;
if (mark & MONITOR_BIT) {
mark = *(uintptr_t*)(mark ^ MONITOR_BIT);
}
narrow_klass = mark >> _markWord_klass_shift;
} else {
narrow_klass = *(unsigned int*)(oop + _oop_klass_offset);
}
return VMKlass::cast((const void*)(_narrow_klass_base + (narrow_klass << _narrow_klass_shift)));
} else {
return VMKlass::load_then_cast((const void*)(oop + _oop_klass_offset));
}
}
VMSymbol* name() {
assert(_klass_name_offset >= 0);
return VMSymbol::load_then_cast(at(_klass_name_offset));
}
VMClassLoaderData* classLoaderData() {
assert(_class_loader_data_offset >= 0);
return VMClassLoaderData::load_then_cast(at(_class_loader_data_offset));
}
int methodCount() {
assert(_methods_offset >= 0);
int* methods = *(int**) at(_methods_offset);
return methods == NULL ? 0 : *methods & 0xffff;
}
jmethodID* jmethodIDs() {
assert(_jmethod_ids_offset >= 0);
return __atomic_load_n((jmethodID**) at(_jmethod_ids_offset), __ATOMIC_ACQUIRE);
}
DECLARE_END
DECLARE(VMJavaFrameAnchor)
private:
enum { MAX_CALL_WRAPPER_DISTANCE = 512 };
public:
NOADDRSANITIZE static VMJavaFrameAnchor* fromEntryFrame(uintptr_t fp) {
assert(_frame_entry_frame_call_wrapper_offset != -1);
assert(_call_wrapper_anchor_offset >= 0);
const char* call_wrapper = (const char*) SafeAccess::loadPtr((void**)(fp + _frame_entry_frame_call_wrapper_offset), nullptr);
if (!goodPtr(call_wrapper) || (uintptr_t)call_wrapper - fp > MAX_CALL_WRAPPER_DISTANCE) {
return NULL;
}
return VMJavaFrameAnchor::cast((const void*)(call_wrapper + _call_wrapper_anchor_offset));
}
NOADDRSANITIZE uintptr_t lastJavaSP() {
assert(_anchor_sp_offset >= 0);
return (uintptr_t) SafeAccess::loadPtr((void**) at(_anchor_sp_offset), nullptr);
}
NOADDRSANITIZE uintptr_t lastJavaFP() {
assert(_anchor_fp_offset >= 0);
return (uintptr_t) SafeAccess::loadPtr((void**) at(_anchor_fp_offset), nullptr);
}
NOADDRSANITIZE const void* lastJavaPC() {
assert(_anchor_pc_offset >= 0);
return SafeAccess::loadPtr((void**) at(_anchor_pc_offset), nullptr);
}
void setLastJavaPC(const void* pc) {
assert(_anchor_pc_offset >= 0);
*(const void**) at(_anchor_pc_offset) = pc;
}
NOADDRSANITIZE bool getFrame(const void*& pc, uintptr_t& sp, uintptr_t& fp) {
if (lastJavaPC() != NULL && lastJavaSP() != 0) {
pc = lastJavaPC();
sp = lastJavaSP();
fp = lastJavaFP();
return true;
}
return false;
}
DECLARE_END
// Copied from JDK's globalDefinitions.hpp 'JavaThreadState' enum
enum JVMJavaThreadState {
_thread_uninitialized = 0, // should never happen (missing initialization)
_thread_new = 2, // just starting up, i.e., in process of being initialized
_thread_new_trans = 3, // corresponding transition state (not used, included for completeness)
_thread_in_native = 4, // running in native code
_thread_in_native_trans = 5, // corresponding transition state
_thread_in_vm = 6, // running in VM
_thread_in_vm_trans = 7, // corresponding transition state
_thread_in_Java = 8, // running in Java or in stub code
_thread_in_Java_trans = 9, // corresponding transition state (not used, included for completeness)
_thread_blocked = 10, // blocked in vm
_thread_blocked_trans = 11, // corresponding transition state
_thread_max_state = 12 // maximum thread state+1 - used for statistics allocation
};
DECLARE(VMThread)
public:
static VMThread* current();
static int key() {
return _tls_index;
}
static VMThread* fromJavaThread(JNIEnv* env, jthread thread) {
return VMThread::cast_raw((const void*)env->GetLongField(thread, _eetop));
}
static jlong javaThreadId(JNIEnv* env, jthread thread) {
return env->GetLongField(thread, _tid);
}
static int nativeThreadId(JNIEnv* jni, jthread thread);
int osThreadId();
JNIEnv* jni();
const void** vtable() {
assert(SafeAccess::isReadable(this));
return *(const void***)this;
}
// This thread is considered a JavaThread if at least 2 of the selected 3 vtable entries
// match those of a known JavaThread (which is either application thread or AttachListener).
// Indexes were carefully chosen to work on OpenJDK 8 to 25, both product an debug builds.
bool isJavaThread() {
const void** vtbl = vtable();
return (vtbl[1] == _java_thread_vtbl[1]) +
(vtbl[3] == _java_thread_vtbl[3]) +
(vtbl[5] == _java_thread_vtbl[5]) >= 2;
}
// Cached version of isJavaThread(). On first call per thread, computes the
// vtable check and caches the result in ProfiledThread for O(1) subsequent
// lookups. This is needed because JVMTI ThreadStart only fires for application
// threads, not for JVM-internal JavaThread subclasses (CompilerThread, etc.).
bool cachedIsJavaThread() {
ProfiledThread* pt = ProfiledThread::currentSignalSafe();
if (pt != NULL) {
if (!pt->isJavaThreadKnown()) {
pt->cacheJavaThread(isJavaThread());
}
bool result = pt->isJavaThread();
if (!result) Counters::increment(WALKVM_CACHED_NOT_JAVA);
return result;
}
bool result = isJavaThread();
if (!result) Counters::increment(WALKVM_CACHED_NOT_JAVA);
return result;
}
OSThreadState osThreadState();
int state();
bool inJava() {
return state() == 8;
}
bool inDeopt() {
if (!cachedIsJavaThread()) return false;
assert(_thread_vframe_offset >= 0);
return SafeAccess::loadPtr((void**) at(_thread_vframe_offset), nullptr) != NULL;
}
// Check if the thread object memory is readable up to the largest used
// offset. On some JVMs (e.g. GraalVM 25 aarch64), a wall-clock signal
// can hit a thread whose memory is only partially mapped — the vtable
// at offset 0 may be readable while fields deeper in the object are not.
// On non-HotSpot JVMs (J9, Zing) offsets stay at -1; skip the check.
bool isThreadAccessible() {
int max_offset = -1;
if (_thread_exception_offset > max_offset) max_offset = _thread_exception_offset;
if (_thread_state_offset > max_offset) max_offset = _thread_state_offset;
if (_thread_osthread_offset > max_offset) max_offset = _thread_osthread_offset;
if (_thread_anchor_offset > max_offset) max_offset = _thread_anchor_offset;
if (_thread_vframe_offset > max_offset) max_offset = _thread_vframe_offset;
if (max_offset < 0) return true;
return SafeAccess::isReadableRange(this, max_offset + sizeof(void*));
}
void*& exception() {
if (_thread_exception_offset < 0) {
static void* _null_exception = nullptr;
return _null_exception;
}
return *(void**) at(_thread_exception_offset);
}
NOADDRSANITIZE VMJavaFrameAnchor* anchor() {
if (!cachedIsJavaThread()) return NULL;
assert(_thread_anchor_offset >= 0);
return VMJavaFrameAnchor::cast(at(_thread_anchor_offset));
}
inline VMMethod* compiledMethod();
DECLARE_END
DECLARE(VMConstMethod)
DECLARE_END
DECLARE(VMMethod)
private:
static bool check_jmethodID_J9(jmethodID id);
static bool check_jmethodID_hotspot(jmethodID id);
public:
jmethodID id();
// Performs extra validation when VMMethod comes from incomplete frame
jmethodID validatedId();
// Workaround for JDK-8313816
static bool isStaleMethodId(jmethodID id) {
if (!_can_dereference_jmethod_id) return false;
VMMethod* vm_method = VMMethod::load_then_cast((const void*)id);
return vm_method == NULL || vm_method->id() == NULL;
}
const char* bytecode() {
assert(_method_constmethod_offset >= 0);
return *(const char**) at(_method_constmethod_offset) + VMConstMethod::type_size();
}
inline VMNMethod* code();
static bool check_jmethodID(jmethodID id);
DECLARE_END
// Inline string comparison to avoid indirect call to strncmp
template<size_t N>
static inline bool startsWith(const char* s, const char (&pattern)[N]) {
for (size_t i = 0; i < N - 1; i++) {
if (s[i] != pattern[i]) return false;
}
return true;
}
DECLARE(VMNMethod)
public:
int size() {
assert(_blob_size_offset >= 0);
return *(int*) at(_blob_size_offset);
}
int frameSize() {
assert(_frame_size_offset >= 0);
return *(int*) at(_frame_size_offset);
}
short frameCompleteOffset() {
assert(_frame_complete_offset >= 0);
return *(short*) at(_frame_complete_offset);
}
void setFrameCompleteOffset(int offset) {
if (_nmethod_immutable_offset > 0) {
// _frame_complete_offset is short on JDK 23+
*(short*) at(_frame_complete_offset) = offset;
} else {
*(int*) at(_frame_complete_offset) = offset;
}
}
const char* immutableDataAt(int offset) {
if (_nmethod_immutable_offset > 0) {
return *(const char**) at(_nmethod_immutable_offset) + offset;
}
return at(offset);
}
const char* code() {
if (_code_offset != -1) { // JDK23+
return at(*(int*) at(_code_offset));
} else {
return *(const char**) at(_code_address);
}
}
const char* scopes() {
if (_scopes_data_offset != -1) { // JDK23+
return immutableDataAt(*(int*) at(_scopes_data_offset));
} else {
return *(const char**) at(_scopes_data_address);
}
}
const void* entry() {
if (_nmethod_entry_offset != -1) { // JDK23+
return at(*(int*) at(_code_offset) + *(unsigned short*) at(_nmethod_entry_offset));
} else {
return *(void**) at(_nmethod_entry_address);
}
}
bool contains(const void* pc) {
return pc >= this && pc < at(size());
}
bool isFrameCompleteAt(const void* pc) {
return pc >= code() + frameCompleteOffset();
}
bool isEntryFrame(const void* pc) {
return pc == _call_stub_return;
}
const char* name() {
assert(_nmethod_name_offset >= 0);
return *(const char**) at(_nmethod_name_offset);
}
bool isInterpreter() {
return this == _interpreter_nm;
}
bool isNMethod() {
const char* n = name();
return n != NULL && (startsWith(n, "nmethod\0") || startsWith(n, "native nmethod\0"));
}
bool isStub() {
const char* n = name();
return n != NULL && startsWith(n, "StubRoutines");
}
bool isVTableStub() {
const char* n = name();
return n != NULL && startsWith(n, "vtable chunks");
}
VMMethod* method() {
assert(_nmethod_method_offset >= 0);
return VMMethod::load_then_cast((const void*)at(_nmethod_method_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() {
if (_mutable_data_offset >= 0) {
// Since JDK 25
assert(_relocation_size_offset >= 0);
return (VMMethod**) (*(char**) at(_mutable_data_offset) + *(int*) at(_relocation_size_offset));
} else if (_data_offset > 0) {
// since JDK 23
assert(_nmethod_metadata_offset >= 0);
assert(_data_offset >= 0);
return (VMMethod**) at(*(int*) at(_data_offset) + *(unsigned short*) at(_nmethod_metadata_offset));
}
assert(_nmethod_metadata_offset >= 0);
return (VMMethod**) at(*(int*) at(_nmethod_metadata_offset));
}
int findScopeOffset(const void* pc);
DECLARE_END
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 VMNMethod* 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 void setInterpreterStart(const void* start) {
_interpreter_start = start;
_interpreter_nm = findNMethod(start);