aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2020-12-03 13:33:44 +0100
committerSergei Trofimovich <slyfox@gentoo.org>2020-12-08 07:34:24 +0000
commite88935d964f9a4f8274d30369de4a427be1caef2 (patch)
tree915cae9ccc197c697baa65351aa0a197de6ce911
parent__vfscanf_internal: fix aliasing violation (bug 26690) (diff)
downloadglibc-e88935d964f9a4f8274d30369de4a427be1caef2.tar.gz
glibc-e88935d964f9a4f8274d30369de4a427be1caef2.tar.bz2
glibc-e88935d964f9a4f8274d30369de4a427be1caef2.zip
x86: Fix THREAD_SELF definition to avoid ld.so crash (bug 27004)
The previous definition of THREAD_SELF did not tell the compiler that %fs (or %gs) usage is invalid for the !DL_LOOKUP_GSCOPE_LOCK case in _dl_lookup_symbol_x. As a result, ld.so could try to use the TCB before it was initialized. As the comment in tls.h explains, asm volatile is undesirable here. Using the __seg_fs (or __seg_gs) namespace does not interfere with optimization, and expresses that THREAD_SELF is potentially trapping. (cherry picked from commit 1d9cbb96082e646de7515a1667efa041ffb79958) Added to Gentoo for gcc-11 compatibility.
-rw-r--r--sysdeps/i386/nptl/tls.h7
-rw-r--r--sysdeps/x86_64/nptl/tls.h7
2 files changed, 12 insertions, 2 deletions
diff --git a/sysdeps/i386/nptl/tls.h b/sysdeps/i386/nptl/tls.h
index ec03fc189c..01945220cb 100644
--- a/sysdeps/i386/nptl/tls.h
+++ b/sysdeps/i386/nptl/tls.h
@@ -240,11 +240,16 @@ tls_fill_user_desc (union user_desc_init *desc,
assignments like
pthread_descr self = thread_self();
do not get optimized away. */
-# define THREAD_SELF \
+# if __GNUC_PREREQ (6, 0)
+# define THREAD_SELF \
+ (*(struct pthread *__seg_gs *) offsetof (struct pthread, header.self))
+# else
+# define THREAD_SELF \
({ struct pthread *__self; \
asm ("movl %%gs:%c1,%0" : "=r" (__self) \
: "i" (offsetof (struct pthread, header.self))); \
__self;})
+# endif
/* Magic for libthread_db to know how to do THREAD_SELF. */
# define DB_THREAD_SELF \
diff --git a/sysdeps/x86_64/nptl/tls.h b/sysdeps/x86_64/nptl/tls.h
index 7ba9c4e69b..8f32a669ca 100644
--- a/sysdeps/x86_64/nptl/tls.h
+++ b/sysdeps/x86_64/nptl/tls.h
@@ -186,11 +186,16 @@ _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80,
assignments like
pthread_descr self = thread_self();
do not get optimized away. */
-# define THREAD_SELF \
+# if __GNUC_PREREQ (6, 0)
+# define THREAD_SELF \
+ (*(struct pthread *__seg_fs *) offsetof (struct pthread, header.self))
+# else
+# define THREAD_SELF \
({ struct pthread *__self; \
asm ("mov %%fs:%c1,%0" : "=r" (__self) \
: "i" (offsetof (struct pthread, header.self))); \
__self;})
+# endif
/* Magic for libthread_db to know how to do THREAD_SELF. */
# define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */