mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-30 01:02:29 +00:00
Make build hermetic without shell scripts
- Fix some minor issues in ar.com - Have execve() look for `ape` command - Rewrite NT paths using /c/ rather /??/c:/ - Replace broken GCC symlinks with .sym files - Rewrite $PATH environment variables on startup - Make $(APE_NO_MODIFY_SELF) the default bootloader - Add all build command dependencies to build/bootstrap - Get the repository mostly building from source on non-Linux
This commit is contained in:
parent
d44ff6ce1f
commit
d230a01222
160 changed files with 2754 additions and 1342 deletions
132
test/libc/log/backtrace.c
Normal file
132
test/libc/log/backtrace.c
Normal file
|
@ -0,0 +1,132 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/symbols.internal.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
int StackOverflow(int f(), int n) {
|
||||
if (n < INT_MAX) {
|
||||
return f(f, n + 1) - 1;
|
||||
} else {
|
||||
return INT_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
void FpuCrash(void) {
|
||||
typedef char xmm_t __attribute__((__vector_size__(16)));
|
||||
xmm_t v = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
|
||||
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
|
||||
volatile int x = 0;
|
||||
asm volatile("fldpi");
|
||||
asm volatile("mov\t%0,%%r15" : /* no outputs */ : "g"(0x3133731337));
|
||||
asm volatile("movaps\t%0,%%xmm15" : /* no outputs */ : "x"(v));
|
||||
fputc(7 / x, stdout);
|
||||
}
|
||||
|
||||
char bss[10];
|
||||
void BssOverrunCrash(int n) {
|
||||
int i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
bss[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
char data[10] = "abcdeabcde";
|
||||
void DataOverrunCrash(int n) {
|
||||
int i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
data[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
const char rodata[10] = "abcdeabcde";
|
||||
int RodataOverrunCrash(int i) {
|
||||
return rodata[i];
|
||||
}
|
||||
|
||||
char *StackOverrunCrash(int n) {
|
||||
int i;
|
||||
char stack[10];
|
||||
bzero(stack, sizeof(stack));
|
||||
for (i = 0; i < n; ++i) {
|
||||
stack[i] = i;
|
||||
}
|
||||
return strdup(stack);
|
||||
}
|
||||
|
||||
char *MemoryLeakCrash(void) {
|
||||
char *p = strdup("doge");
|
||||
CheckForMemoryLeaks();
|
||||
return p;
|
||||
}
|
||||
|
||||
int NpeCrash(char *p) {
|
||||
asm("nop"); // xxx: due to backtrace addr-1 thing
|
||||
return *p;
|
||||
}
|
||||
|
||||
void (*pFpuCrash)(void) = FpuCrash;
|
||||
void (*pBssOverrunCrash)(int) = BssOverrunCrash;
|
||||
void (*pDataOverrunCrash)(int) = DataOverrunCrash;
|
||||
int (*pRodataOverrunCrash)(int) = RodataOverrunCrash;
|
||||
char *(*pStackOverrunCrash)(int) = StackOverrunCrash;
|
||||
char *(*pMemoryLeakCrash)(void) = MemoryLeakCrash;
|
||||
int (*pNpeCrash)(char *) = NpeCrash;
|
||||
int (*pStackOverflow)(int (*)(), int) = StackOverflow;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
ShowCrashReports();
|
||||
if (argc > 1) {
|
||||
switch (atoi(argv[1])) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
pFpuCrash();
|
||||
exit(0);
|
||||
case 2:
|
||||
pBssOverrunCrash(10 + 1);
|
||||
exit(0);
|
||||
case 3:
|
||||
exit(pRodataOverrunCrash(10 + 1));
|
||||
case 4:
|
||||
pDataOverrunCrash(10 + 1);
|
||||
exit(0);
|
||||
case 5:
|
||||
exit((intptr_t)pStackOverrunCrash(10 + 1));
|
||||
case 6:
|
||||
exit((intptr_t)pMemoryLeakCrash());
|
||||
case 7:
|
||||
exit(pNpeCrash(0));
|
||||
case 8:
|
||||
__cxa_finalize(0);
|
||||
exit(pNpeCrash(0));
|
||||
case 9:
|
||||
exit(pStackOverflow(pStackOverflow, 0));
|
||||
default:
|
||||
fputs("error: unrecognized argument\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
} else {
|
||||
fputs("error: too few args\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@
|
|||
#include "libc/limits.h"
|
||||
#include "libc/log/libfatal.internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/io.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/runtime/internal.h"
|
||||
|
@ -40,115 +41,30 @@
|
|||
#include "libc/x/x.h"
|
||||
#include "net/http/escape.h"
|
||||
|
||||
int StackOverflow(int f(), int n) {
|
||||
if (n < INT_MAX) {
|
||||
return f(f, n + 1) - 1;
|
||||
} else {
|
||||
return INT_MAX;
|
||||
}
|
||||
STATIC_YOINK("zip_uri_support");
|
||||
STATIC_YOINK("backtrace.com");
|
||||
STATIC_YOINK("backtrace.com.dbg");
|
||||
|
||||
char testlib_enable_tmp_setup_teardown_once;
|
||||
|
||||
void Extract(const char *from, const char *to, int mode) {
|
||||
ASSERT_SYS(0, 3, open(from, O_RDONLY));
|
||||
ASSERT_SYS(0, 4, creat(to, mode));
|
||||
ASSERT_NE(-1, _copyfd(3, 4, -1));
|
||||
EXPECT_SYS(0, 0, close(4));
|
||||
EXPECT_SYS(0, 0, close(3));
|
||||
}
|
||||
|
||||
void SetUpOnce(void) {
|
||||
ASSERT_NE(-1, mkdir("bin", 0755));
|
||||
Extract("/zip/backtrace.com", "bin/backtrace.com", 0755);
|
||||
Extract("/zip/backtrace.com.dbg", "bin/backtrace.com.dbg", 0755);
|
||||
}
|
||||
|
||||
static bool OutputHasSymbol(const char *output, const char *s) {
|
||||
return strstr(output, s) || (!FindDebugBinary() && strstr(output, "NULL"));
|
||||
}
|
||||
|
||||
void FpuCrash(void) {
|
||||
typedef char xmm_t __attribute__((__vector_size__(16)));
|
||||
xmm_t v = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
|
||||
0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
|
||||
volatile int x = 0;
|
||||
asm volatile("fldpi");
|
||||
asm volatile("mov\t%0,%%r15" : /* no outputs */ : "g"(0x3133731337));
|
||||
asm volatile("movaps\t%0,%%xmm15" : /* no outputs */ : "x"(v));
|
||||
fputc(7 / x, stdout);
|
||||
}
|
||||
|
||||
char bss[10];
|
||||
void BssOverrunCrash(int n) {
|
||||
int i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
bss[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
char data[10] = "abcdeabcde";
|
||||
void DataOverrunCrash(int n) {
|
||||
int i;
|
||||
for (i = 0; i < n; ++i) {
|
||||
data[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
const char rodata[10] = "abcdeabcde";
|
||||
int RodataOverrunCrash(int i) {
|
||||
return rodata[i];
|
||||
}
|
||||
|
||||
char *StackOverrunCrash(int n) {
|
||||
int i;
|
||||
char stack[10];
|
||||
bzero(stack, sizeof(stack));
|
||||
for (i = 0; i < n; ++i) {
|
||||
stack[i] = i;
|
||||
}
|
||||
return strdup(stack);
|
||||
}
|
||||
|
||||
char *MemoryLeakCrash(void) {
|
||||
char *p = strdup("doge");
|
||||
CheckForMemoryLeaks();
|
||||
return p;
|
||||
}
|
||||
|
||||
int NpeCrash(char *p) {
|
||||
asm("nop"); // xxx: due to backtrace addr-1 thing
|
||||
return *p;
|
||||
}
|
||||
|
||||
void (*pFpuCrash)(void) = FpuCrash;
|
||||
void (*pBssOverrunCrash)(int) = BssOverrunCrash;
|
||||
void (*pDataOverrunCrash)(int) = DataOverrunCrash;
|
||||
int (*pRodataOverrunCrash)(int) = RodataOverrunCrash;
|
||||
char *(*pStackOverrunCrash)(int) = StackOverrunCrash;
|
||||
char *(*pMemoryLeakCrash)(void) = MemoryLeakCrash;
|
||||
int (*pNpeCrash)(char *) = NpeCrash;
|
||||
int (*pStackOverflow)(int (*)(), int) = StackOverflow;
|
||||
|
||||
void SetUp(void) {
|
||||
ShowCrashReports();
|
||||
if (__argc == 2) {
|
||||
switch (atoi(__argv[1])) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
pFpuCrash();
|
||||
exit(0);
|
||||
case 2:
|
||||
pBssOverrunCrash(10 + 1);
|
||||
exit(0);
|
||||
case 3:
|
||||
exit(pRodataOverrunCrash(10 + 1));
|
||||
case 4:
|
||||
pDataOverrunCrash(10 + 1);
|
||||
exit(0);
|
||||
case 5:
|
||||
exit((intptr_t)pStackOverrunCrash(10 + 1));
|
||||
case 6:
|
||||
exit((intptr_t)pMemoryLeakCrash());
|
||||
case 7:
|
||||
exit(pNpeCrash(0));
|
||||
case 8:
|
||||
__cxa_finalize(0);
|
||||
exit(pNpeCrash(0));
|
||||
case 9:
|
||||
exit(pStackOverflow(pStackOverflow, 0));
|
||||
default:
|
||||
printf("preventing fork recursion: %s\n", __argv[1]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UNFREED MEMORY
|
||||
// o/dbg/test/libc/log/backtrace_test.com
|
||||
// max allocated space 655,360
|
||||
|
@ -189,9 +105,8 @@ TEST(ShowCrashReports, testMemoryLeakCrash) {
|
|||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv(GetProgramExecutableName(),
|
||||
(char *const[]){GetProgramExecutableName(), "6", 0});
|
||||
_exit(127);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "6", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
|
@ -267,9 +182,8 @@ TEST(ShowCrashReports, testStackOverrunCrash) {
|
|||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv(GetProgramExecutableName(),
|
||||
(char *const[]){GetProgramExecutableName(), "5", 0});
|
||||
_exit(127);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "5", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
|
@ -376,9 +290,8 @@ TEST(ShowCrashReports, testDivideByZero) {
|
|||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv(GetProgramExecutableName(),
|
||||
(char *const[]){GetProgramExecutableName(), "1", 0});
|
||||
_exit(127);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "1", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
|
@ -454,9 +367,8 @@ TEST(ShowCrashReports, testStackOverflow) {
|
|||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv(GetProgramExecutableName(),
|
||||
(char *const[]){GetProgramExecutableName(), "9", "--strace", 0});
|
||||
_exit(127);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "9", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
|
@ -571,9 +483,8 @@ TEST(ShowCrashReports, testBssOverrunCrash) {
|
|||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv(GetProgramExecutableName(),
|
||||
(char *const[]){GetProgramExecutableName(), "2", 0});
|
||||
_exit(127);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "2", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
|
@ -650,9 +561,8 @@ TEST(ShowCrashReports, testNpeCrash) {
|
|||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv(GetProgramExecutableName(),
|
||||
(char *const[]){GetProgramExecutableName(), "7", 0});
|
||||
_exit(127);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "7", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
|
@ -710,9 +620,8 @@ TEST(ShowCrashReports, testDataOverrunCrash) {
|
|||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv(GetProgramExecutableName(),
|
||||
(char *const[]){GetProgramExecutableName(), "4", 0});
|
||||
_exit(127);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "4", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
|
@ -765,9 +674,8 @@ TEST(ShowCrashReports, testNpeCrashAfterFinalize) {
|
|||
if (!pid) {
|
||||
dup2(fds[1], 1);
|
||||
dup2(fds[1], 2);
|
||||
execv(GetProgramExecutableName(),
|
||||
(char *const[]){GetProgramExecutableName(), "8", 0});
|
||||
_exit(127);
|
||||
execv("bin/backtrace.com", (char *const[]){"bin/backtrace.com", "8", 0});
|
||||
_Exit(127);
|
||||
}
|
||||
close(fds[1]);
|
||||
output = 0;
|
||||
|
|
|
@ -6,56 +6,82 @@ PKGS += TEST_LIBC_LOG
|
|||
TEST_LIBC_LOG_SRCS := $(wildcard test/libc/log/*.c)
|
||||
TEST_LIBC_LOG_SRCS_TEST = $(filter %_test.c,$(TEST_LIBC_LOG_SRCS))
|
||||
|
||||
TEST_LIBC_LOG_OBJS = \
|
||||
$(TEST_LIBC_LOG_SRCS:%.c=o/$(MODE)/%.o)
|
||||
TEST_LIBC_LOG_OBJS = \
|
||||
$(TEST_LIBC_LOG_SRCS:%.c=o/$(MODE)/%.o) \
|
||||
o/$(MODE)/test/libc/log/backtrace.com.zip.o \
|
||||
o/$(MODE)/test/libc/log/backtrace.com.dbg.zip.o
|
||||
|
||||
TEST_LIBC_LOG_COMS = \
|
||||
TEST_LIBC_LOG_COMS = \
|
||||
$(TEST_LIBC_LOG_SRCS:%.c=o/$(MODE)/%.com)
|
||||
|
||||
TEST_LIBC_LOG_BINS = \
|
||||
$(TEST_LIBC_LOG_COMS) \
|
||||
TEST_LIBC_LOG_BINS = \
|
||||
$(TEST_LIBC_LOG_COMS) \
|
||||
$(TEST_LIBC_LOG_COMS:%=%.dbg)
|
||||
|
||||
TEST_LIBC_LOG_TESTS = \
|
||||
TEST_LIBC_LOG_TESTS = \
|
||||
$(TEST_LIBC_LOG_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
|
||||
|
||||
TEST_LIBC_LOG_CHECKS = \
|
||||
TEST_LIBC_LOG_CHECKS = \
|
||||
$(TEST_LIBC_LOG_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
|
||||
|
||||
TEST_LIBC_LOG_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
LIBC_RUNTIME \
|
||||
NET_HTTP \
|
||||
LIBC_STDIO \
|
||||
LIBC_X \
|
||||
LIBC_INTRIN \
|
||||
LIBC_FMT \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_LOG \
|
||||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_SYSV \
|
||||
LIBC_LOG
|
||||
TEST_LIBC_LOG_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
LIBC_RUNTIME \
|
||||
NET_HTTP \
|
||||
LIBC_STDIO \
|
||||
LIBC_X \
|
||||
LIBC_INTRIN \
|
||||
LIBC_FMT \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_LOG \
|
||||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_SYSV \
|
||||
LIBC_LOG \
|
||||
LIBC_ZIPOS
|
||||
|
||||
TEST_LIBC_LOG_DEPS := \
|
||||
TEST_LIBC_LOG_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_LIBC_LOG_DIRECTDEPS),$($(x))))
|
||||
|
||||
o/$(MODE)/test/libc/log/log.pkg: \
|
||||
$(TEST_LIBC_LOG_OBJS) \
|
||||
o/$(MODE)/test/libc/log/log.pkg: \
|
||||
$(TEST_LIBC_LOG_OBJS) \
|
||||
$(foreach x,$(TEST_LIBC_LOG_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/test/libc/log/%.com.dbg: \
|
||||
$(TEST_LIBC_LOG_DEPS) \
|
||||
o/$(MODE)/test/libc/log/%.o \
|
||||
o/$(MODE)/test/libc/log/log.pkg \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE)
|
||||
o/$(MODE)/test/libc/log/%.com.dbg: \
|
||||
$(TEST_LIBC_LOG_DEPS) \
|
||||
o/$(MODE)/test/libc/log/%.o \
|
||||
o/$(MODE)/test/libc/log/log.pkg \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/test/libc/log/backtrace_test.com.dbg: \
|
||||
$(TEST_LIBC_LOG_DEPS) \
|
||||
o/$(MODE)/test/libc/log/backtrace.com.zip.o \
|
||||
o/$(MODE)/test/libc/log/backtrace.com.dbg.zip.o \
|
||||
o/$(MODE)/test/libc/log/backtrace_test.o \
|
||||
o/$(MODE)/test/libc/log/log.pkg \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/test/libc/log/backtrace.com.dbg: \
|
||||
$(TEST_LIBC_LOG_DEPS) \
|
||||
o/$(MODE)/test/libc/log/backtrace.o \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/test/libc/log/backtrace.com.zip.o \
|
||||
o/$(MODE)/test/libc/log/backtrace.com.dbg.zip.o: \
|
||||
ZIPOBJ_FLAGS += \
|
||||
-B
|
||||
|
||||
.PHONY: o/$(MODE)/test/libc/log
|
||||
o/$(MODE)/test/libc/log: \
|
||||
$(TEST_LIBC_LOG_BINS) \
|
||||
o/$(MODE)/test/libc/log: \
|
||||
$(TEST_LIBC_LOG_BINS) \
|
||||
$(TEST_LIBC_LOG_CHECKS)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue