Skip to content

Commit

Permalink
Merge pull request #17087 from ChengJin01/ffi_jtreg_fix_return_pointe…
Browse files Browse the repository at this point in the history
…r_for_memsegmt_jdk20

[FFI/JDK20_Jtreg] Fix the issue with the arguments/return value
  • Loading branch information
tajila committed Apr 25, 2023
2 parents f82ffb6 + 6f50368 commit ab8319b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Expand Up @@ -64,8 +64,8 @@
import java.lang.foreign.ValueLayout;
import java.lang.foreign.VaList;
/*[IF JAVA_SPEC_VERSION >= 20]*/
import jdk.internal.foreign.Utils;
import jdk.internal.foreign.abi.LinkerOptions;
import jdk.internal.foreign.abi.SharedUtils;
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
import jdk.internal.foreign.MemorySessionImpl;
/*[ELSE] JAVA_SPEC_VERSION >= 19 */
Expand Down Expand Up @@ -161,7 +161,7 @@ public class InternalDowncallHandler {
private static final MethodHandle longObjToFloatRetFilter;
private static final MethodHandle longObjToDoubleRetFilter;
/*[IF JAVA_SPEC_VERSION >= 20]*/
private static final MethodHandle longObjToMemSegmtRetFilter;
private MethodHandle longObjToMemSegmtRetFilter;
/*[ELSE] JAVA_SPEC_VERSION >= 20 */
private static final MethodHandle longObjToMemAddrRetFilter;
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
Expand Down Expand Up @@ -201,11 +201,9 @@ private static final class PrivateClassLock {
longObjToLongRetFilter = lookup.findStatic(InternalDowncallHandler.class, "longObjToLongRet", methodType(long.class, Object.class)); //$NON-NLS-1$
longObjToFloatRetFilter = lookup.findStatic(InternalDowncallHandler.class, "longObjToFloatRet", methodType(float.class, Object.class)); //$NON-NLS-1$
longObjToDoubleRetFilter = lookup.findStatic(InternalDowncallHandler.class, "longObjToDoubleRet", methodType(double.class, Object.class)); //$NON-NLS-1$
/*[IF JAVA_SPEC_VERSION >= 20]*/
longObjToMemSegmtRetFilter = lookup.findStatic(InternalDowncallHandler.class, "longObjToMemSegmtRet", methodType(MemorySegment.class, Object.class)); //$NON-NLS-1$
/*[ELSE] JAVA_SPEC_VERSION >= 20 */
/*[IF JAVA_SPEC_VERSION <= 19]*/
longObjToMemAddrRetFilter = lookup.findStatic(InternalDowncallHandler.class, "longObjToMemAddrRet", methodType(MemoryAddress.class, Object.class)); //$NON-NLS-1$
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
/*[ENDIF] JAVA_SPEC_VERSION <= 19 */
objToMemSegmtRetFilter = lookup.findStatic(InternalDowncallHandler.class, "objToMemSegmtRet", methodType(MemorySegment.class, Object.class)); //$NON-NLS-1$
} catch (IllegalAccessException | NoSuchMethodException e) {
throw new InternalError(e);
Expand Down Expand Up @@ -400,9 +398,14 @@ private static final double longObjToDoubleRet(Object retValue) {

/*[IF JAVA_SPEC_VERSION >= 20]*/
/* Intended for longObjToMemSegmtRetFilter that converts the Long object to the memory segment. */
private static final MemorySegment longObjToMemSegmtRet(Object retValue) {
private MemorySegment longObjToMemSegmtRet(Object retValue) {
long tmpValue = ((Long)retValue).longValue();
return MemorySegment.ofAddress(tmpValue);
/* Utils.pointeeSize() introduced in JDK20 calls isUnbounded() for the ADDRESS layout
* to determine whether the specified address layout is an unbounded address or not.
* For an unbounded address, it returns Long.MAX_VALUE for direct access; otherwise,
* it returns zero in which case the address can't be directly accessed.
*/
return MemorySegment.ofAddress(tmpValue, Utils.pointeeSize(realReturnLayout));
}
/*[ELSE] JAVA_SPEC_VERSION >= 20 */
/* Intended for longObjToMemAddrRetFilter that converts the Long object to the memory address. */
Expand Down Expand Up @@ -495,9 +498,11 @@ public InternalDowncallHandler(Addressable downcallAddr, MethodType functionMeth
/*[ENDIF] JAVA_SPEC_VERSION == 17 */

try {
/*[IF JAVA_SPEC_VERSION <= 19]*/
/*[IF JAVA_SPEC_VERSION >= 20]*/
longObjToMemSegmtRetFilter = lookup.bind(this, "longObjToMemSegmtRet", methodType(MemorySegment.class, Object.class)); //$NON-NLS-1$
/*[ELSE] JAVA_SPEC_VERSION >= 20 */
memAddrToLongArgFilter = lookup.bind(this, "memAddrToLongArg", methodType(long.class, addrClass)); //$NON-NLS-1$
/*[ENDIF] JAVA_SPEC_VERSION <= 19 */
/*[ENDIF] JAVA_SPEC_VERSION >= 20 */
memSegmtToLongArgFilter = lookup.bind(this, "memSegmtToLongArg", methodType(long.class, MemorySegment.class)); //$NON-NLS-1$
} catch (ReflectiveOperationException e) {
throw new InternalError(e);
Expand Down
13 changes: 6 additions & 7 deletions runtime/vm/UpcallVMHelpers.cpp
Expand Up @@ -565,11 +565,10 @@ storeMemArgObjectsToJavaArray(J9UpcallMetaData *data, void *argsListPointer, J9V
if (J9_FFI_UPCALL_SIG_TYPE_POINTER == argSigType) {
I_64 offset = *(I_64*)getArgPointer(nativeSig, argsListPointer, argIndex);
#if JAVA_SPEC_VERSION >= 20
/* A pointer argument is wrapped as a zero-sized memory segment given all
* MemoryAdress related classes are removed against the latest APIs as
* specified in JDK20+.
/* A pointer argument is wrapped as an unbounded memory segment in upcall
* to pass the access check on the boundary as specified in JDK20+.
*/
memArgObject = createMemSegmentObject(data, offset, 0);
memArgObject = createMemSegmentObject(data, offset, (U_32)LONG_MAX);
#else /* JAVA_SPEC_VERSION => 20 */
memArgObject = createMemAddressObject(data, offset);
#endif /* JAVA_SPEC_VERSION => 20 */
Expand Down Expand Up @@ -650,11 +649,11 @@ createMemAddressObject(J9UpcallMetaData *data, I_64 offset)

/**
* @brief Generate an object of the MemorySegment's subclass on the heap with the specified
* native address to the requested struct or pointer(JDK20+).
* native address to the requested struct or the unbounded pointer(JDK20+).
*
* @param data a pointer to J9UpcallMetaData
* @param offset the native address to the requested struct OR pointer(JDK20+)
* @param sigTypeSize the byte size of the requested struct or zero in the case of pointer in JDK20+
* @param offset the native address to the requested struct or the unbounded pointer(JDK20+)
* @param sigTypeSize the byte size of the requested struct or the unbounded pointer(JDK20+)
* @return a MemorySegment object
*/
static j9object_t
Expand Down

0 comments on commit ab8319b

Please sign in to comment.