Unsafe making too strong assumptions about array alignment?
Andrew Haley
aph-open at littlepinkcloud.com
Wed May 28 09:51:51 UTC 2025
On 5/27/25 23:30, some-java-user-99206970363698485155 at vodafonemail.de wrote:
> Yes, that is what I meant. But I am not sure if compact object headers
> would be really a problem. I think in that case for example
> ARRAY_BYTE_BASE_OFFSET would simply be < 16,
Probably, but again there are no hard guarantees.
> A few days ago I accidentally only responded to Maurizio instead of the
> mailing list. I will repeat my message here:
>
> My concern here is that the code in `putLongUnaligned` decides if it is
> an unaligned store just based on the `offset`, and I am wondering if
> that information alone is really enough to tell that.
No. It isn't.
> Because if it determines that the store is aligned it delegates to
> `putLong` and that requires that the store is aligned, if that is not
> actually the case it is undefined behavior (if I understand it correctly).
>
> So this means the JVM must guarantee that the physical address of the
> array is aligned by 8 to make sure `putLongUnaligned` for a byte[] is
> actually safe?
Indeed so. My belief is that any JVM running on a machine which has both
strict alignment and 8-byte stores will 8-align all objects (or never
use 8-byte-wide stores) but there's no guarantee of that. It's quite
conceivable that a JVM could be written that only 8-aligns objects with
8-byte-wide fields, 4-aligning everything else.
--
Andrew Haley (he/him)
Java Platform Lead Engineer
Red Hat UK Ltd. <https://www.redhat.com>
https://keybase.io/andrewhaley
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671
More information about the panama-dev
mailing list