llvm-c166/libc/include/pthread.yaml
Pavel Labath f5d4c79964
[libc] Add pthread_getstack_np extension (#217049)
The function returns the stack (size and address) of the given thread,
much like a combination of pthread_getattr_np+pthread_attr_getstack.
However, unlike that combination, this function is guaranteed to be
async-signal-safe. I'm adding it because guaranteeing
async-signal-safety for pthread_getattr_np is tricky because of the
increased scope. In particular, it's hard to avoid memory allocation as
that function would need to store CPU affinity for an unknown number of
CPUs.

I'm not trying to emulate OpenBSD's
[pthread_stackseg_np](https://man.openbsd.org/pthread_stackseg_np.3) as
we discussed previously, because the function has an unusual interface
(it returns the stack through a stack_t, but it has ss_sp point to the
top of the stack instead of the base). It also does not claim to be
async-signal-safe. With a view towards a standardization attempt, I
tried to design something that fits in with the existing APIs. Note that
this does not mean that the design is final, and I am open to changing
the interface (before or after submitting this patch).

The trickiest part of this patch is what to do with the main thread,
which has a kernel-allocated dynamically-growing stack. Glibc's
pthread_getattr_np computes this values by parsing /proc/$PID/maps,
which is another reason this function is not async-signal-safe (and it
also requires /proc to be mounted). While one can parse /proc in an
async-signal-safe way, doing that is not trivial, and it still requires
/proc.

Other options I've considered is using `getrlimit(RLIMIT_STACK`
(captured either at application startup or at the time of the
pthread_getstack_np call), but I rejected that because that does not
return an accurate value -- it can both under- (if the limit is
increased) or over-estimate (if the limit decreases or something is
mapped in the way of the expanding stack) the size of the stack.

This is why I came to believe that the most correct option is to return
"I don't know" -- a special constant (PTHREAD_STACK_DYNAMIC_NP)
indicating that the stack size may change (another "quirk" of the main
thread).

I calculate the top of the stack by taking the value of AT_EXECFN
(normally the first thing placed on the stack by the kernel). I do this
in order to best match the behavior of the function for non-main thread
(where the returned region typically includes other objects stored on
the "stack" (e.g. the TLS data).
2026-08-21 10:38:02 -07:00

487 lines
13 KiB
YAML

header: pthread.h
standards:
- posix
macros:
- macro_name: "PTHREAD_NULL"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_CREATE_JOINABLE"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_CREATE_DETACHED"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_MUTEX_NORMAL"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_MUTEX_ERRORCHECK"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_MUTEX_RECURSIVE"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_MUTEX_DEFAULT"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_MUTEX_STALLED"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_MUTEX_ROBUST"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_BARRIER_SERIAL_THREAD"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_ONCE_INIT"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_PROCESS_PRIVATE"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_PROCESS_SHARED"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_MUTEX_INITIALIZER"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_COND_INITIALIZER"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_RWLOCK_INITIALIZER"
macro_header: pthread-macros.h
- macro_name: "PTHREAD_STACK_MIN"
standards:
- gnu
macro_header: pthread-macros.h
- macro_name: "PTHREAD_RWLOCK_PREFER_READER_NP"
standards:
- gnu
macro_header: pthread-macros.h
- macro_name: "PTHREAD_RWLOCK_PREFER_WRITER_NP"
standards:
- gnu
macro_header: pthread-macros.h
- macro_name: "PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP"
standards:
- gnu
macro_header: pthread-macros.h
- macro_name: "PTHREAD_STACK_DYNAMIC_NP"
standards:
- llvm_libc_ext
macro_header: pthread-macros.h
types:
- type_name: pthread_t
- type_name: pthread_once_t
- type_name: pthread_mutex_t
- type_name: pthread_mutexattr_t
- type_name: pthread_barrier_t
- type_name: pthread_barrierattr_t
- type_name: pthread_key_t
- type_name: pthread_cond_t
- type_name: pthread_condattr_t
- type_name: pthread_rwlock_t
- type_name: pthread_rwlockattr_t
- type_name: pthread_attr_t
- type_name: pthread_spinlock_t
- type_name: pthread_id_np_t
standards:
- llvm_libc_ext
functions:
- name: pthread_atfork
return_type: int
arguments:
- type: __atfork_callback_t
- type: __atfork_callback_t
- type: __atfork_callback_t
- name: pthread_attr_destroy
return_type: int
arguments:
- type: pthread_attr_t *
- name: pthread_attr_getdetachstate
return_type: int
arguments:
- type: const pthread_attr_t *
- type: int *
- name: pthread_attr_getguardsize
return_type: int
arguments:
- type: const pthread_attr_t *__restrict
- type: size_t *__restrict
- name: pthread_attr_getschedparam
return_type: int
arguments:
- type: const pthread_attr_t *__restrict
- type: struct sched_param *__restrict
- name: pthread_attr_getstack
return_type: int
arguments:
- type: const pthread_attr_t *__restrict
- type: void **__restrict
- type: size_t *__restrict
- name: pthread_attr_getstacksize
return_type: int
arguments:
- type: const pthread_attr_t *__restrict
- type: size_t *__restrict
- name: pthread_attr_init
return_type: int
arguments:
- type: pthread_attr_t *
- name: pthread_attr_setdetachstate
return_type: int
arguments:
- type: pthread_attr_t *
- type: int
- name: pthread_attr_setguardsize
return_type: int
arguments:
- type: pthread_attr_t *
- type: size_t
- name: pthread_attr_setschedparam
return_type: int
arguments:
- type: pthread_attr_t *__restrict
- type: const struct sched_param *__restrict
- name: pthread_attr_setstack
return_type: int
arguments:
- type: pthread_attr_t *
- type: void *
- type: size_t
- name: pthread_attr_setstacksize
return_type: int
arguments:
- type: pthread_attr_t *
- type: size_t
- name: pthread_condattr_destroy
return_type: int
arguments:
- type: pthread_condattr_t *
- name: pthread_condattr_getclock
return_type: int
arguments:
- type: const pthread_condattr_t *__restrict
- type: clockid_t *__restrict
- name: pthread_condattr_getpshared
return_type: int
arguments:
- type: const pthread_condattr_t *__restrict
- type: int *__restrict
- name: pthread_condattr_init
return_type: int
arguments:
- type: pthread_condattr_t *
- name: pthread_condattr_setclock
return_type: int
arguments:
- type: pthread_condattr_t *
- type: clockid_t
- name: pthread_condattr_setpshared
return_type: int
arguments:
- type: pthread_condattr_t *
- type: int
- name: pthread_cond_broadcast
return_type: int
arguments:
- type: pthread_cond_t *
- name: pthread_cond_clockwait
return_type: int
arguments:
- type: pthread_cond_t *__restrict
- type: pthread_mutex_t *__restrict
- type: clockid_t
- type: const struct timespec *__restrict
- name: pthread_cond_destroy
return_type: int
arguments:
- type: pthread_cond_t *
- name: pthread_cond_init
return_type: int
arguments:
- type: pthread_cond_t *__restrict
- type: const pthread_condattr_t *__restrict
- name: pthread_cond_signal
return_type: int
arguments:
- type: pthread_cond_t *
- name: pthread_cond_timedwait
return_type: int
arguments:
- type: pthread_cond_t *__restrict
- type: pthread_mutex_t *__restrict
- type: const struct timespec *__restrict
- name: pthread_cond_wait
return_type: int
arguments:
- type: pthread_cond_t *__restrict
- type: pthread_mutex_t *__restrict
- name: pthread_create
return_type: int
arguments:
- type: pthread_t *__restrict
- type: const pthread_attr_t *__restrict
- type: __pthread_start_t
- type: void *
- name: pthread_detach
return_type: int
arguments:
- type: pthread_t
- name: pthread_equal
return_type: int
arguments:
- type: pthread_t
- type: pthread_t
- name: pthread_exit
return_type: _Noreturn void
arguments:
- type: void *
- name: pthread_getattr_np
standards:
- gnu
return_type: int
arguments:
- type: pthread_t
- type: pthread_attr_t *
- name: pthread_getname_np
standards:
- gnu
return_type: int
arguments:
- type: pthread_t
- type: char *
- type: size_t
- name: pthread_getschedparam
return_type: int
arguments:
- type: pthread_t
- type: int *__restrict
- type: struct sched_param *__restrict
- name: pthread_getspecific
return_type: void *
arguments:
- type: pthread_key_t
- name: pthread_getstack_np
standards:
- llvm_libc_ext
return_type: int
arguments:
- type: pthread_t
- type: void **__restrict
- type: size_t *__restrict
- name: pthread_join
return_type: int
arguments:
- type: pthread_t
- type: void **
- name: pthread_key_create
return_type: int
arguments:
- type: pthread_key_t *
- type: __pthread_tss_dtor_t
- name: pthread_key_delete
return_type: int
arguments:
- type: pthread_key_t
- name: pthread_mutex_destroy
return_type: int
arguments:
- type: pthread_mutex_t *
- name: pthread_mutex_init
return_type: int
arguments:
- type: pthread_mutex_t *__restrict
- type: const pthread_mutexattr_t *__restrict
- name: pthread_mutex_lock
return_type: int
arguments:
- type: pthread_mutex_t *
- name: pthread_mutex_trylock
return_type: int
arguments:
- type: pthread_mutex_t *
- name: pthread_mutex_unlock
return_type: int
arguments:
- type: pthread_mutex_t *
- name: pthread_mutexattr_destroy
return_type: int
arguments:
- type: pthread_mutexattr_t *
- name: pthread_mutexattr_getpshared
return_type: int
arguments:
- type: const pthread_mutexattr_t *__restrict
- type: int *__restrict
- name: pthread_mutexattr_getrobust
return_type: int
arguments:
- type: const pthread_mutexattr_t *__restrict
- type: int *__restrict
- name: pthread_mutexattr_gettype
return_type: int
arguments:
- type: const pthread_mutexattr_t *__restrict
- type: int *__restrict
- name: pthread_mutexattr_init
return_type: int
arguments:
- type: pthread_mutexattr_t *
- name: pthread_mutexattr_setpshared
return_type: int
arguments:
- type: pthread_mutexattr_t *__restrict
- type: int
- name: pthread_mutexattr_setrobust
return_type: int
arguments:
- type: pthread_mutexattr_t *__restrict
- type: int
- name: pthread_mutexattr_settype
return_type: int
arguments:
- type: pthread_mutexattr_t *__restrict
- type: int
- name: pthread_barrier_init
return_type: int
arguments:
- type: pthread_barrier_t *__restrict
- type: const pthread_barrierattr_t *__restrict
- type: int
- name: pthread_barrier_wait
return_type: int
arguments:
- type: pthread_barrier_t *
- name: pthread_barrier_destroy
return_type: int
arguments:
- type: pthread_barrier_t *
- name: pthread_once
return_type: int
arguments:
- type: pthread_once_t *
- type: __pthread_once_func_t
- name: pthread_rwlock_clockrdlock
return_type: int
arguments:
- type: pthread_rwlock_t *__restrict
- type: clockid_t
- type: const struct timespec *__restrict
- name: pthread_rwlock_clockwrlock
return_type: int
arguments:
- type: pthread_rwlock_t *__restrict
- type: clockid_t
- type: const struct timespec *__restrict
- name: pthread_rwlock_destroy
return_type: int
arguments:
- type: pthread_rwlock_t *
- name: pthread_rwlock_init
return_type: int
arguments:
- type: pthread_rwlock_t *
- type: const pthread_rwlockattr_t *__restrict
- name: pthread_rwlock_rdlock
return_type: int
arguments:
- type: pthread_rwlock_t *
- name: pthread_rwlock_timedrdlock
return_type: int
arguments:
- type: pthread_rwlock_t *__restrict
- type: const struct timespec *__restrict
- name: pthread_rwlock_timedwrlock
return_type: int
arguments:
- type: pthread_rwlock_t *__restrict
- type: const struct timespec *__restrict
- name: pthread_rwlock_tryrdlock
return_type: int
arguments:
- type: pthread_rwlock_t *
- name: pthread_rwlock_trywrlock
return_type: int
arguments:
- type: pthread_rwlock_t *
- name: pthread_rwlock_unlock
return_type: int
arguments:
- type: pthread_rwlock_t *
- name: pthread_rwlock_wrlock
return_type: int
arguments:
- type: pthread_rwlock_t *
- name: pthread_rwlockattr_destroy
return_type: int
arguments:
- type: pthread_rwlockattr_t *
- name: pthread_rwlockattr_getkind_np
standards:
- gnu
return_type: int
arguments:
- type: pthread_rwlockattr_t *
- type: int *
- name: pthread_rwlockattr_getpshared
return_type: int
arguments:
- type: const pthread_rwlockattr_t *
- type: int *
- name: pthread_rwlockattr_init
return_type: int
arguments:
- type: pthread_rwlockattr_t *
- name: pthread_rwlockattr_setkind_np
standards:
- gnu
return_type: int
arguments:
- type: pthread_rwlockattr_t *
- type: int
- name: pthread_rwlockattr_setpshared
return_type: int
arguments:
- type: pthread_rwlockattr_t *
- type: int
- name: pthread_self
return_type: pthread_t
arguments:
- type: void
- name: pthread_setname_np
standards:
- gnu
return_type: int
arguments:
- type: pthread_t
- type: const char *
- name: pthread_setschedparam
return_type: int
arguments:
- type: pthread_t
- type: int
- type: const struct sched_param *
- name: pthread_setspecific
return_type: int
arguments:
- type: pthread_key_t
- type: const void *
- name: pthread_spin_destroy
return_type: int
arguments:
- type: pthread_spinlock_t *
- name: pthread_spin_init
return_type: int
arguments:
- type: pthread_spinlock_t *
- type: int
- name: pthread_spin_lock
return_type: int
arguments:
- type: pthread_spinlock_t *
- name: pthread_spin_trylock
return_type: int
arguments:
- type: pthread_spinlock_t *
- name: pthread_spin_unlock
return_type: int
arguments:
- type: pthread_spinlock_t *
- name: pthread_getthreadid_np
standards:
- llvm_libc_ext
return_type: pthread_id_np_t
arguments:
- type: void
- name: pthread_getunique_np
standards:
- llvm_libc_ext
return_type: int
arguments:
- type: const pthread_t *__restrict
- type: pthread_id_np_t *__restrict