Skip to content

Auto-port 5.0: Fix memoryAddress() for direct ByteBuffers wrapped by Unpooled without Unsafe#16823

Merged
normanmaurer merged 1 commit into
5.0from
auto-port-pr-16788-to-5.0
May 19, 2026
Merged

Auto-port 5.0: Fix memoryAddress() for direct ByteBuffers wrapped by Unpooled without Unsafe#16823
normanmaurer merged 1 commit into
5.0from
auto-port-pr-16788-to-5.0

Conversation

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port of #16788 to 5.0
Cherry-picked commit: f7b1b7d


Motivation:

Unpooled.wrappedBuffer(ByteBuffer) behaves differently depending on whether Unsafe is available.

When Unsafe is enabled, wrapping a direct ByteBuffer returns a buffer that exposes memoryAddress():

  ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
  ByteBuf byteBuf = Unpooled.wrappedBuffer(byteBuffer);
  System.out.println(byteBuf.memoryAddress());

When Unsafe is disabled, the same direct ByteBuffer is wrapped by the non-unsafe path. Even if the platform can still obtain the direct buffer address through FFM, memoryAddress() throws UnsupportedOperationException.

A similar inconsistency exists for the empty buffer returned by:

  ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(0, 0);
  System.out.println(byteBuf.memoryAddress());

Modification:

  • Use PlatformDependent.hasDirectByteBufferAddress(...) in the unpooled direct wrapper path instead of relying only on Unsafe.
  • Use the same address availability check when initializing EmptyByteBuf's cached direct buffer address.

Result:

Fixes #.

Unpooled wrapped direct buffers expose memoryAddress() consistently when platform address access is available,including when Unsafe is disabled and FFM is used

…t Unsafe (#16788)

Motivation:

Unpooled.wrappedBuffer(ByteBuffer) behaves differently depending on
whether Unsafe is available.

When Unsafe is enabled, wrapping a direct ByteBuffer returns a buffer
that exposes memoryAddress():

```java
  ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
  ByteBuf byteBuf = Unpooled.wrappedBuffer(byteBuffer);
  System.out.println(byteBuf.memoryAddress());
```

When Unsafe is disabled, the same direct ByteBuffer is wrapped by the
non-unsafe path. Even if the platform can still obtain the direct buffer
address through FFM, memoryAddress() throws
UnsupportedOperationException.

A similar inconsistency exists for the empty buffer returned by:
``` java
  ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(0, 0);
  System.out.println(byteBuf.memoryAddress());
```

Modification:

- Use PlatformDependent.hasDirectByteBufferAddress(...) in the unpooled
direct wrapper path instead of relying only on Unsafe.
- Use the same address availability check when initializing
EmptyByteBuf's cached direct buffer address.

Result:

Fixes #<GitHub issue number>.

Unpooled wrapped direct buffers expose memoryAddress() consistently when
platform address access is available,including when Unsafe is disabled
and FFM is used

Co-authored-by: Jonas Konrad <jonas.konrad@oracle.com>
(cherry picked from commit f7b1b7d)
@normanmaurer normanmaurer merged commit 17edcd1 into 5.0 May 19, 2026
20 of 23 checks passed
@normanmaurer normanmaurer deleted the auto-port-pr-16788-to-5.0 branch May 19, 2026 06:18
@normanmaurer normanmaurer added this to the 5.0.0.Final milestone May 19, 2026
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