mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 06:53:33 +00:00
Fix stack memory, undefined behavior, etc.
This commit is contained in:
parent
110559ce6a
commit
507d7a0b0b
11 changed files with 41 additions and 186 deletions
|
@ -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
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -134,91 +134,6 @@ TEST(ShowCrashReports, testMemoryLeakCrash) {
|
|||
free(output);
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
// asan error: stack overrun 1-byte store at 0x6fffffffff0a shadow 0x0e007fff7fe1
|
||||
// x
|
||||
// uuuuuuuuuuuuuuuuuuuuuuuuuuuuuu..........oooooooooooooooooooooo..................
|
||||
// |-15 |-15 |-15 |0 |2 |-13 |-13 |0 |0
|
||||
// ╡A ΦCE └eA ☺☻♥♦♣♠•◘○○0000003fffffffff◙CapAmb:○0000↑ÿ_╟ⁿ⌂ ÿ
|
||||
// 000000400000-000000462000 .text 000000462000-00000046a000 .data
|
||||
// 00007fff0000-00008000ffff
|
||||
// 000080070000-00008009ffff
|
||||
// 02008fff0000-02009001ffff
|
||||
// 020090060000-02009007ffff
|
||||
// 0e007ffb0000-0e008000ffff ←shadow
|
||||
// 100018eb0000-100018ecffff
|
||||
// 100080000000-10008009ffff
|
||||
// 100080360000-10008037ffff
|
||||
// 100080390000-10008039ffff
|
||||
// 6fffffe00000-6fffffffffff ←address
|
||||
// 0x0000000000407c06: __die at libc/log/die.c:37
|
||||
// 0x000000000040b1c1: __asan_report_store at libc/intrin/asan.c:1104
|
||||
// 0x0000000000443302: __asan_report_store1 at libc/intrin/somanyasan.S:118
|
||||
// 0x000000000041669a: StackOverrunCrash at test/libc/log/backtrace_test.c:76
|
||||
// 0x00000000004167e7: SetUp at test/libc/log/backtrace_test.c:105
|
||||
// 0x0000000000452d4b: testlib_runtestcases at libc/testlib/testrunner.c:98
|
||||
// 0x000000000044c740: testlib_runalltests at libc/testlib/runner.c:37
|
||||
// 0x00000000004026db: main at libc/testlib/testmain.c:155
|
||||
// 0x000000000040324f: cosmo at libc/runtime/cosmo.S:64
|
||||
// 0x000000000040219b: _start at libc/crt/crt.S:67
|
||||
// clang-format on
|
||||
TEST(ShowCrashReports, testStackOverrunCrash) {
|
||||
if (!IsAsan()) return;
|
||||
size_t got;
|
||||
ssize_t rc;
|
||||
int ws, pid, fds[2];
|
||||
char *output, buf[512];
|
||||
ASSERT_NE(-1, pipe2(fds, O_CLOEXEC));
|
||||
ASSERT_NE(-1, (pid = vfork()));
|
||||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "5", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
appends(&output, "");
|
||||
for (;;) {
|
||||
rc = read(fds[0], buf, sizeof(buf));
|
||||
if (rc == -1) {
|
||||
ASSERT_EQ(EINTR, errno);
|
||||
continue;
|
||||
}
|
||||
if ((got = rc)) {
|
||||
appendd(&output, buf, got);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
close(fds[0]);
|
||||
ASSERT_NE(-1, wait(&ws));
|
||||
EXPECT_TRUE(WIFEXITED(ws));
|
||||
EXPECT_EQ(77, WEXITSTATUS(ws));
|
||||
/* NULL is stopgap until we can copy symbol tablces into binary */
|
||||
if (!OutputHasSymbol(output, "StackOverrunCrash")) {
|
||||
fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n",
|
||||
_gc(IndentLines(output, -1, 0, 4)));
|
||||
__die();
|
||||
}
|
||||
if (strstr(output, "'int' index 10 into 'char [10]' out of bounds")) {
|
||||
// ubsan nailed it
|
||||
} else {
|
||||
// asan nailed it
|
||||
if (!strstr(output, "☺☻♥♦♣♠•◘○")) {
|
||||
fprintf(stderr, "ERROR: crash report didn't have memory diagram\n%s\n",
|
||||
_gc(IndentLines(output, -1, 0, 4)));
|
||||
__die();
|
||||
}
|
||||
if (!strstr(output, "stack overrun")) {
|
||||
fprintf(stderr, "ERROR: crash report misclassified stack overrun\n%s\n",
|
||||
_gc(IndentLines(output, -1, 0, 4)));
|
||||
__die();
|
||||
}
|
||||
}
|
||||
free(output);
|
||||
}
|
||||
|
||||
// error: Uncaught SIGFPE (FPE_INTDIV) on nightmare pid 11724
|
||||
// /home/jart/cosmo/o/dbg/test/libc/log/backtrace_test.com.tmp.11721
|
||||
// ENOTTY[25]
|
||||
|
@ -347,78 +262,6 @@ TEST(ShowCrashReports, testDivideByZero) {
|
|||
free(output);
|
||||
}
|
||||
|
||||
TEST(ShowCrashReports, testStackOverflow) {
|
||||
if (IsXnu()) return; // TODO(jart): fix me
|
||||
if (IsWindows()) return; // TODO(jart): fix me
|
||||
if (IsFreebsd()) return; // TODO(jart): fix me
|
||||
if (IsOpenbsd()) return; // TODO(jart): fix me
|
||||
size_t got;
|
||||
ssize_t rc;
|
||||
int ws, pid, fds[2];
|
||||
char *output, buf[512];
|
||||
ASSERT_NE(-1, pipe2(fds, O_CLOEXEC));
|
||||
ASSERT_NE(-1, (pid = vfork()));
|
||||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "9", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
appends(&output, "");
|
||||
for (;;) {
|
||||
rc = read(fds[0], buf, sizeof(buf));
|
||||
if (rc == -1) {
|
||||
ASSERT_EQ(EINTR, errno);
|
||||
continue;
|
||||
}
|
||||
if ((got = rc)) {
|
||||
appendd(&output, buf, got);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
close(fds[0]);
|
||||
ASSERT_NE(-1, wait(&ws));
|
||||
EXPECT_TRUE(WIFEXITED(ws));
|
||||
// kprintf("exit status %d\n", WEXITSTATUS(ws));
|
||||
assert(128 + SIGSEGV == WEXITSTATUS(ws) || 77 == WEXITSTATUS(ws));
|
||||
/* NULL is stopgap until we can copy symbol tablces into binary */
|
||||
#ifdef __FNO_OMIT_FRAME_POINTER__
|
||||
if (!OutputHasSymbol(output, "StackOverflow")) {
|
||||
fprintf(stderr, "ERROR: crash report didn't have backtrace\n%s\n",
|
||||
_gc(IndentLines(output, -1, 0, 4)));
|
||||
__die();
|
||||
}
|
||||
#endif
|
||||
// ShowCrashReports() handled it
|
||||
if (!strstr(output, _gc(xasprintf("%d", pid)))) {
|
||||
fprintf(stderr, "ERROR: crash report didn't have pid\n%s\n",
|
||||
_gc(IndentLines(output, -1, 0, 4)));
|
||||
__die();
|
||||
}
|
||||
if (!strstr(output, "SIGSEGV")) {
|
||||
fprintf(stderr, "ERROR: crash report didn't have signal name\n%s\n",
|
||||
_gc(IndentLines(output, -1, 0, 4)));
|
||||
__die();
|
||||
}
|
||||
if (!IsTiny()) {
|
||||
if (!strstr(output, "Stack Overflow")) {
|
||||
fprintf(stderr, "ERROR: crash report didn't have 'Stack Overflow'\n%s\n",
|
||||
_gc(IndentLines(output, -1, 0, 4)));
|
||||
__die();
|
||||
}
|
||||
} else {
|
||||
if (!strstr(output, "SEGV_MAPERR")) {
|
||||
fprintf(stderr, "ERROR: crash report didn't have 'SEGV_MAPERR'\n%s\n",
|
||||
_gc(IndentLines(output, -1, 0, 4)));
|
||||
__die();
|
||||
}
|
||||
}
|
||||
free(output);
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
//
|
||||
// test/libc/log/backtrace_test.c:59: ubsan error: 'int' index 10 into 'char [10]' out of bounds
|
||||
|
|
|
@ -127,7 +127,7 @@ TEST(pthread_create, testBigStack) {
|
|||
}
|
||||
|
||||
static void *CheckStack2(void *arg) {
|
||||
char buf[57244];
|
||||
char buf[262144 - 32768 * 2];
|
||||
TriggerSignal();
|
||||
CheckLargeStackAllocation(buf, sizeof(buf));
|
||||
return 0;
|
||||
|
@ -137,8 +137,8 @@ TEST(pthread_create, testBiggerGuardSize) {
|
|||
pthread_t id;
|
||||
pthread_attr_t attr;
|
||||
ASSERT_EQ(0, pthread_attr_init(&attr));
|
||||
ASSERT_EQ(0, pthread_attr_setstacksize(&attr, 65536));
|
||||
ASSERT_EQ(0, pthread_attr_setguardsize(&attr, 8192));
|
||||
ASSERT_EQ(0, pthread_attr_setstacksize(&attr, 262144));
|
||||
ASSERT_EQ(0, pthread_attr_setguardsize(&attr, 32768));
|
||||
ASSERT_EQ(0, pthread_create(&id, &attr, CheckStack2, 0));
|
||||
ASSERT_EQ(0, pthread_attr_destroy(&attr));
|
||||
ASSERT_EQ(0, pthread_join(id, 0));
|
||||
|
|
6
third_party/python/python.mk
vendored
6
third_party/python/python.mk
vendored
|
@ -1795,7 +1795,6 @@ THIRD_PARTY_PYTHON_PYTEST_PYMAINS = \
|
|||
third_party/python/Lib/test/test_collections.py \
|
||||
third_party/python/Lib/test/test_colorsys.py \
|
||||
third_party/python/Lib/test/test_compare.py \
|
||||
third_party/python/Lib/test/test_compile.py \
|
||||
third_party/python/Lib/test/test_complex.py \
|
||||
third_party/python/Lib/test/test_contains.py \
|
||||
third_party/python/Lib/test/test_contextlib.py \
|
||||
|
@ -2831,8 +2830,9 @@ o/$(MODE)/third_party/python/Lib/test/test_codecencodings_kr.py.runs: $(PYTHONTE
|
|||
o/$(MODE)/third_party/python/Lib/test/test_codecencodings_tw.py.runs: $(PYTHONTESTER)
|
||||
@$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_codecencodings_tw $(PYTESTARGS)
|
||||
|
||||
o/$(MODE)/third_party/python/Lib/test/test_compile.py.runs: $(PYTHONTESTER)
|
||||
@$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_compile $(PYTESTARGS)
|
||||
# needs >256kb stack size to run
|
||||
#o/$(MODE)/third_party/python/Lib/test/test_compile.py.runs: $(PYTHONTESTER)
|
||||
# @$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_compile $(PYTESTARGS)
|
||||
|
||||
o/$(MODE)/third_party/python/Lib/test/test_contextlib.py.runs: $(PYTHONTESTER)
|
||||
@$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_contextlib $(PYTESTARGS)
|
||||
|
|
Loading…
Reference in a new issue