: RE: Inappropriate use of the implicit session in allocating the upcall stub in SafeFunctionAccessTest.java

Cheng Jin jincheng at ca.ibm.com
Thu Feb 2 17:11:20 UTC 2023


Hi Maurizio,

I really appreciate your explanation in more details which helps me better understand how to deal with the lifetime of the memory segment in downcall/upcall.

Best Regards
Cheng Jin

From: Maurizio Cimadamore <maurizio.cimadamore at oracle.com>
Sent: February 2, 2023 9:09 AM
To: Jorn Vernee <jorn.vernee at oracle.com>; Cheng Jin <jincheng at ca.ibm.com>; panama-dev at openjdk.org
Subject: Re: [External] : RE: Inappropriate use of the implicit session in allocating the upcall stub in SafeFunctionAccessTest.java

To add to Jorn’s great reply, let’s also look at this from a different angle. The question you are asking in reality has nothing to do with upcalls. It has to do with memory segments that are passed by reference (e. g. by passing their address
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.
ZjQcmQRYFpfptBannerEnd

To add to Jorn’s great reply, let’s also look at this from a different angle.

The question you are asking in reality has nothing to do with upcalls. It has to do with memory segments that are passed by reference (e.g. by passing their address in some register and/or stack slot).

Even a call to something as simple as this:

       size_t strlen(const char *s);

can be problematic: consider the case where the string is stored in a memory segment whose lifetime is managed by an automatic scope. It would be bad if the segment could become unreachable while executing strlen.

This is why the Linker provides a lifetime guarantee for all memory segments passed by reference: such arguments will be forcefully kept alive for the entire duration of the call. This means that a segment passed to strlen, no matter how allocated, will never be deallocated while the strlen code is executing.

This is, of course an 80% (90% ?) guarantee: it works well when the native code uses the provided segment locally. But in cases where the native code stashes the segment pointer somewhere (e.g. a global variable) and then, later on, re-access that same pointer (maybe while evaluating a different function) there can be issues. The problem here is that the native code as certain expectations on the lifetime of the provided pointer (e.g. this pointer should remain alive after the call). In such cases, the Java code using Linker should reflect the expected lifetime accordingly, either by adding reachability fences as required (if the segment is managed by the GC), or by keeping the Arena alive for as long as required. Whenever this doesn’t happen, the native code can be exposed to use-after-free bugs. There is nothing a Java API can do to protect against these sneaky cases: handling these cases correctly is really a result of a deeper understanding of the lifetime expectations of the library we’re interacting with.

Upcall stubs are “just another kind of segment” that can be passed by reference. As such, it is subject to the same guarantees described above, which can fail exactly in the same ways as described above for non-upcall segments.

Cheers
Maurizio

On 31/01/2023 22:20, Jorn Vernee wrote:

Hi,

The section of javadoc that you reference is talking about a case like this:

    static MemorySegment target() {
        try (Arena arena = Arena.openConfined()) {
            return arena.allocate(JAVA_INT);
        }
    }

Note that the memory will be freed before the target function returns, so if this function is used as the target method of an upcall stub, it will effectively return a dangling pointer to native code. If the native code then dereferences the returned pointer, this can crash the VM.

> 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.

In the case of an implicit scope, the scope of the upcall stub should be kept reachable (per [1]) until the upcall stub can no longer be called. The Reference::reachability fence method can be used for this. But, it might be easier to use an explicitly closed scope for the upcall stub instead, if you plan on storing it in native memory and calling it later.

> 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]?

Not quite. The current implementation guarantees that the implicit session of an upcall stub is kept alive (and the upcall stub works) during and invocation of a _downcall_ to which the upcall stub memory segment is passed. i.e. if an upcall stub is passed to a downcall, the scope it is attached to can not be closed for the duration of that downcall (and this goes for any MemorySegment passed to a downcall). This is achieved by calling ImplicitSession::release [2] after a downcall returns.

There is no guarantee that an arbitrary upcall stub will be kept alive until it is invoked. For instance, if I pass an upcall stub to native code, and store the pointer in some memory there (e.g. a global variable), then I return back to Java. If I don't have a strong reference to the upcall stub's scope, the upcall stub might be freed. If I then later try to invoke the upcall stub again through the pointer I stored in native code, the VM can crash. In short: the JVM can not 'see' any pointer to the upcall stub from native code, so they don't count toward its reachability.

To illustrate the problematic case, consider this native library and Java program that calls into it (using the Java 20 API):

libMylib.c:

typedef void (*callback_t)(void);

static callback_t GLOBAL_CB; // global pointer variable

void store(callback_t cb) {
  GLOBAL_CB = cb;
}

void call() {
  GLOBAL_CB();
}

Main.java:

import java.lang.foreign.*;
import java.lang.invoke.*;

import static java.lang.foreign.ValueLayout.*;

public class Main {
  static final Linker LINKER = Linker.nativeLinker();

  static final MethodHandle STORE;
  static final MethodHandle CALL;
  static final MethodHandle TARGET;

  static {
    System.loadLibrary("Mylib");
    SymbolLookup lookup = SymbolLookup.loaderLookup();
    STORE = LINKER.downcallHandle(lookup.find("store").get(), FunctionDescriptor.ofVoid(ADDRESS));
    CALL = LINKER.downcallHandle(lookup.find("call").get(), FunctionDescriptor.ofVoid());
    try {
      TARGET = MethodHandles.lookup().findStatic(Main.class, "target", MethodType.methodType(void.class));
    } catch (ReflectiveOperationException e) {
      throw new InternalError(e);
    }
  }
  public static void main(String[] args) throws Throwable {
    store();
    System.gc(); // invoke gc which can clean up 'stub'
    call();
  }

  static void store() throws Throwable {
    MemorySegment stub = LINKER.upcallStub(TARGET, FunctionDescriptor.ofVoid(), SegmentScope.auto());
    STORE.invokeExact(stub);
    // stub is unreachable here
  }

  static void call() throws Throwable {
    CALL.invokeExact();
  }

  static void target() {}
}

On my machine this code reliably crashes the VM, since the upcall stub is called after it becomes unreachable (and is freed).

In order to fix this, I would have to keep the stub alive until after I call the 'call' function from the native library:

  public static void main(String[] args) throws Throwable {
    MemorySegment stub = store(); // <-----
    System.gc(); // invoke gc
    call();
    Reference.reachabilityFence(stub); // <----- keep stub alive until after I call it
  }

  static MemorySegment store() throws Throwable {
    MemorySegment stub = LINKER.upcallStub(TARGET, FunctionDescriptor.ofVoid(), SegmentScope.auto());
    STORE.invokeExact(stub);
    return stub; // <--- return stub here
  }

Hope that helps,
Jorn

[1]: https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/ref/package-summary.html#reachability<https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/ref/package-summary.html#reachability>
[2]: https://github.com/openjdk/panama-foreign/blob/a943c2eb10ba40c36d5a6e874160d0a747457510/src/java.base/share/classes/jdk/internal/foreign/ImplicitSession.java#L50<https://github.com/openjdk/panama-foreign/blob/a943c2eb10ba40c36d5a6e874160d0a747457510/src/java.base/share/classes/jdk/internal/foreign/ImplicitSession.java#L50>
On 31/01/2023 21:01, Cheng Jin wrote:
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><mailto:jorn.vernee at oracle.com>
Sent: January 31, 2023 2:14 PM
To: Cheng Jin <jincheng at ca.ibm.com><mailto:jincheng at ca.ibm.com>; panama-dev at openjdk.org<mailto: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/20230202/3e9f1943/attachment-0001.htm>


More information about the panama-dev mailing list