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

@ -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

View file

@ -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));