Make major improvements to redbean and libraries

The most exciting improvement is dynamic pages will soon be able to use
the executable itself as an object store. it required a heroic technique
for overcoming ETXTBSY restrictions which lets us open the executable in
read/write mode, which means (1) wa can restore the APE header, and (2)
we can potentially containerize redbean extension code so that modules
you download for your redbean online will only impact your redbean.

Here's a list of breaking changes to redbean:

- Remove /tool/net/ prefix from magic ZIP paths
- GetHeader() now returns NIL if header is absent

Here's a list of fixes and enhancements to redbean:

- Support 64-bit ZIP archives
- Record User-Agent header in logs
- Add twelve error handlers to accept()
- Display octal st_mode on listing page
- Show ZIP file comments on listing page
- Restore APE MZ header on redbean startup
- Track request count on redbean index page
- Report server uptime on redbean index page
- Don't bind server socket using SO_REUSEPORT
- Fix #151 where Lua LoadAsset() could free twice
- Report rusage accounting when workers exit w/ -vv
- Use ZIP iattr field as text/plain vs. binary hint
- Add ParseUrl() API for parsing things like a.href
- Add ParseParams() API for parsing HTTP POST bodies
- Add IsAcceptablePath() API for checking dots, etc.
- Add IsValidHttpToken() API for validating sane ASCII
- Add IsAcceptableHostPort() for validating HOST[:PORT]
- Send 400 response to HTTP/1.1 requests without a Host
- Send 403 response if ZIP or file isn't other readable
- Add virtual hosting that tries prepending Host to path
- Route requests based on Host in Request-URI if present
- Host routing will attempt to remove or add the www. prefix
- Sign-extend UNIX timestamps and don't adjust FileTime zone

Here's some of the improvements made to Cosmopolitan Libc:

- Fix ape.S indentation
- Improve consts.sh magnums
- Write pretty good URL parser
- Improve rusage accounting apis
- Bring mremap() closer to working
- Added ZIP APIs which will change
- Check for overflow in reallocarray()
- Remove overly fancy linkage in strerror()
- Fix GDB attach on crash w/ OpenBSD msyscall()
- Make sigqueue() portable to most UNIX distros
- Make integer serialization macros more elegant
- Bring back 34x tprecode8to16() performance boost
- Make malloc() more resilient to absurdly large sizes
This commit is contained in:
Justine Tunney 2021-04-18 11:34:59 -07:00
parent 69c508729e
commit bf03b2e64c
307 changed files with 4557 additions and 2581 deletions

75
libc/runtime/clktck.c Normal file
View file

@ -0,0 +1,75 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/runtime/clktck.h"
#include "libc/sysv/consts/auxv.h"
struct clockinfo_netbsd {
int32_t hz; // number of clock ticks per second
int32_t tick; // µs per tick
int32_t tickadj; // skew rate for adjtime()
int32_t stathz; // statistics clock frequency
int32_t profhz; // profiling clock frequency
};
static int clk_tck;
static noinline int __clk_tck_init(void) {
int x;
int cmd[2];
size_t len;
struct clockinfo_netbsd clock;
if (IsXnu() || IsOpenbsd()) {
x = 100;
} else if (IsFreebsd()) {
x = 128;
} else if (IsNetbsd()) {
cmd[0] = 1; // CTL_KERN
cmd[1] = 12; // KERN_CLOCKRATE
len = sizeof(clock);
if (sysctl(cmd, 2, &clock, &len, NULL, 0) != -1) {
x = clock.hz;
} else {
x = -1;
}
} else {
x = getauxval(AT_CLKTCK);
}
if (x < 1) x = 100;
clk_tck = x;
return x;
}
/**
* Returns system clock ticks per second.
*
* The returned value is memoized. This function is intended to be
* used via the `CLK_TCK` macro wrapper.
*
* The returned value is always greater than zero. It's usually 100
* hertz which means each clock tick is 10 milliseconds long.
*/
int __clk_tck(void) {
if (clk_tck) {
return clk_tck;
} else {
return __clk_tck_init();
}
}

12
libc/runtime/clktck.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_CLKTCK_H_
#define COSMOPOLITAN_LIBC_RUNTIME_CLKTCK_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define CLK_TCK (__clk_tck())
int __clk_tck(void) pureconst;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_CLKTCK_H_ */

View file

@ -59,8 +59,9 @@
*/
void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) {
struct DirectMap dm;
int i, x, n, a, b, f;
int i, x, n, m, a, b, f;
if (!size) return VIP(einval());
if (size > 0x0000010000000000) return VIP(enomem());
if (!ALIGNED(off)) return VIP(einval());
if (!ALIGNED(addr)) return VIP(einval());
if (!CANONICAL(addr)) return VIP(einval());
@ -77,6 +78,7 @@ void *mmap(void *addr, size_t size, int prot, int flags, int fd, int64_t off) {
n = ROUNDUP(size, FRAMESIZE) >> 16;
for (i = 0; i < _mmi.i; ++i) {
if (_mmi.p[i].y < x) continue;
if (__builtin_add_overflow(_mmi.p[i].y, n, &m)) return VIP(enomem());
if (_mmi.p[i].x > x + n - 1) break;
x = _mmi.p[i].y + 1;
}

View file

@ -0,0 +1,55 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/mremap.h"
#include "libc/sysv/errfuns.h"
privileged void *sys_mremap(void *p, size_t n, size_t m, int f, void *q) {
bool cf;
uintptr_t rax, rdi, rsi, rdx;
register uintptr_t r8 asm("r8");
register uintptr_t r10 asm("r10");
if (IsLinux()) {
r10 = f;
r8 = (uintptr_t)q;
asm("syscall"
: "=a"(rax)
: "0"(0x019), "D"(p), "S"(n), "d"(m), "r"(r10), "r"(r8)
: "rcx", "r11", "memory", "cc");
if (rax > -4096ul) errno = -rax, rax = -1;
} else if (IsNetbsd()) {
if (f & MREMAP_MAYMOVE) {
rax = 0x19B;
r10 = m;
r8 = (f & MREMAP_FIXED) ? MAP_FIXED : 0;
asm(CFLAG_ASM("syscall")
: CFLAG_CONSTRAINT(cf), "+a"(rax)
: "D"(p), "S"(n), "d"(q), "r"(r10), "r"(r8)
: "rcx", "r11", "memory", "cc");
if (cf) errno = rax, rax = -1;
} else {
rax = einval();
}
} else {
rax = enosys();
}
return (void *)rax;
}

View file

@ -17,9 +17,46 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/sysv/consts/mremap.h"
#include "libc/sysv/errfuns.h"
void *mremap(void *old_address, size_t old_size, size_t new_size, int flags,
void *new_address) {
return (void *)(intptr_t)enosys();
#define IP(X) (intptr_t)(X)
#define VIP(X) (void *)IP(X)
#define ALIGNED(p) (!(IP(p) & (FRAMESIZE - 1)))
/**
* Relocates mapping.
*
* @param p is old address
* @param n is old size
* @param m is new size
* @param f should have MREMAP_MAYMOVE and may have MAP_FIXED
* @param q is new address
*/
void *mremap(void *p, size_t n, size_t m, int f, ... /* void *q */) {
return VIP(enosys()); /* TODO: Implement Me! */
void *q;
va_list va;
if (!IsWindows()) {
if (!n) return VIP(einval());
if (!m) return VIP(einval());
if (!ALIGNED(p)) return VIP(einval());
n = ROUNDUP(n, FRAMESIZE);
m = ROUNDUP(m, FRAMESIZE);
if (f & MREMAP_FIXED) {
va_start(va, f);
q = va_arg(va, void *);
va_end(va);
if (!ALIGNED(q)) return VIP(einval());
} else {
q = NULL;
if (!(f & MREMAP_MAYMOVE)) {
}
}
return VIP(enosys());
} else {
return VIP(enosys());
}
}

View file

@ -0,0 +1,188 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/prot.h"
.privileged
// Opens executable in O_RDWR mode.
//
// To avoid ETXTBSY we need to unmap the running executable first,
// then open the file, and finally load the code back into memory.
//
// @return file descriptor
// @note only works on .com binary (not .com.dbg)
// @note only supports linux, freebsd, openbsd, and netbsd
OpenExecutable:
push %rbp
mov %rsp,%rbp
pushq __NR_open(%rip) # -0x08(%rbp)
pushq __NR_mmap(%rip) # -0x10(%rbp)
pushq __NR_munmap(%rip) # -0x18(%rbp)
pushq O_RDWR(%rip) # -0x20(%rbp)
pushq MAP_ANONYMOUS(%rip) # -0x28(%rbp)
pushq MAP_PRIVATE(%rip) # -0x30(%rbp)
pushq MAP_FIXED(%rip) # -0x38(%rbp)
pushq MAP_SHARED(%rip) # -0x40(%rbp)
push %rbx # code buffer
push %r12 # data buffer
push %r14 # filename
push %r15 # fd
// Get filename.
mov AT_EXECFN,%edi
call getauxval
mov %rax,%r14
// Allocate code buffer.
mov -0x10(%rbp),%eax # __NR_mmap
xor %edi,%edi
mov $PAGESIZE,%esi
mov $PROT_READ|PROT_WRITE|PROT_EXEC,%edx
mov -0x28(%rbp),%r10d # MAP_ANONYMOUS
or -0x30(%rbp),%r10d # MAP_PRIVATE
mov $-1,%r8
mov $0,%r9
push %r9 # openbsd:pad
push %r9 # openbsd:align
syscall
pop %r9
pop %r9
mov %rax,%rbx
// Allocate data buffer.
mov -0x10(%rbp),%eax # __NR_mmap
xor %edi,%edi
mov $ape_ram_filesz,%esi
mov $PROT_READ|PROT_WRITE,%edx
mov -0x28(%rbp),%r10d # MAP_ANONYMOUS
or -0x30(%rbp),%r10d # MAP_PRIVATE
mov $-1,%r8
mov $0,%r9
push %r9 # openbsd:pad
push %r9 # openbsd:align
syscall
pop %r9
pop %r9
mov %rax,%r12
// Move data.
mov %r12,%rdi
mov $ape_ram_vaddr,%esi
mov $ape_ram_filesz,%ecx
rep movsb
// Move code.
mov %rbx,%rdi
mov $8f,%esi
mov $9f-8f,%ecx
rep movsb
jmp *%rbx
// <LIMBO>
// Unmap code segment.
8: mov -0x18(%rbp),%eax # __NR_munmap
mov $ape_rom_vaddr,%edi
mov $ape_rom_filesz,%esi
syscall
// Unmap data segment.
mov -0x18(%rbp),%eax # __NR_munmap
mov $ape_ram_vaddr,%edi
mov $ape_ram_filesz,%esi
syscall
// Open executable in read-write mode.
mov -0x08(%rbp),%eax # __NR_open
mov %r14,%rdi
mov -0x20(%rbp),%esi # O_RDWR
syscall
mov %eax,%r15d
// Map code segment.
mov -0x10(%rbp),%eax # __NR_mmap
mov $ape_rom_vaddr,%edi
mov $ape_rom_filesz,%esi
mov $PROT_READ|PROT_EXEC,%edx
mov -0x38(%rbp),%r10d # MAP_FIXED
or -0x40(%rbp),%r10d # MAP_SHARED
mov %r15d,%r8d
mov $ape_rom_offset,%r9d
push %r9 # openbsd:pad
push %r9 # openbsd:align
syscall
pop %r9
pop %r9
// Allocate data segment.
mov -0x10(%rbp),%eax # __NR_mmap
mov $ape_ram_vaddr,%edi
mov $ape_ram_filesz,%esi
mov $PROT_READ|PROT_WRITE,%edx
mov -0x38(%rbp),%r10d # MAP_FIXED
or -0x30(%rbp),%r10d # MAP_PRIVATE
or -0x28(%rbp),%r10d # MAP_ANONYMOUS
mov $-1,%r8
mov $0,%r9
push %r9 # openbsd:pad
push %r9 # openbsd:align
syscall
pop %r9
pop %r9
// Put data back.
mov $ape_ram_vaddr,%edi
xchg %eax,%esi
mov $ape_ram_filesz,%ecx
rep movsb
// Jump back.
mov $9f,%eax
jmp *%rax
// </LIMBO>
// Deallocate code buffer.
9: mov __NR_munmap,%eax
mov %rbx,%rdi
mov $PAGESIZE,%esi
syscall
// Deallocate data buffer.
mov __NR_munmap,%eax
mov %r12,%rdi
mov $ape_ram_filesz,%esi
syscall
mov %r15d,%eax
pop %r15
pop %r14
pop %r12
pop %rbx
leave
ret
9: .endfn OpenExecutable,globl
.weak ape_rom_vaddr
.weak ape_rom_filesz
.weak ape_rom_offset
.weak ape_ram_vaddr
.weak ape_ram_filesz

View file

@ -55,7 +55,7 @@ int clearenv(void);
void fpreset(void);
int issetugid(void);
void *mmap(void *, uint64_t, int32_t, int32_t, int32_t, int64_t);
void *mremap(void *, uint64_t, uint64_t, int32_t, void *);
void *mremap(void *, size_t, size_t, int, ...);
int munmap(void *, uint64_t);
int mprotect(void *, uint64_t, int) privileged;
int msync(void *, size_t, int);
@ -87,6 +87,7 @@ void _savexmm(void *);
void _weakfree(void *);
void free_s(void *) paramsnonnull() libcesque;
int close_s(int *) paramsnonnull() libcesque;
int OpenExecutable(void);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -16,10 +16,32 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/clktck.h"
#include "libc/runtime/sysconf.h"
/**
* Returns configuration value about system.
* @param thing can be _SC_XXX
*
* The following parameters are supported:
*
* - `_SC_CLK_TCK` returns number of clock ticks per second
* - `_SC_ARG_MAX` currently always returns 32768 due to Windows
* - `_SC_PAGESIZE` currently always returns 65536 due to Windows
*
* You are encouraged to undiamond calls to this API as follows:
*
* - Use `CLK_TCK` instead of `getconf(_SC_CLK_TCK)`
* - Use `PAGESIZE` or `FRAMESIZE` instead of `getconf(_SC_PAGESIZE)`
*/
long(sysconf)(int thing) { return __sysconf(thing); }
long sysconf(int name) {
switch (name) {
case _SC_ARG_MAX:
return ARG_MAX;
case _SC_CLK_TCK:
return CLK_TCK;
case _SC_PAGESIZE:
return FRAMESIZE;
default:
return -1;
}
}

View file

@ -1,7 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_RUNTIME_SYSCONF_H_
#define COSMOPOLITAN_LIBC_RUNTIME_SYSCONF_H_
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/auxv.h"
#define _SC_ARG_MAX 0
#define _SC_CLK_TCK 2
@ -13,26 +11,6 @@ COSMOPOLITAN_C_START_
long sysconf(int);
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define sysconf(X) __sysconf(X)
forceinline long __sysconf(int thing) {
switch (thing) {
case _SC_ARG_MAX:
return ARG_MAX;
case _SC_CLK_TCK: {
extern const long __AT_CLKTCK asm("AT_CLKTCK");
long res = getauxval(__AT_CLKTCK);
if (!res) res = 100;
return res;
}
case _SC_PAGESIZE:
return FRAMESIZE;
default:
return -1;
}
}
#endif /* GNU && !ANSI */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_RUNTIME_SYSCONF_H_ */

View file

@ -160,17 +160,15 @@ static noasan textwindows wontreturn void WinMainNew(void) {
* able to assume that stack addresses are located at higher
* addresses than heap and program memory.
*
* 5. Windows users are afraid of "drive-by downloads" where someone
* might accidentally an evil DLL to their Downloads folder which
* then overrides the behavior of a legitimate EXE being run from
* the downloads folder. Since we don't even use dynamic linking,
* we've cargo culted some API calls, that may harden against it.
* 5. Reconfigure x87 FPU so long double is actually long (80 bits).
*
* 6. Reconfigure x87 FPU so long double is actually long (80 bits).
*
* 7. Finally, we need fork. Microsoft designed Windows to prevent us
* from having fork() so we pass pipe handles in an environment
* variable literally copy all the memory.
* 6. Finally, we need fork. Since disagreeing with fork is axiomatic to
* Microsoft's engineering culture, we need to go to great lengths to
* have it anyway without breaking Microsoft's rules: using the WIN32
* API (i.e. not NTDLL) to copy MAP_PRIVATE pages via a pipe. It'd go
* faster if the COW pages CreateFileMappingNuma claims to have turns
* out to be true. Until then we have a "PC Scale" and entirely legal
* workaround that they hopefully won't block using Windows Defender.
*
* @param hInstance call GetModuleHandle(NULL) from main if you need it
*/