From 6974c9ac9effee9c265b403cae7ad3b4b8d4aa4c Mon Sep 17 00:00:00 2001 From: Babneet Singh Date: Tue, 7 Jan 2025 11:19:53 -0500 Subject: [PATCH] Create an adequately sized J9Heap to store the UpcallThunk 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 --- runtime/vm/UpcallThunkMem.cpp | 26 ++++++++++++++++++-------- runtime/vm/j9vm.tdf | 4 ++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/runtime/vm/UpcallThunkMem.cpp b/runtime/vm/UpcallThunkMem.cpp index 158ee2c9e87..e7cf306535a 100644 --- a/runtime/vm/UpcallThunkMem.cpp +++ b/runtime/vm/UpcallThunkMem.cpp @@ -22,6 +22,7 @@ #include "j9.h" #include "j9protos.h" +#include "OMR/Bytes.hpp" #include "ut_j9vm.h" #include "vm_internal.h" @@ -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); @@ -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; @@ -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; diff --git a/runtime/vm/j9vm.tdf b/runtime/vm/j9vm.tdf index 9cfe25c5e6b..3aa0023b5f7 100644 --- a/runtime/vm/j9vm.tdf +++ b/runtime/vm/j9vm.tdf @@ -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"