Skip to content

Commit

Permalink
Merge pull request #20926 from babsingh/main12
Browse files Browse the repository at this point in the history
Create an adequately sized J9Heap to store the UpcallThunk
  • Loading branch information
tajila authored Jan 16, 2025
2 parents 4f82e99 + 6974c9a commit 55155f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
26 changes: 18 additions & 8 deletions runtime/vm/UpcallThunkMem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "j9.h"
#include "j9protos.h"
#include "OMR/Bytes.hpp"
#include "ut_j9vm.h"
#include "vm_internal.h"

Expand Down Expand Up @@ -175,6 +176,7 @@ allocateThunkHeap(J9UpcallMetaData *data)
J9HashTable *metaDataHashTable = NULL;
void *allocMemPtr = NULL;
J9Heap *thunkHeap = NULL;
uintptr_t heapSize = pageSize;
J9PortVmemIdentifier vmemID;

Trc_VM_allocateThunkHeap_Entry(thunkSize);
Expand All @@ -193,27 +195,35 @@ allocateThunkHeap(J9UpcallMetaData *data)
goto freeAllMemoryThenExit;
}

if (thunkSize > heapSize) {
/* If page size is insufficient to store the thunk, create a heap that is adequately sized
* and aligned to the page size to store the thunk.
*/
heapSize = OMR::align((size_t)thunkSize, (size_t)pageSize);
}

/* Reserve a block of memory with the fixed page size for heap creation. */
allocMemPtr = j9vmem_reserve_memory(NULL, pageSize, &vmemID,
J9PORT_VMEM_MEMORY_MODE_READ | J9PORT_VMEM_MEMORY_MODE_WRITE | J9PORT_VMEM_MEMORY_MODE_EXECUTE | J9PORT_VMEM_MEMORY_MODE_COMMIT,
pageSize, J9MEM_CATEGORY_VM_FFI);
allocMemPtr = j9vmem_reserve_memory(
NULL, heapSize, &vmemID,
J9PORT_VMEM_MEMORY_MODE_READ | J9PORT_VMEM_MEMORY_MODE_WRITE | J9PORT_VMEM_MEMORY_MODE_EXECUTE | J9PORT_VMEM_MEMORY_MODE_COMMIT,
pageSize, J9MEM_CATEGORY_VM_FFI);
if (NULL == allocMemPtr) {
Trc_VM_allocateThunkHeap_reserve_memory_failed(pageSize);
Trc_VM_allocateThunkHeap_reserve_memory_failed(heapSize);
goto freeAllMemoryThenExit;
}

omrthread_jit_write_protect_disable();

/* Initialize the allocated memory as a J9Heap. */
thunkHeap = j9heap_create(allocMemPtr, pageSize, 0);
thunkHeap = j9heap_create(allocMemPtr, heapSize, 0);
if (NULL == thunkHeap) {
Trc_VM_allocateThunkHeap_create_heap_failed(allocMemPtr, pageSize);
Trc_VM_allocateThunkHeap_create_heap_failed(allocMemPtr, heapSize);
goto freeAllMemoryThenExit;
}

/* Store the heap handle in J9UpcallThunkHeapWrapper if the thunk heap is successfully created. */
thunkHeapWrapper->heap = thunkHeap;
thunkHeapWrapper->heapSize = pageSize;
thunkHeapWrapper->heapSize = heapSize;
thunkHeapWrapper->vmemID = vmemID;
thunkHeapNode->thunkHeapWrapper = thunkHeapWrapper;

Expand Down Expand Up @@ -258,7 +268,7 @@ allocateThunkHeap(J9UpcallMetaData *data)
}
if (NULL != allocMemPtr) {
omrthread_jit_write_protect_enable();
j9vmem_free_memory(allocMemPtr, pageSize, &vmemID);
j9vmem_free_memory(allocMemPtr, heapSize, &vmemID);
allocMemPtr = NULL;
}
goto done;
Expand Down
4 changes: 2 additions & 2 deletions runtime/vm/j9vm.tdf
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,8 @@ TraceExit=Trc_VM_subAllocateThunkFromHeap_Exit NoEnv Overhead=1 Level=3 Template
TraceEntry=Trc_VM_allocateThunkHeap_Entry NoEnv Overhead=1 Level=3 Template="Enter allocateThunkHeap - The requested thunk size(%zu)"
TraceEvent=Trc_VM_allocateThunkHeap_allocate_thunk_heap_wrapper_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to allocate memory for the thunk heap wrapper"
TraceEvent=Trc_VM_allocateThunkHeap_create_metadata_hash_table_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to create hashtable for the generated thunk and the corresponding metadata in upcall"
TraceEvent=Trc_VM_allocateThunkHeap_reserve_memory_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to allocate reserve the memory with the requested page size(%zu)"
TraceEvent=Trc_VM_allocateThunkHeap_create_heap_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to create a heap from the reserved memory (%p) with the requested page size(%zu)"
TraceEvent=Trc_VM_allocateThunkHeap_reserve_memory_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to allocate reserve the memory with the requested size(%zu)"
TraceEvent=Trc_VM_allocateThunkHeap_create_heap_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to create a heap from the reserved memory (%p) with the requested size(%zu)"
TraceExit=Trc_VM_allocateThunkHeap_Exit NoEnv Overhead=1 Level=3 Template="Exit allocateThunkHeap - The suballocated thunk heap is %p"

TraceEvent=Trc_VM_allocateThunkHeap_allocate_thunk_heap_node_failed NoEnv Overhead=1 Level=3 Template="allocateThunkHeap - Failed to allocate memory for the thunk heap node"
Expand Down

0 comments on commit 55155f7

Please sign in to comment.