Skip to content

Commit

Permalink
#923: Adjust VMStructs for JDK 23
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed May 13, 2024
1 parent ed7d848 commit 048cafa
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
60 changes: 36 additions & 24 deletions src/vmStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ int VMStructs::_anchor_pc_offset = -1;
int VMStructs::_anchor_fp_offset = -1;
int VMStructs::_frame_size_offset = -1;
int VMStructs::_frame_complete_offset = -1;
int VMStructs::_code_begin_offset = -1;
int VMStructs::_scopes_begin_offset = -1;
int VMStructs::_code_offset = -1;
int VMStructs::_data_offset = -1;
int VMStructs::_scopes_pcs_offset = -1;
int VMStructs::_scopes_data_offset = -1;
int VMStructs::_nmethod_name_offset = -1;
int VMStructs::_nmethod_method_offset = -1;
int VMStructs::_nmethod_entry_offset = -1;
int VMStructs::_nmethod_state_offset = -1;
int VMStructs::_nmethod_level_offset = -1;
int VMStructs::_nmethod_metadata_offset = -1;
int VMStructs::_nmethod_pcs_begin_offset = -1;
int VMStructs::_nmethod_pcs_end_offset = -1;
int VMStructs::_nmethod_immutable_offset = -1;
int VMStructs::_method_constmethod_offset = -1;
int VMStructs::_method_code_offset = -1;
int VMStructs::_constmethod_constants_offset = -1;
Expand All @@ -66,6 +67,7 @@ int VMStructs::_array_data_offset = -1;
int VMStructs::_code_heap_memory_offset = -1;
int VMStructs::_code_heap_segmap_offset = -1;
int VMStructs::_code_heap_segment_shift = -1;
int VMStructs::_heap_block_used_offset = -1;
int VMStructs::_vs_low_bound_offset = -1;
int VMStructs::_vs_high_bound_offset = -1;
int VMStructs::_vs_low_offset = -1;
Expand Down Expand Up @@ -192,22 +194,24 @@ void VMStructs::initOffsets() {
} else if (strcmp(type, "CompiledMethod") == 0 || strcmp(type, "nmethod") == 0) {
if (strcmp(field, "_method") == 0) {
_nmethod_method_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_verified_entry_point") == 0) {
} else if (strcmp(field, "_verified_entry_offset") == 0) {
_nmethod_entry_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_verified_entry_point") == 0) {
_nmethod_entry_offset = - *(int*)(entry + offset_offset);
} else if (strcmp(field, "_state") == 0) {
_nmethod_state_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_comp_level") == 0) {
_nmethod_level_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_metadata_offset") == 0) {
_nmethod_metadata_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_immutable_data") == 0) {
_nmethod_immutable_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_scopes_pcs_offset") == 0) {
_nmethod_pcs_begin_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_dependencies_offset") == 0) {
_nmethod_pcs_end_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_scopes_data_begin") == 0) {
_scopes_begin_offset = *(int*)(entry + offset_offset);
_scopes_pcs_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_scopes_data_offset") == 0) {
_scopes_begin_offset = - *(int*)(entry + offset_offset);
_scopes_data_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_scopes_data_begin") == 0) {
_scopes_data_offset = - *(int*)(entry + offset_offset);
}
} else if (strcmp(type, "Method") == 0) {
if (strcmp(field, "_constMethod") == 0) {
Expand Down Expand Up @@ -284,10 +288,12 @@ void VMStructs::initOffsets() {
_frame_size_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_frame_complete_offset") == 0) {
_frame_complete_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_code_begin") == 0) {
_code_begin_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_code_offset") == 0) {
_code_begin_offset = - *(int*)(entry + offset_offset);
_code_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_code_begin") == 0) {
_code_offset = - *(int*)(entry + offset_offset);
} else if (strcmp(field, "_data_offset") == 0) {
_data_offset = *(int*)(entry + offset_offset);
} else if (strcmp(field, "_name") == 0) {
_nmethod_name_offset = *(int*)(entry + offset_offset);
}
Expand All @@ -309,6 +315,10 @@ void VMStructs::initOffsets() {
} else if (strcmp(field, "_log2_segment_size") == 0) {
_code_heap_segment_shift = *(int*)(entry + offset_offset);
}
} else if (strcmp(type, "HeapBlock::Header") == 0) {
if (strcmp(field, "_used") == 0) {
_heap_block_used_offset = *(int*)(entry + offset_offset);
}
} else if (strcmp(type, "VirtualSpace") == 0) {
if (strcmp(field, "_low_boundary") == 0) {
_vs_low_bound_offset = *(int*)(entry + offset_offset);
Expand Down Expand Up @@ -419,7 +429,7 @@ void VMStructs::resolveOffsets() {

_has_method_structs = _jmethod_ids_offset >= 0
&& _nmethod_method_offset >= 0
&& _nmethod_entry_offset >= 0
&& _nmethod_entry_offset != -1
&& _nmethod_state_offset >= 0
&& _method_constmethod_offset >= 0
&& _method_code_offset >= 0
Expand Down Expand Up @@ -455,11 +465,11 @@ void VMStructs::resolveOffsets() {

_has_stack_structs = _has_method_structs
&& _interpreter_frame_bcp_offset != 0
&& _code_begin_offset != -1
&& _scopes_begin_offset != -1
&& _code_offset != -1
&& _scopes_data_offset != -1
&& _scopes_pcs_offset >= 0
&& _nmethod_immutable_offset < 0 // TODO: not yet supported
&& _nmethod_metadata_offset >= 0
&& _nmethod_pcs_begin_offset >= 0
&& _nmethod_pcs_end_offset >= 0
&& _thread_vframe_offset >= 0
&& _thread_exception_offset >= 0
&& _constmethod_size >= 0;
Expand All @@ -484,7 +494,8 @@ void VMStructs::resolveOffsets() {
_code_heap_segment_shift = *(int*)(_code_heap[0] + _code_heap_segment_shift);
}
if (_code_heap_memory_offset < 0 || _code_heap_segmap_offset < 0 ||
_code_heap_segment_shift < 0 || _code_heap_segment_shift > 16) {
_code_heap_segment_shift < 0 || _code_heap_segment_shift > 16 ||
_heap_block_used_offset < 0) {
memset(_code_heap, 0, sizeof(_code_heap));
}

Expand Down Expand Up @@ -643,8 +654,8 @@ NMethod* CodeHeap::findNMethod(char* heap, const void* pc) {
idx -= segmap[idx];
}

unsigned char* block = heap_start + (idx << _code_heap_segment_shift);
return block[sizeof(size_t)] ? (NMethod*)(block + 2 * sizeof(size_t)) : NULL;
unsigned char* block = heap_start + (idx << _code_heap_segment_shift) + _heap_block_used_offset;
return *block ? align<NMethod*>(block + sizeof(uintptr_t)) : NULL;
}

JVMFlag* JVMFlag::find(const char* name) {
Expand All @@ -665,8 +676,9 @@ int NMethod::findScopeOffset(const void* pc) {
return -1;
}

PcDesc* pcd = (PcDesc*) at(*(int*) at(_nmethod_pcs_begin_offset));
PcDesc* pcd_end = (PcDesc*) at(*(int*) at(_nmethod_pcs_end_offset));
const int* scopes_pcs = (const int*) at(_scopes_pcs_offset);
PcDesc* pcd = (PcDesc*) at(scopes_pcs[0]);
PcDesc* pcd_end = (PcDesc*) at(scopes_pcs[1]);
int low = 0;
int high = (pcd_end - pcd) - 1;

Expand Down
44 changes: 28 additions & 16 deletions src/vmStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ class VMStructs {
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 _code_offset;
static int _data_offset;
static int _scopes_pcs_offset;
static int _scopes_data_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 _nmethod_immutable_offset;
static int _method_constmethod_offset;
static int _method_code_offset;
static int _constmethod_constants_offset;
Expand All @@ -71,6 +72,7 @@ class VMStructs {
static int _code_heap_memory_offset;
static int _code_heap_segmap_offset;
static int _code_heap_segment_shift;
static int _heap_block_used_offset;
static int _vs_low_bound_offset;
static int _vs_high_bound_offset;
static int _vs_low_offset;
Expand Down Expand Up @@ -133,6 +135,11 @@ class VMStructs {
return (uintptr_t)ptr >= 0x1000 && ((uintptr_t)ptr & (sizeof(uintptr_t) - 1)) == 0;
}

template<typename T>
static T align(const void* ptr) {
return (T)((uintptr_t)ptr & ~(sizeof(T) - 1));
}

public:
static void init(CodeCache* libjvm);
static void ready();
Expand Down Expand Up @@ -385,27 +392,36 @@ class NMethod : VMStructs {
return *(int*) at(_frame_size_offset);
}

int frameCompleteOffset() {
return *(int*) at(_frame_complete_offset);
short frameCompleteOffset() {
return *(short*) at(_frame_complete_offset);
}

// TODO: offset is short on JDK 23+
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);
if (_code_offset > 0) {
return at(*(int*) at(_code_offset));
} else {
return at(*(int*) at(-_code_begin_offset));
return *(const char**) at(-_code_offset);
}
}

const char* scopes() {
if (_scopes_begin_offset >= 0) {
return *(const char**) at(_scopes_begin_offset);
if (_scopes_data_offset > 0) {
return at(*(int*) at(_scopes_data_offset));
} else {
return *(const char**) at(-_scopes_data_offset);
}
}

const void* entry() {
if (_nmethod_entry_offset > 0) {
return at(*(int*) at(_code_offset) + *(unsigned short*) at(_nmethod_entry_offset));
} else {
return at(*(int*) at(-_scopes_begin_offset));
return *(void**) at(-_nmethod_entry_offset);
}
}

Expand Down Expand Up @@ -435,10 +451,6 @@ class NMethod : VMStructs {
return *(VMMethod**) at(_nmethod_method_offset);
}

void* entry() {
return *(void**) at(_nmethod_entry_offset);
}

char state() {
return *at(_nmethod_state_offset);
}
Expand Down

0 comments on commit 048cafa

Please sign in to comment.