public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug nptl/25997] New: pthread_mutex_lock QoI issue for unaligned futex.
@ 2020-05-15 17:56 carlos at redhat dot com
  2020-05-20 14:33 ` [Bug nptl/25997] " koct9i at gmail dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: carlos at redhat dot com @ 2020-05-15 17:56 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=25997

            Bug ID: 25997
           Summary: pthread_mutex_lock QoI issue for unaligned futex.
           Product: glibc
           Version: 2.32
            Status: NEW
          Severity: normal
          Priority: P2
         Component: nptl
          Assignee: unassigned at sourceware dot org
          Reporter: carlos at redhat dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

If you use an unaligned mutex, and hence an unaligned futex, the kernel returns
EINVAL and we ignore that and busy loop consuming CPU.

#include <stdlib.h>
#include <pthread.h>

int main(int argc, char **argv)
{
        char buf[sizeof(pthread_mutex_t) + 1];
        pthread_mutex_t *mutex = (pthread_mutex_t *)(buf + 1);

        pthread_mutex_init(mutex, NULL);
        pthread_mutex_lock(mutex);
        pthread_mutex_lock(mutex);
}

The fix is to use the futex-internal.h interfaces check check for error and
terminate the process if we get a fatal error from the kernel.

For example the following simple change causes the process to termiante:

diff --git a/nptl/lowlevellock.c b/nptl/lowlevellock.c
index f69547a235..d4d16d1340 100644
--- a/nptl/lowlevellock.c
+++ b/nptl/lowlevellock.c
@@ -18,6 +18,7 @@
    <https://www.gnu.org/licenses/>.  */

 #include <sysdep.h>
+#include <futex-internal.h>
 #include <lowlevellock.h>
 #include <atomic.h>
 #include <stap-probe.h>
@@ -49,7 +50,7 @@ __lll_lock_wait (int *futex, int private)
     {
     futex:
       LIBC_PROBE (lll_lock_wait, 1, futex);
-      lll_futex_wait (futex, 2, private); /* Wait if *futex == 2.  */
+      futex_wait_simple ((unsigned int *)futex, 2, private); /* Wait if *futex
== 2.  */
     }
 }
 #endif
---

Program terminated with signal SIGABRT, Aborted.
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:49
49        return ret;
(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:49
#1  0x00007f1cac0d2872 in __GI_abort () at abort.c:79
#2  0x00007f1cac12a248 in __libc_message (action=action@entry=do_abort,
fmt=fmt@entry=0x7f1cac234a57 "%s")
    at ../sysdeps/posix/libc_fatal.c:155
#3  0x00007f1cac12a27a in __GI___libc_fatal (
    message=message@entry=0x7f1cac288000 "The futex facility returned an
unexpected error code.\n")
    at ../sysdeps/posix/libc_fatal.c:164
#4  0x00007f1cac283fdc in futex_fatal_error () at
../sysdeps/nptl/futex-internal.h:157
#5  futex_wait (private=<optimized out>, expected=2, futex_word=0x7f1cac283fdc
<__lll_lock_wait+92>)
    at ../sysdeps/nptl/futex-internal.h:157
#6  futex_wait_simple (private=<optimized out>, expected=2,
futex_word=0x7f1cac283fdc <__lll_lock_wait+92>)
    at ../sysdeps/nptl/futex-internal.h:172
#7  __lll_lock_wait (futex=futex@entry=0x7ffdb1f0a2c1, private=<optimized out>)
at lowlevellock.c:53
#8  0x00007f1cac27cbf3 in __GI___pthread_mutex_lock (mutex=0x7ffdb1f0a2c1) at
../nptl/pthread_mutex_lock.c:80
#9  0x000000000040117a in main (argc=1, argv=0x7ffdb1f0a3f8) at test.c:11

You get a clear backtrace for the location of the failure.

As far back as AIX, if I remember correctly, you've never been able to pack a
pthread_mutex_t, you needed it to be word aligned in order for the rest of the
alignment to be correct (locks etc).

Can we make this a compiler error?

-- 
You are receiving this mail because:
You are on the CC list for the bug.

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

end of thread, other threads:[~2020-05-20 20:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-15 17:56 [Bug nptl/25997] New: pthread_mutex_lock QoI issue for unaligned futex carlos at redhat dot com
2020-05-20 14:33 ` [Bug nptl/25997] " koct9i at gmail dot com
2020-05-20 17:49 ` koct9i at gmail dot com
2020-05-20 18:15 ` adhemerval.zanella at linaro dot org
2020-05-20 20:35 ` adhemerval.zanella at linaro dot org

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).