Questions about the trivial downcall

Cheng Jin jincheng at ca.ibm.com
Thu Jul 27 00:20:42 UTC 2023


Hi Maurizio,

>> … Also I notice that you speak of "JNI" upcall. Is that what you really mean - e.g. a trivial FFM downcall making an upcall using JNI?

Yes. That’s exactly something I’d like to know: e.g. what happens if a JNI upcall is triggered in a trivial FFM downcall?  Crashing the VM to prevent the JNI upcall?

>>  In general, I think it's a bit hard for downcalls to do anything that has to do with JNI because the invoked native function is not passed a JNIEnv - FFM really just bridges the Java code with the native library function you want to call, nothing more. (I suppose that function could get its hands on the VM using the JNI attach API, but I feel that's a different question from what you were asking?).

The only way (I can image) to achieve a JNI upcall from a FFI downcall is to 1) do a JNI downcall at first to store a JNIEnv as a global variable in native and then 2) do a FFM downcall to trigger the JNI upcall with the stored global JNIEnv.

Suppose it works in this way, my question is whether such a JNI upcall should also be prevented/disallowed in a trivial FFM downcall based on the description of https://download.java.net/java/early_access/jdk21/docs/api/java.base/java/lang/foreign/Linker.Option.html#isTrivial() as follows:

A trivial function is a function that has an extremely short running time in all cases (similar to calling an empty function),

and does not call back into Java (e.g. using an upcall stub). <--------------------------- it occurs to me that any kind of upcall should be disallowed in such case whether it is JNI or FFM, am I correct?

Best Regards
Cheng Jin

From: Maurizio Cimadamore <maurizio.cimadamore at oracle.com>
Sent: Wednesday, July 26, 2023 7:44 PM
To: Cheng Jin <jincheng at ca.ibm.com>; panama-dev at openjdk.org
Subject: [EXTERNAL] Re: Questions about the trivial downcall

Hi, a trivial downcall is supposed to target a native function that (a) terminates quickly (so as not to block the GC for too long) and (b) does not upcall into Java. So, no, a trivial downcall cannot trigger upcalls (I don't think we detect
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
    Report Suspicious  <https://us-phishalarm-ewt.proofpoint.com/EWT/v1/PjiDSg!2c-r455VanMQ2qYReaGl7Z4QRx46ATMxAjK9nbXsj13QlLeimn0PtcIAb-U80cJ-A34Q8dYfoQ6JN5nMYOpBz-Hn3cuAGTZLJQ$>   ‌
ZjQcmQRYFpfptBannerEnd

Hi,
a trivial downcall is supposed to target a native function that (a) terminates quickly (so as not to block the GC for too long) and (b) does not upcall into Java.

So, no, a trivial downcall cannot trigger upcalls (I don't think we detect this, I believe the JVM just crashes if you do).

Also I notice that you speak of "JNI" upcall. Is that what you really mean - e.g. a trivial FFM downcall making an upcall using JNI? In general, I think it's a bit hard for downcalls to do anything that has to do with JNI because the invoked native function is not passed a JNIEnv - FFM really just bridges the Java code with the native library function you want to call, nothing more. (I suppose that function could get its hands on the VM using the JNI attach API, but I feel that's a different question from what you were asking?).

Maurizio
On 26/07/2023 22:23, Cheng Jin wrote:
Hi there,

I’ve got a couple of questions about the behavior of a trivial downcall (with isTrivial specified).

Is a FFI downcall able to trigger a JNI upcall ?
If so, should a JNI upcall should be captured in this trivial downcall?

Best Regards
Cheng Jin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20230727/b1ab703f/attachment-0001.htm>


More information about the panama-dev mailing list