Improve memory safety

This commit makes numerous refinements to cosmopolitan memory handling.

The default stack size has been reduced from 2mb to 128kb. A new macro
is now provided so you can easily reconfigure the stack size to be any
value you want. Work around the breaking change by adding to your main:

    STATIC_STACK_SIZE(0x00200000);  // 2mb stack

If you're not sure how much stack you need, then you can use:

    STATIC_YOINK("stack_usage_logging");

After which you can `sort -nr o/$MODE/stack.log`. Based on the unit test
suite, nothing in the Cosmopolitan repository (except for Python) needs
a stack size greater than 30kb. There are also new macros for detecting
the size and address of the stack at runtime, e.g. GetStackAddr(). We
also now support sigaltstack() so if you want to see nice looking crash
reports whenever a stack overflow happens, you can put this in main():

    ShowCrashReports();

Under `make MODE=dbg` and `make MODE=asan` the unit testing framework
will now automatically print backtraces of memory allocations when
things like memory leaks happen. Bugs are now fixed in ASAN global
variable overrun detection. The memtrack and asan runtimes also handle
edge cases now. The new tools helped to identify a few memory leaks,
which are fixed by this change.

This change should fix an issue reported in #288 with ARG_MAX limits.
Fixing this doubled the performance of MKDEPS.COM and AR.COM yet again.
This commit is contained in:
Justine Tunney 2021-10-13 17:27:13 -07:00
parent a0b39f886c
commit 226aaf3547
317 changed files with 6474 additions and 3993 deletions

View file

@ -66,20 +66,31 @@ static char *output_file;
static StringArray input_paths;
char **chibicc_tmpfiles;
static void usage(int status) {
char *p;
size_t n;
p = gc(xslurp("/zip/third_party/chibicc/help.txt", &n));
xwrite(1, p, n);
exit(status);
}
static void version(void) {
printf("\
static const char kChibiccVersion[] = "\
chibicc (cosmopolitan) 9.0.0\n\
copyright 2019 rui ueyama\n\
copyright 2020 justine alexandra roberts tunney\n");
exit(0);
copyright 2020 justine alexandra roberts tunney\n";
static void chibicc_version(void) {
xwrite(1, kChibiccVersion, sizeof(kChibiccVersion) - 1);
_Exit(0);
}
static void chibicc_usage(int status) {
char *p;
size_t n;
p = xslurp("/zip/third_party/chibicc/help.txt", &n);
xwrite(1, p, n);
_Exit(status);
}
void chibicc_cleanup(void) {
size_t i;
if (chibicc_tmpfiles && !opt_save_temps) {
for (i = 0; chibicc_tmpfiles[i]; i++) {
unlink(chibicc_tmpfiles[i]);
}
}
}
static bool take_arg(char *arg) {
@ -185,7 +196,7 @@ static void parse_args(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (take_arg(argv[i])) {
if (!argv[++i]) {
usage(1);
chibicc_usage(1);
}
}
}
@ -196,9 +207,9 @@ static void parse_args(int argc, char **argv) {
} else if (!strcmp(argv[i], "-cc1")) {
opt_cc1 = true;
} else if (!strcmp(argv[i], "--help")) {
usage(0);
chibicc_usage(0);
} else if (!strcmp(argv[i], "--version")) {
version();
chibicc_version();
} else if (!strcmp(argv[i], "-v")) {
opt_verbose = true;
atexit(PrintMemoryUsage);
@ -365,15 +376,6 @@ static char *replace_extn(char *tmpl, char *extn) {
return buf;
}
static void cleanup(void) {
size_t i;
if (chibicc_tmpfiles && !opt_save_temps) {
for (i = 0; chibicc_tmpfiles[i]; i++) {
unlink(chibicc_tmpfiles[i]);
}
}
}
static char *create_tmpfile(void) {
char *path = xjoinpaths(kTmpPath, "chibicc-XXXXXX");
int fd = mkstemp(path);
@ -442,7 +444,7 @@ static bool run_subprocess(char **argv) {
if (!vfork()) {
// Child process. Run a new command.
execvp(argv[0], argv);
_exit(1);
_Exit(1);
}
// Wait for the child process to finish.
do rc = wait(&ws);
@ -683,9 +685,7 @@ static void OnCtrlC(int sig, siginfo_t *si, ucontext_t *ctx) {
}
int chibicc(int argc, char **argv) {
showcrashreports();
sigaction(SIGINT, &(struct sigaction){.sa_sigaction = OnCtrlC}, NULL);
atexit(cleanup);
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-cc1")) {
opt_cc1 = true;