Skip to content

Commit 9c852df

Browse files
committed
8318966: Some methods make promises about Java array element alignment that are too strong
Reviewed-by: psandoz, mcimadamore
1 parent 737b4c5 commit 9c852df

19 files changed

+1693
-4521
lines changed

src/java.base/share/classes/java/lang/invoke/MethodHandles.java

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4513,48 +4513,18 @@ public static VarHandle arrayElementVarHandle(Class<?> arrayClass) throws Illega
45134513
* or greater than the {@code byte[]} array length minus the size (in bytes)
45144514
* of {@code T}.
45154515
* <p>
4516-
* Access of bytes at an index may be aligned or misaligned for {@code T},
4517-
* with respect to the underlying memory address, {@code A} say, associated
4518-
* with the array and index.
4519-
* If access is misaligned then access for anything other than the
4520-
* {@code get} and {@code set} access modes will result in an
4521-
* {@code IllegalStateException}. In such cases atomic access is only
4522-
* guaranteed with respect to the largest power of two that divides the GCD
4523-
* of {@code A} and the size (in bytes) of {@code T}.
4524-
* If access is aligned then following access modes are supported and are
4525-
* guaranteed to support atomic access:
4526-
* <ul>
4527-
* <li>read write access modes for all {@code T}, with the exception of
4528-
* access modes {@code get} and {@code set} for {@code long} and
4529-
* {@code double} on 32-bit platforms.
4530-
* <li>atomic update access modes for {@code int}, {@code long},
4531-
* {@code float} or {@code double}.
4532-
* (Future major platform releases of the JDK may support additional
4533-
* types for certain currently unsupported access modes.)
4534-
* <li>numeric atomic update access modes for {@code int} and {@code long}.
4535-
* (Future major platform releases of the JDK may support additional
4536-
* numeric types for certain currently unsupported access modes.)
4537-
* <li>bitwise atomic update access modes for {@code int} and {@code long}.
4538-
* (Future major platform releases of the JDK may support additional
4539-
* numeric types for certain currently unsupported access modes.)
4540-
* </ul>
4541-
* <p>
4542-
* Misaligned access, and therefore atomicity guarantees, may be determined
4543-
* for {@code byte[]} arrays without operating on a specific array. Given
4544-
* an {@code index}, {@code T} and its corresponding boxed type,
4545-
* {@code T_BOX}, misalignment may be determined as follows:
4546-
* <pre>{@code
4547-
* int sizeOfT = T_BOX.BYTES; // size in bytes of T
4548-
* int misalignedAtZeroIndex = ByteBuffer.wrap(new byte[0]).
4549-
* alignmentOffset(0, sizeOfT);
4550-
* int misalignedAtIndex = (misalignedAtZeroIndex + index) % sizeOfT;
4551-
* boolean isMisaligned = misalignedAtIndex != 0;
4552-
* }</pre>
4553-
* <p>
4554-
* If the variable type is {@code float} or {@code double} then atomic
4555-
* update access modes compare values using their bitwise representation
4556-
* (see {@link Float#floatToRawIntBits} and
4557-
* {@link Double#doubleToRawLongBits}, respectively).
4516+
* Only plain {@linkplain VarHandle.AccessMode#GET get} and {@linkplain VarHandle.AccessMode#SET set}
4517+
* access modes are supported by the returned var handle. For all other access modes, an
4518+
* {@link UnsupportedOperationException} will be thrown.
4519+
*
4520+
* @apiNote if access modes other than plain access are required, clients should
4521+
* consider using off-heap memory through
4522+
* {@linkplain java.nio.ByteBuffer#allocateDirect(int) direct byte buffers} or
4523+
* off-heap {@linkplain java.lang.foreign.MemorySegment memory segments},
4524+
* or memory segments backed by a
4525+
* {@linkplain java.lang.foreign.MemorySegment#ofArray(long[]) {@code long[]}},
4526+
* for which stronger alignment guarantees can be made.
4527+
*
45584528
* @param viewArrayClass the view array class, with a component type of
45594529
* type {@code T}
45604530
* @param byteOrder the endianness of the view array elements, as
@@ -4600,7 +4570,13 @@ public static VarHandle byteArrayViewVarHandle(Class<?> viewArrayClass,
46004570
* or greater than the {@code ByteBuffer} limit minus the size (in bytes) of
46014571
* {@code T}.
46024572
* <p>
4603-
* Access of bytes at an index may be aligned or misaligned for {@code T},
4573+
* For heap byte buffers, access is always unaligned. As a result, only the plain
4574+
* {@linkplain VarHandle.AccessMode#GET get}
4575+
* and {@linkplain VarHandle.AccessMode#SET set} access modes are supported by the
4576+
* returned var handle. For all other access modes, an {@link IllegalStateException}
4577+
* will be thrown.
4578+
* <p>
4579+
* For direct buffers only, access of bytes at an index may be aligned or misaligned for {@code T},
46044580
* with respect to the underlying memory address, {@code A} say, associated
46054581
* with the {@code ByteBuffer} and index.
46064582
* If access is misaligned then access for anything other than the

0 commit comments

Comments
 (0)