RFR: 8274400: HotSpot Style Guide should permit use of alignof [v3]

Xin Liu xliu at openjdk.org
Tue Jan 31 04:52:59 UTC 2023


On Sat, 28 Jan 2023 13:13:16 GMT, Kim Barrett <kbarrett at openjdk.org> wrote:

>>> In order to access through an object, the object must be correctly aligned for its type, else UB.
>> 
>> hi, @kimbarrett, 
>> Thanks you for letting me know `alignof`.
>> 
>> I know the concept of 'nature alignment'. an object is aligned to its size. The rule is enforced by C/C++ compiler. Is it really UB if I violate it? 
>> 
>> In this case, sizeof(Message) is 64. I don't think we have to put an instance of Message at an address which can be divided by 64. In particular, Message is POD. We would like to have compact 'buffer'.
>
>> > In order to access through an object, the object must be correctly aligned for its type, else UB.
>> 
>> hi, @kimbarrett, Thanks you for letting me know `alignof`.
>> 
>> I know the concept of 'nature alignment'. an object is aligned to its size. The rule is enforced by C/C++ compiler. Is it really UB if I violate it?
>> 
>> In this case, sizeof(Message) is 64. I don't think we have to put an instance of Message at an address which can be divided by 64. In particular, Message is POD. We would like to have compact 'buffer'.
> 
> Objects are not aligned to their size. Alignment and size are independent
> properties of a type. The alignment of fundamental types is determined by ABI
> and processor constraints. It's common for fundamental types to have alignment
> requirements equal to their size. Compound types generally (barring use of
> `alignas` specifiers and maybe other corner cases I'm forgetting) have the
> maximum alignment of their components, to ensure the components are properly
> aligned.  This may result in the compiler adding padding between class members.
> 
> And yes, misaligned accesses are UB.
> 
> BTW, `Message` is not POD.  It might be standard-layout (I haven't checked).

Hi, @kimbarrett 
Thank you for the explanation. I will look into this issue and track it using a standalone JBS. If gcc/ub-santizer flags "Message" is UB, I plan to give up placement-new and fall back to C-style 'struct'.

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

PR: https://git.openjdk.org/jdk/pull/11761


More information about the hotspot-dev mailing list