Make changes needed for new demo

This commit is contained in:
Justine Tunney 2023-06-15 23:22:49 -07:00
parent c3440d040c
commit e6b7c16a53
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
13 changed files with 96 additions and 47 deletions

View file

@ -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);

View file

@ -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));

View file

@ -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();

View file

@ -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);

View file

@ -55,7 +55,7 @@ static const std::map<std::string, llama_ftype> 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();

View file

@ -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:

View file

@ -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)) {
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},

View file

@ -48,7 +48,7 @@ static const std::map<std::string, enum gptneox_ftype> 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();

View file

@ -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

View file

@ -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) {

View file

@ -50,7 +50,7 @@ static const std::map<std::string, enum gptneox_ftype> 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();

View file

@ -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");

View file

@ -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,
section ? GetElfString(
elf, st->st_size,
GetElfSectionNameStringTable(elf, st->st_size),
section->sh_name));
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);
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);
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);