public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Use of warn_if_not_aligned for pthread_mutex_t?
@ 2020-05-15 20:37 Carlos O'Donell
  2020-05-15 20:54 ` H.J. Lu
  0 siblings, 1 reply; 4+ messages in thread
From: Carlos O'Donell @ 2020-05-15 20:37 UTC (permalink / raw)
  To: libc-alpha, H.J. Lu

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

-- 
Cheers,
Carlos.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Use of warn_if_not_aligned for pthread_mutex_t?
  2020-05-15 20:37 Use of warn_if_not_aligned for pthread_mutex_t? Carlos O'Donell
@ 2020-05-15 20:54 ` H.J. Lu
  2020-05-15 21:55   ` Carlos O'Donell
  0 siblings, 1 reply; 4+ messages in thread
From: H.J. Lu @ 2020-05-15 20:54 UTC (permalink / raw)
  To: Carlos O'Donell; +Cc: libc-alpha

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.   I don't think warn_if_not_aligned can help here.
warn_if_not_aligned is used to check the alignment of member of
struct.

-- 
H.J.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Use of warn_if_not_aligned for pthread_mutex_t?
  2020-05-15 20:54 ` H.J. Lu
@ 2020-05-15 21:55   ` Carlos O'Donell
  2020-05-15 22:51     ` H.J. Lu
  0 siblings, 1 reply; 4+ messages in thread
From: Carlos O'Donell @ 2020-05-15 21:55 UTC (permalink / raw)
  To: H.J. Lu; +Cc: libc-alpha

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.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Use of warn_if_not_aligned for pthread_mutex_t?
  2020-05-15 21:55   ` Carlos O'Donell
@ 2020-05-15 22:51     ` H.J. Lu
  0 siblings, 0 replies; 4+ messages in thread
From: H.J. Lu @ 2020-05-15 22:51 UTC (permalink / raw)
  To: Carlos O'Donell; +Cc: libc-alpha

On Fri, May 15, 2020 at 2:56 PM Carlos O'Donell <carlos@redhat.com> wrote:
>
> 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?
>

warn_if_not_aligned checks if the offset of struct member has the desired
alignment.  If struct itself is unaligned, warn_if_not_aligned won't help.

-- 
H.J.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-05-15 22:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-15 20:37 Use of warn_if_not_aligned for pthread_mutex_t? Carlos O'Donell
2020-05-15 20:54 ` H.J. Lu
2020-05-15 21:55   ` Carlos O'Donell
2020-05-15 22:51     ` H.J. Lu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).