[libc] Ensure the result of the clone syscall is not on stack in thrd_create.
authorSiva Chandra Reddy <sivachandra@google.com>
Mon, 30 Aug 2021 04:31:30 +0000 (04:31 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Mon, 30 Aug 2021 04:35:40 +0000 (04:35 +0000)
Also, added a call to munmap on error in thrd_create.

libc/src/threads/linux/CMakeLists.txt
libc/src/threads/linux/thrd_create.cpp

index 6e8b212..730314c 100644 (file)
@@ -50,6 +50,7 @@ add_entrypoint_object(
     libc.src.errno.__errno_location
     libc.src.sys.mman.mmap
   COMPILE_OPTIONS
+    -O3
     -fno-omit-frame-pointer # This allows us to sniff out the thread args from
                             # the new thread's stack reliably.
 )
index 5a6bc11..59d260d 100644 (file)
@@ -82,13 +82,15 @@ LLVM_LIBC_FUNCTION(int, thrd_create,
   // but it might differ for other architectures. So, make this call
   // architecture independent. May be implement a glibc like wrapper for clone
   // and use it here.
-  long clone_result =
+  long register clone_result asm("rax");
+  clone_result =
       __llvm_libc::syscall(SYS_clone, clone_flags, adjusted_stack,
                            &thread->__tid, clear_tid_address, 0);
 
   if (clone_result == 0) {
     start_thread();
   } else if (clone_result < 0) {
+    __llvm_libc::munmap(thread->__stack, thread->__stack_size);
     int error_val = -clone_result;
     return error_val == ENOMEM ? thrd_nomem : thrd_error;
   }