From e6b7c16a5357acc6f885e9eb7b45a00eb9fb5397 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 15 Jun 2023 23:22:49 -0700 Subject: [PATCH] Make changes needed for new demo --- libc/calls/unveil.c | 1 + .../makeprocessnice.c => calls/verynice.c} | 3 +- libc/runtime/runtime.h | 2 +- third_party/ggml/main.cc | 2 +- third_party/ggml/quantize.cc | 2 +- third_party/python/Lib/socketserver.py | 9 +++ third_party/python/Python/cosmomodule.c | 31 ++++++-- third_party/radpajama/copy-gptneox.cc | 2 +- third_party/radpajama/main-redpajama-chat.cc | 4 +- third_party/radpajama/main-redpajama.cc | 2 +- third_party/radpajama/quantize-gptneox.cc | 2 +- tool/build/pledge.c | 5 +- tool/decode/elf.c | 78 ++++++++++++------- 13 files changed, 96 insertions(+), 47 deletions(-) rename libc/{log/makeprocessnice.c => calls/verynice.c} (97%) diff --git a/libc/calls/unveil.c b/libc/calls/unveil.c index 3ca8c473f..637fbd3e0 100644 --- a/libc/calls/unveil.c +++ b/libc/calls/unveil.c @@ -419,6 +419,7 @@ int unveil(const char *path, const char *permissions) { // if the host environment enables unveil() to impose true security // restrictions because the default behavior is to silently succeed // so that programs will err on the side of working if distributed. + if (permissions) return einval(); if (IsOpenbsd()) return 0; if (landlock_abi_version != -1) { _unassert(landlock_abi_version >= 1); diff --git a/libc/log/makeprocessnice.c b/libc/calls/verynice.c similarity index 97% rename from libc/log/makeprocessnice.c rename to libc/calls/verynice.c index 0a3299cce..3f4097b6d 100644 --- a/libc/log/makeprocessnice.c +++ b/libc/calls/verynice.c @@ -19,7 +19,6 @@ #include "libc/calls/calls.h" #include "libc/calls/struct/sched_param.h" #include "libc/errno.h" -#include "libc/log/log.h" #include "libc/sysv/consts/ioprio.h" #include "libc/sysv/consts/prio.h" #include "libc/sysv/consts/sched.h" @@ -30,7 +29,7 @@ * @return 0 on success, or -1 w/ errno * @note error reporting currently not implemented */ -int MakeProcessNice(void) { +int verynice(void) { int e = errno; setpriority(PRIO_PROCESS, 0, 10); ioprio_set(IOPRIO_WHO_PROCESS, 0, IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)); diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 3a962e4a3..c664757be 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -104,7 +104,7 @@ char *GetInterpreterExecutableName(char *, size_t); int _OpenExecutable(void); bool _IsDynamicExecutable(const char *); /* execution control */ -int MakeProcessNice(void); +int verynice(void); axdx_t setlongerjmp(jmp_buf) libcesque returnstwice paramsnonnull(); void longerjmp(jmp_buf, intptr_t) libcesque wontreturn paramsnonnull(); diff --git a/third_party/ggml/main.cc b/third_party/ggml/main.cc index b59f9670f..f81cd70d0 100644 --- a/third_party/ggml/main.cc +++ b/third_party/ggml/main.cc @@ -212,7 +212,7 @@ static int on_missing_feature(const char *name) { int main(int argc, char ** argv) { - MakeProcessNice(); + verynice(); ShowCrashReports(); setvbuf(stdin, NULL, _IONBF, 0); diff --git a/third_party/ggml/quantize.cc b/third_party/ggml/quantize.cc index 2d179ddce..838d0189f 100644 --- a/third_party/ggml/quantize.cc +++ b/third_party/ggml/quantize.cc @@ -55,7 +55,7 @@ static const std::map LLAMA_FTYPE_MAP = { // ./quantize models/llama/ggml-model.bin models/llama/ggml-model-quant.bin type [nthreads] // int main(int argc, char ** argv) { - MakeProcessNice(); + verynice(); ShowCrashReports(); ggjt_v3(); diff --git a/third_party/python/Lib/socketserver.py b/third_party/python/Lib/socketserver.py index 6a0aeee0c..3c9f889b0 100644 --- a/third_party/python/Lib/socketserver.py +++ b/third_party/python/Lib/socketserver.py @@ -359,6 +359,14 @@ class BaseServer: """ pass + def forked_request(self, request, client_address): + """Called in child after os.fork() is called. + + May be overridden. + + """ + pass + def finish_request(self, request, client_address): """Finish one request by instantiating RequestHandlerClass.""" self.RequestHandlerClass(request, client_address, self) @@ -617,6 +625,7 @@ if hasattr(os, "fork"): # This must never return, hence os._exit()! status = 1 try: + self.forked_request(request, client_address) self.finish_request(request, client_address) status = 0 except Exception: diff --git a/third_party/python/Python/cosmomodule.c b/third_party/python/Python/cosmomodule.c index 10aebb28a..8ccd02001 100644 --- a/third_party/python/Python/cosmomodule.c +++ b/third_party/python/Python/cosmomodule.c @@ -146,6 +146,18 @@ cosmo_crc32c(PyObject *self, PyObject *args) return PyLong_FromUnsignedLong(crc); } +PyDoc_STRVAR(verynice_doc, +"verynice($module)\n\ +--\n\n\ +Makes current process as low-priority as possible."); + +static PyObject * +cosmo_verynice(PyObject *self, PyObject *args) +{ + verynice(); + Py_RETURN_NONE; +} + PyDoc_STRVAR(decimate_doc, "decimate($module, bytes)\n\ --\n\n\ @@ -202,6 +214,7 @@ PyDoc_STRVAR(pledge_doc, --\n\n\ Permits syscall operations, e.g.\n\ \n\ + >>> cosmo.pledge(None, None) # assert support\n\ >>> cosmo.pledge('stdio rpath tty', None)\n\ \n\ This function implements the OpenBSD pledge() API for\n\ @@ -213,7 +226,7 @@ cosmo_pledge(PyObject *self, PyObject *args) { int e = errno; const char *x, *y; - if (!PyArg_ParseTuple(args, "sz:pledge", &x, &y)) return 0; + if (!PyArg_ParseTuple(args, "zz:pledge", &x, &y)) return 0; __pledge_mode = PLEDGE_PENALTY_RETURN_EPERM; if (!pledge(x, y)) { Py_RETURN_NONE; @@ -229,8 +242,9 @@ PyDoc_STRVAR(unveil_doc, --\n\n\ Permits filesystem operations, e.g.\n\ \n\ - >>> cosmo.unveil('.', 'rwcx')\n\ - >>> cosmo.unveil(None, None)\n\ + >>> cosmo.unveil('', None) # assert support\n\ + >>> cosmo.unveil('.', 'rwcx') # permit current dir\n\ + >>> cosmo.unveil(None, None) # commit policy\n\ \n\ This function implements the OpenBSD unveil() API for\n\ OpenBSD and Linux where we use Landlock LSM. Read the\n\ @@ -239,11 +253,15 @@ Cosmopolitan Libc documentation to learn more."); static PyObject * cosmo_unveil(PyObject *self, PyObject *args) { - int e = errno; const char *x, *y; + int abi, e = errno; if (!PyArg_ParseTuple(args, "zz:unveil", &x, &y)) return 0; - if (!unveil(x, y)) { - Py_RETURN_NONE; + if ((abi = unveil(x, y)) != -1) { + if (abi) { + return PyLong_FromUnsignedLong(abi); + } else { + Py_RETURN_NONE; + } } else { PyErr_SetString(PyExc_SystemError, strerror(errno)); errno = e; @@ -338,6 +356,7 @@ static PyMethodDef cosmo_methods[] = { {"syscount", cosmo_syscount, METH_NOARGS, syscount_doc}, {"popcount", cosmo_popcount, METH_VARARGS, popcount_doc}, {"decimate", cosmo_decimate, METH_VARARGS, decimate_doc}, + {"verynice", cosmo_verynice, METH_VARARGS, verynice_doc}, #ifdef __x86_64__ {"getcpucore", cosmo_getcpucore, METH_NOARGS, getcpucore_doc}, {"getcpunode", cosmo_getcpunode, METH_NOARGS, getcpunode_doc}, diff --git a/third_party/radpajama/copy-gptneox.cc b/third_party/radpajama/copy-gptneox.cc index 0df494882..fd5940bcd 100644 --- a/third_party/radpajama/copy-gptneox.cc +++ b/third_party/radpajama/copy-gptneox.cc @@ -48,7 +48,7 @@ static const std::map GPTNEOX_FTYPE_MAP = { // ./quantize models/llama/ggml-model.bin models/llama/ggml-model-quant.bin type // int main(int argc, char ** argv) { - MakeProcessNice(); + verynice(); ShowCrashReports(); ggjt_v1(); diff --git a/third_party/radpajama/main-redpajama-chat.cc b/third_party/radpajama/main-redpajama-chat.cc index dff7b3d6a..2a293ac06 100644 --- a/third_party/radpajama/main-redpajama-chat.cc +++ b/third_party/radpajama/main-redpajama-chat.cc @@ -99,7 +99,7 @@ int main(int argc, char ** argv) { params.instruct = true; params.interactive = true; - MakeProcessNice(); + verynice(); ShowCrashReports(); if (gpt_params_parse(argc, argv, params) == false) { return 1; } @@ -137,7 +137,7 @@ int main(int argc, char ** argv) { } } - MakeProcessNice(); + verynice(); ShowCrashReports(); // Always interactive for RedPajama chat model diff --git a/third_party/radpajama/main-redpajama.cc b/third_party/radpajama/main-redpajama.cc index d6a5af27d..62b2df4a0 100644 --- a/third_party/radpajama/main-redpajama.cc +++ b/third_party/radpajama/main-redpajama.cc @@ -84,7 +84,7 @@ int main(int argc, char ** argv) { gpt_params params; params.model = "./examples/redpajama/models/pythia/ggml-RedPajama-INCITE-Instruct-3B-v1-f16.bin"; - MakeProcessNice(); + verynice(); ShowCrashReports(); if (gpt_params_parse(argc, argv, params) == false) { diff --git a/third_party/radpajama/quantize-gptneox.cc b/third_party/radpajama/quantize-gptneox.cc index 182188cd2..0bd1ee235 100644 --- a/third_party/radpajama/quantize-gptneox.cc +++ b/third_party/radpajama/quantize-gptneox.cc @@ -50,7 +50,7 @@ static const std::map GPTNEOX_FTYPE_MAP = { // ./quantize models/llama/ggml-model.bin models/llama/ggml-model-quant.bin type // int main(int argc, char ** argv) { - MakeProcessNice(); + verynice(); ShowCrashReports(); ggjt_v2(); diff --git a/tool/build/pledge.c b/tool/build/pledge.c index 8680c84ca..c73923180 100644 --- a/tool/build/pledge.c +++ b/tool/build/pledge.c @@ -576,8 +576,9 @@ int main(int argc, char *argv[]) { NormalizeFileDescriptors(); } - // set resource limits - MakeProcessNice(); + if (g_nice) { + verynice(); + } if (SetCpuLimit(g_cpuquota) == -1) { kprintf("error: setrlimit(%s) failed: %m\n", "RLIMIT_CPU"); diff --git a/tool/decode/elf.c b/tool/decode/elf.c index 0c6646238..a2790c2dc 100644 --- a/tool/decode/elf.c +++ b/tool/decode/elf.c @@ -105,6 +105,7 @@ static void printelfehdr(void) { static void printelfsegmentheader(int i) { Elf64_Phdr *phdr = GetElfSegmentHeaderAddress(elf, st->st_size, i); + if (!phdr) return; printf("/\tElf64_Phdr *phdr = GetElfSegmentHeaderAddress(elf, st->st_size, " "%d)\n", i); @@ -133,6 +134,7 @@ static void printelfsegmentheaders(void) { static void printelfsectionheader(int i, char *shstrtab) { Elf64_Shdr *shdr; shdr = GetElfSectionHeaderAddress(elf, st->st_size, i); + if (!shdr) return; printf("/\tElf64_Shdr *shdr = GetElfSectionHeaderAddress(elf, st->st_size, " "%d)\n", i); @@ -160,7 +162,11 @@ static void printelfsectionheader(int i, char *shstrtab) { static void printelfsectionheaders(void) { Elf64_Half i; char *shstrtab; + const char *str; + Elf64_Shdr *shdr, *shshdr; + shshdr = GetElfSectionHeaderAddress(elf, st->st_size, elf->e_shstrndx); shstrtab = GetElfSectionNameStringTable(elf, st->st_size); + if (!shshdr || !shstrtab) return; if (shstrtab) { printf("\n"); printf("\t.org\t%#x\n", elf->e_shoff); @@ -168,12 +174,11 @@ static void printelfsectionheaders(void) { printelfsectionheader(i, shstrtab); } printf("\n/\t%s\n", "elf->e_shstrndx"); - printf("\t.org\t%#x\n", - GetElfSectionHeaderAddress(elf, st->st_size, elf->e_shstrndx) - ->sh_offset); + printf("\t.org\t%#x\n", shshdr->sh_offset); for (i = 0; i < elf->e_shnum; ++i) { - Elf64_Shdr *shdr = GetElfSectionHeaderAddress(elf, st->st_size, i); - const char *str = GetElfString(elf, st->st_size, shstrtab, shdr->sh_name); + shdr = GetElfSectionHeaderAddress(elf, st->st_size, i); + if (!shdr) break; + str = GetElfString(elf, st->st_size, shstrtab, shdr->sh_name); show(".asciz", format(b1, "%`'s", str), NULL); } } @@ -182,13 +187,18 @@ static void printelfsectionheaders(void) { static void printelfgroups(void) { for (int i = 0; i < elf->e_shnum; ++i) { Elf64_Shdr *shdr = GetElfSectionHeaderAddress(elf, st->st_size, i); + if (!shdr) break; if (shdr->sh_type == SHT_GROUP) { const Elf64_Shdr *symhdr = GetElfSectionHeaderAddress(elf, st->st_size, shdr->sh_link); + if (!symhdr) break; const Elf64_Shdr *strhdr = GetElfSectionHeaderAddress(elf, st->st_size, symhdr->sh_link); + if (!strhdr) break; Elf64_Sym *syms = GetElfSectionAddress(elf, st->st_size, symhdr); + if (!syms) break; char *strs = GetElfSectionAddress(elf, st->st_size, strhdr); + if (!strs) break; printf("\n"); printf("//\t%s group\n", GetElfString(elf, st->st_size, strs, syms[shdr->sh_info].st_name)); @@ -206,9 +216,11 @@ static void printelfgroups(void) { const Elf64_Shdr *section = GetElfSectionHeaderAddress(elf, st->st_size, READ32LE(p)); printf("\t.long\t%#x\t\t\t# %s\n", READ32LE(p), - GetElfString(elf, st->st_size, - GetElfSectionNameStringTable(elf, st->st_size), - section->sh_name)); + section ? GetElfString( + elf, st->st_size, + GetElfSectionNameStringTable(elf, st->st_size), + section->sh_name) + : 0); } shdr->sh_offset; } @@ -244,14 +256,15 @@ static void printelfsymbol(Elf64_Sym *sym, char *strtab, char *shstrtab) { GetElfString(elf, st->st_size, strtab, sym->st_name))); printelfsymbolinfo(sym); printelfsymbolother(sym); + Elf64_Shdr *shdr = + GetElfSectionHeaderAddress(elf, st->st_size, sym->st_shndx); show(".short", format(b1, "%d", sym->st_shndx), format(b2, "%s sym->st_shndx", sym->st_shndx < 0xff00 ? format(b1, "%`'s", - GetElfString(elf, st->st_size, shstrtab, - GetElfSectionHeaderAddress( - elf, st->st_size, sym->st_shndx) - ->sh_name)) + shdr ? GetElfString(elf, st->st_size, shstrtab, + shdr->sh_name) + : 0) : findnamebyid(kElfSpecialSectionNames, sym->st_shndx))); show(".quad", format(b1, "%#x", sym->st_value), "sym->st_value"); show(".quad", format(b1, "%#x", sym->st_size), "sym->st_size"); @@ -260,13 +273,14 @@ static void printelfsymbol(Elf64_Sym *sym, char *strtab, char *shstrtab) { static void printelfsymboltable(void) { size_t i, symcount = 0; Elf64_Sym *symtab = GetElfSymbolTable(elf, st->st_size, &symcount); + if (!symtab) return; char *strtab = GetElfStringTable(elf, st->st_size); char *shstrtab = GetElfSectionNameStringTable(elf, st->st_size); - if (symtab && strtab) { - printf("\n\n"); - printf("\t.org\t%#x\n", (intptr_t)symtab - (intptr_t)elf); - for (i = 0; i < symcount; ++i) { - printf(".Lsym%d:\n", i); + printf("\n\n"); + printf("\t.org\t%#x\n", (intptr_t)symtab - (intptr_t)elf); + for (i = 0; i < symcount; ++i) { + printf(".Lsym%d:\n", i); + if (strtab && shstrtab) { printelfsymbol(&symtab[i], strtab, shstrtab); } } @@ -275,13 +289,14 @@ static void printelfsymboltable(void) { static void printelfdynsymboltable(void) { size_t i, symcount = 0; Elf64_Sym *symtab = GetElfDynSymbolTable(elf, st->st_size, &symcount); + if (!symtab) return; char *strtab = GetElfDynStringTable(elf, st->st_size); char *shstrtab = GetElfSectionNameStringTable(elf, st->st_size); - if (symtab && strtab) { - printf("\n\n"); - printf("\t.org\t%#x\n", (intptr_t)symtab - (intptr_t)elf); - for (i = 0; i < symcount; ++i) { - printf(".Lsym%d:\n", i); + printf("\n\n"); + printf("\t.org\t%#x\n", (intptr_t)symtab - (intptr_t)elf); + for (i = 0; i < symcount; ++i) { + printf(".Lsym%d:\n", i); + if (strtab && shstrtab) { printelfsymbol(&symtab[i], strtab, shstrtab); } } @@ -309,8 +324,8 @@ static void printelfrelocations(void) { size_t i, j, count; const Elf64_Sym *syms; const Elf64_Rela *rela; - const Elf64_Shdr *shdr, *symtab; char *strtab, *shstrtab, *symbolname; + const Elf64_Shdr *shdr, *shdr2, *symtab; strtab = GetElfStringTable(elf, st->st_size); shstrtab = GetElfSectionNameStringTable(elf, st->st_size); for (i = 0; i < elf->e_shnum; ++i) { @@ -324,20 +339,25 @@ static void printelfrelocations(void) { (uintptr_t)elf + shdr->sh_offset + shdr->sh_size)); ++rela, ++j) { symtab = GetElfSectionHeaderAddress(elf, st->st_size, shdr->sh_link); + if (!symtab) continue; count = symtab->sh_size / symtab->sh_entsize; syms = GetElfSectionAddress(elf, st->st_size, symtab); sym = ELF64_R_SYM(rela->r_info); if (0 <= sym && sym < count) { - symbolname = - getelfsymbolname(elf, st->st_size, strtab, shstrtab, syms + sym); + if (syms && strtab && shstrtab) { + symbolname = getelfsymbolname(elf, st->st_size, strtab, shstrtab, + syms + sym); + } else { + symbolname = xasprintf("evil-sym-%d", sym); + } } else { symbolname = xasprintf("bad-sym-%d", sym); } printf("/\t%s+%#lx → %s%c%#lx\n", - GetElfString( - elf, st->st_size, shstrtab, - GetElfSectionHeaderAddress(elf, st->st_size, shdr->sh_info) - ->sh_name), + (shdr2 = + GetElfSectionHeaderAddress(elf, st->st_size, shdr->sh_info)) + ? GetElfString(elf, st->st_size, shstrtab, shdr2->sh_name) + : 0, rela->r_offset, symbolname, rela->r_addend >= 0 ? '+' : '-', ABS(rela->r_addend)); printf("%s_%zu_%zu:\n", ".Lrela", i, j);