cosmopolitan/libc/thread/pthread_attr_setstacksize.c
Justine Tunney 379cd77078
Improve memory manager and signal handling
On Windows, mmap() now chooses addresses transactionally. It reduces the
risk of badness when interacting with the WIN32 memory manager. We don't
throw darts anymore. There is also no more retry limit, since we recover
from mystery maps more gracefully. The subroutine for combining adjacent
maps has been rewritten for clarity. The print maps subroutine is better

This change goes to great lengths to perfect the stack overflow code. On
Windows you can now longjmp() out of a crash signal handler. Guard pages
previously weren't being restored properly by the signal handler. That's
fixed, so on Windows you can now handle a stack overflow multiple times.
Great thought has been put into selecting the perfect SIGSTKSZ constants
so you can save sigaltstack() memory. You can now use kprintf() with 512
bytes of stack available. The guard pages beneath the main stack are now
recorded in the memory manager.

This change fixes getcontext() so it works right with the %rax register.
2024-12-27 01:33:00 -08:00

54 lines
3.2 KiB
C

/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2022 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or distribute this software for │
│ any purpose with or without fee is hereby granted, provided that the │
│ above copyright notice and this permission notice appear in all copies. │
│ │
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/errno.h"
#include "libc/thread/thread.h"
/**
* Specifies minimum stack size for thread, e.g.
*
* pthread_t th;
* pthread_attr_t attr;
* pthread_attr_init(&attr);
* pthread_attr_setguardsize(&attr, 4096);
* pthread_attr_setstacksize(&attr, 61440);
* pthread_create(&th, &attr, thfunc, arg);
* pthread_attr_destroy(&attr);
*
* On Linux, if you're not using `cosmocc -mtiny`, and you're not using
* cosmo_dlopen(), and guard size is nonzero, then `MAP_GROWSDOWN` will
* be used to create your stack memory. This helps minimize virtual
* memory consumption. Please note this is only possible if `stacksize`
* is no larger than the current `RLIMIT_STACK`, otherwise the runtime
* will map your stack using plain old mmap().
*
* Non-custom stacks may be recycled by the cosmo runtime. You can
* control this behavior by calling cosmo_stack_setmaxstacks(). It's
* useful for both tuning performance and hardening security. See also
* pthread_attr_setguardsize() which is important for security too.
*
* @param stacksize contains stack size in bytes
* @return 0 on success, or errno on error
* @raise EINVAL if `stacksize` is less than `PTHREAD_STACK_MIN`
*/
errno_t pthread_attr_setstacksize(pthread_attr_t *a, size_t stacksize) {
if (stacksize < PTHREAD_STACK_MIN)
return EINVAL;
a->__stacksize = stacksize;
return 0;
}