diff --git a/jcl/src/java.base/share/classes/openj9/internal/foreign/abi/InternalUpcallHandler.java b/jcl/src/java.base/share/classes/openj9/internal/foreign/abi/InternalUpcallHandler.java index f085de39dd5..264b5f53497 100644 --- a/jcl/src/java.base/share/classes/openj9/internal/foreign/abi/InternalUpcallHandler.java +++ b/jcl/src/java.base/share/classes/openj9/internal/foreign/abi/InternalUpcallHandler.java @@ -144,7 +144,7 @@ private long getUpcallThunkAddr(MethodHandle target, ResourceScope sessionOrScop * is only alive for a MemorySession(JDK19)/ResourceScope(JDK17/18) specified in java, which means the upcall handler * and its UpcallMHMetaData object will be cleaned up automatically once their session/scope is closed. */ - metaData = new UpcallMHMetaData(target, sessionOrScope); + metaData = new UpcallMHMetaData(target, argLayoutCount, sessionOrScope); return allocateUpcallStub(metaData, nativeSignatureStrs); } diff --git a/jcl/src/java.base/share/classes/openj9/internal/foreign/abi/UpcallMHMetaData.java b/jcl/src/java.base/share/classes/openj9/internal/foreign/abi/UpcallMHMetaData.java index b75a11cfea8..8fc4d0c86d5 100644 --- a/jcl/src/java.base/share/classes/openj9/internal/foreign/abi/UpcallMHMetaData.java +++ b/jcl/src/java.base/share/classes/openj9/internal/foreign/abi/UpcallMHMetaData.java @@ -50,6 +50,11 @@ final class UpcallMHMetaData { * by MethodHandleResolver.upcallLinkCallerMethod(). */ private Object[] invokeCache; + /* The argument array stores the memory specific argument(struct/pointer) object + * being allocated in native for upcall to stop GC from updating the previously + * allocated argument reference when allocating the next argument. + */ + private Object[] nativeArgArray; /*[IF JAVA_SPEC_VERSION >= 19]*/ private MemorySession session; @@ -68,21 +73,22 @@ final class UpcallMHMetaData { } /*[IF JAVA_SPEC_VERSION >= 19]*/ - UpcallMHMetaData(MethodHandle targetHandle, MemorySession session) + UpcallMHMetaData(MethodHandle targetHandle, int nativeArgCount, MemorySession session) /*[ELSE] JAVA_SPEC_VERSION >= 19 */ - UpcallMHMetaData(MethodHandle targetHandle, ResourceScope scope) + UpcallMHMetaData(MethodHandle targetHandle, int nativeArgCount, ResourceScope scope) /*[ENDIF] JAVA_SPEC_VERSION >= 19 */ { calleeMH = targetHandle; calleeType = targetHandle.type(); + nativeArgArray = new Object[nativeArgCount]; /* Only hold the confined session/scope (owned by the current thread) - * will be used to construct a MemorySegment object for argument in - * the native dispatcher in upcall. + * or the shared session/scope will be used to construct a MemorySegment + * object for argument in the native dispatcher in upcall. */ /*[IF JAVA_SPEC_VERSION >= 19]*/ - this.session = ((session != null) && (session.ownerThread() != null)) ? session : null; + this.session = ((session != null) && (session.ownerThread() != null)) ? session : MemorySession.openShared(); /*[ELSE] JAVA_SPEC_VERSION >= 19 */ - this.scope = ((scope != null) && (scope.ownerThread() != null)) ? scope : null;; + this.scope = ((scope != null) && (scope.ownerThread() != null)) ? scope : ResourceScope.newSharedScope(); /*[ENDIF] JAVA_SPEC_VERSION >= 19 */ } } diff --git a/runtime/oti/vmconstantpool.xml b/runtime/oti/vmconstantpool.xml index c946328511b..6c2c886b670 100644 --- a/runtime/oti/vmconstantpool.xml +++ b/runtime/oti/vmconstantpool.xml @@ -424,6 +424,8 @@ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-excepti