Skip to content

Commit

Permalink
[libc] Ensure the result of the clone syscall is not on stack in thrd…
Browse files Browse the repository at this point in the history
…_create.

Also, added a call to munmap on error in thrd_create.
  • Loading branch information
Siva Chandra Reddy committed Aug 30, 2021
1 parent 3bdd850 commit 3383ec5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 1 addition & 0 deletions libc/src/threads/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
)
Expand Down
4 changes: 3 additions & 1 deletion libc/src/threads/linux/thrd_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 3383ec5

Please sign in to comment.