diff --git a/ape/ape-m1.c b/ape/ape-m1.c index 1cd64af16..f7e7941c7 100644 --- a/ape/ape-m1.c +++ b/ape/ape-m1.c @@ -87,11 +87,9 @@ struct Syslib { #define AT_RANDOM 25 #define AT_EXECFN 31 -#define STACK_SIZE (8ul * 1024 * 1024) -#define STACK_ALIGN (sizeof(long) * 2) -#define AUXV_BYTES (sizeof(long) * 2 * 15) +#define AUXV_WORDS 29 -// from the xnu codebase +/* from the xnu codebase */ #define _COMM_PAGE_START_ADDRESS 0x0000000FFFFFC000ul #define _COMM_PAGE_APRR_SUPPORT (_COMM_PAGE_START_ADDRESS + 0x10C) #define _COMM_PAGE_APRR_WRITE_ENABLE (_COMM_PAGE_START_ADDRESS + 0x110) @@ -149,7 +147,7 @@ union ElfEhdrBuf { union ElfPhdrBuf { struct ElfPhdr phdr; - char buf[4096]; + char buf[1024]; }; struct PathSearcher { @@ -160,15 +158,7 @@ struct PathSearcher { }; struct ApeLoader { - union ElfEhdrBuf ehdr; struct PathSearcher ps; - // this memory shall be discarded by the guest - ////////////////////////////////////////////// - // this memory shall be known to guest program - union { - char argblock[ARG_MAX]; - long numblock[ARG_MAX / sizeof(long)]; - }; union ElfPhdrBuf phdr; struct Syslib lib; char rando[16]; @@ -561,19 +551,15 @@ __attribute__((__noreturn__)) static void Spawn(const char *exe, int fd, unsigned long wipe; prot1 = prot; prot2 = prot; - /* - * when we ask the system to map the interval [vaddr,vaddr+filesz) - * it might schlep extra file content into memory on both the left - * and the righthand side. that's because elf doesn't require that - * either side of the interval be aligned on the system page size. - * - * normally we can get away with ignoring these junk bytes. but if - * the segment defines bss memory (i.e. memsz > filesz) then we'll - * need to clear the extra bytes in the page, if they exist. - * - * since we can't do that if we're mapping a read-only page, we'll - * actually map it with write permissions and protect it afterward - */ + /* when we ask the system to map the interval [vaddr,vaddr+filesz) + it might schlep extra file content into memory on both the left + and the righthand side. that's because elf doesn't require that + either side of the interval be aligned on the system page size. + normally we can get away with ignoring these junk bytes. but if + the segment defines bss memory (i.e. memsz > filesz) then we'll + need to clear the extra bytes in the page, if they exist. since + we can't do that if we're mapping a read-only page, we can just + map it with write permissions and call mprotect on it afterward */ a = p[i].p_vaddr + p[i].p_filesz; /* end of file content */ b = (a + (pagesz - 1)) & -pagesz; /* first pure bss page */ c = p[i].p_vaddr + p[i].p_memsz; /* end of segment data */ @@ -648,16 +634,17 @@ __attribute__((__noreturn__)) static void Spawn(const char *exe, int fd, __builtin_unreachable(); } -static const char *TryElf(struct ApeLoader *M, const char *exe, int fd, - long *sp, long *bp, char *execfn) { +static const char *TryElf(struct ApeLoader *M, union ElfEhdrBuf *ebuf, + const char *exe, int fd, long *sp, long *auxv, + char *execfn) { long i, rc; unsigned size; struct ElfEhdr *e; struct ElfPhdr *p; /* validate elf header */ - e = &M->ehdr.ehdr; - if (READ32(M->ehdr.buf) != READ32("\177ELF")) { + e = &ebuf->ehdr; + if (READ32(ebuf->buf) != READ32("\177ELF")) { return "didn't embed ELF magic"; } if (e->e_ident[EI_CLASS] == ELFCLASS32) { @@ -678,7 +665,7 @@ static const char *TryElf(struct ApeLoader *M, const char *exe, int fd, } /* read program headers */ - rc = pread(fd, M->phdr.buf, size, M->ehdr.ehdr.e_phoff); + rc = pread(fd, M->phdr.buf, size, ebuf->ehdr.e_phoff); if (rc < 0) return "failed to read ELF program headers"; if (rc != size) return "truncated read of ELF program headers"; @@ -735,26 +722,35 @@ static const char *TryElf(struct ApeLoader *M, const char *exe, int fd, } /* simulate linux auxiliary values */ - long auxv[][2] = { - {AT_PHDR, (long)&M->phdr.phdr}, // - {AT_PHENT, M->ehdr.ehdr.e_phentsize}, // - {AT_PHNUM, M->ehdr.ehdr.e_phnum}, // - {AT_ENTRY, M->ehdr.ehdr.e_entry}, // - {AT_PAGESZ, pagesz}, // - {AT_UID, getuid()}, // - {AT_EUID, geteuid()}, // - {AT_GID, getgid()}, // - {AT_EGID, getegid()}, // - {AT_HWCAP, 0xffb3ffffu}, // - {AT_HWCAP2, 0x181}, // - {AT_SECURE, issetugid()}, // - {AT_RANDOM, (long)M->rando}, // - {AT_EXECFN, (long)execfn}, // - {0, 0}, // - }; - _Static_assert(sizeof(auxv) == AUXV_BYTES, - "Please update the AUXV_BYTES constant"); - MemMove(bp, auxv, sizeof(auxv)); + auxv[0] = AT_PHDR; + auxv[1] = (long)&M->phdr.phdr; + auxv[2] = AT_PHENT; + auxv[3] = ebuf->ehdr.e_phentsize; + auxv[4] = AT_PHNUM; + auxv[5] = ebuf->ehdr.e_phnum; + auxv[6] = AT_ENTRY; + auxv[7] = ebuf->ehdr.e_entry; + auxv[8] = AT_PAGESZ; + auxv[9] = pagesz; + auxv[10] = AT_UID; + auxv[11] = getuid(); + auxv[12] = AT_EUID; + auxv[13] = geteuid(); + auxv[14] = AT_GID; + auxv[15] = getgid(); + auxv[16] = AT_EGID; + auxv[17] = getegid(); + auxv[18] = AT_HWCAP; + auxv[19] = 0xffb3ffffu; + auxv[20] = AT_HWCAP2; + auxv[21] = 0x181; + auxv[22] = AT_SECURE; + auxv[23] = issetugid(); + auxv[24] = AT_RANDOM; + auxv[25] = (long)M->rando; + auxv[26] = AT_EXECFN; + auxv[27] = (long)execfn; + auxv[28] = 0; /* we're now ready to load */ Spawn(exe, fd, sp, e, p, &M->lib); @@ -788,31 +784,17 @@ static long sys_mmap(void *addr, size_t size, int prot, int flags, int fd, int main(int argc, char **argv, char **envp) { long z; void *map; - long *sp, *bp, *ip; int c, i, n, fd, rc; struct ApeLoader *M; - unsigned char rando[24]; + long *sp, *sp2, *auxv; + union ElfEhdrBuf *ebuf; char *p, *pe, *tp, *exe, *prog, *execfn; - // generate some hard random data - if (getentropy(rando, sizeof(rando))) { - Pexit(argv[0], -1, "getentropy"); - } + /* allocate loader memory in program's arg block */ + n = sizeof(struct ApeLoader); + M = __builtin_alloca(n); - // make the stack look like a linux one - map = mmap((void *)(0x7f0000000000 | (long)rando[23] << 32), STACK_SIZE, - PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - if (map == MAP_FAILED) { - Pexit(argv[0], -1, "stack mmap"); - } - - // put argument block at top of allocated stack - z = (long)map; - z += STACK_SIZE - sizeof(struct ApeLoader); - z &= -_Alignof(struct ApeLoader); - M = (struct ApeLoader *)z; - - // expose screwy apple libs + /* expose apple libs */ M->lib.magic = SYSLIB_MAGIC; M->lib.version = SYSLIB_VERSION; M->lib.fork = sys_fork; @@ -833,44 +815,24 @@ int main(int argc, char **argv, char **envp) { M->lib.dispatch_semaphore_wait = dispatch_semaphore_wait; M->lib.dispatch_walltime = dispatch_walltime; - // copy system provided argument block - bp = M->numblock; - tp = M->argblock + sizeof(M->argblock); - *bp++ = argc; - for (i = 0; i < argc; ++i) { - tp -= (n = StrLen(argv[i]) + 1); - MemMove(tp, argv[i], n); - *bp++ = (long)tp; - } - *bp++ = 0; - for (i = 0; envp[i]; ++i) { - tp -= (n = StrLen(envp[i]) + 1); - MemMove(tp, envp[i], n); - *bp++ = (long)tp; - } - *bp++ = 0; - - // get arguments that point into our block - sp = M->numblock; - argc = *sp; - argv = (char **)(sp + 1); - envp = (char **)(sp + 1 + argc + 1); - - // xnu stores getauxval(at_execfn) in getenv("_") + /* getenv("_") is close enough to at_execfn */ execfn = argc > 0 ? argv[0] : 0; for (i = 0; envp[i]; ++i) { if (envp[i][0] == '_' && envp[i][1] == '=') { execfn = envp[i] + 2; - break; } } - // interpret command line arguments + /* sneak the system five abi back out of args */ + sp = (long *)(argv - 1); + auxv = (long *)(envp + i + 1); + + /* interpret command line arguments */ if (argc >= 3 && !StrCmp(argv[1], "-")) { - // if the first argument is a hyphen then we give the user the - // power to change argv[0] or omit it entirely. most operating - // systems don't permit the omission of argv[0] but we do, b/c - // it's specified by ANSI X3.159-1988. + /* if the first argument is a hyphen then we give the user the + power to change argv[0] or omit it entirely. most operating + systems don't permit the omission of argv[0] but we do, b/c + it's specified by ANSI X3.159-1988. */ prog = (char *)sp[3]; argc = sp[3] = sp[0] - 3; argv = (char **)((sp += 3) + 1); @@ -887,19 +849,41 @@ int main(int argc, char **argv, char **envp) { argv = (char **)((sp += 1) + 1); } - // search for executable + /* create new bottom of stack for spawned program + system v abi aligns this on a 16-byte boundary + grows down the alloc by poking the guard pages */ + n = (auxv - sp + AUXV_WORDS + 1) * sizeof(long); + sp2 = __builtin_alloca(n); + if ((long)sp2 & 15) ++sp2; + for (; n > 0; n -= pagesz) { + ((char *)sp2)[n - 1] = 0; + } + MemMove(sp2, sp, (auxv - sp) * sizeof(long)); + argv = (char **)(sp2 + 1); + envp = (char **)(sp2 + 1 + argc + 1); + auxv = sp2 + (auxv - sp); + sp = sp2; + + /* allocate ephemeral memory for reading file */ + n = sizeof(union ElfEhdrBuf); + ebuf = __builtin_alloca(n); + for (; n > 0; n -= pagesz) { + ((char *)ebuf)[n - 1] = 0; + } + + /* search for executable */ if (!(exe = Commandv(&M->ps, prog, GetEnv(envp, "PATH")))) { Pexit(prog, 0, "not found (maybe chmod +x)"); } else if ((fd = openat(AT_FDCWD, exe, O_RDONLY)) < 0) { Pexit(exe, -1, "open"); - } else if ((rc = read(fd, M->ehdr.buf, sizeof(M->ehdr.buf))) < 0) { + } else if ((rc = read(fd, ebuf->buf, sizeof(ebuf->buf))) < 0) { Pexit(exe, -1, "read"); - } else if ((unsigned long)rc < sizeof(M->ehdr.ehdr)) { + } else if ((unsigned long)rc < sizeof(ebuf->ehdr)) { Pexit(exe, 0, "too small"); } - pe = M->ehdr.buf + rc; + pe = ebuf->buf + rc; - // resolve argv[0] to reflect path search + /* resolve argv[0] to reflect path search */ if ((argc > 0 && *prog != '/' && *exe == '/' && !StrCmp(prog, argv[0])) || !StrCmp(BaseName(prog), argv[0])) { tp -= (n = StrLen(exe) + 1); @@ -907,28 +891,20 @@ int main(int argc, char **argv, char **envp) { argv[0] = tp; } - // squeeze and align the argument block - ip = (long *)(((long)tp - AUXV_BYTES) & -sizeof(long)); - ip -= (n = bp - sp); - ip = (long *)((long)ip & -STACK_ALIGN); - MemMove(ip, sp, n * sizeof(long)); - bp = ip + n; - sp = ip; + /* generate some hard random data */ + if (getentropy(M->rando, sizeof(M->rando))) { + Pexit(argv[0], -1, "getentropy"); + } - // relocate the guest's random numbers - MemMove(M->rando, rando, sizeof(M->rando)); - Bzero(rando, sizeof(rando)); - - // ape intended behavior - // 1. if file is an elf executable, it'll be used as-is - // 2. if ape, will scan shell script for elf printf statements - // 3. shell script may have multiple lines producing elf headers - // 4. all elf printf lines must exist in the first 8192 bytes of file - // 5. elf program headers may appear anywhere in the binary - if (READ64(M->ehdr.buf) == READ64("MZqFpD='") || - READ64(M->ehdr.buf) == READ64("jartsr='") || - READ64(M->ehdr.buf) == READ64("APEDBG='")) { - for (p = M->ehdr.buf; p < pe; ++p) { + /* ape intended behavior + 1. if ape, will scan shell script for elf printf statements + 2. shell script may have multiple lines producing elf headers + 3. all elf printf lines must exist in the first 8192 bytes of file + 4. elf program headers may appear anywhere in the binary */ + if (READ64(ebuf->buf) == READ64("MZqFpD='") || + READ64(ebuf->buf) == READ64("jartsr='") || + READ64(ebuf->buf) == READ64("APEDBG='")) { + for (p = ebuf->buf; p < pe; ++p) { if (READ64(p) != READ64("printf '")) { continue; } @@ -946,15 +922,15 @@ int main(int argc, char **argv, char **envp) { } } } - M->ehdr.buf[i++] = c; - if (i >= sizeof(M->ehdr.buf)) { + ebuf->buf[i++] = c; + if (i >= sizeof(ebuf->buf)) { break; } } - if (i >= sizeof(M->ehdr.ehdr)) { - TryElf(M, exe, fd, sp, bp, execfn); + if (i >= sizeof(ebuf->ehdr)) { + TryElf(M, ebuf, exe, fd, sp, auxv, execfn); } } } - Pexit(exe, 0, TryElf(M, exe, fd, sp, bp, execfn)); + Pexit(exe, 0, TryElf(M, ebuf, exe, fd, sp, auxv, execfn)); } diff --git a/ape/ape.S b/ape/ape.S index ed97635b0..292216969 100644 --- a/ape/ape.S +++ b/ape/ape.S @@ -610,7 +610,7 @@ apesh: .ascii "\n@\n#'\"\n" // sixth edition shebang // extract the loader into a temp folder, and use it to // load the APE without modifying it. .ascii "[ x\"$1\" != x--assimilate ] && {\n" - .ascii "t=\"${TMPDIR:-${HOME:-.}}/.ape-1.6\"\n" + .ascii "t=\"${TMPDIR:-${HOME:-.}}/.ape-1.7\"\n" .ascii "[ -x \"$t\" ] || {\n" .ascii "mkdir -p \"${t%/*}\" &&\n" .ascii "dd if=\"$o\" of=\"$t.$$\" skip=" @@ -818,7 +818,7 @@ ape.ident: .long 1 1: .asciz "APE" 2: .balign 4 -3: .long 106000000 +3: .long 107000000 4: .size ape.ident,.-ape.ident .type ape.ident,@object .previous diff --git a/ape/loader.c b/ape/loader.c index 065b0444a..2b5d898d9 100644 --- a/ape/loader.c +++ b/ape/loader.c @@ -158,7 +158,7 @@ { \ char ibuf[19] = {0}; \ Utox(ibuf, VAR); \ - Print(os, 2, #VAR " ", ibuf, "\n", 0l); \ + Print(os, 2, ibuf, " " #VAR, "\n", 0l); \ } struct ElfEhdr { @@ -196,7 +196,7 @@ union ElfEhdrBuf { union ElfPhdrBuf { struct ElfPhdr phdr; - char buf[4096]; + char buf[1024]; }; struct PathSearcher { @@ -208,7 +208,6 @@ struct PathSearcher { }; struct ApeLoader { - union ElfEhdrBuf ehdr; union ElfPhdrBuf phdr; struct PathSearcher ps; char path[1024]; @@ -622,7 +621,6 @@ __attribute__((__noreturn__)) static void Spawn(int os, const char *exe, int fd, found_code = 0; found_entry = 0; virtmin = virtmax = 0; - if (!pagesz) pagesz = 4096; if (pagesz & (pagesz - 1)) { Pexit(os, exe, 0, "AT_PAGESZ isn't two power"); } @@ -715,19 +713,15 @@ __attribute__((__noreturn__)) static void Spawn(int os, const char *exe, int fd, unsigned long wipe; prot1 = prot; prot2 = prot; - /* - * when we ask the system to map the interval [vaddr,vaddr+filesz) - * it might schlep extra file content into memory on both the left - * and the righthand side. that's because elf doesn't require that - * either side of the interval be aligned on the system page size. - * - * normally we can get away with ignoring these junk bytes. but if - * the segment defines bss memory (i.e. memsz > filesz) then we'll - * need to clear the extra bytes in the page, if they exist. - * - * since we can't do that if we're mapping a read-only page, we'll - * actually map it with write permissions and protect it afterward - */ + /* when we ask the system to map the interval [vaddr,vaddr+filesz) + it might schlep extra file content into memory on both the left + and the righthand side. that's because elf doesn't require that + either side of the interval be aligned on the system page size. + normally we can get away with ignoring these junk bytes. but if + the segment defines bss memory (i.e. memsz > filesz) then we'll + need to clear the extra bytes in the page, if they exist. since + we can't do that if we're mapping a read-only page, we can just + map it with write permissions and call mprotect on it afterward */ a = p[i].p_vaddr + p[i].p_filesz; /* end of file content */ b = (a + (pagesz - 1)) & -pagesz; /* first pure bss page */ c = p[i].p_vaddr + p[i].p_memsz; /* end of segment data */ @@ -768,8 +762,9 @@ __attribute__((__noreturn__)) static void Spawn(int os, const char *exe, int fd, Launch(IsFreebsd() ? sp : 0, dynbase + e->e_entry, sp, os); } -static const char *TryElf(struct ApeLoader *M, const char *exe, int fd, - long *sp, long *auxv, unsigned long pagesz, int os) { +static const char *TryElf(struct ApeLoader *M, union ElfEhdrBuf *ebuf, + const char *exe, int fd, long *sp, long *auxv, + unsigned long pagesz, int os) { long i, rc; unsigned size; struct ElfEhdr *e; @@ -782,8 +777,8 @@ static const char *TryElf(struct ApeLoader *M, const char *exe, int fd, } /* validate elf header */ - e = &M->ehdr.ehdr; - if (READ32(M->ehdr.buf) != READ32("\177ELF")) { + e = &ebuf->ehdr; + if (READ32(ebuf->buf) != READ32("\177ELF")) { return "didn't embed ELF magic"; } if (e->e_ident[EI_CLASS] == ELFCLASS32) { @@ -891,7 +886,7 @@ static __attribute__((__noreturn__)) void ShowUsage(int os, int fd, int rc) { Print(os, fd, "NAME\n" "\n" - " actually portable executable loader version 1.6\n" + " actually portable executable loader version 1.7\n" " copyright 2023 justine alexandra roberts tunney\n" " https://justine.lol/ape.html\n" "\n" @@ -909,14 +904,14 @@ static __attribute__((__noreturn__)) void ShowUsage(int os, int fd, int rc) { Exit(rc, os); } -__attribute__((__noreturn__)) // -void ApeLoader(long di, long *sp, char dl) { - int rc; - unsigned i, n; +__attribute__((__noreturn__)) void ApeLoader(long di, long *sp, char dl) { + int rc, n; + unsigned i; int c, fd, os, argc; struct ApeLoader *M; unsigned long pagesz; - long *auxv, *ap, *ew; + union ElfEhdrBuf *ebuf; + long *auxv, *ap, *endp, *sp2; char *p, *pe, *exe, *ape, *prog, **argv, **envp; (void)Utox; @@ -935,7 +930,7 @@ void ApeLoader(long di, long *sp, char dl) { argc = *sp; argv = (char **)(sp + 1); envp = (char **)(sp + 1 + argc + 1); - auxv = (long *)(sp + 1 + argc + 1); + auxv = sp + 1 + argc + 1; for (;;) { if (!*auxv++) { break; @@ -960,15 +955,12 @@ void ApeLoader(long di, long *sp, char dl) { os = NETBSD; } } - ew = ap + 1; + if (!pagesz) { + pagesz = 4096; + } + endp = ap + 1; - /* allocate loader memory */ - n = sizeof(*M) / sizeof(long); - MemMove(sp - n, sp, (char *)ew - (char *)sp); - sp -= n, argv -= n, envp -= n, auxv -= n; - M = (struct ApeLoader *)(ew - n); - - /* default operating system */ + /* the default operating system */ if (!os) { os = LINUX; } @@ -1005,17 +997,43 @@ void ApeLoader(long di, long *sp, char dl) { argv = (char **)((sp += 1) + 1); } + /* allocate loader memory in program's arg block */ + n = sizeof(struct ApeLoader); + M = __builtin_alloca(n); + + /* create new bottom of stack for spawned program + system v abi aligns this on a 16-byte boundary + grows down the alloc by poking the guard pages */ + n = (endp - sp + 1) * sizeof(long); + sp2 = __builtin_alloca(n); + if ((long)sp2 & 15) ++sp2; + for (; n > 0; n -= pagesz) { + ((char *)sp2)[n - 1] = 0; + } + MemMove(sp2, sp, (endp - sp) * sizeof(long)); + argv = (char **)(sp2 + 1); + envp = (char **)(sp2 + 1 + argc + 1); + auxv = (char **)(sp2 + (auxv - sp)); + sp = sp2; + + /* allocate ephemeral memory for reading file */ + n = sizeof(union ElfEhdrBuf); + ebuf = __builtin_alloca(n); + for (; n > 0; n -= pagesz) { + ((char *)ebuf)[n - 1] = 0; + } + /* resolve path of executable and read its first page */ if (!(exe = Commandv(&M->ps, os, prog, GetEnv(envp, "PATH")))) { Pexit(os, prog, 0, "not found (maybe chmod +x or ./ needed)"); } else if ((fd = Open(exe, O_RDONLY, 0, os)) < 0) { Pexit(os, exe, fd, "open"); - } else if ((rc = Pread(fd, M->ehdr.buf, sizeof(M->ehdr.buf), 0, os)) < 0) { + } else if ((rc = Pread(fd, ebuf->buf, sizeof(ebuf->buf), 0, os)) < 0) { Pexit(os, exe, rc, "read"); - } else if ((unsigned long)rc < sizeof(M->ehdr.ehdr)) { + } else if ((unsigned long)rc < sizeof(ebuf->ehdr)) { Pexit(os, exe, 0, "too small"); } - pe = M->ehdr.buf + rc; + pe = ebuf->buf + rc; /* change argv[0] to resolved path if it's ambiguous */ if ((argc > 0 && *prog != '/' && *exe == '/' && !StrCmp(prog, argv[0])) || @@ -1028,10 +1046,10 @@ void ApeLoader(long di, long *sp, char dl) { 2. shell script may have multiple lines producing elf headers 3. all elf printf lines must exist in the first 8192 bytes of file 4. elf program headers may appear anywhere in the binary */ - if (READ64(M->ehdr.buf) == READ64("MZqFpD='") || - READ64(M->ehdr.buf) == READ64("jartsr='") || - READ64(M->ehdr.buf) == READ64("APEDBG='")) { - for (p = M->ehdr.buf; p < pe; ++p) { + if (READ64(ebuf->buf) == READ64("MZqFpD='") || + READ64(ebuf->buf) == READ64("jartsr='") || + READ64(ebuf->buf) == READ64("APEDBG='")) { + for (p = ebuf->buf; p < pe; ++p) { if (READ64(p) != READ64("printf '")) { continue; } @@ -1049,15 +1067,15 @@ void ApeLoader(long di, long *sp, char dl) { } } } - M->ehdr.buf[i++] = c; - if (i >= sizeof(M->ehdr.buf)) { + ebuf->buf[i++] = c; + if (i >= sizeof(ebuf->buf)) { break; } } - if (i >= sizeof(M->ehdr.ehdr)) { - TryElf(M, exe, fd, sp, auxv, pagesz, os); + if (i >= sizeof(ebuf->ehdr)) { + TryElf(M, ebuf, exe, fd, sp, auxv, pagesz, os); } } } - Pexit(os, exe, 0, TryElf(M, exe, fd, sp, auxv, pagesz, os)); + Pexit(os, exe, 0, TryElf(M, ebuf, exe, fd, sp, auxv, pagesz, os)); } diff --git a/ape/start.S b/ape/start.S index 7a99d6f38..c75183c8e 100644 --- a/ape/start.S +++ b/ape/start.S @@ -32,7 +32,6 @@ XnuEntrypoint: mov $_HOSTXNU,%dl // xnu's not unix! ElfEntrypoint: mov %rsp,%rsi // save real stack - sub $1024*1024,%rsp // room for allocs call ApeLoader .endfn ElfEntrypoint,globl .endfn XnuEntrypoint,globl @@ -65,7 +64,7 @@ ape.ident: .long 1 1: .asciz "APE" 2: .balign 4 -3: .long 106000000 +3: .long 107000000 4: .size ape.ident,.-ape.ident .type ape.ident,@object diff --git a/bin/ape-install b/bin/ape-install index 2552a1f4a..829e27c58 100755 --- a/bin/ape-install +++ b/bin/ape-install @@ -23,6 +23,8 @@ echo "Author: Justine Tunney " >&2 if [ -f o/depend ] && make -j8 o//ape; then echo "successfully recompiled ape loader" >&2 +elif [ -x o//ape/ape.elf ] && [ -x o//ape/ape.macho ]; then + echo "using ape loader you compiled earlier" >&2 elif [ -d build/bootstrap ]; then # if make isn't being used then it's unlikely the user changed the sources # in that case the prebuilt binaries should be completely up-to-date diff --git a/bin/ape-uninstall b/bin/ape-uninstall index e50d8b60e..f18c38473 100755 --- a/bin/ape-uninstall +++ b/bin/ape-uninstall @@ -48,6 +48,7 @@ for x in .ape \ .ape-1.4 \ .ape-1.5 \ .ape-1.6 \ + .ape-1.7 \ .ape-blink-0.9.2 \ .ape-blink-1.0.0; do rm -f \ diff --git a/build/bootstrap/ape.aarch64 b/build/bootstrap/ape.aarch64 index 329e3eb31..6dfb4e999 100755 Binary files a/build/bootstrap/ape.aarch64 and b/build/bootstrap/ape.aarch64 differ diff --git a/build/bootstrap/ape.elf b/build/bootstrap/ape.elf index 1b7284332..ffc3e5a40 100755 Binary files a/build/bootstrap/ape.elf and b/build/bootstrap/ape.elf differ diff --git a/build/bootstrap/ape.macho b/build/bootstrap/ape.macho index 4987e2ff7..782047171 100755 Binary files a/build/bootstrap/ape.macho and b/build/bootstrap/ape.macho differ diff --git a/build/bootstrap/ape.silicon b/build/bootstrap/ape.silicon deleted file mode 100755 index 3fd81f30b..000000000 Binary files a/build/bootstrap/ape.silicon and /dev/null differ diff --git a/examples/ctrlc.c b/examples/ctrlc.c index b8a1cb1d2..8fa769f2f 100644 --- a/examples/ctrlc.c +++ b/examples/ctrlc.c @@ -10,53 +10,75 @@ #include "libc/calls/calls.h" #include "libc/calls/struct/sigaction.h" #include "libc/errno.h" -#include "libc/log/check.h" -#include "libc/log/color.internal.h" -#include "libc/log/log.h" -#include "libc/mem/mem.h" -#include "libc/nt/thread.h" #include "libc/runtime/runtime.h" #include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/exit.h" -#include "libc/sysv/consts/fileno.h" +#include "libc/sysv/consts/limits.h" #include "libc/sysv/consts/sig.h" -volatile bool gotctrlc; - -void GotCtrlC(int sig) { - gotctrlc = true; +void SignalHandler(int sig) { + // we don't need to do anything in our signal handler since the signal + // delivery itself causes a visible state change, saying what happened } int main(int argc, char *argv[]) { - ssize_t rc; - size_t got, wrote; - unsigned char buf[512]; - struct sigaction saint = {.sa_handler = GotCtrlC}; - fprintf(stderr, "This echos stdio until Ctrl+C is pressed.\n"); - CHECK_NE(-1, sigaction(SIGINT, &saint, NULL)); + + printf("echoing stdin until ctrl+c is pressed\n"); + + // you need to set your signal handler using sigaction() rather than + // signal(), since the latter uses .sa_flags=SA_RESTART, which means + // read will restart itself after signals, rather than raising EINTR + sigaction(SIGINT, &(struct sigaction){.sa_handler = SignalHandler}, 0); + for (;;) { - rc = read(0, buf, 512); - if (rc != -1) { - got = rc; - } else { - CHECK_EQ(EINTR, errno); - break; + + // posix guarantees atomic i/o if you use pipe_buf sized buffers + // that way we don't need to worry about things like looping and + // we can also be assured that multiple actors wont have tearing + char buf[PIPE_BUF]; + + // read data from standard input + // + // since this is a blocking operation and we're not performing a + // cpu-bound operation it is almost with absolute certainty that + // when the ctrl-c signal gets delivered, it'll happen in read() + // + // it's possible to be more precise if we were building library + // code. for example, you can block signals using sigprocmask() + // and then use pselect() to do the waiting. + int got = read(0, buf, sizeof(buf)); + + // check if the read operation failed + // negative one is the *only* return value to indicate errors + if (got == -1) { + if (errno == EINTR) { + // a signal handler was invoked during the read operation + // since we have only one such signal handler it's sigint + // if we didn't have any signal handlers in our app, then + // we wouldn't need to check this errno. using SA_RESTART + // is another great way to avoid having to worry about it + // however EINTR is very useful, when we choose to use it + // the \r character is needed so when the line is printed + // it'll overwrite the ^C that got echo'd with the ctrl-c + printf("\rgot ctrl+c\n"); + exit(0); + } else { + // log it in the unlikely event something else went wrong + perror(""); + exit(1); + } } - if (!got) break; - rc = write(1, buf, got); - if (rc != -1) { - wrote = rc; - } else { - CHECK_EQ(EINTR, errno); - break; + + // check if the user typed ctrl-d which closes the input handle + if (!got) { + printf("got eof\n"); + exit(0); } - CHECK_EQ(got, wrote); + + // relay read data to standard output + // + // it's usually safe to ignore the return code of write. the + // operating system will send SIGPIPE if there's any problem + // which kills the process by default + write(1, buf, got); } - if (gotctrlc) { - fprintf(stderr, "Got Ctrl+C\n"); - } else { - fprintf(stderr, "Got EOF\n"); - } - return 0; } diff --git a/examples/stackexplorer.c b/examples/stackexplorer.c new file mode 100644 index 000000000..13f60f134 --- /dev/null +++ b/examples/stackexplorer.c @@ -0,0 +1,69 @@ +#if 0 +/*─────────────────────────────────────────────────────────────────╗ +│ To the extent possible under law, Justine Tunney has waived │ +│ all copyright and related or neighboring rights to this file, │ +│ as it is written in the following disclaimers: │ +│ • http://unlicense.org/ │ +│ • http://creativecommons.org/publicdomain/zero/1.0/ │ +╚─────────────────────────────────────────────────────────────────*/ +#endif +#include "libc/mem/alg.h" +#include "libc/mem/mem.h" +#include "libc/runtime/runtime.h" +#include "libc/stdio/stdio.h" +#include "libc/x/xasprintf.h" + +/** + * @fileoverview Process Initialization Stack Explorer + */ + +struct Thing { + intptr_t i; + char *s; +}; + +struct Things { + size_t n; + struct Thing *p; +} things; + +void Append(intptr_t i, char *s) { + things.p = realloc(things.p, ++things.n * sizeof(*things.p)); + things.p[things.n - 1].i = i; + things.p[things.n - 1].s = s; +} + +int Compare(const void *a, const void *b) { + struct Thing *x = (struct Thing *)a; + struct Thing *y = (struct Thing *)b; + if (x->i < y->i) return +1; + if (x->i > y->i) return -1; + return 0; +} + +int main(int argc, char *argv[]) { + Append((uintptr_t)__builtin_frame_address(0), "__builtin_frame_address(0)"); + Append((uintptr_t)__oldstack, "__oldstack"); + for (int i = 0;; ++i) { + Append((uintptr_t)&argv[i], xasprintf("&argv[%d] = %`'s", i, argv[i])); + if (!argv[i]) break; + Append((uintptr_t)argv[i], xasprintf("argv[%d] = %`'s", i, argv[i])); + } + for (int i = 0;; ++i) { + Append((uintptr_t)&environ[i], + xasprintf("&environ[%d] = %`'s", i, environ[i])); + if (!environ[i]) break; + Append((uintptr_t)environ[i], + xasprintf("environ[%d] = %`'s", i, environ[i])); + } + for (int i = 0;; i += 2) { + Append((uintptr_t)&__auxv[i], xasprintf("&auxv[%d] = %ld", i, __auxv[i])); + if (!__auxv[i]) break; + Append((uintptr_t)&__auxv[i + 1], + xasprintf("&auxv[%d] = %#lx", i + 1, __auxv[i + 1])); + } + qsort(things.p, things.n, sizeof(*things.p), Compare); + for (int i = 0; i < things.n; ++i) { + printf("%012lx %s\n", things.p[i].i, things.p[i].s); + } +} diff --git a/examples/stat.c b/examples/stat.c index 5ad75aa02..080e2d3be 100644 --- a/examples/stat.c +++ b/examples/stat.c @@ -7,8 +7,8 @@ │ • http://creativecommons.org/publicdomain/zero/1.0/ │ ╚─────────────────────────────────────────────────────────────────*/ #endif -#include "libc/calls/calls.h" #include "libc/calls/struct/stat.h" +#include "libc/calls/calls.h" #include "libc/errno.h" #include "libc/fmt/conv.h" #include "libc/fmt/fmt.h" @@ -90,6 +90,10 @@ void PrintFileMetadata(const char *pathname, struct stat *st) { int main(int argc, char *argv[]) { size_t i; struct stat st; + if (argc <= 1) { + PrintFileMetadata(".", &st); + return 0; + } for (i = 1; i < argc; ++i) { if (!strcmp(argv[i], "-n")) { numeric = true; diff --git a/examples/statfs.c b/examples/statfs.c index 1c5560dca..ce6367794 100644 --- a/examples/statfs.c +++ b/examples/statfs.c @@ -40,7 +40,9 @@ dontinline void ShowIt(const char *path) { "total file nodes in filesystem"); printf("f_ffree = %,zu (%s)\n", sf.f_ffree, "free file nodes in filesystem"); - printf("f_fsid = %#lx (%s)\n", sf.f_fsid, "filesystem id"); + printf("f_fsid = %#lx (%s)\n", + sf.f_fsid.__val[0] | (uint64_t)sf.f_fsid.__val[1] << 32, + "filesystem id"); printf("f_owner = %#lx (%s)\n", sf.f_owner, "user that created mount"); printf("f_namelen = %,zu (%s)\n", sf.f_namelen, "maximum length of filenames"); diff --git a/libc/calls/__sig2.c b/libc/calls/__sig2.c index a82910732..a1bd6d9a5 100644 --- a/libc/calls/__sig2.c +++ b/libc/calls/__sig2.c @@ -180,6 +180,7 @@ textwindows bool __sig_handle(int sigops, int sig, int si_code, case (intptr_t)SIG_DFL: if (__sig_is_fatal(sig)) { STRACE("terminating on %G", sig); + _restorewintty(); ExitProcess(sig); } // fallthrough diff --git a/libc/calls/execve-sysv.c b/libc/calls/execve-sysv.c index 68853b8e1..19f0352d5 100644 --- a/libc/calls/execve-sysv.c +++ b/libc/calls/execve-sysv.c @@ -85,8 +85,8 @@ int sys_execve(const char *prog, char *const argv[], char *const envp[]) { (CanExecute((ape = "/usr/bin/ape")) || CanExecute((ape = Join(firstnonnull(getenv("TMPDIR"), firstnonnull(getenv("HOME"), ".")), - ".ape-1.6", buf))) || - CanExecute((ape = Join(firstnonnull(getenv("HOME"), "."), ".ape-1.6", + ".ape-1.7", buf))) || + CanExecute((ape = Join(firstnonnull(getenv("HOME"), "."), ".ape-1.7", buf))))) { shargs[0] = ape; shargs[1] = "-"; diff --git a/libc/calls/getrlimit.c b/libc/calls/getrlimit.c index 8ec08d5dd..54f66935c 100644 --- a/libc/calls/getrlimit.c +++ b/libc/calls/getrlimit.c @@ -43,8 +43,8 @@ int getrlimit(int resource, struct rlimit *rlim) { } else if (!IsWindows()) { rc = sys_getrlimit(resource, rlim); } else if (resource == RLIMIT_STACK) { - rlim->rlim_cur = (uintptr_t)ape_stack_memsz; - rlim->rlim_max = (uintptr_t)ape_stack_memsz; + rlim->rlim_cur = GetStaticStackSize(); + rlim->rlim_max = GetStaticStackSize(); rc = 0; } else if (resource == RLIMIT_AS) { rlim->rlim_cur = __virtualmax; diff --git a/libc/calls/read.c b/libc/calls/read.c index 7beb98342..fc2914b9c 100644 --- a/libc/calls/read.c +++ b/libc/calls/read.c @@ -26,10 +26,10 @@ #include "libc/intrin/asan.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" +#include "libc/runtime/zipos.internal.h" #include "libc/sock/internal.h" #include "libc/sock/sock.h" #include "libc/sysv/errfuns.h" -#include "libc/runtime/zipos.internal.h" /** * Reads data from file descriptor. diff --git a/libc/calls/setrlimit.c b/libc/calls/setrlimit.c index 21779e720..e43e05cb9 100644 --- a/libc/calls/setrlimit.c +++ b/libc/calls/setrlimit.c @@ -23,6 +23,7 @@ #include "libc/intrin/asan.internal.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/strace.internal.h" +#include "libc/macros.internal.h" #include "libc/sysv/consts/rlimit.h" #include "libc/sysv/errfuns.h" @@ -31,6 +32,12 @@ * * The following resources are recommended: * + * - `RLIMIT_STACK` controls how much stack memory is available to the + * main thread. This setting is inherited across fork() and execve() + * Please note it's only safe for Cosmopolitan programs, to set this + * value to at least `PTHREAD_STACK_MIN * 2`. On Windows this cannot + * be used to extend the stack, which is currently hard-coded. + * * - `RLIMIT_AS` limits the size of the virtual address space. This will * work on all platforms except WSL. It is emulated on XNU and Windows * which means it won't propagate across execve() currently. diff --git a/libc/calls/statfs2cosmo.c b/libc/calls/statfs2cosmo.c index 084417657..60cc73805 100644 --- a/libc/calls/statfs2cosmo.c +++ b/libc/calls/statfs2cosmo.c @@ -278,7 +278,7 @@ void statfs2cosmo(struct statfs *f, const union statfs_meta *m) { f_files = m->netbsd.f_files; f_ffree = m->netbsd.f_ffree; f_fsid = m->netbsd.f_fsid; - f_namelen = f->f_namelen; + f_namelen = 511; f_frsize = m->netbsd.f_bsize; f_flags = m->netbsd.f_flags; f_owner = m->netbsd.f_owner; diff --git a/libc/intrin/describeframe.c b/libc/intrin/describeframe.c index 8d13ba28e..a651a825f 100644 --- a/libc/intrin/describeframe.c +++ b/libc/intrin/describeframe.c @@ -48,13 +48,10 @@ static const char *GetFrameName(int x) { } else if (IsMemtrackFrame(x)) { return "memtrack"; } else if (IsOldStackFrame(x)) { - return "oldstack"; - } else if (IsWindows() && - (((GetStaticStackAddr(0) + GetStackSize()) >> 16) <= x && - x <= ((GetStaticStackAddr(0) + GetStackSize() + - sizeof(struct WinArgs) - 1) >> - 16))) { - return "mainstack"; + return "system stack"; + } else if (((GetStaticStackAddr(0) + GetStackSize()) >> 16) <= x && + x <= ((GetStaticStackAddr(0) + GetStackSize() - 1) >> 16)) { + return "static stack"; } else if ((int)((intptr_t)__executable_start >> 16) <= x && x <= (int)(((intptr_t)_end - 1) >> 16)) { return "image"; diff --git a/libc/runtime/cosmo2.c b/libc/runtime/cosmo2.c index 3cae9c5d0..e999b31c7 100644 --- a/libc/runtime/cosmo2.c +++ b/libc/runtime/cosmo2.c @@ -110,9 +110,9 @@ wontreturn textstartup void cosmo(long *sp, struct Syslib *m1) { // get page size unsigned long pagesz = 4096; - for (int i = 0; auxv[i]; auxv += 2) { - if (auxv[0] == AT_PAGESZ) { - pagesz = auxv[1]; + for (int i = 0; auxv[i]; i += 2) { + if (auxv[i] == AT_PAGESZ) { + pagesz = auxv[i + 1]; break; } } diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index cebe1fb5e..b69815b8e 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -83,6 +83,7 @@ extern char kTmpPath[]; extern const char kNtSystemDirectory[]; extern const char kNtWindowsDirectory[]; extern size_t __virtualmax; +extern size_t __stackmax; extern bool __isworker; /* utilities */ void _intsort(int *, size_t); diff --git a/libc/runtime/winmain.greg.c b/libc/runtime/winmain.greg.c index 918e8285e..361fd022b 100644 --- a/libc/runtime/winmain.greg.c +++ b/libc/runtime/winmain.greg.c @@ -48,6 +48,7 @@ #include "libc/nt/thunk/msabi.h" #include "libc/runtime/internal.h" #include "libc/runtime/memtrack.internal.h" +#include "libc/runtime/runtime.h" #include "libc/runtime/stack.h" #include "libc/runtime/winargs.internal.h" #include "libc/sock/internal.h" @@ -119,9 +120,9 @@ __msabi static textwindows void DeduplicateStdioHandles(void) { } __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) { + size_t stacksize; struct WinArgs *wa; - size_t allocsize, stacksize; - uintptr_t stackaddr, allocaddr; + uintptr_t stackaddr; __oldstack = (intptr_t)__builtin_frame_address(0); if (NtGetPeb()->OSMajorVersion >= 10 && (intptr_t)v_ntsubsystem == kNtImageSubsystemWindowsCui) { @@ -138,26 +139,23 @@ __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) { _mmi.n = ARRAYLEN(_mmi.s); stackaddr = GetStaticStackAddr(0); stacksize = GetStaticStackSize(); - allocaddr = stackaddr; - allocsize = stacksize + sizeof(struct WinArgs); __imp_MapViewOfFileEx((_mmi.p[0].h = __imp_CreateFileMappingW( -1, &kNtIsInheritable, kNtPageExecuteReadwrite, - allocsize >> 32, allocsize, NULL)), - kNtFileMapWrite | kNtFileMapExecute, 0, 0, allocsize, - (void *)allocaddr); + stacksize >> 32, stacksize, NULL)), + kNtFileMapWrite | kNtFileMapExecute, 0, 0, stacksize, + (void *)stackaddr); int prot = (intptr_t)ape_stack_prot; if (~prot & PROT_EXEC) { - uint32_t oldprot; - __imp_VirtualProtect((void *)allocaddr, allocsize, kNtPageReadwrite, - &oldprot); + uint32_t old; + __imp_VirtualProtect((void *)stackaddr, stacksize, kNtPageReadwrite, &old); } - _mmi.p[0].x = allocaddr >> 16; - _mmi.p[0].y = (allocaddr >> 16) + ((allocsize - 1) >> 16); + _mmi.p[0].x = stackaddr >> 16; + _mmi.p[0].y = (stackaddr >> 16) + ((stacksize - 1) >> 16); _mmi.p[0].prot = prot; _mmi.p[0].flags = 0x00000026; // stack+anonymous - _mmi.p[0].size = allocsize; + _mmi.p[0].size = stacksize; _mmi.i = 1; - wa = (struct WinArgs *)(allocaddr + stacksize); + wa = (struct WinArgs *)(stackaddr + (stacksize - sizeof(struct WinArgs))); int count = GetDosArgv(cmdline, wa->argblock, ARRAYLEN(wa->argblock), wa->argv, ARRAYLEN(wa->argv)); for (int i = 0; wa->argv[0][i]; ++i) { @@ -169,7 +167,7 @@ __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) { GetDosEnviron(env16, wa->envblock, ARRAYLEN(wa->envblock) - 8, wa->envp, ARRAYLEN(wa->envp) - 1); __imp_FreeEnvironmentStringsW(env16); - _jmpstack((char *)(stackaddr + stacksize - (intptr_t)ape_stack_align), cosmo, + _jmpstack((char *)(stackaddr + (stacksize - sizeof(struct WinArgs))), cosmo, count, wa->argv, wa->envp, wa->auxv); } diff --git a/test/libc/calls/statfs_test.c b/test/libc/calls/statfs_test.c index 1f9c6ca24..24b6d4167 100644 --- a/test/libc/calls/statfs_test.c +++ b/test/libc/calls/statfs_test.c @@ -16,8 +16,9 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/calls/calls.h" #include "libc/calls/struct/statfs.h" +#include "libc/calls/calls.h" +#include "libc/calls/struct/stat.h" #include "libc/sysv/consts/o.h" #include "libc/testlib/testlib.h" diff --git a/tool/build/apelink.c b/tool/build/apelink.c index 56e9c9ac0..cbffbd1b1 100644 --- a/tool/build/apelink.c +++ b/tool/build/apelink.c @@ -1390,7 +1390,7 @@ static char *GenerateElfNotes(char *p) { char *save; save = p = ALIGN(p, 4); noteoff = p - prologue; - p = GenerateElfNote(p, "APE", 1, 106000000); + p = GenerateElfNote(p, "APE", 1, 107000000); if (support_vector & _HOSTOPENBSD) { p = GenerateElfNote(p, "OpenBSD", 1, 0); } @@ -1946,7 +1946,7 @@ int main(int argc, char *argv[]) { // otherwise try to use the ad-hoc self-extracted loader, securely if (loaders.n) { - p = stpcpy(p, "t=\"${TMPDIR:-${HOME:-.}}/.ape-1.6\"\n" + p = stpcpy(p, "t=\"${TMPDIR:-${HOME:-.}}/.ape-1.7\"\n" "[ x\"$1\" != x--assimilate ] && " "[ -x \"$t\" ] && " "exec \"$t\" \"$o\" \"$@\"\n"); diff --git a/tool/build/lib/errnos.S b/tool/build/lib/errnos.S deleted file mode 100644 index 0cc9e3307..000000000 --- a/tool/build/lib/errnos.S +++ /dev/null @@ -1,123 +0,0 @@ -/*-*- mode:asm; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│ -│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│ -╞══════════════════════════════════════════════════════════════════════════════╡ -│ Copyright 2020 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/macros.internal.h" - - .macro .e local:req linux:req - .globl \local - .long \local-kLinuxErrnos - .byte \linux - .endm - -// Lookup table translating errnos between systems. -// -// @see libc/sysv/systemfive.S - .rodata - .balign 4 -kLinuxErrnos: - .e EPERM,1 - .e ENOENT,2 - .e ESRCH,3 - .e EINTR,4 - .e EIO,5 - .e ENXIO,6 - .e E2BIG,7 - .e ENOEXEC,8 - .e EBADF,9 - .e ECHILD,10 - .e EAGAIN,11 - .e ENOMEM,12 - .e EACCES,13 - .e EFAULT,14 - .e ENOTBLK,15 - .e EBUSY,16 - .e EEXIST,17 - .e EXDEV,18 - .e ENODEV,19 - .e ENOTDIR,20 - .e EISDIR,21 - .e EINVAL,22 - .e ENFILE,23 - .e EMFILE,24 - .e ENOTTY,25 - .e ETXTBSY,26 - .e EFBIG,27 - .e ENOSPC,28 - .e ESPIPE,29 - .e EROFS,30 - .e EMLINK,31 - .e EPIPE,32 - .e EDOM,33 - .e ERANGE,34 - .e EDEADLK,35 - .e ENAMETOOLONG,36 - .e ENOLCK,37 - .e ENOSYS,38 - .e ENOTEMPTY,39 - .e ELOOP,40 - .e ENOMSG,42 - .e EIDRM,43 - .e EUSERS,87 - .e ENOTSOCK,88 - .e EDESTADDRREQ,89 - .e EMSGSIZE,90 - .e EPROTOTYPE,91 - .e ENOPROTOOPT,92 - .e EPROTONOSUPPORT,93 - .e ESOCKTNOSUPPORT,94 - .e EOPNOTSUPP,95 - .e EPFNOSUPPORT,96 - .e EAFNOSUPPORT,97 - .e EADDRINUSE,98 - .e EADDRNOTAVAIL,99 - .e EL2NSYNC,45 - .e EL3HLT,46 - .e EL3RST,47 - .e ELNRNG,48 - .e ETIME,62 - .e ENONET,64 - .e EREMOTE,66 - .e EPROTO,71 - .e EBADMSG,74 - .e EOVERFLOW,75 - .e EILSEQ,84 - .e ERESTART,85 - .e ENETDOWN,100 - .e ENETUNREACH,101 - .e ENETRESET,102 - .e ECONNABORTED,103 - .e ECONNRESET,104 - .e ENOBUFS,105 - .e EISCONN,106 - .e ENOTCONN,107 - .e ESHUTDOWN,108 - .e ETOOMANYREFS,109 - .e ETIMEDOUT,110 - .e ECONNREFUSED,111 - .e EHOSTDOWN,112 - .e EHOSTUNREACH,113 - .e EALREADY,114 - .e EINPROGRESS,115 - .e ESTALE,116 - .e EDQUOT,122 - .e ECANCELED,125 - .e EOWNERDEAD,130 - .e ENOTRECOVERABLE,131 - .long 0 - .byte 0 - .endobj kLinuxErrnos,globl diff --git a/tool/build/lib/lines.c b/tool/build/lib/lines.c deleted file mode 100644 index 7c68f47e6..000000000 --- a/tool/build/lib/lines.c +++ /dev/null @@ -1,56 +0,0 @@ -/*-*- 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 "tool/build/lib/lines.h" -#include "libc/limits.h" -#include "libc/mem/mem.h" -#include "libc/str/str.h" - -struct Lines *NewLines(void) { - return calloc(1, sizeof(struct Lines)); -} - -void FreeLines(struct Lines *lines) { - size_t i; - for (i = 0; i < lines->n; ++i) { - free(lines->p[i]); - } - free(lines); -} - -void AppendLine(struct Lines *lines, const char *s, size_t n) { - lines->p = realloc(lines->p, ++lines->n * sizeof(*lines->p)); - lines->p[lines->n - 1] = strndup(s, n); -} - -void AppendLines(struct Lines *lines, const char *s) { - const char *p; - for (;;) { - p = strchr(s, '\n'); - if (p) { - AppendLine(lines, s, p - s); - s = p + 1; - } else { - if (*s) { - // gcc11 whines about SIZE_MAX > PTRDIFF_MAX - AppendLine(lines, s, PTRDIFF_MAX); - } - break; - } - } -} diff --git a/tool/build/lib/lines.h b/tool/build/lib/lines.h deleted file mode 100644 index bbd3bc78e..000000000 --- a/tool/build/lib/lines.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef COSMOPOLITAN_TOOL_BUILD_LIB_LINES_H_ -#define COSMOPOLITAN_TOOL_BUILD_LIB_LINES_H_ -#if !(__ASSEMBLER__ + __LINKER__ + 0) -COSMOPOLITAN_C_START_ - -struct Lines { - size_t n; - char **p; -}; - -struct Lines *NewLines(void); -void FreeLines(struct Lines *); -void AppendLine(struct Lines *, const char *, size_t); -void AppendLines(struct Lines *, const char *); - -COSMOPOLITAN_C_END_ -#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ -#endif /* COSMOPOLITAN_TOOL_BUILD_LIB_LINES_H_ */ diff --git a/tool/build/unbuffer.c b/tool/build/unbuffer.c index 6686b6b30..17c681640 100644 --- a/tool/build/unbuffer.c +++ b/tool/build/unbuffer.c @@ -81,13 +81,13 @@ int main(int argc, char *argv[]) { if (!(prog = commandv(argv[optind], pathbuf, sizeof(pathbuf)))) { kprintf("%s: command not found\n", argv[optind]); - return __COUNTER__ + 1; + exit(1); } if (outputpath) { if ((outfd = creat(outputpath, 0644)) == -1) { perror(outputpath); - return __COUNTER__ + 1; + exit(1); } } @@ -97,12 +97,12 @@ int main(int argc, char *argv[]) { if (tcgetattr(1, &tio)) { perror("tcgetattr"); - return __COUNTER__ + 1; + exit(1); } if (openpty(&mfd, &sfd, 0, &tio, &wsz)) { perror("openpty"); - return __COUNTER__ + 1; + exit(1); } ignore.sa_flags = 0; @@ -116,7 +116,7 @@ int main(int argc, char *argv[]) { if ((pid = fork()) == -1) { perror("fork"); - return __COUNTER__ + 1; + exit(1); } if (!pid) { @@ -148,13 +148,13 @@ int main(int argc, char *argv[]) { rc = 0; } else { perror("read"); - return __COUNTER__ + 1; + exit(1); } } if (!(got = rc)) { if (waitpid(pid, &ws, 0) == -1) { perror("waitpid"); - return __COUNTER__ + 1; + exit(1); } break; } @@ -164,7 +164,7 @@ int main(int argc, char *argv[]) { wrote = rc; } else { perror("write"); - return __COUNTER__ + 1; + exit(1); } } if (outputpath) { @@ -174,7 +174,7 @@ int main(int argc, char *argv[]) { wrote = rc; } else { perror("write"); - return __COUNTER__ + 1; + exit(1); } } } @@ -187,6 +187,7 @@ int main(int argc, char *argv[]) { if (WIFEXITED(ws)) { return WEXITSTATUS(ws); } else { - return 128 + WTERMSIG(ws); + raise(WTERMSIG(ws)); + exit(128 + WTERMSIG(ws)); } } diff --git a/tool/build/x86combos.c b/tool/build/x86combos.c deleted file mode 100644 index a21459407..000000000 --- a/tool/build/x86combos.c +++ /dev/null @@ -1,191 +0,0 @@ -/*-*- 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 2020 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/calls/calls.h" -#include "libc/log/check.h" -#include "libc/macros.internal.h" -#include "libc/stdio/stdio.h" -#include "libc/str/str.h" -#include "libc/sysv/consts/o.h" -#include "third_party/xed/x86.h" - -const char kPrefixes[][8] = { - {}, - {0x66}, - {0x67}, - {0x40}, - {0x6F}, - {0x66, 0x6F}, - {0x66, 0x40}, - {0x66, 0x6F}, - {0x67, 0x40}, - {0x67, 0x6F}, - {0x66, 0x67}, - {0x66, 0x67, 0x40}, - {0x66, 0x67, 0x6F}, - - {0x0F}, - {0x0F, 0x66}, - {0x0F, 0x67}, - {0x0F, 0x40}, - {0x0F, 0x6F}, - {0x0F, 0x66, 0x6F}, - {0x0F, 0x66, 0x40}, - {0x0F, 0x66, 0x6F}, - {0x0F, 0x67, 0x40}, - {0x0F, 0x67, 0x6F}, - {0x0F, 0x66, 0x67}, - {0x0F, 0x66, 0x67, 0x40}, - {0x0F, 0x66, 0x67, 0x6F}, - - {0xF3, 0x0F}, - {0xF3, 0x66, 0x0F}, - {0xF3, 0x67, 0x0F}, - {0xF3, 0x40, 0x0F}, - {0xF3, 0x6F, 0x0F}, - {0xF3, 0x66, 0x6F, 0x0F}, - {0xF3, 0x66, 0x40, 0x0F}, - {0xF3, 0x66, 0x6F, 0x0F}, - {0xF3, 0x67, 0x40, 0x0F}, - {0xF3, 0x67, 0x6F, 0x0F}, - {0xF3, 0x66, 0x67, 0x0F}, - {0xF3, 0x66, 0x67, 0x40, 0x0F}, - {0xF3, 0x66, 0x67, 0x6F, 0x0F}, - - {0xF2, 0x0F}, - {0xF2, 0x66, 0x0F}, - {0xF2, 0x67, 0x0F}, - {0xF2, 0x40, 0x0F}, - {0xF2, 0x6F, 0x0F}, - {0xF2, 0x66, 0x6F, 0x0F}, - {0xF2, 0x66, 0x40, 0x0F}, - {0xF2, 0x66, 0x6F, 0x0F}, - {0xF2, 0x67, 0x40, 0x0F}, - {0xF2, 0x67, 0x6F, 0x0F}, - {0xF2, 0x66, 0x67, 0x0F}, - {0xF2, 0x66, 0x67, 0x40, 0x0F}, - {0xF2, 0x66, 0x67, 0x6F, 0x0F}, - -}; - -const uint8_t kModrmPicks[] = { - 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x0, 0x8, 0x10, - 0x18, 0x20, 0x28, 0x30, 0x38, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, - 0x46, 0x47, 0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78, 0x80, - 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x80, 0x88, 0x90, 0x98, - 0xa0, 0xa8, 0xb0, 0xb8, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, - 0xc7, 0xc0, 0xc8, 0xd0, 0xd8, 0xe0, 0xe8, 0xf0, 0xf8, -}; - -const uint8_t kOpMap0[] = { - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, - 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, - 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, - 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, - 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, - 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, - 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, - 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, - 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, - 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, - 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, - 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, - 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, - 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, - 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, - 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, - 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, - 255, -}; - -void WriteOp(int fd, struct XedDecodedInst *xedd) { - int i; - char buf[128], *p; - p = stpcpy(buf, ".byte "); - for (i = 0; i < xedd->length; ++i) { - if (i) *p++ = ','; - *p++ = '0'; - *p++ = 'x'; - *p++ = "0123456789abcdef"[(xedd->bytes[i] & 0xf0) >> 4]; - *p++ = "0123456789abcdef"[xedd->bytes[i] & 0x0f]; - } - *p++ = '\n'; - CHECK_NE(-1, write(fd, buf, p - buf)); -} - -int main(int argc, char *argv[]) { - int i, j, k, l, m, n, p, o, fd; - uint8_t op[16]; - struct XedDecodedInst xedd[1]; - fd = open("/tmp/ops.s", O_CREAT | O_TRUNC | O_RDWR, 0644); - for (o = 0; o < ARRAYLEN(kOpMap0); ++o) { - for (p = 0; p < ARRAYLEN(kPrefixes); ++p) { - memset(op, 0x55, 16); - n = strlen(kPrefixes[p]); - memcpy(op, kPrefixes[p], n); - op[n] = kOpMap0[o]; - xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64); - if (!xed_instruction_length_decode(xedd, op, XED_MAX_INSTRUCTION_BYTES)) { - if (xedd->op.has_modrm && xedd->op.has_sib) { - for (i = 0; i < ARRAYLEN(kModrmPicks); ++i) { - for (j = 0; j < ARRAYLEN(kModrmPicks); ++j) { - memset(op, 0x55, 16); - n = strlen(kPrefixes[p]); - memcpy(op, kPrefixes[p], n); - op[n] = kOpMap0[o]; - op[xedd->op.pos_modrm] = kModrmPicks[i]; - op[xedd->op.pos_sib] = kModrmPicks[j]; - xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64); - if (!xed_instruction_length_decode(xedd, op, - XED_MAX_INSTRUCTION_BYTES)) { - WriteOp(fd, xedd); - } - } - } - } else if (xedd->op.has_modrm) { - for (i = 0; i < ARRAYLEN(kModrmPicks); ++i) { - memset(op, 0x55, 16); - n = strlen(kPrefixes[p]); - memcpy(op, kPrefixes[p], n); - op[n] = kOpMap0[o]; - op[xedd->op.pos_modrm] = kModrmPicks[i]; - xed_decoded_inst_zero_set_mode(xedd, XED_MACHINE_MODE_LONG_64); - if (!xed_instruction_length_decode(xedd, op, - XED_MAX_INSTRUCTION_BYTES)) { - WriteOp(fd, xedd); - } - } - } else { - WriteOp(fd, xedd); - } - } - } - } - close(fd); - system("as -o /tmp/ops.o /tmp/ops.s"); - system("objdump -wd /tmp/ops.o |" - " grep -v data16 |" - " grep -v addr32 |" - " grep -v '(bad)' |" - " sed 's/^[ :[:xdigit:]]*//' |" - " sed 's/^[ :[:xdigit:]]*//' |" - " sed 's/[[:space:]]#.*$//' |" - " grep -v 'rex\\.' |" - " sort -u"); - return 0; -}