mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-30 14:30:27 +00:00
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:
parent
a0b39f886c
commit
226aaf3547
317 changed files with 6474 additions and 3993 deletions
169
tool/build/ar.c
169
tool/build/ar.c
|
@ -37,6 +37,7 @@
|
|||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "tool/build/lib/getargs.h"
|
||||
|
||||
/**
|
||||
* @fileoverview System Five Static Archive Builder.
|
||||
|
@ -55,7 +56,7 @@
|
|||
*/
|
||||
|
||||
struct Args {
|
||||
size_t n;
|
||||
size_t i, n;
|
||||
char **p;
|
||||
};
|
||||
|
||||
|
@ -79,6 +80,26 @@ struct Header {
|
|||
char fmag[2];
|
||||
};
|
||||
|
||||
static void NewInts(struct Ints *l, size_t n) {
|
||||
l->i = 0;
|
||||
l->n = n;
|
||||
l->p = xmalloc(n * sizeof(int));
|
||||
}
|
||||
|
||||
static void NewString(struct String *s, size_t n) {
|
||||
s->i = 0;
|
||||
s->n = n;
|
||||
s->p = xmalloc(n);
|
||||
}
|
||||
|
||||
static void AppendInt(struct Ints *l, int i) {
|
||||
APPEND(&l->p, &l->i, &l->n, &i);
|
||||
}
|
||||
|
||||
static void AppendArg(struct Args *l, char *s) {
|
||||
APPEND(&l->p, &l->i, &l->n, &s);
|
||||
}
|
||||
|
||||
static void MakeHeader(struct Header *h, const char *name, int ref, int mode,
|
||||
int size) {
|
||||
size_t n;
|
||||
|
@ -106,14 +127,21 @@ int main(int argc, char *argv[]) {
|
|||
char *line;
|
||||
char *strs;
|
||||
ssize_t rc;
|
||||
int *offsets;
|
||||
size_t wrote;
|
||||
size_t remain;
|
||||
struct stat *st;
|
||||
uint32_t outpos;
|
||||
Elf64_Sym *syms;
|
||||
const char *arg;
|
||||
struct Args args;
|
||||
uint64_t outsize;
|
||||
uint8_t *tablebuf;
|
||||
struct GetArgs ga;
|
||||
struct Ints modes;
|
||||
struct Ints names;
|
||||
struct Ints sizes;
|
||||
const char *reason;
|
||||
struct iovec iov[7];
|
||||
const char *symname;
|
||||
const char *outpath;
|
||||
|
@ -122,74 +150,45 @@ int main(int argc, char *argv[]) {
|
|||
struct String symbols;
|
||||
struct String filenames;
|
||||
struct Header *header1, *header2;
|
||||
int *offsets, *modes, *sizes, *names;
|
||||
int i, j, fd, err, name, outfd, tablebufsize;
|
||||
|
||||
if (argc == 2 && !strcmp(argv[1], "-n")) exit(0);
|
||||
|
||||
if (!(argc > 2 && strcmp(argv[1], "rcsD") == 0)) {
|
||||
fprintf(stderr, "%s%s%s\n", "Usage: ", argv[0], " rcsD ARCHIVE FILE...");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bzero(&args, sizeof(args));
|
||||
for (i = 3; i < argc; ++i) {
|
||||
if (argv[i][0] != '@') {
|
||||
args.p = realloc(args.p, ++args.n * sizeof(*args.p));
|
||||
args.p[args.n - 1] = strdup(argv[i]);
|
||||
} else {
|
||||
CHECK_NOTNULL((f = fopen(argv[i] + 1, "r")));
|
||||
while ((line = chomp(xgetline(f)))) {
|
||||
if (!isempty(line)) {
|
||||
args.p = realloc(args.p, ++args.n * sizeof(*args.p));
|
||||
args.p[args.n - 1] = line;
|
||||
} else {
|
||||
free(line);
|
||||
}
|
||||
}
|
||||
CHECK_NE(-1, fclose(f));
|
||||
}
|
||||
}
|
||||
|
||||
st = xmalloc(sizeof(struct stat));
|
||||
symbols.i = 0;
|
||||
symbols.n = 4096;
|
||||
symbols.p = xmalloc(symbols.n);
|
||||
filenames.i = 0;
|
||||
filenames.n = 1024;
|
||||
filenames.p = xmalloc(filenames.n);
|
||||
symnames.i = 0;
|
||||
symnames.n = 1024;
|
||||
symnames.p = xmalloc(symnames.n * sizeof(int));
|
||||
|
||||
outpath = argv[2];
|
||||
modes = xmalloc(sizeof(int) * args.n);
|
||||
names = xmalloc(sizeof(int) * args.n);
|
||||
sizes = xmalloc(sizeof(int) * args.n);
|
||||
bzero(&args, sizeof(args));
|
||||
st = xmalloc(sizeof(struct stat));
|
||||
NewInts(&modes, 128);
|
||||
NewInts(&names, 128);
|
||||
NewInts(&sizes, 128);
|
||||
NewInts(&symnames, 1024);
|
||||
NewString(&symbols, 4096);
|
||||
NewString(&filenames, 1024);
|
||||
getargs_init(&ga, argv + 3);
|
||||
|
||||
// load global symbols and populate page cache
|
||||
for (i = 0; i < args.n; ++i) {
|
||||
for (i = 0;; ++i) {
|
||||
TryAgain:
|
||||
CHECK_NE(-1, (fd = open(args.p[i], O_RDONLY)), "%s", args.p[i]);
|
||||
if (!(arg = getargs_next(&ga))) break;
|
||||
CHECK_NE(-1, (fd = open(arg, O_RDONLY)), "%s", arg);
|
||||
CHECK_NE(-1, fstat(fd, st));
|
||||
CHECK_LT(st->st_size, 0x7ffff000);
|
||||
if (!st->st_size || S_ISDIR(st->st_mode) || endswith(args.p[i], ".pkg")) {
|
||||
close(fd);
|
||||
for (j = i; j + 1 < args.n; ++j) {
|
||||
args.p[j] = args.p[j + 1];
|
||||
}
|
||||
--args.n;
|
||||
if (!st->st_size || S_ISDIR(st->st_mode) || endswith(arg, ".pkg")) {
|
||||
goto TryAgain;
|
||||
}
|
||||
names[i] = filenames.i;
|
||||
sizes[i] = st->st_size;
|
||||
modes[i] = st->st_mode;
|
||||
CONCAT(&filenames.p, &filenames.i, &filenames.n, basename(args.p[i]),
|
||||
strlen(basename(args.p[i])));
|
||||
AppendArg(&args, xstrdup(arg));
|
||||
AppendInt(&names, filenames.i);
|
||||
AppendInt(&sizes, st->st_size);
|
||||
AppendInt(&modes, st->st_mode);
|
||||
CONCAT(&filenames.p, &filenames.i, &filenames.n, basename(arg),
|
||||
strlen(basename(arg)));
|
||||
CONCAT(&filenames.p, &filenames.i, &filenames.n, "/\n", 2);
|
||||
CHECK_NE(MAP_FAILED,
|
||||
(elf = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0)));
|
||||
CHECK(IsElf64Binary(elf, st->st_size), "%s", args.p[i]);
|
||||
(elf = mmap(0, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0)));
|
||||
CHECK(IsElf64Binary(elf, st->st_size), "%s", arg);
|
||||
CHECK_NOTNULL((strs = GetElfStringTable(elf, st->st_size)));
|
||||
CHECK_NOTNULL((syms = GetElfSymbolTable(elf, st->st_size, &symcount)));
|
||||
for (j = 0; j < symcount; ++j) {
|
||||
|
@ -198,7 +197,7 @@ int main(int argc, char *argv[]) {
|
|||
if (ELF64_ST_BIND(syms[j].st_info) == STB_LOCAL) continue;
|
||||
symname = GetElfString(elf, st->st_size, strs, syms[j].st_name);
|
||||
CONCAT(&symbols.p, &symbols.i, &symbols.n, symname, strlen(symname) + 1);
|
||||
APPEND(&symnames.p, &symnames.i, &symnames.n, &i);
|
||||
AppendInt(&symnames, i);
|
||||
}
|
||||
CHECK_NE(-1, munmap(elf, st->st_size));
|
||||
close(fd);
|
||||
|
@ -209,7 +208,7 @@ int main(int argc, char *argv[]) {
|
|||
outsize = 0;
|
||||
tablebufsize = 4 + symnames.i * 4;
|
||||
tablebuf = xmalloc(tablebufsize);
|
||||
offsets = xmalloc(args.n * 4);
|
||||
offsets = xmalloc(args.i * 4);
|
||||
header1 = xmalloc(sizeof(struct Header));
|
||||
header2 = xmalloc(sizeof(struct Header));
|
||||
iov[0].iov_base = "!<arch>\n";
|
||||
|
@ -226,11 +225,11 @@ int main(int argc, char *argv[]) {
|
|||
outsize += (iov[5].iov_len = 60);
|
||||
iov[6].iov_base = filenames.p;
|
||||
outsize += (iov[6].iov_len = filenames.i);
|
||||
for (i = 0; i < args.n; ++i) {
|
||||
for (i = 0; i < args.i; ++i) {
|
||||
outsize += outsize & 1;
|
||||
offsets[i] = outsize;
|
||||
outsize += 60;
|
||||
outsize += sizes[i];
|
||||
outsize += sizes.p[i];
|
||||
}
|
||||
CHECK_LE(outsize, 0x7ffff000);
|
||||
|
||||
|
@ -245,40 +244,60 @@ int main(int argc, char *argv[]) {
|
|||
// write output archive
|
||||
CHECK_NE(-1, (outfd = open(outpath, O_WRONLY | O_TRUNC | O_CREAT, 0644)));
|
||||
ftruncate(outfd, outsize);
|
||||
if ((outsize = writev(outfd, iov, ARRAYLEN(iov))) == -1) goto fail;
|
||||
for (i = 0; i < args.n; ++i) {
|
||||
if ((fd = open(args.p[i], O_RDONLY)) == -1) goto fail;
|
||||
if ((outsize = writev(outfd, iov, ARRAYLEN(iov))) == -1) {
|
||||
reason = "writev1 failed";
|
||||
goto fail;
|
||||
}
|
||||
for (i = 0; i < args.i; ++i) {
|
||||
if ((fd = open(args.p[i], O_RDONLY)) == -1) {
|
||||
reason = "open failed";
|
||||
goto fail;
|
||||
}
|
||||
iov[0].iov_base = "\n";
|
||||
outsize += (iov[0].iov_len = outsize & 1);
|
||||
iov[1].iov_base = header1;
|
||||
outsize += (iov[1].iov_len = 60);
|
||||
MakeHeader(header1, "/", names[i], modes[i], sizes[i]);
|
||||
if (writev(outfd, iov, 2) == -1) goto fail;
|
||||
outsize += (remain = sizes[i]);
|
||||
if (copy_file_range(fd, NULL, outfd, NULL, remain, 0) != remain) goto fail;
|
||||
MakeHeader(header1, "/", names.p[i], modes.p[i], sizes.p[i]);
|
||||
if (writev(outfd, iov, 2) == -1) {
|
||||
reason = "writev2 failed";
|
||||
goto fail;
|
||||
}
|
||||
outsize += (remain = sizes.p[i]);
|
||||
while ((rc = copy_file_range(fd, 0, outfd, 0, remain, 0)) < remain) {
|
||||
if (rc <= 0) {
|
||||
reason = "copy_file_range failed";
|
||||
goto fail;
|
||||
} else {
|
||||
remain -= rc;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
close(outfd);
|
||||
|
||||
for (i = 0; i < args.n; ++i) free(args.p[i]);
|
||||
free(args.p);
|
||||
free(header2);
|
||||
free(header1);
|
||||
free(offsets);
|
||||
free(tablebuf);
|
||||
free(sizes);
|
||||
free(names);
|
||||
free(modes);
|
||||
free(symbols.p);
|
||||
for (i = 0; i < args.i; ++i) free(args.p[i]);
|
||||
getargs_destroy(&ga);
|
||||
free(filenames.p);
|
||||
free(symnames.p);
|
||||
free(symbols.p);
|
||||
free(tablebuf);
|
||||
free(modes.p);
|
||||
free(names.p);
|
||||
free(sizes.p);
|
||||
free(offsets);
|
||||
free(header1);
|
||||
free(header2);
|
||||
free(args.p);
|
||||
free(st);
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
err = errno;
|
||||
if (!err) err = 1;
|
||||
unlink(outpath);
|
||||
fputs("error: ar failed\n", stderr);
|
||||
return err;
|
||||
fputs("error: ar failed: ", stderr);
|
||||
fputs(reason, stderr);
|
||||
fputs(": ", stderr);
|
||||
fputs(strerror(err), stderr);
|
||||
fputs("\n", stderr);
|
||||
return 1;
|
||||
}
|
||||
|
|
|
@ -1410,7 +1410,7 @@ static void DrawMemoryUnzoomed(struct Panel *p, struct MemoryView *view,
|
|||
x = 129; /* PURPLE: shadow corruption */
|
||||
} else if (sc == kAsanHeapFree) {
|
||||
x = 20; /* BLUE: heap freed */
|
||||
} else if (sc == kAsanRelocated) {
|
||||
} else if (sc == kAsanHeapRelocated) {
|
||||
x = 16; /* BLACK: heap relocated */
|
||||
} else if (sc == kAsanHeapUnderrun || sc == kAsanAllocaUnderrun) {
|
||||
x = 53; /* RED+PURPLETINGE: heap underrun */
|
||||
|
|
|
@ -482,7 +482,7 @@ void SetMemLimit(long n) {
|
|||
struct rlimit rlim = {n, n};
|
||||
if (n <= 0) return;
|
||||
if (IsWindows() || IsXnu()) return;
|
||||
setrlimit(!IsOpenbsd() ? RLIMIT_AS : RLIMIT_DATA, &rlim);
|
||||
setrlimit(RLIMIT_AS, &rlim);
|
||||
}
|
||||
|
||||
int Launch(void) {
|
||||
|
|
171
tool/build/lib/getargs.c
Normal file
171
tool/build/lib/getargs.c
Normal file
|
@ -0,0 +1,171 @@
|
|||
/*-*- 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 2021 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/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/stat.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "tool/build/lib/getargs.h"
|
||||
|
||||
/**
|
||||
* @fileoverview Fast Command Line Argument Ingestion.
|
||||
*
|
||||
* The purpose of this library is to be able to have build commands with
|
||||
* huge argument lists. The way we do that is by replacing commands like
|
||||
*
|
||||
* foo.com lots of args
|
||||
*
|
||||
* with this
|
||||
*
|
||||
* echo of args >args
|
||||
* foo.com lots @args
|
||||
*
|
||||
* This iterator abstracts the process of reading the special `@`
|
||||
* prefixed args. In order to do that quickly and easily, we make the
|
||||
* following assumptions:
|
||||
*
|
||||
* 1. Arguments don't have whitespace.
|
||||
* 2. Files have a trailing whitespace.
|
||||
*
|
||||
* We need (1) so GNU Make can go faster. Assume we tokenized based on
|
||||
* newlines. We would would write that in our Makefile as follows:
|
||||
*
|
||||
* # don't do this
|
||||
* target: thousands of args
|
||||
* $(file >$@.args) $(foreach x,$^,$(file >>$@.args,$(x)))
|
||||
* tool.com -o $@ @$@.args
|
||||
*
|
||||
* That is slow because it needs to open and close the args file
|
||||
* thousands of times. If we trade away filenames with spaces then the
|
||||
* following will only require a couple system calls:
|
||||
*
|
||||
* # do this
|
||||
* target: thousands of args
|
||||
* $(file >$@.args,$^)
|
||||
* tool.com -o $@ @$@.args
|
||||
*
|
||||
* We need (2) because it make the code in this file simpler and avoids
|
||||
* a malloc() dependency. Having that trailing character means argument
|
||||
* parsing from files can be a zero-copy operation.
|
||||
*/
|
||||
|
||||
#define IsSpace(c) ((255 & (c)) <= ' ')
|
||||
|
||||
/**
|
||||
* Zeroes GetArgs object and sets its fields.
|
||||
* @param args is borrowed for the lifetime of the GetArgs object
|
||||
*/
|
||||
void getargs_init(struct GetArgs *ga, char **args) {
|
||||
assert(args);
|
||||
bzero(ga, sizeof(*ga));
|
||||
ga->args = args;
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases memory associated with GetArgs object and zeroes it.
|
||||
*/
|
||||
void getargs_destroy(struct GetArgs *ga) {
|
||||
if (ga->map) munmap(ga->map, ga->mapsize);
|
||||
bzero(ga, sizeof(*ga));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets next argument, e.g.
|
||||
*
|
||||
* const char *s;
|
||||
* while ((s = getargs_next(&ga))) {
|
||||
* printf("%s\n", s);
|
||||
* }
|
||||
*
|
||||
* @return NUL-terminated string; it should not be freed; it should be
|
||||
* assumed that it stays in scope until the next getargs_next call
|
||||
*/
|
||||
const char *getargs_next(struct GetArgs *ga) {
|
||||
int fd;
|
||||
char *p;
|
||||
size_t k;
|
||||
unsigned m;
|
||||
struct stat st;
|
||||
do {
|
||||
if (ga->map) {
|
||||
for (; ga->j < ga->mapsize; ++ga->j) {
|
||||
if (!IsSpace(ga->map[ga->j])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
k = 0;
|
||||
#if defined(__x86__) && defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
||||
typedef unsigned char xmm_t
|
||||
__attribute__((__vector_size__(16), __aligned__(1)));
|
||||
for (; ga->j + k + 16 <= ga->mapsize; k += 16) {
|
||||
if ((m = __builtin_ia32_pmovmskb128(
|
||||
*(const xmm_t *)(ga->map + ga->j + k) >
|
||||
(xmm_t){' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||
' ', ' ', ' ', ' ', ' ', ' '}) ^
|
||||
0xffff)) {
|
||||
k += __builtin_ctzl(m);
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
for (; ga->j + k < ga->mapsize; ++k) {
|
||||
if (IsSpace(ga->map[ga->j + k])) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (k) {
|
||||
if (ga->j + k < ga->mapsize) {
|
||||
ga->map[ga->j + k] = 0;
|
||||
p = ga->map + ga->j;
|
||||
ga->j += ++k;
|
||||
return p;
|
||||
} else {
|
||||
eio();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (munmap(ga->map, ga->mapsize) == -1) break;
|
||||
ga->map = 0;
|
||||
ga->mapsize = 0;
|
||||
ga->j = 0;
|
||||
}
|
||||
if (!(p = ga->args[ga->i])) return 0;
|
||||
++ga->i;
|
||||
if (*p != '@') return p;
|
||||
++p;
|
||||
if ((fd = open((ga->path = p), O_RDONLY)) != -1) {
|
||||
fstat(fd, &st);
|
||||
if ((p = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,
|
||||
0)) != MAP_FAILED) {
|
||||
ga->map = p;
|
||||
ga->mapsize = st.st_size;
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
} while (ga->map);
|
||||
perror(ga->path);
|
||||
exit(1);
|
||||
}
|
20
tool/build/lib/getargs.h
Normal file
20
tool/build/lib/getargs.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
#ifndef COSMOPOLITAN_TOOL_BUILD_LIB_GETARGS_H_
|
||||
#define COSMOPOLITAN_TOOL_BUILD_LIB_GETARGS_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
struct GetArgs {
|
||||
size_t i, j;
|
||||
char **args;
|
||||
char *path;
|
||||
char *map;
|
||||
size_t mapsize;
|
||||
};
|
||||
|
||||
void getargs_init(struct GetArgs *, char **);
|
||||
const char *getargs_next(struct GetArgs *);
|
||||
void getargs_destroy(struct GetArgs *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_TOOL_BUILD_LIB_GETARGS_H_ */
|
|
@ -60,6 +60,7 @@
|
|||
#include "libc/sysv/consts/so.h"
|
||||
#include "libc/sysv/consts/sock.h"
|
||||
#include "libc/sysv/consts/sol.h"
|
||||
#include "libc/sysv/consts/ss.h"
|
||||
#include "libc/sysv/consts/tcp.h"
|
||||
#include "libc/sysv/consts/termios.h"
|
||||
#include "libc/sysv/consts/w.h"
|
||||
|
|
|
@ -45,6 +45,7 @@
|
|||
#include "libc/sysv/consts/prot.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "third_party/getopt/getopt.h"
|
||||
#include "tool/build/lib/getargs.h"
|
||||
|
||||
#define MAX_READ FRAMESIZE
|
||||
|
||||
|
@ -223,59 +224,51 @@ wontreturn void OnMissingFile(const char *list, const char *src) {
|
|||
* notation on mkdeps related rules, the build will
|
||||
* automatically restart itself.
|
||||
*/
|
||||
fprintf(stderr, "%s %s...\n", "Refreshing", list);
|
||||
unlink(list);
|
||||
if (list) {
|
||||
fprintf(stderr, "%s %s...\n", "Refreshing", list);
|
||||
unlink(list);
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void LoadRelationships(int argc, char *argv[]) {
|
||||
int fd;
|
||||
char *buf;
|
||||
ssize_t rc;
|
||||
bool skipme;
|
||||
FILE *finpaths;
|
||||
struct Edge edge;
|
||||
char *line, *buf;
|
||||
struct GetArgs ga;
|
||||
unsigned srcid, dependency;
|
||||
size_t i, linecap, inclen, size;
|
||||
size_t i, inclen, size;
|
||||
const char *p, *pe, *src, *path, *pathend;
|
||||
line = NULL;
|
||||
linecap = 0;
|
||||
inclen = strlen(kIncludePrefix);
|
||||
buf = gc(xmemalign(PAGESIZE, PAGESIZE + MAX_READ + 16));
|
||||
buf += PAGESIZE;
|
||||
buf[-1] = '\n';
|
||||
for (i = optind; i < argc; ++i) {
|
||||
if (!(finpaths = fopen(argv[i], "r"))) {
|
||||
fprintf(stderr, "\n\e[1mERROR: %s FAILED BECAUSE %s CAUSED %m\e[0m\n\n",
|
||||
argv[0], argv[i]);
|
||||
exit(1);
|
||||
}
|
||||
while (getline(&line, &linecap, finpaths) != -1) {
|
||||
src = chomp(line);
|
||||
if (ShouldSkipSource(src)) continue;
|
||||
srcid = GetSourceId(src, strlen(src));
|
||||
if ((fd = open(src, O_RDONLY)) == -1) OnMissingFile(argv[i], src);
|
||||
CHECK_NE(-1, (rc = read(fd, buf, MAX_READ)));
|
||||
close(fd);
|
||||
size = rc;
|
||||
bzero(buf + size, 16);
|
||||
for (p = buf, pe = p + size; p < pe; ++p) {
|
||||
p = strstr(p, kIncludePrefix);
|
||||
if (!p) break;
|
||||
path = p + inclen;
|
||||
pathend = memchr(path, '"', pe - path);
|
||||
if (pathend && (p[-1] == '#' || p[-1] == '.') && p[-2] == '\n') {
|
||||
dependency = GetSourceId(path, pathend - path);
|
||||
edge.from = srcid;
|
||||
edge.to = dependency;
|
||||
append(&edges, &edge);
|
||||
p = pathend;
|
||||
}
|
||||
getargs_init(&ga, argv + optind);
|
||||
while ((src = getargs_next(&ga))) {
|
||||
if (ShouldSkipSource(src)) continue;
|
||||
srcid = GetSourceId(src, strlen(src));
|
||||
if ((fd = open(src, O_RDONLY)) == -1) OnMissingFile(ga.path, src);
|
||||
CHECK_NE(-1, (rc = read(fd, buf, MAX_READ)));
|
||||
close(fd);
|
||||
size = rc;
|
||||
bzero(buf + size, 16);
|
||||
for (p = buf, pe = p + size; p < pe; ++p) {
|
||||
p = strstr(p, kIncludePrefix);
|
||||
if (!p) break;
|
||||
path = p + inclen;
|
||||
pathend = memchr(path, '"', pe - path);
|
||||
if (pathend && (p[-1] == '#' || p[-1] == '.') && p[-2] == '\n') {
|
||||
dependency = GetSourceId(path, pathend - path);
|
||||
edge.from = srcid;
|
||||
edge.to = dependency;
|
||||
append(&edges, &edge);
|
||||
p = pathend;
|
||||
}
|
||||
}
|
||||
CHECK_NE(-1, fclose(finpaths));
|
||||
}
|
||||
free(line);
|
||||
getargs_destroy(&ga);
|
||||
}
|
||||
|
||||
void GetOpts(int argc, char *argv[]) {
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#include "libc/sysv/consts/map.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/sysv/consts/prot.h"
|
||||
#include "tool/build/lib/getargs.h"
|
||||
|
||||
#define LOOKINGAT(p, pe, s) LookingAt(p, pe, s, strlen(s))
|
||||
#define APPENDSTR(s) AppendData(s, strlen(s))
|
||||
|
@ -137,7 +138,8 @@ static void Visit(const char *path) {
|
|||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int i;
|
||||
const char *src;
|
||||
struct GetArgs ga;
|
||||
APPENDSTR("#ifndef COSMOPOLITAN_H_\n");
|
||||
APPENDSTR("#define COSMOPOLITAN_H_\n");
|
||||
/* APPENDSTR("#define IMAGE_BASE_VIRTUAL "); */
|
||||
|
@ -146,9 +148,11 @@ int main(int argc, char *argv[]) {
|
|||
/* APPENDSTR("#define IMAGE_BASE_PHYSICAL "); */
|
||||
/* AppendInt(IMAGE_BASE_PHYSICAL); */
|
||||
/* APPENDSTR("\n"); */
|
||||
for (i = 1; i < argc; ++i) {
|
||||
Visit(argv[i]);
|
||||
getargs_init(&ga, argv + 1);
|
||||
while ((src = getargs_next(&ga))) {
|
||||
Visit(src);
|
||||
}
|
||||
getargs_destroy(&ga);
|
||||
APPENDSTR("\n");
|
||||
APPENDSTR("#endif /* COSMOPOLITAN_H_ */\n");
|
||||
CHECK_EQ(output.i, write(1, output.p, output.i));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue