Fix stack memory, undefined behavior, etc.

This commit is contained in:
Justine Tunney 2023-08-15 19:09:35 -07:00
parent 110559ce6a
commit 507d7a0b0b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
11 changed files with 41 additions and 186 deletions

View file

@ -67,16 +67,11 @@ _start:
lea 16(%rsp,%rbx,8),%rdx // envp
mov %rsp,__oldstack(%rip)
// setup a stack frame
// align stack to GetStackSize() so GetStackAddr() is fast
.weak ape_stack_memsz
mov $ape_stack_memsz,%r9d
mov $16,%r8d
test %r9d,%r9d
cmovnz %r9,%r8
neg %r8
and %r8,%rsp
// setup backtraces
xor %ebp,%ebp
// make process stack (8mb) follow thread stack (256kb) alignment
and $-(256*1024),%rsp
// bofram 9f
#if SupportsWindows()
@ -126,6 +121,10 @@ _start:
// this is the first argument to cosmo() below
mov x0,sp
// make process stack (8mb) conform to thread stack (256kb) alignment
mov x1,sp
and sp,x1,-(256*1024)
// second arg shall be struct Syslib passed by ape-m1.c
// used to talk to apple's authoritarian libraries
// should be set to zero on other platforms

View file

@ -74,11 +74,11 @@
#define __BIGGEST_ALIGNMENT__ 16
#endif
#define APE_STACKSIZE 8388608 /* default 8mb stack */
#define APE_PAGESIZE 0x10000 /* i386+ */
#define APE_STACKSIZE 8388608
#define APE_PAGESIZE 65536
#ifdef _COSMO_SOURCE
#define FRAMESIZE 0x10000
#define _PAGESIZE 0x1000 /* i386+ */
#define FRAMESIZE 65536
#define _PAGESIZE 4096
#endif
#define BUFSIZ 0x1000 /* best stdio default */

View file

@ -49,7 +49,7 @@ int(_bsrl)(long x) {
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
return kDebruijn[(x * 0x03f79d71b4cb0a89) >> 58];
return kDebruijn[(x * 0x03f79d71b4cb0a89ull) >> 58];
}
__weak_reference(_bsrl, _bsrll);

View file

@ -54,7 +54,7 @@ static const char *GetFrameName(int x) {
x <= ((GetStaticStackAddr(0) + GetStackSize() +
sizeof(struct WinArgs) - 1) >>
16))) {
return "winargs";
return "mainstack";
} else if ((int)((intptr_t)__executable_start >> 16) <= x &&
x <= (int)(((intptr_t)_end - 1) >> 16)) {
return "image";

View file

@ -45,7 +45,7 @@ void *NewCosmoStack(void) {
MAP_STACK | MAP_ANONYMOUS, -1, 0)) != MAP_FAILED) {
if (IsAsan()) {
__asan_poison(p + GetStackSize() - 16, 16, kAsanStackOverflow);
__asan_poison(p, 4096, kAsanStackOverflow);
__asan_poison(p, GetGuardSize(), kAsanStackOverflow);
}
return p;
} else {

View file

@ -372,13 +372,13 @@ dontasan inline void *__mmap_unlocked(void *addr, size_t size, int prot,
if ((dm = sys_mmap(p + size - SIGSTKSZ, SIGSTKSZ, prot,
f | MAP_GROWSDOWN_linux, fd, off))
.addr != MAP_FAILED) {
npassert(sys_mmap(p, page_size, PROT_NONE,
npassert(sys_mmap(p, GetGuardSize(), PROT_NONE,
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
.addr == p);
dm.addr = p;
p = __finish_memory(p, size, prot, flags, fd, off, f, x, n, dm);
if (IsAsan() && p != MAP_FAILED) {
__asan_poison(p, page_size, kAsanStackOverflow);
__asan_poison(p, GetGuardSize(), kAsanStackOverflow);
}
return p;
} else if (errno == ENOTSUP) {

View file

@ -81,9 +81,18 @@ extern char ape_stack_memsz[] __attribute__((__weak__));
extern char ape_stack_align[] __attribute__((__weak__));
/**
* Returns size of stack, which is always a two power.
* Returns preferred size and alignment of thread stack.
*
* This will always be equal to `PTHREAD_STACK_MIN`.
*/
#define GetStackSize() ((uintptr_t)ape_stack_memsz)
#define GetStackSize() 262144
/**
* Returns preferred stack guard size.
*
* This is the max cpu page size of supported architectures.
*/
#define GetGuardSize() 16384
/**
* Returns address of bottom of stack.
@ -124,9 +133,13 @@ extern char ape_stack_align[] __attribute__((__weak__));
/**
* Returns true if at least `n` bytes of stack are available.
*/
#define HaveStackMemory(n) \
((intptr_t)__builtin_frame_address(0) >= GetStackAddr() + 16384 + (n))
#define HaveStackMemory(n) \
((intptr_t)__builtin_frame_address(0) >= \
GetStackAddr() + GetGuardSize() + (n))
/**
* Extends stack memory by poking large allocations.
*/
forceinline void CheckLargeStackAllocation(void *p, ssize_t n) {
for (; n > 0; n -= 4096) {
((char *)p)[n - 1] = 0;

View file

@ -2,7 +2,7 @@
#define COSMOPOLITAN_LIBC_THREAD_THREAD_H_
#define PTHREAD_KEYS_MAX 128
#define PTHREAD_STACK_MIN 65536
#define PTHREAD_STACK_MIN 262144
#define PTHREAD_DESTRUCTOR_ITERATIONS 4
#define PTHREAD_BARRIER_SERIAL_THREAD 31337