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.
This commit is contained in:
Justine Tunney 2022-10-03 13:43:52 -07:00
parent 6ba3b448f3
commit 3b4fcd8575
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
9 changed files with 14 additions and 16 deletions

View file

@ -68,7 +68,7 @@
/* TODO(jart): Remove this in favor of GetStackSize() */ /* TODO(jart): Remove this in favor of GetStackSize() */
#if defined(COSMO) && (defined(MODE_DBG) || defined(__SANITIZE_ADDRESS__)) #if defined(COSMO) && (defined(MODE_DBG) || defined(__SANITIZE_ADDRESS__))
#define STACKSIZE 524288 /* 512kb stack */ #define STACKSIZE 262144 /* 256kb stack */
#elif defined(COSMO) #elif defined(COSMO)
#define STACKSIZE 65536 /* 64kb stack */ #define STACKSIZE 65536 /* 64kb stack */
#else #else

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/kprintf.h" #include "libc/intrin/kprintf.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h" #include "libc/runtime/runtime.h"
privileged void __stracef(const char *fmt, ...) { privileged void __stracef(const char *fmt, ...) {

View file

@ -361,6 +361,10 @@ static noasan inline void *Mmap(void *addr, size_t size, int prot, int flags,
f |= MAP_STACK_openbsd; f |= MAP_STACK_openbsd;
needguard = true; needguard = true;
} else if (IsLinux()) { } else if (IsLinux()) {
// make sure there's no existing stuff existing between our stack
// starting page and the bottom guard page, since that would stop
// our stack page from growing down.
_npassert(!sys_munmap(p, size));
// by default MAP_GROWSDOWN will auto-allocate 10mb of pages. it's // by default MAP_GROWSDOWN will auto-allocate 10mb of pages. it's
// supposed to stop growing if an adjacent allocation exists, to // supposed to stop growing if an adjacent allocation exists, to
// prevent your stacks from overlapping on each other. we're not // 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,
.addr == MAP_FAILED) { .addr == MAP_FAILED) {
return MAP_FAILED; return MAP_FAILED;
} }
sys_mmap(p, PAGESIZE, PROT_NONE, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, _npassert(sys_mmap(p, PAGESIZE, PROT_NONE,
-1, 0); MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)
.addr == p);
dm.addr = p; dm.addr = p;
return FinishMemory(p, size, prot, flags, fd, off, f, x, n, dm); return FinishMemory(p, size, prot, flags, fd, off, f, x, n, dm);
} else { } else {

View file

@ -16,12 +16,12 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/intrin/strace.internal.h"
#include "libc/calls/syscall-sysv.internal.h" #include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/errno.h" #include "libc/errno.h"
#include "libc/intrin/describeflags.internal.h" #include "libc/intrin/describeflags.internal.h"
#include "libc/intrin/likely.h" #include "libc/intrin/likely.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/internal.h" #include "libc/runtime/internal.h"
#include "libc/sysv/consts/prot.h" #include "libc/sysv/consts/prot.h"

View file

@ -1,8 +1,9 @@
#ifndef COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_ #ifndef COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_
#define COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_ #define COSMOPOLITAN_LIBC_STDIO_FFLUSH_H_
#include "libc/intrin/nopl.internal.h" #include "libc/intrin/nopl.internal.h"
#include "libc/thread/thread.h"
#include "libc/stdio/stdio.h" #include "libc/stdio/stdio.h"
#include "libc/thread/thread.h"
#include "libc/thread/tls.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_

View file

@ -579,8 +579,6 @@ syscon sicode SYS_USER_DISPATCH 2 -1 -1 -1 -1 -1 # SIGSYS; syscall
# sigaltstack() values # sigaltstack() values
# #
# group name GNU/Systemd XNU's Not UNIX! FreeBSD OpenBSD NetBSD The New Technology Commentary # group name GNU/Systemd XNU's Not UNIX! FreeBSD OpenBSD NetBSD The New Technology Commentary
syscon ss SIGSTKSZ 8192 131072 34816 28672 28672 8192 # overlaid with FRAMESIZE; you need to #undef SIGSTKSZ to access this symbol
syscon ss MINSIGSTKSZ 2048 32768 2048 12288 8192 2048 # overlaid with 32768; you need to #undef MINSIGSTKSZ to access this symbol
syscon ss SS_ONSTACK 1 1 1 1 1 1 # unix consensus syscon ss SS_ONSTACK 1 1 1 1 1 1 # unix consensus
syscon ss SS_DISABLE 2 4 4 4 4 2 # bsd consensus syscon ss SS_DISABLE 2 4 4 4 4 2 # bsd consensus

View file

@ -1,2 +0,0 @@
.include "o/libc/sysv/consts/syscon.internal.inc"
.syscon ss,MINSIGSTKSZ,2048,32768,2048,12288,8192,2048

View file

@ -1,2 +0,0 @@
.include "o/libc/sysv/consts/syscon.internal.inc"
.syscon ss,SIGSTKSZ,8192,131072,34816,28672,28672,8192

View file

@ -3,15 +3,13 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
extern const size_t SIGSTKSZ;
extern const size_t MINSIGSTKSZ;
extern const int SS_DISABLE; extern const int SS_DISABLE;
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#define SIGSTKSZ FRAMESIZE #define SIGSTKSZ 32768
#define MINSIGSTKSZ 32768 #define MINSIGSTKSZ 32768 /* xnu defines the highest minimum */
#define SS_ONSTACK 1 #define SS_ONSTACK 1
#define SS_DISABLE SS_DISABLE #define SS_DISABLE SS_DISABLE