Skip to content

Commit 3b4fcd8

Browse files
committed
Fix corner case in Linux stack mappings
We need to make sure no existing mappings exist between the MAP_GROWSDOWN page and the guard page, since otherwise it's not going to be able to grow down thus causing difficult to troubleshoot failures.
1 parent 6ba3b44 commit 3b4fcd8

File tree

9 files changed

+14
-16
lines changed

9 files changed

+14
-16
lines changed

libc/integral/normalize.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868

6969
/* TODO(jart): Remove this in favor of GetStackSize() */
7070
#if defined(COSMO) && (defined(MODE_DBG) || defined(__SANITIZE_ADDRESS__))
71-
#define STACKSIZE 524288 /* 512kb stack */
71+
#define STACKSIZE 262144 /* 256kb stack */
7272
#elif defined(COSMO)
7373
#define STACKSIZE 65536 /* 64kb stack */
7474
#else

libc/intrin/stracef.greg.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/intrin/strace.internal.h"
2019
#include "libc/intrin/kprintf.h"
20+
#include "libc/intrin/strace.internal.h"
2121
#include "libc/runtime/runtime.h"
2222

2323
privileged void __stracef(const char *fmt, ...) {

libc/runtime/mmap.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ static noasan inline void *Mmap(void *addr, size_t size, int prot, int flags,
361361
f |= MAP_STACK_openbsd;
362362
needguard = true;
363363
} else if (IsLinux()) {
364+
// make sure there's no existing stuff existing between our stack
365+
// starting page and the bottom guard page, since that would stop
366+
// our stack page from growing down.
367+
_npassert(!sys_munmap(p, size));
364368
// by default MAP_GROWSDOWN will auto-allocate 10mb of pages. it's
365369
// supposed to stop growing if an adjacent allocation exists, to
366370
// prevent your stacks from overlapping on each other. we're not
@@ -376,8 +380,9 @@ static noasan inline void *Mmap(void *addr, size_t size, int prot, int flags,
376380
.addr == MAP_FAILED) {
377381
return MAP_FAILED;
378382
}
379-
sys_mmap(p, PAGESIZE, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS,
380-
-1, 0);
383+
_npassert(sys_mmap(p, PAGESIZE, PROT_NONE,
384+
MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
385+
.addr == p);
381386
dm.addr = p;
382387
return FinishMemory(p, size, prot, flags, fd, off, f, x, n, dm);
383388
} else {

libc/runtime/mprotect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/intrin/strace.internal.h"
2019
#include "libc/calls/syscall-sysv.internal.h"
2120
#include "libc/dce.h"
2221
#include "libc/errno.h"
2322
#include "libc/intrin/describeflags.internal.h"
2423
#include "libc/intrin/likely.h"
24+
#include "libc/intrin/strace.internal.h"
2525
#include "libc/runtime/internal.h"
2626
#include "libc/sysv/consts/prot.h"
2727

libc/stdio/fflush.internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#ifndef COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_
22
#define COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_
33
#include "libc/intrin/nopl.internal.h"
4-
#include "libc/thread/thread.h"
54
#include "libc/stdio/stdio.h"
5+
#include "libc/thread/thread.h"
6+
#include "libc/thread/tls.h"
67
#if !(__ASSEMBLER__ + __LINKER__ + 0)
78
COSMOPOLITAN_C_START_
89

libc/sysv/consts.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,6 @@ syscon sicode SYS_USER_DISPATCH 2 -1 -1 -1 -1 -1 # SIGSYS; syscall
579579
# sigaltstack() values
580580
#
581581
# group name GNU/Systemd XNU's Not UNIX! FreeBSD OpenBSD NetBSD The New Technology Commentary
582-
syscon ss SIGSTKSZ 8192 131072 34816 28672 28672 8192 # overlaid with FRAMESIZE; you need to #undef SIGSTKSZ to access this symbol
583-
syscon ss MINSIGSTKSZ 2048 32768 2048 12288 8192 2048 # overlaid with 32768; you need to #undef MINSIGSTKSZ to access this symbol
584582
syscon ss SS_ONSTACK 1 1 1 1 1 1 # unix consensus
585583
syscon ss SS_DISABLE 2 4 4 4 4 2 # bsd consensus
586584

libc/sysv/consts/MINSIGSTKSZ.s

Lines changed: 0 additions & 2 deletions
This file was deleted.

libc/sysv/consts/SIGSTKSZ.s

Lines changed: 0 additions & 2 deletions
This file was deleted.

libc/sysv/consts/ss.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33
#if !(__ASSEMBLER__ + __LINKER__ + 0)
44
COSMOPOLITAN_C_START_
55

6-
extern const size_t SIGSTKSZ;
7-
extern const size_t MINSIGSTKSZ;
86
extern const int SS_DISABLE;
97

108
COSMOPOLITAN_C_END_
119
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
1210

13-
#define SIGSTKSZ FRAMESIZE
14-
#define MINSIGSTKSZ 32768
11+
#define SIGSTKSZ 32768
12+
#define MINSIGSTKSZ 32768 /* xnu defines the highest minimum */
1513
#define SS_ONSTACK 1
1614
#define SS_DISABLE SS_DISABLE
1715

0 commit comments

Comments
 (0)