Use of warn_if_not_aligned for pthread_mutex_t?

Carlos O'Donell carlos@redhat.com
Fri May 15 21:55:50 GMT 2020


On 5/15/20 4:54 PM, H.J. Lu wrote:
> On Fri, May 15, 2020 at 1:37 PM Carlos O'Donell <carlos@redhat.com> wrote:
>>
>> HJ,
>>
>> Should it be possible to use warn_if_not_aligned to keep the
>> futex in the pthread_mutex_t aligned and warn if it's not
>> on an address that is a multiple of 4?
>>
>> I just did some testing on gcc 10 in f32 and warn_if_not_aligned
>> didn't quite behave like I would expect in this case.
>>
>> Specifically this use case:
>> https://lore.kernel.org/linux-api/403cc691-4ec5-8b3f-382c-4820736da41d@redhat.com/T/#t
>>
>> After which I filed this bug:
>> https://sourceware.org/bugzilla/show_bug.cgi?id=25997
>>
> 
> There are
> 
> char buf[sizeof(pthread_mutex_t) + 1];
> pthread_mutex_t *mutex = (pthread_mutex_t *)(buf + 1);
> 
> mutex is unaligned.

The mutex is absolutely unaligned.

Can the compiler help detect this undefined behaviour and complain?

> I don't think warn_if_not_aligned can help here.

Would anything else help?

> warn_if_not_aligned is used to check the alignment of member of
> struct.

What about embedding a pthread_mutex_t in another structure? Like
many projects do? Could we detect if such an embedding was misaligned?

-- 
Cheers,
Carlos.



More information about the Libc-alpha mailing list