[libc-commits] [PATCH] D151142: [LIBC] Cleanup code in `thread_exit`

Noah Goldstein via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon May 22 13:54:35 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG916e9be4aa0a: Cleanup code in `thread_exit` (authored by goldstein.w.n).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151142/new/

https://reviews.llvm.org/D151142

Files:
  libc/src/__support/threads/linux/thread.cpp
  libc/src/__support/threads/thread.h


Index: libc/src/__support/threads/thread.h
===================================================================
--- libc/src/__support/threads/thread.h
+++ libc/src/__support/threads/thread.h
@@ -228,7 +228,7 @@
 extern thread_local Thread self;
 
 // Platforms should implement this function.
-void thread_exit(ThreadReturnValue retval, ThreadStyle style);
+[[noreturn]] void thread_exit(ThreadReturnValue retval, ThreadStyle style);
 
 namespace internal {
 // Internal namespace containing utilities which are to be used by platform
Index: libc/src/__support/threads/linux/thread.cpp
===================================================================
--- libc/src/__support/threads/linux/thread.cpp
+++ libc/src/__support/threads/linux/thread.cpp
@@ -117,7 +117,11 @@
   return reinterpret_cast<void *>(mmap_result);
 }
 
-LIBC_INLINE void free_stack(void *stack, size_t stacksize, size_t guardsize) {
+// This must always be inlined as we may be freeing the calling threads stack in
+// which case a normal return from the top the stack would cause an invalid
+// memory read.
+static __attribute__((always_inline)) inline void
+free_stack(void *stack, size_t stacksize, size_t guardsize) {
   uintptr_t stackaddr = reinterpret_cast<uintptr_t>(stack);
   stackaddr -= guardsize;
   stack = reinterpret_cast<void *>(stackaddr);
@@ -137,7 +141,11 @@
   void *arg;
 };
 
-static void cleanup_thread_resources(ThreadAttributes *attrib) {
+// This must always be inlined as we may be freeing the calling threads stack in
+// which case a normal return from the top the stack would cause an invalid
+// memory read.
+static __attribute__((always_inline)) inline void
+cleanup_thread_resources(ThreadAttributes *attrib) {
   // Cleanup the TLS before the stack as the TLS information is stored on
   // the stack.
   cleanup_tls(attrib->tls, attrib->tls_size);
@@ -496,14 +504,18 @@
     // Set the CLEAR_TID address to nullptr to prevent the kernel
     // from signalling at a non-existent futex location.
     __llvm_libc::syscall_impl(SYS_set_tid_address, 0);
+    // Return value for detached thread should be unused. We need to avoid
+    // referencing `style` or `retval.*` because they may be stored on the stack
+    // and we have deallocated our stack!
+    __llvm_libc::syscall_impl(SYS_exit, 0);
+    __builtin_unreachable();
   }
 
-  // TODO: We may have deallocated the stack. Can we reference local variables
-  // that may have spilled?
   if (style == ThreadStyle::POSIX)
     __llvm_libc::syscall_impl(SYS_exit, retval.posix_retval);
   else
     __llvm_libc::syscall_impl(SYS_exit, retval.stdc_retval);
+  __builtin_unreachable();
 }
 
 } // namespace __llvm_libc


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151142.524478.patch
Type: text/x-patch
Size: 2683 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230522/1be7c59a/attachment.bin>


More information about the libc-commits mailing list