RFR: 8307181: MemoryLayout.structLayout uses undocumented strict alignment constraints

Maurizio Cimadamore mcimadamore at openjdk.org
Wed May 3 14:57:16 UTC 2023


On Wed, 3 May 2023 14:26:03 GMT, Jorn Vernee <jvernee at openjdk.org> wrote:

> > Actually, now that I look at it, it seems that the exception we raise is not correct. I think we need to throw if the element size is not a multiple of the alignment. That is, the size determines the stride, so that has to be a multiple of the alignment, otherwise it won't work.
> 
> Hmm, I guess the current check/assertion doesn't work out if we have an element layout which has a size that is not a multiple of its alignment constraint (i.e. it lacks trailing padding). For instance a struct of size 12, with alignment 8. The second element of a sequence with that struct layout would not be aligned.
> 
> I think we went back-and-forth on the need to enforce trailing padding in the `structLayout` factory a few times.

I think the rationale is: we can't enforce, in general, that a layout size is a multiple of its alignment. Because, for instance, we might want to construct "hyper-aligned" layouts (e.g. a struct layout aligned at page boundary. That's fine.

But I think when you use a layout as a sequence element, then we should check that the thing makes sense - e.g. that the element layout has a size that is indeed a multiple of the alignment. Otherwise it just seems like a "bad" sequence.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13770#issuecomment-1533182343


More information about the core-libs-dev mailing list