[all-commits] [llvm/llvm-project] 9028ff: [libc] Implement clone(2) and use it in thread spa...

Pavel Labath via All-commits all-commits at lists.llvm.org
Mon Sep 21 08:05:57 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 9028ff14b18ea678683a4960306051044160bb67
      https://github.com/llvm/llvm-project/commit/9028ff14b18ea678683a4960306051044160bb67
  Author: Pavel Labath <pavel at labath.sk>
  Date:   2026-09-21 (Mon, 21 Sep 2026)

  Changed paths:
    M libc/config/linux/aarch64/entrypoints.txt
    M libc/config/linux/riscv/entrypoints.txt
    M libc/config/linux/x86_64/entrypoints.txt
    M libc/include/sched.yaml
    M libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
    A libc/src/__support/OSUtil/linux/syscall_wrappers/aarch64/clone.h
    A libc/src/__support/OSUtil/linux/syscall_wrappers/clone.h
    A libc/src/__support/OSUtil/linux/syscall_wrappers/riscv/clone.h
    A libc/src/__support/OSUtil/linux/syscall_wrappers/x86_64/clone.h
    M libc/src/__support/threads/linux/CMakeLists.txt
    M libc/src/__support/threads/linux/thread.cpp
    M libc/src/pthread/CMakeLists.txt
    M libc/src/sched/CMakeLists.txt
    A libc/src/sched/clone.h
    M libc/src/sched/linux/CMakeLists.txt
    A libc/src/sched/linux/clone.cpp
    M libc/src/threads/CMakeLists.txt
    M libc/test/src/sched/CMakeLists.txt
    A libc/test/src/sched/linux/CMakeLists.txt
    A libc/test/src/sched/linux/clone_test.cpp

  Log Message:
  -----------
  [libc] Implement clone(2) and use it in thread spawning (#224257)

This patch implements an internal clone syscall wrapper and uses it both
to implement the public clone(2) entry point and to spawn new threads in
libc's thread implementation.

Previously, thread creation in thread.cpp invoked the raw SYS_clone
syscall directly, requiring target-specific inline assembly or register
variables and subtle tricks with __builtin_frame_address to pass
arguments to start_thread in the newly spawned thread.

By introducing an inline assembly clone wrapper that sets up func and
arg on the child stack and jumps to the entry function upon clone
returning in the child, we can simplify start_thread to a normal
function taking a single void * argument and eliminate the frame pointer
hacks as well as the need to compile thread.cpp with
-fno-omit-frame-pointer or optimizations.

The public clone(2) entrypoint delegates to this wrapper after unpacking
its varargs and validating pointers (returning EINVAL like glibc).

A particularly tricky aspect of this patch is the invalidation of the
cached thread IDs. As with fork(), we do this in the parent, but we
cannot do this safely for clone() in all situations. The problematic
case is where the user does not set a custom TLS block (which means the
child uses the parent's block), does not clone the address space (no
copy-on-write), and does not suspend the parent (vfork semantics). In
this case, we just give up and don't touch the thread ID. For this
particular flag, most of the operations in the child are not safe, so
we're assuming the user is prepared for such a restricted environment.

Support is implemented for x86_64, aarch64, and riscv. The
architecture-specific assembly is split out into detail::clone_impl
inside per-architecture headers.

Assisted-by: Gemini



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list