Skip to content

Commit 47e00d8

Browse files
authored
Merge pull request eclipse-openj9#21197 from theresa-m/fix_ffi
Support heap memory for ffi CaptureCallState
2 parents eb5b49d + 878b3c4 commit 47e00d8

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

jcl/src/java.base/share/classes/openj9/internal/foreign/abi/InternalDowncallHandler.java

+27
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ void clear() {
189189
private static synchronized native void resolveRequiredFields();
190190
private native void initCifNativeThunkData(String[] argLayouts, String retLayout, boolean newArgTypes, int varArgIndex);
191191
private native long invokeNative(
192+
/*[IF JAVA_SPEC_VERSION >= 24]*/
193+
Object returnStateMemBase,
194+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
192195
/*[IF JAVA_SPEC_VERSION >= 22]*/
193196
Object[] bases,
194197
long[] offsets,
@@ -887,17 +890,41 @@ Object runNativeMethod(Addressable downcallAddr, SegmentAllocator segmtAllocator
887890
* Note: memArgScopeSet is not empty with the downcall address added to the set.
888891
*/
889892
/*[IF JAVA_SPEC_VERSION >= 21]*/
893+
/*[IF JAVA_SPEC_VERSION >= 24]*/
894+
long returnStateMemAddr;
895+
Object returnStateMemBase;
896+
if (linkerOpts.hasCapturedCallState() && !stateSegmt.isNative()) {
897+
/* The CaptureCallState option can only use heap memory if allowed by the Critical option. */
898+
if (!linkerOpts.isCritical()) {
899+
throw new IllegalArgumentException("Heap segment not allowed");
900+
}
901+
AbstractMemorySegmentImpl segment = (AbstractMemorySegmentImpl)stateSegmt;
902+
returnStateMemAddr = segment.unsafeGetOffset();
903+
returnStateMemBase = segment.unsafeGetBase();
904+
} else {
905+
returnStateMemAddr = getValidDowncallMemAddr(stateSegmt);
906+
returnStateMemBase = null;
907+
}
908+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
909+
890910
try (Arena arena = Arena.ofConfined()) {
891911
SetDependency(arena.scope());
892912
returnVal = invokeNative(
913+
/*[IF JAVA_SPEC_VERSION >= 24]*/
914+
returnStateMemBase,
915+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
893916
/*[IF JAVA_SPEC_VERSION >= 22]*/
894917
(info != null) ? info.bases : null,
895918
(info != null) ? info.offsets : null,
896919
linkerOpts.isCritical(),
897920
/*[ELSE] JAVA_SPEC_VERSION >= 22 */
898921
linkerOpts.isTrivial(),
899922
/*[ENDIF] JAVA_SPEC_VERSION >= 22 */
923+
/*[IF JAVA_SPEC_VERSION >= 24]*/
924+
returnStateMemAddr,
925+
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
900926
getValidDowncallMemAddr(stateSegmt),
927+
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
901928
retMemAddr,
902929
getValidDowncallMemAddr(downcallAddr),
903930
cifNativeThunkAddr,

runtime/vm/BytecodeInterpreter.hpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -5185,7 +5185,11 @@ class INTERPRETER_CLASS
51855185
}
51865186

51875187
#if JAVA_SPEC_VERSION >= 16
5188-
#if JAVA_SPEC_VERSION >= 22
5188+
#if JAVA_SPEC_VERSION >= 24
5189+
/* openj9.internal.foreign.abi.InternalDowncallHandler:
5190+
* private native long invokeNative(Object returnStateMemBase, Object[] bases, long[] offsets, boolean isInCriticalDownCall, long returnStateMemAddr, long returnStructMemAddr, long functionAddr, long calloutThunk, long[] argValues);
5191+
*/
5192+
#elif JAVA_SPEC_VERSION >= 22
51895193
/* openj9.internal.foreign.abi.InternalDowncallHandler:
51905194
* private native long invokeNative(Object[] bases, long[] offsets, boolean isInCriticalDownCall, long returnStateMemAddr, long returnStructMemAddr, long functionAddr, long calloutThunk, long[] argValues);
51915195
*/
@@ -5227,7 +5231,13 @@ class INTERPRETER_CLASS
52275231
U_64 *ffiArgs = _currentThread->ffiArgs;
52285232
U_64 sFfiArgs[16];
52295233
#if JAVA_SPEC_VERSION >= 22
5234+
#if JAVA_SPEC_VERSION >= 24
5235+
UDATA argSlots = 14;
5236+
UDATA returnStateMemAddr;
5237+
j9object_t returnStateMemBase = NULL;
5238+
#else /* JAVA_SPEC_VERSION >= 24 */
52305239
UDATA argSlots = 13;
5240+
#endif /* JAVA_SPEC_VERSION >= 24 */
52315241
I_32 *returnState = NULL;
52325242
UDATA curPtrArgIdx = 0;
52335243
j9object_t heapBase = NULL;
@@ -5258,9 +5268,19 @@ class INTERPRETER_CLASS
52585268

52595269
#if JAVA_SPEC_VERSION >= 21
52605270
/* The native memory is allocated at java level to save the execution state after performing the downcall. */
5271+
#if JAVA_SPEC_VERSION >= 24
5272+
returnStateMemAddr = (UDATA)*(I_64 *)(_sp + 7); /* returnStateMemAddr */
5273+
returnStateMemBase = *(j9object_t *)(_sp + 12); /* returnStateMemBase */
5274+
if (NULL != returnStateMemBase) {
5275+
returnState = (I_32 *)((UDATA)returnStateMemBase + returnStateMemAddr);
5276+
} else {
5277+
returnState = (I_32 *)returnStateMemAddr;
5278+
}
5279+
#else /* JAVA_SPEC_VERSION >= 24 */
52615280
returnState = (I_32 *)(UDATA)*(I_64 *)(_sp + 7); /* returnStateMemAddr */
5281+
#endif /* JAVA_SPEC_VERSION >= 24 */
52625282

5263-
/* Set the linker option to the current thread for the trivial downcall. */
5283+
/* Set the linker option to the current thread for the critical downcall. */
52645284
_currentThread->isInCriticalDownCall = (0 == *(U_32*)(_sp + 9)) ? FALSE : TRUE;
52655285
#endif /* JAVA_SPEC_VERSION >= 21 */
52665286

@@ -5465,7 +5485,7 @@ class INTERPRETER_CLASS
54655485

54665486
done:
54675487
#if JAVA_SPEC_VERSION >= 21
5468-
/* Clear the trivial downcall flag. */
5488+
/* Clear the critical downcall flag. */
54695489
_currentThread->isInCriticalDownCall = FALSE;
54705490
#endif /* JAVA_SPEC_VERSION >= 21 */
54715491

runtime/vm/bindnatv.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ static inlMapping mappings[] = {
301301
{ "Java_sun_reflect_Reflection_getClassAccessFlags__Ljava_lang_Class_2", J9_BCLOOP_SEND_TARGET_INL_REFLECTION_GETCLASSACCESSFLAGS },
302302
#endif /* JAVA_SPEC_VERSION >= 11 */
303303

304-
#if JAVA_SPEC_VERSION >= 22
304+
#if JAVA_SPEC_VERSION >= 24
305+
{ "Java_openj9_internal_foreign_abi_InternalDowncallHandler_invokeNative__Ljava_lang_Object_2_3Ljava_lang_Object_2_3JZJJJJ_3J", J9_BCLOOP_SEND_TARGET_INL_INTERNALDOWNCALLHANDLER_INVOKENATIVE },
306+
#elif JAVA_SPEC_VERSION >= 22
305307
{ "Java_openj9_internal_foreign_abi_InternalDowncallHandler_invokeNative___3Ljava_lang_Object_2_3JZJJJJ_3J", J9_BCLOOP_SEND_TARGET_INL_INTERNALDOWNCALLHANDLER_INVOKENATIVE },
306308
#elif JAVA_SPEC_VERSION == 21
307309
{ "Java_openj9_internal_foreign_abi_InternalDowncallHandler_invokeNative__ZJJJJ_3J", J9_BCLOOP_SEND_TARGET_INL_INTERNALDOWNCALLHANDLER_INVOKENATIVE },

0 commit comments

Comments
 (0)