JDK-8289187 : SegmentAllocator:allocateArray throws IllegalArgumentException in some cases
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang.foreign
  • Affected Version: 19
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • Submitted: 2022-06-26
  • Updated: 2022-07-11
  • Resolved: 2022-07-11
Related Reports
Relates :  
Description
Observed that SegmentAllocator:allocateArray(*) methods throws undocumented IllegalArgumentException  where the layout alignment is more than their bitsize.

The possible scenario of these methods throwing IAEx is not documented.

For the below case :
 SegmentAllocator.implicitAllocator().allocateArray(ValueLayout.JAVA_BYTE.withBitAlignment(16),new byte[]{1,2,
                3});
will result in - java.lang.IllegalArgumentException: Source layout alignment greater than its size

The spec can be enhanced for this case.
Comments
[~mcimadamore] The mentioned IAEx in the spec is - "IllegalArgumentException - if bytesSize < 0, alignmentBytes <= 0, or if alignmentBytes is not a power of 2." The sample attached to the description satisfies all the conditions for "alignemtnBytes" to not throw an IAEx. But actual the IAEx thrown with the reason is - "java.lang.IllegalArgumentException: Source layout alignment greater than its size" Which I see no where documented.
11-07-2022

This is actually covered (implicitly) by the implSpec attached to the method: * @implSpec the default implementation for this method calls {@code this.allocate(MemoryLayout.sequenceLayout(count, elementLayout))}. then: * @implSpec the default implementation for this method calls {@code this.allocate(layout.byteSize(), layout.byteAlignment())}. And, finally: /** * Allocates a memory segment with the given size and alignment constraints. * @param bytesSize the size (in bytes) of the block of memory to be allocated. * @param bytesAlignment the alignment (in bytes) of the block of memory to be allocated. * @return a segment for the newly allocated memory block. * @throws IllegalArgumentException if {@code bytesSize < 0}, {@code alignmentBytes <= 0}, * or if {@code alignmentBytes} is not a power of 2. */ MemorySegment allocate(long bytesSize, long bytesAlignment);
11-07-2022