Skip to content

Auto-port 5.0: fix FFM address semantics in directBufferAddress#16641

Merged
chrisvest merged 1 commit into
5.0from
auto-port-pr-16603-to-5.0
Apr 14, 2026
Merged

Auto-port 5.0: fix FFM address semantics in directBufferAddress#16641
chrisvest merged 1 commit into
5.0from
auto-port-pr-16603-to-5.0

Conversation

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port of #16603 to 5.0
Cherry-picked commit: b4c2ddb


Motivation:

  • Unnecessary JNI fallback
    When Unsafe is disabled or unavailable, but the JDK provides a usable FFM (Foreign Function & Memory) API, the existing code still falls back to JNI to obtain the address of a direct ByteBuffer.

  • Incorrect FFM semantics in PlatformDependent0#directBufferAddress
    The Unsafe branch returns the base address of the direct buffer (index 0), consistent with JNI GetDirectBufferAddress.
    However, the FFM branch uses MemorySegment.ofBuffer(buffer).address(), which effectively returns baseAddress + position.
    This leads to inconsistent semantics between the FFM and Unsafe/JNI implementations.

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);

        //ffm api
        System.out.println(PlatformDependent.directBufferAddress(byteBuffer));
        // jni
        System.out.println(Buffer.memoryAddress(byteBuffer));

        byteBuffer = byteBuffer.position(16);
        System.out.println(PlatformDependent.directBufferAddress(byteBuffer));
        System.out.println(Buffer.memoryAddress(byteBuffer));

On Java 25 and Netty 4.2.12.Final, this code prints:

129502193232224
129502193232224
129502193232240
129502193232224

The third line reflects baseAddress + 16, demonstrating that the FFM path is position-dependent, while the JNI path remains position-independent.

Modification:

  • Prefer the FFM/JVM path when Unsafe is unavailable but FFM is available, avoiding the JNI fallback.
  • Fix the FFM branch to return the same base-address semantics as the Unsafe/JNI paths (i.e., position-independent).

Result:

fix FFM address semantics in directBufferAddress

Motivation:

- Unnecessary JNI fallback
When `Unsafe` is disabled or unavailable, but the JDK provides a usable
FFM (Foreign Function & Memory) API, the existing code still falls back
to JNI to obtain the address of a direct `ByteBuffer`.

- Incorrect FFM semantics in `PlatformDependent0#directBufferAddress`
The `Unsafe` branch returns the **base address** of the direct buffer
(index 0), consistent with JNI `GetDirectBufferAddress`.
However, the FFM branch uses `MemorySegment.ofBuffer(buffer).address()`,
which effectively returns `baseAddress + position`.
This leads to inconsistent semantics between the FFM and Unsafe/JNI
implementations.

``` java
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);

        //ffm api
        System.out.println(PlatformDependent.directBufferAddress(byteBuffer));
        // jni
        System.out.println(Buffer.memoryAddress(byteBuffer));

        byteBuffer = byteBuffer.position(16);
        System.out.println(PlatformDependent.directBufferAddress(byteBuffer));
        System.out.println(Buffer.memoryAddress(byteBuffer));
```

On Java 25 and Netty 4.2.12.Final, this code prints:
```
129502193232224
129502193232224
129502193232240
129502193232224
```

The third line reflects baseAddress + 16, demonstrating that the FFM
path is position-dependent, while the JNI path remains
position-independent.

Modification:

- Prefer the FFM/JVM path when `Unsafe` is unavailable but FFM is
available, avoiding the JNI fallback.
- Fix the FFM branch to return the same **base-address semantics** as
the Unsafe/JNI paths (i.e., position-independent).

Result:

 fix FFM address semantics in directBufferAddress

Co-authored-by: Norman Maurer <norman_maurer@apple.com>
(cherry picked from commit b4c2ddb)
@chrisvest chrisvest added this to the 5.0.0.Final milestone Apr 13, 2026
@chrisvest chrisvest enabled auto-merge (squash) April 13, 2026 18:16
@chrisvest chrisvest disabled auto-merge April 14, 2026 00:46
@chrisvest chrisvest merged commit e16d76f into 5.0 Apr 14, 2026
11 of 13 checks passed
@chrisvest chrisvest deleted the auto-port-pr-16603-to-5.0 branch April 14, 2026 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants