RE: [for-next][PATCH 21/21] tracing: Use a struct alignof to determine trace event field alignment

From: David Laight
Date: Mon Aug 01 2022 - 03:46:31 EST


From: Steven Rostedt
> Sent: 31 July 2022 20:04
>
> alignof() gives an alignment of types as they would be as standalone
> variables. But alignment in structures might be different, and when
> building the fields of events, the alignment must be the actual
> alignment otherwise the field offsets may not match what they actually
> are.
>
> This caused trace-cmd to crash, as libtraceevent did not check if the
> field offset was bigger than the event. The write_msr and read_msr
> events on 32 bit had their fields incorrect, because it had a u64 field
> between two ints. alignof(u64) would give 8, but the u64 field was at a
> 4 byte alignment.
>
> Define a macro as:
>
> ALIGN_STRUCTFIELD(type) ((int)(offsetof(struct {char a; type b;}, b)))
>
> which gives the actual alignment of types in a structure.

The simpler:
__alignof__(struct {type b;})
also works.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)