understanding the effect of `alignas` and ubsan

Viewed 71

I'm looking at some legacy code from someone who is no longer the OSS project I work on. It's a FIFO queue which uses C11 atomics.

Some of the fields in the structures use the alignas keyword, but I'm not entirely sure what the effect is in this case.

The structure definitions are below

typedef struct {
    alignas(128) void       *data;
    atomic_int64_t          seq;
} fr_atomic_queue_entry_t;

struct fr_atomic_queue_s {
    alignas(128) atomic_int64_t head;
    atomic_int64_t          tail;

    size_t              size;

    fr_atomic_queue_entry_t     *entry;
};

Firstly i'm trying to figure out the real world effect on the alignas keyword.

Starting at the top in fr_atomic_queue_entry_t, alignas is added to the definition of the data field. Does this mean padding is inserted between data/seq.

In fr_atomic_queue_s alignas is applied to the head field, which presumably holds the insertion index of the fifo. Does this mean padding is inserted between head/tail?

Secondly, what's the point of 128 byte alignment, is it something to do with the integrity of the atomic operations, or cache alignment?

The full code is available here: https://github.com/FreeRADIUS/freeradius-server/blob/master/src/lib/io/atomic_queue.c

I don't expect a definitive answer, just wondering if anyone has any clues.

Lastly, and the reason why I'm examining the code. We recently enabled LLVM's UBSAN, and it's failing with the following:

src/lib/io/atomic_queue.c:85:7: runtime error: member access within misaligned address 0x00010a16f860 for type 'fr_atomic_queue_t' (aka 'struct fr_atomic_queue_s'), which requires 128 byte alignment
0x00010a16f860: note: pointer points here
 be be be be  be be be be be be be be  be be be be be be be be  be be be be be be be be  be be be be

What is that error actually saying needs to change? fr_atomic_queue_s is heap allocated, does the start of the struct need to be 128 byte aligned?

Should the size of fr_atomic_queue_entry_t be a multiple of 128? Does the start of the array which holds to entries need to be aligned to 128 bytes?

0 Answers
Related