JDK-8318967 : (bf) ByteBuffer::alignedSlice and alignmentOffset should not promise heap alignment
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 22
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2023-10-27
  • Updated: 2023-11-13
Related Reports
Relates :  
Relates :  
Description
The following methods: 

- ByteBuffer::alignedSlice
- ByteBuffer::alignmentOffset

Make too many promises about the alignment of the underlying heap memory.

The VM only guarantees that the elements of a Java array are aligned to their natural alignment. e.g for byte[] that is 1 byte (i.e. no alignment at all, really).

However, these methods promise that accesses will be aligned for certain offsets, even though there is no hard guarantee for that.

alignedSlice depends directly on alignmentOffset, and the latter purports to be able to guarantee alignment when accessing heap memory.

The implementation currently tries to compute an alignment offset for heap buffers when the access size (unitSize) is > 1, which can not be guaranteed to be correct.
Comments
I suspect this one will require discussion.
27-10-2023