Skip to content

Commit e3977b0

Browse files
committed
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 <sbabneet@ca.ibm.com>
1 parent b7d59c7 commit e3977b0

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

runtime/vm/UpcallThunkMem.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
extern "C" {
2929

30+
#define ROUND_TO(granularity, number) (((UDATA)(number) + (granularity) - 1) & ~((UDATA)(granularity) - 1))
31+
3032
#if JAVA_SPEC_VERSION >= 16
3133

3234
static void * subAllocateThunkFromHeap(J9UpcallMetaData *data);
@@ -175,6 +177,7 @@ allocateThunkHeap(J9UpcallMetaData *data)
175177
J9HashTable *metaDataHashTable = NULL;
176178
void *allocMemPtr = NULL;
177179
J9Heap *thunkHeap = NULL;
180+
uintptr_t heapSize = pageSize;
178181
J9PortVmemIdentifier vmemID;
179182

180183
Trc_VM_allocateThunkHeap_Entry(thunkSize);
@@ -193,27 +196,35 @@ allocateThunkHeap(J9UpcallMetaData *data)
193196
goto freeAllMemoryThenExit;
194197
}
195198

199+
if (thunkSize > heapSize) {
200+
/* If page size is insufficient to store the thunk, create a heap that is adequately sized
201+
* and aligned to the page size to store the thunk.
202+
*/
203+
heapSize = ROUND_TO(pageSize, thunkSize);
204+
}
205+
196206
/* Reserve a block of memory with the fixed page size for heap creation. */
197-
allocMemPtr = j9vmem_reserve_memory(NULL, pageSize, &vmemID,
198-
J9PORT_VMEM_MEMORY_MODE_READ | J9PORT_VMEM_MEMORY_MODE_WRITE | J9PORT_VMEM_MEMORY_MODE_EXECUTE | J9PORT_VMEM_MEMORY_MODE_COMMIT,
199-
pageSize, J9MEM_CATEGORY_VM_FFI);
207+
allocMemPtr = j9vmem_reserve_memory(
208+
NULL, heapSize, &vmemID,
209+
J9PORT_VMEM_MEMORY_MODE_READ | J9PORT_VMEM_MEMORY_MODE_WRITE | J9PORT_VMEM_MEMORY_MODE_EXECUTE | J9PORT_VMEM_MEMORY_MODE_COMMIT,
210+
pageSize, J9MEM_CATEGORY_VM_FFI);
200211
if (NULL == allocMemPtr) {
201-
Trc_VM_allocateThunkHeap_reserve_memory_failed(pageSize);
212+
Trc_VM_allocateThunkHeap_reserve_memory_failed(heapSize);
202213
goto freeAllMemoryThenExit;
203214
}
204215

205216
omrthread_jit_write_protect_disable();
206217

207218
/* Initialize the allocated memory as a J9Heap. */
208-
thunkHeap = j9heap_create(allocMemPtr, pageSize, 0);
219+
thunkHeap = j9heap_create(allocMemPtr, heapSize, 0);
209220
if (NULL == thunkHeap) {
210-
Trc_VM_allocateThunkHeap_create_heap_failed(allocMemPtr, pageSize);
221+
Trc_VM_allocateThunkHeap_create_heap_failed(allocMemPtr, heapSize);
211222
goto freeAllMemoryThenExit;
212223
}
213224

214225
/* Store the heap handle in J9UpcallThunkHeapWrapper if the thunk heap is successfully created. */
215226
thunkHeapWrapper->heap = thunkHeap;
216-
thunkHeapWrapper->heapSize = pageSize;
227+
thunkHeapWrapper->heapSize = heapSize;
217228
thunkHeapWrapper->vmemID = vmemID;
218229
thunkHeapNode->thunkHeapWrapper = thunkHeapWrapper;
219230

@@ -258,7 +269,7 @@ allocateThunkHeap(J9UpcallMetaData *data)
258269
}
259270
if (NULL != allocMemPtr) {
260271
omrthread_jit_write_protect_enable();
261-
j9vmem_free_memory(allocMemPtr, pageSize, &vmemID);
272+
j9vmem_free_memory(allocMemPtr, heapSize, &vmemID);
262273
allocMemPtr = NULL;
263274
}
264275
goto done;

runtime/vm/j9vm.tdf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,8 @@ TraceExit=Trc_VM_subAllocateThunkFromHeap_Exit NoEnv Overhead=1 Level=3 Template
920920
TraceEntry=Trc_VM_allocateThunkHeap_Entry NoEnv Overhead=1 Level=3 Template="Enter allocateThunkHeap - The requested thunk size(%zu)"
921921
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"
922922
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"
923-
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)"
924-
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)"
923+
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)"
924+
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)"
925925
TraceExit=Trc_VM_allocateThunkHeap_Exit NoEnv Overhead=1 Level=3 Template="Exit allocateThunkHeap - The suballocated thunk heap is %p"
926926

927927
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"

0 commit comments

Comments
 (0)