Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support heap memory for ffi CaptureCallState #21197

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ void clear() {
private static synchronized native void resolveRequiredFields();
private native void initCifNativeThunkData(String[] argLayouts, String retLayout, boolean newArgTypes, int varArgIndex);
private native long invokeNative(
/*[IF JAVA_SPEC_VERSION >= 24]*/
Object returnStateMemBase,
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
/*[IF JAVA_SPEC_VERSION >= 22]*/
Object[] bases,
long[] offsets,
Expand Down Expand Up @@ -887,17 +890,41 @@ Object runNativeMethod(Addressable downcallAddr, SegmentAllocator segmtAllocator
* Note: memArgScopeSet is not empty with the downcall address added to the set.
*/
/*[IF JAVA_SPEC_VERSION >= 21]*/
/*[IF JAVA_SPEC_VERSION >= 24]*/
long returnStateMemAddr;
Object returnStateMemBase;
if (linkerOpts.hasCapturedCallState() && !stateSegmt.isNative()) {
/* The CaptureCallState option can only use heap memory if allowed by the Critical option. */
if (!linkerOpts.isCritical()) {
throw new IllegalArgumentException("Heap segment not allowed");
}
AbstractMemorySegmentImpl segment = (AbstractMemorySegmentImpl)stateSegmt;
returnStateMemAddr = segment.unsafeGetOffset();
returnStateMemBase = segment.unsafeGetBase();
} else {
returnStateMemAddr = getValidDowncallMemAddr(stateSegmt);
returnStateMemBase = null;
}
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */

try (Arena arena = Arena.ofConfined()) {
SetDependency(arena.scope());
returnVal = invokeNative(
/*[IF JAVA_SPEC_VERSION >= 24]*/
returnStateMemBase,
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
/*[IF JAVA_SPEC_VERSION >= 22]*/
(info != null) ? info.bases : null,
(info != null) ? info.offsets : null,
linkerOpts.isCritical(),
/*[ELSE] JAVA_SPEC_VERSION >= 22 */
linkerOpts.isTrivial(),
/*[ENDIF] JAVA_SPEC_VERSION >= 22 */
/*[IF JAVA_SPEC_VERSION >= 24]*/
returnStateMemAddr,
/*[ELSE] JAVA_SPEC_VERSION >= 24 */
getValidDowncallMemAddr(stateSegmt),
/*[ENDIF] JAVA_SPEC_VERSION >= 24 */
retMemAddr,
getValidDowncallMemAddr(downcallAddr),
cifNativeThunkAddr,
Expand Down
26 changes: 23 additions & 3 deletions runtime/vm/BytecodeInterpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5185,7 +5185,11 @@ class INTERPRETER_CLASS
}

#if JAVA_SPEC_VERSION >= 16
#if JAVA_SPEC_VERSION >= 22
#if JAVA_SPEC_VERSION >= 24
/* openj9.internal.foreign.abi.InternalDowncallHandler:
* private native long invokeNative(Object returnStateMemBase, Object[] bases, long[] offsets, boolean isInCriticalDownCall, long returnStateMemAddr, long returnStructMemAddr, long functionAddr, long calloutThunk, long[] argValues);
*/
#elif JAVA_SPEC_VERSION >= 22
/* openj9.internal.foreign.abi.InternalDowncallHandler:
* private native long invokeNative(Object[] bases, long[] offsets, boolean isInCriticalDownCall, long returnStateMemAddr, long returnStructMemAddr, long functionAddr, long calloutThunk, long[] argValues);
*/
Expand Down Expand Up @@ -5227,7 +5231,13 @@ class INTERPRETER_CLASS
U_64 *ffiArgs = _currentThread->ffiArgs;
U_64 sFfiArgs[16];
#if JAVA_SPEC_VERSION >= 22
#if JAVA_SPEC_VERSION >= 24
UDATA argSlots = 14;
UDATA returnStateMemAddr;
j9object_t returnStateMemBase = NULL;
#else /* JAVA_SPEC_VERSION >= 24 */
UDATA argSlots = 13;
#endif /* JAVA_SPEC_VERSION >= 24 */
I_32 *returnState = NULL;
UDATA curPtrArgIdx = 0;
j9object_t heapBase = NULL;
Expand Down Expand Up @@ -5258,9 +5268,19 @@ class INTERPRETER_CLASS

#if JAVA_SPEC_VERSION >= 21
/* The native memory is allocated at java level to save the execution state after performing the downcall. */
#if JAVA_SPEC_VERSION >= 24
returnStateMemAddr = (UDATA)*(I_64 *)(_sp + 7); /* returnStateMemAddr */
returnStateMemBase = *(j9object_t *)(_sp + 12); /* returnStateMemBase */
if (NULL != returnStateMemBase) {
returnState = (I_32 *)((UDATA)returnStateMemBase + returnStateMemAddr);
} else {
returnState = (I_32 *)returnStateMemAddr;
}
#else /* JAVA_SPEC_VERSION >= 24 */
returnState = (I_32 *)(UDATA)*(I_64 *)(_sp + 7); /* returnStateMemAddr */
#endif /* JAVA_SPEC_VERSION >= 24 */

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

Expand Down Expand Up @@ -5465,7 +5485,7 @@ class INTERPRETER_CLASS

done:
#if JAVA_SPEC_VERSION >= 21
/* Clear the trivial downcall flag. */
/* Clear the critical downcall flag. */
_currentThread->isInCriticalDownCall = FALSE;
#endif /* JAVA_SPEC_VERSION >= 21 */

Expand Down
4 changes: 3 additions & 1 deletion runtime/vm/bindnatv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ static inlMapping mappings[] = {
{ "Java_sun_reflect_Reflection_getClassAccessFlags__Ljava_lang_Class_2", J9_BCLOOP_SEND_TARGET_INL_REFLECTION_GETCLASSACCESSFLAGS },
#endif /* JAVA_SPEC_VERSION >= 11 */

#if JAVA_SPEC_VERSION >= 22
#if JAVA_SPEC_VERSION >= 24
{ "Java_openj9_internal_foreign_abi_InternalDowncallHandler_invokeNative__Ljava_lang_Object_2_3Ljava_lang_Object_2_3JZJJJJ_3J", J9_BCLOOP_SEND_TARGET_INL_INTERNALDOWNCALLHANDLER_INVOKENATIVE },
#elif JAVA_SPEC_VERSION >= 22
{ "Java_openj9_internal_foreign_abi_InternalDowncallHandler_invokeNative___3Ljava_lang_Object_2_3JZJJJJ_3J", J9_BCLOOP_SEND_TARGET_INL_INTERNALDOWNCALLHANDLER_INVOKENATIVE },
#elif JAVA_SPEC_VERSION == 21
{ "Java_openj9_internal_foreign_abi_InternalDowncallHandler_invokeNative__ZJJJJ_3J", J9_BCLOOP_SEND_TARGET_INL_INTERNALDOWNCALLHANDLER_INVOKENATIVE },
Expand Down