Inappropriate use of the implicit session in allocating the upcall stub in SafeFunctionAccessTest.java
Cheng Jin
jincheng at ca.ibm.com
Tue Jan 31 20:01:47 UTC 2023
Hi Jorn,
Correct but these bullets are only intended for the downcall invocation rather than the upcall stub.
According to the explanation of upcall stub in [1] as follows:
“When creating upcall stubs the linker runtime....
Moreover, if the target method handle associated with an upcall stub returns a memory address,
clients must ensure that this address cannot become invalid after the upcall completes. <-------------------
This can lead to unspecified behavior, and even JVM crashes, since an upcall is
typically executed in the context of a downcall method handle invocation.”
I don’t understand how/what clients should do to ensure the address of the upcall stub is alive during the upcall invocation within an implication session.
Does that mean the implicit session is ALREADY kept alive in the existing implementation of OpenJDK (which I don’t think so) to guarantee the upcall stub works during the upcall invocation based on the explanation in [1]?
Best Regards
Cheng Jin
From: Jorn Vernee <jorn.vernee at oracle.com>
Sent: January 31, 2023 2:14 PM
To: Cheng Jin <jincheng at ca.ibm.com>; panama-dev at openjdk.org
Subject: [EXTERNAL] Re: Inappropriate use of the implicit session in allocating the upcall stub in SafeFunctionAccessTest.java
Hi Cheng Jin, Thanks for the email. In this case it is not a problem since memory sessions (including upcall stubs) are kept alive/reachable for the duration of a downcall (see the third bullet here: [1]) "The memory session of R is kept
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd
Hi Cheng Jin,
Thanks for the email.
In this case it is not a problem since memory sessions (including upcall stubs) are kept alive/reachable for the duration of a downcall (see the third bullet here: [1])
"The memory session of R is kept alive (and cannot be closed) during the invocation."
Since we don't store a reference to the upcall stub in native code until after the downcall returns, this is safe to rely on.
Cheers,
Jorn
[1]: https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/foreign/Linker.html#safety<https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/foreign/Linker.html#safety>
On 31/01/2023 18:49, Cheng Jin wrote:
Hi there,
I notice there might be an issue with the allocation of upcall stub under an implicit session at test/jdk/java/foreign/SafeFunctionAccessTest.java in JDK19+ as follows:
e.g.
@Test
public void testClosedStructCallback() throws Throwable {
MethodHandle handle = Linker.nativeLinker().downcallHandle(
findNativeOrThrow("addr_func_cb"),
FunctionDescriptor.ofVoid(C_POINTER, C_POINTER));
try (MemorySession session = MemorySession.openConfined()) {
MemorySegment segment = MemorySegment.allocateNative(POINT, session);
handle.invoke(segment, sessionChecker(session)); <----------------- the upcall stub is allocated in sessionChecker()
}
}
MemorySegment sessionChecker(MemorySession session) {
try {
MethodHandle handle = MethodHandles.lookup().findStatic(SafeFunctionAccessTest.class, "checkSession",
MethodType.methodType(void.class, MemorySession.class));
handle = handle.bindTo(session);
return Linker.nativeLinker().upcallStub(handle, FunctionDescriptor.ofVoid(), MemorySession.openImplicit()); <-------the upcall stub is allocated with an implicit session
} catch (Throwable ex) {
throw new AssertionError(ex);
}
}
It is correct to allocate the upcall stub with a session different from the current session in tests given these tests intend to close the current session in upcall to verify the behavior. But it is problematic to exploit an implicit session backed by GC, in which case the memory of the upcall stub is more likely to be forced to release by GC (when implicitly closing the session to free memory) especially on the memory-restricted machines, which leads to unexpected behavior in upcall (e.g. crash which was captured recently). To work around this case, I’d suggest to replace it with a global session or others so as to keep the upcall stub alive during the upcall.
Best Regards
Cheng Jin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20230131/9f01da52/attachment-0001.htm>
More information about the panama-dev
mailing list