Skip to content

Commit

Permalink
Rewrite ZipOS
Browse files Browse the repository at this point in the history
This reduces the virtual memory usage of Emacs for me by 30%. We now
have a simpler implementation that uses read(), rather mmap()ing the
whole executable.
  • Loading branch information
jart committed Oct 3, 2023
1 parent ff77f2a commit b01282e
Show file tree
Hide file tree
Showing 21 changed files with 406 additions and 419 deletions.
21 changes: 7 additions & 14 deletions examples/nesemu1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "dsp/tty/tty.h"
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/dirent.h"
#include "libc/calls/struct/itimerval.h"
#include "libc/calls/struct/winsize.h"
#include "libc/calls/termios.h"
Expand All @@ -28,7 +29,6 @@
#include "libc/mem/arraylist2.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/sock/sock.h"
#include "libc/sock/struct/pollfd.h"
#include "libc/stdio/stdio.h"
Expand Down Expand Up @@ -1808,19 +1808,12 @@ void GetOpts(int argc, char* argv[]) {
}

size_t FindZipGames(void) {
char* name;
size_t i, cf;
struct Zipos* zipos;
if ((zipos = __zipos_get())) {
for (i = 0, cf = ZIP_CDIR_OFFSET(zipos->cdir);
i < ZIP_CDIR_RECORDS(zipos->cdir);
++i, cf += ZIP_CFILE_HDRSIZE(zipos->map + cf)) {
if (ZIP_CFILE_NAMESIZE(zipos->map + cf) > 4 &&
!memcmp((ZIP_CFILE_NAME(zipos->map + cf) +
ZIP_CFILE_NAMESIZE(zipos->map + cf) - 4),
".nes", 4) &&
(name = xasprintf("/zip/%.*s", ZIP_CFILE_NAMESIZE(zipos->map + cf),
ZIP_CFILE_NAME(zipos->map + cf)))) {
DIR* dir;
if ((dir = opendir("/zip/usr/share/rom"))) {
struct dirent* ent;
while ((ent = readdir(dir))) {
if (endswith(ent->d_name, ".nes")) {
char* name = xasprintf("/zip/usr/share/rom/%s", ent->d_name);
APPEND(&zipgames_.p, &zipgames_.i, &zipgames_.n, &name);
}
}
Expand Down
14 changes: 7 additions & 7 deletions libc/intrin/getmainstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@
#include "libc/sysv/consts/rlim.h"
#include "libc/sysv/consts/rlimit.h"

// Hack for guessing boundaries of _start()'s stack
// Hack for guessing the unknowable boundaries of _start()'s stack
//
// Every UNIX system in our support vector creates arg blocks like:
// This code always guesses correctly on Windows because WinMain()
// is written to allocate a stack ourself. Local testing on Linux,
// XNU, FreeBSD, OpenBSD, and NetBSD says that accuracy is ±1 page
// and that error rate applies to both beginning and end addresses
//
// Every UNIX system in our support vector creates arg blocks like
//
// <HIGHEST-STACK-ADDRESS>
// last environ string
Expand All @@ -55,11 +60,6 @@
// up to the microprocessor page size (this computes the top addr)
// and the bottom is computed by subtracting RLIMIT_STACK rlim_cur
// It's simple but gets tricky if we consider environ can be empty
//
// This code always guesses correctly on Windows because WinMain()
// is written to allocate a stack ourself. Local testing on Linux,
// XNU, FreeBSD, OpenBSD, and NetBSD says that accuracy is ±1 page
// and that error rate applies to both beginning and end addresses

static char *__get_last(char **list) {
char *res = 0;
Expand Down
7 changes: 3 additions & 4 deletions libc/intrin/kprintf.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ __msabi extern typeof(WriteFile) *const __imp_WriteFile;
// clang-format on

long __klog_handle;
extern struct SymbolTable *__symtab;

__funline char *kadvance(char *p, char *e, long n) {
intptr_t t = (intptr_t)p;
Expand Down Expand Up @@ -755,11 +754,11 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt,
// can be manually consulted to look up the faulting code.
int idx;
x = va_arg(va, intptr_t);
if (_weaken(__symtab) && *_weaken(__symtab) &&
if (_weaken(__symtab) && _weaken(__symtab)->st &&
(idx = _weaken(__get_symbol)(0, x)) != -1) {
if (p + 1 <= e) *p++ = '&';
s = (*_weaken(__symtab))->name_base +
(*_weaken(__symtab))->names[idx];
s = _weaken(__symtab)->st->name_base +
_weaken(__symtab)->st->names[idx];
goto FormatString;
}
base = 4;
Expand Down
6 changes: 2 additions & 4 deletions libc/runtime/getsymbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"

extern struct SymbolTable *__symtab;

/**
* Returns low index into symbol table for address.
*
Expand All @@ -33,8 +31,8 @@ privileged int __get_symbol(struct SymbolTable *t, intptr_t a) {
// we don't want function tracing because:
// function tracing depends on this function via kprintf
unsigned l, m, r, n, k;
if (!t && __symtab) {
t = __symtab;
if (!t && __symtab.st) {
t = __symtab.st;
}
if (t) {
l = 0;
Expand Down
146 changes: 54 additions & 92 deletions libc/runtime/getsymboltable.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,127 +16,89 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/intrin/bits.h"
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/cosmo.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/kprintf.h"
#include "libc/intrin/promises.internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/intrin/weaken.h"
#include "libc/macros.internal.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#include "libc/runtime/zipos.internal.h"
#include "libc/str/str.h"
#include "libc/thread/thread.h"
#include "libc/x/x.h"
#include "libc/zip.internal.h"
#include "third_party/puff/puff.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"

__static_yoink("__get_symbol");

static pthread_spinlock_t g_lock;
struct SymbolTable *__symtab; // for kprintf
struct SymbolTableLoader __symtab;

static ssize_t GetZipFile(struct Zipos *zipos, const char *name) {
size_t i, n, c, z;
z = strlen(name);
c = GetZipCdirOffset(zipos->cdir);
n = GetZipCdirRecords(zipos->cdir);
for (i = 0; i < n; ++i, c += ZIP_CFILE_HDRSIZE(zipos->map + c)) {
if (ZIP_CFILE_NAMESIZE(zipos->map + c) == z &&
!memcmp(ZIP_CFILE_NAME(zipos->map + c), name, z)) {
return c;
}
}
return -1;
}

/**
* Reads symbol table from zip directory.
* @note This code can't depend on dlmalloc()
*/
static struct SymbolTable *GetSymbolTableFromZip(struct Zipos *zipos) {
ssize_t cf, lf;
size_t size, size2;
static struct SymbolTable *GetSymbolTableFromZip(void) {
int fd;
struct SymbolTable *res = 0;
if ((cf = GetZipFile(zipos, ".symtab." _ARCH_NAME)) != -1 ||
(cf = GetZipFile(zipos, ".symtab")) != -1) {
lf = GetZipCfileOffset(zipos->map + cf);
size = GetZipLfileUncompressedSize(zipos->map + lf);
size2 = ROUNDUP(size, FRAMESIZE);
if ((res = _mapanon(size2))) {
switch (ZIP_LFILE_COMPRESSIONMETHOD(zipos->map + lf)) {
case kZipCompressionNone:
memcpy(res, (void *)ZIP_LFILE_CONTENT(zipos->map + lf), size);
break;
case kZipCompressionDeflate:
if (__inflate((void *)res, size,
(void *)ZIP_LFILE_CONTENT(zipos->map + lf),
GetZipLfileCompressedSize(zipos->map + lf))) {
munmap(res, size2);
res = 0;
}
break;
default:
munmap(res, size2);
res = 0;
break;
}
if ((fd = open("/zip/.symtab." _ARCH_NAME, O_RDONLY)) != -1 ||
(fd = open("/zip/.symtab", O_RDONLY)) != -1) {
void *map;
ssize_t size;
if ((size = lseek(fd, 0, SEEK_END)) != -1 &&
(map = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
MAP_FAILED) {
res = map;
}
close(fd);
}
STRACE("GetSymbolTableFromZip() → %p", res);
return res;
}

/**
* Reads symbol table from .com.dbg file.
* @note This code can't depend on dlmalloc()
*/
static struct SymbolTable *GetSymbolTableFromElf(void) {
const char *s;
if (PLEDGED(RPATH) && (s = FindDebugBinary())) {
return OpenSymbolTable(s);
const char *path;
if ((path = FindDebugBinary())) {
return OpenSymbolTable(path);
} else {
return 0;
}
}

static void GetSymbolTableInit(void) {
if (!PLEDGED(RPATH)) return;
int e = errno;
if ((__symtab.st = GetSymbolTableFromZip())) {
__symtab.st->names =
(uint32_t *)((char *)__symtab.st + __symtab.st->names_offset);
__symtab.st->name_base =
(char *)((char *)__symtab.st + __symtab.st->name_base_offset);
}
if (!__symtab.st) {
__symtab.st = GetSymbolTableFromElf();
}
errno = e;
}

/**
* Returns symbol table singleton.
*
* This uses multiple strategies to find the symbol table. The first
* strategy, depends on whether or not the following is linked:
*
* __static_yoink("__zipos_get");
* __static_yoink("zipos");
*
* In that case, the symbol table may be read from `/zip/.symtab` which
* is generated by `o//tool/build/symtab.com`. The second strategy is to
* look for the concomitant `.com.dbg` executable, which may very well
* be the one currently executing, or it could be placed in the same
* folder as your `.com` binary, or lastly, it could be explicitly
* specified via the `COMDBG` environment variable.
* In that case, the symbol table may be read from `/zip/.symtab.ARCH`
* or `/zip/.symtab` which are generated by `o//tool/build/symtab.com`
* or `o//tool/build/apelink.com`.
*
* Function tracing is disabled throughout the duration of this call.
* Backtraces and other core runtime functionality depend on this.
* The second strategy is to look for the ELF executable for the current
* program. If you're running a .com binary, it'll look for the .com.dbg
* file. If it's running the .com.dbg or .elf file, then it'll just read
* the symbols from itself. In other cases, you can explicitly specify a
* debug symbol binary via the `COMDBG` environment variable.
*
* When using pledge() security, it's recommended that this function get
* called *before* calling pledge() if it lacks the rpath promise, so it
* can load the symbols into memory before the filesystem goes away.
*
* @return symbol table, or NULL if not found
*/
struct SymbolTable *GetSymbolTable(void) {
struct Zipos *z;
if (pthread_spin_trylock(&g_lock)) return 0;
if (!__symtab && !__isworker) {
if (_weaken(__zipos_get) && (z = _weaken(__zipos_get)())) {
if ((__symtab = GetSymbolTableFromZip(z))) {
__symtab->names =
(uint32_t *)((char *)__symtab + __symtab->names_offset);
__symtab->name_base =
(char *)((char *)__symtab + __symtab->name_base_offset);
}
}
if (!__symtab) {
__symtab = GetSymbolTableFromElf();
}
}
pthread_spin_unlock(&g_lock);
return __symtab;
cosmo_once(&__symtab.once, GetSymbolTableInit);
return __symtab.st;
}
6 changes: 6 additions & 0 deletions libc/runtime/symbols.internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ struct SymbolTable {
struct Symbol symbols[]; /* sorted and non-overlapping intervals */
};

struct SymbolTableLoader {
_Atomic(unsigned) once;
struct SymbolTable *st;
};

extern struct SymbolTableLoader __symtab;
struct SymbolTable *GetSymbolTable(void);
const char *FindComBinary(void);
const char *FindDebugBinary(void);
Expand Down
4 changes: 2 additions & 2 deletions libc/runtime/zipos-access.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ int __zipos_access(struct ZiposUri *name, int amode) {
return enoexec();
}

ssize_t cf;
int cf;
if ((cf = __zipos_find(z, name)) == -1) {
return -1;
}

int mode;
if (cf != ZIPOS_SYNTHETIC_DIRECTORY) {
mode = GetZipCfileMode(z->map + cf);
mode = GetZipCfileMode(z->cdir + cf);
} else {
mode = S_IFDIR | 0555;
}
Expand Down
19 changes: 9 additions & 10 deletions libc/runtime/zipos-close.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/syscall-sysv.internal.h"
Expand All @@ -30,16 +31,14 @@
* @vforksafe
*/
int __zipos_close(int fd) {
int rc;
struct ZiposHandle *h;
h = (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle;
if (!IsWindows()) {
rc = sys_close(fd);
} else {
rc = 0; // no system file descriptor needed on nt
}
if (!__vforked) {
__zipos_free(h);
if (__vforked) {
sys_close(fd);
return 0;
}
struct ZiposHandle *h = (struct ZiposHandle *)(intptr_t)g_fds.p[fd].handle;
g_fds.p[fd].handle = h->handle;
g_fds.p[fd].kind = kFdFile;
int rc = close(fd);
__zipos_free(h);
return rc;
}
Loading

0 comments on commit b01282e

Please sign in to comment.