Skip to content

Commit

Permalink
Create an adequately sized J9Heap to store the UpcallThunk
Browse files Browse the repository at this point in the history
Currently, the J9Heap size to store the UpcallThunk is restricted to
the page size. If the thunk is greater than the page size, then the
existing code ends in an infinite loop creating J9Heaps of page size.
The correct sized J9Heap will never be created so the code will never
progress.

To resolve this issue, an adequately sized J9Heap, aligned to the page
size, is created to store the UpcallThunk.

Signed-off-by: Babneet Singh <[email protected]>
  • Loading branch information
babsingh committed Jan 15, 2025
1 parent b7d59c7 commit 6974c9a
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 6974c9a

Please sign in to comment.