Make improvements to redbean

The following Lua APIs have been added:

  - IsDaemon() → bool
  - ProgramPidPath(str)

The following Lua hooks have been added:

  - OnClientConnection(ip:int,port:int,serverip:int,serverport:int) → bool
  - OnProcessCreate(pid:int,ip:int,port:int,serverip:int,serverport:int)
  - OnProcessDestroy(pid:int)
  - OnServerStart()
  - OnServerStop()
  - OnWorkerStart()
  - OnWorkerStop()

redbean now does a better job at applying gzip on the fly from the local
filesystem, using a streaming chunked api with constant memory, which is
useful for doing things like serving a 4gb text file off NFS, and having
it start transmitting in milliseconds. redbean will also compute entropy
on the beginnings of files to determine if compression is profitable.

This change pays off technical debts relating to memory, such as relying
on exit() to free() allocations. That's now mostly fixed so it should be
easier now to spot memory leaks in malloc traces.

This change also fixes bugs and makes improvements to our SSL support.
Uniprocess mode failed handshakes are no longer an issue. Token Alpn is
offered so curl -v looks less weird. Hybrid SSL certificate loading is
now smarter about naming conflicts. Self-signed CA root anchors will no
longer be delivered to the client during the handshake.
This commit is contained in:
Justine Tunney 2021-07-10 15:02:03 -07:00
parent 98c674d915
commit 8c4cce043c
25 changed files with 22326 additions and 359 deletions

View file

@ -24,7 +24,7 @@ size_t bulk_free(void *array[], size_t nelem) {
for (a = array; a != fence; ++a) {
void *mem = *a;
if (mem != 0) {
mchunkptr p = mem2chunk(ADDRESS_DEATH_ACTION(mem));
mchunkptr p = mem2chunk(AddressDeathAction(mem));
size_t psize = chunksize(p);
#if FOOTERS
if (get_mstate_for(p) != g_dlmalloc) {

View file

@ -74,14 +74,14 @@ static void **ialloc(mstate m, size_t n_elements, size_t *sizes, int opts,
size_t array_chunk_size;
array_chunk = chunk_plus_offset(p, contents_size);
array_chunk_size = remainder_size - contents_size;
marray = ADDRESS_BIRTH_ACTION((void **)(chunk2mem(array_chunk)));
marray = AddressBirthAction((void **)(chunk2mem(array_chunk)));
set_size_and_pinuse_of_inuse_chunk(m, array_chunk, array_chunk_size);
remainder_size = contents_size;
}
/* split out elements */
for (i = 0;; ++i) {
marray[i] = ADDRESS_BIRTH_ACTION(chunk2mem(p));
marray[i] = AddressBirthAction(chunk2mem(p));
if (i != n_elements - 1) {
if (element_size != 0)
size = element_size;

View file

@ -578,7 +578,7 @@ static void *tmalloc_large(mstate m, size_t nb) {
return 0;
}
void *dlmalloc(size_t bytes) {
void *dlmalloc_impl(size_t bytes, bool takeaction) {
/*
Basic algorithm:
If a small request (< 256 bytes minus per-chunk overhead):
@ -708,12 +708,16 @@ void *dlmalloc(size_t bytes) {
postaction:
POSTACTION(g_dlmalloc);
return ADDRESS_BIRTH_ACTION(mem);
return takeaction ? AddressBirthAction(mem) : mem;
}
return 0;
}
void *dlmalloc(size_t bytes) {
return dlmalloc_impl(bytes, true);
}
void dlfree(void *mem) {
/*
Consolidate freed chunks with preceeding or succeeding bordering
@ -721,7 +725,7 @@ void dlfree(void *mem) {
with special cases for top, dv, mmapped chunks, and usage errors.
*/
if (mem != 0) {
mem = ADDRESS_DEATH_ACTION(mem);
mem = AddressDeathAction(mem);
mchunkptr p = mem2chunk(mem);
#if FOOTERS
@ -887,7 +891,7 @@ void *dlmemalign$impl(mstate m, size_t alignment, size_t bytes) {
} else {
size_t nb = request2size(bytes);
size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD;
mem = dlmalloc(req);
mem = dlmalloc_impl(req, false);
if (mem != 0) {
mchunkptr p = mem2chunk(mem);
if (PREACTION(m)) return 0;
@ -936,7 +940,7 @@ void *dlmemalign$impl(mstate m, size_t alignment, size_t bytes) {
POSTACTION(m);
}
}
return ADDRESS_BIRTH_ACTION(mem);
return AddressBirthAction(mem);
}
void *dlmemalign(size_t alignment, size_t bytes) {

View file

@ -3,9 +3,12 @@
#ifndef __STRICT_ANSI__
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/log/backtrace.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/symbols.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#if 0
@ -674,15 +677,64 @@ extern struct MallocState g_dlmalloc[1];
/* ─────────────────────────────── Hooks ──────────────────────────────── */
#ifdef MTRACE /* TODO(jart): Add --mtrace flag for this */
void *AddressBirthAction(void *);
void *AddressDeathAction(void *);
#define ADDRESS_BIRTH_ACTION(A) AddressBirthAction(A)
#define ADDRESS_DEATH_ACTION(A) AddressDeathAction(A)
#else
#define ADDRESS_BIRTH_ACTION(A) (A)
#define ADDRESS_DEATH_ACTION(A) (A)
/*
d = {}
lines = open("log").read().split('\n')
def bad(i):
while i < len(lines):
if lines[i].startswith(('BIRTH', 'DEATH')):
break
print lines[i]
i += 1
for i, line in enumerate(lines):
i += 1
x = line.split()
if len(x) != 2: continue
if x[0] == 'DEATH':
b = int(x[1], 16)
if b in d:
if d[b] < 0:
print "OH NO", i, d[b]
else:
d[b] = -d[b]
else:
print "wut", i
elif x[0] == 'BIRTH':
b = int(x[1], 16)
if b in d:
if d[b] > 0:
print "bad malloc", i, d[b]
d[b] = i
else:
d[b] = i
for k,v in d.items():
if v > 0:
print "unfreed", v
bad(v)
*/
#define MALLOC_TRACE 0
static inline void *AddressBirthAction(void *p) {
#if MALLOC_TRACE
(dprintf)(2, "BIRTH %p\n", p);
if (weaken(PrintBacktraceUsingSymbols) && weaken(GetSymbolTable)) {
weaken(PrintBacktraceUsingSymbols)(2, __builtin_frame_address(0),
weaken(GetSymbolTable)());
}
#endif
return p;
}
static inline void *AddressDeathAction(void *p) {
#if MALLOC_TRACE
(dprintf)(2, "DEATH %p\n", p);
if (weaken(PrintBacktraceUsingSymbols) && weaken(GetSymbolTable)) {
weaken(PrintBacktraceUsingSymbols)(2, __builtin_frame_address(0),
weaken(GetSymbolTable)());
}
#endif
return p;
}
/*
PREACTION should be defined to return 0 on success, and nonzero on
@ -907,7 +959,7 @@ extern struct MallocParams g_mparams;
#else /* GNUC */
#define RTCHECK(e) (e)
#endif /* GNUC */
#else /* !IsTrustworthy() */
#else /* !IsTrustworthy() */
#define RTCHECK(e) (1)
#endif /* !IsTrustworthy() */

View file

@ -1,54 +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/fmt/itoa.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "third_party/dlmalloc/dlmalloc.internal.h"
static uintptr_t lastfree_;
void *AddressBirthAction(void *addr) {
char buf[64], *p;
p = buf;
p = stpcpy(p, __FUNCTION__);
p = stpcpy(p, ": 0x");
p += uint64toarray_radix16((uintptr_t)addr, p);
*p++ = '\n';
__print(buf, p - buf);
if (lastfree_ == (uintptr_t)addr) {
lastfree_ = 0;
}
return addr;
}
void *AddressDeathAction(void *addr) {
char buf[64], *p;
p = buf;
p = stpcpy(p, __FUNCTION__);
p = stpcpy(p, ": 0x");
p += uint64toarray_radix16((uintptr_t)addr, p);
if (lastfree_ != (uintptr_t)addr) {
lastfree_ = (uintptr_t)addr;
} else {
p = stpcpy(p, " [OBVIOUS DOUBLE FREE]");
}
*p++ = '\n';
__print(buf, p - buf);
return addr;
}