Fix bugs and make improvements to redbean

- Abort if .init.lua fails
- Refactor redbean to use new append library
- Use first certificate if SNI routing fails
- Use function/data sections when building Lua
- Don't use self-signed auto-generated cert for client
- Add -D staging dirs to redbean lua module default path
This commit is contained in:
Justine Tunney 2021-08-06 14:12:11 -07:00
parent 55a15c204e
commit aeeb851422
26 changed files with 703 additions and 513 deletions

View file

@ -44,9 +44,10 @@ double MeasureEntropy(const char *p, size_t n) {
if (h[i]) {
x = h[i];
x /= n;
e += x * log2(1 / x);
e += x * log(x);
}
}
e = -(e / M_LN2);
}
return e;
}

View file

@ -32,16 +32,18 @@ struct MemoryIntervals {
} p[128];
};
extern struct MemoryIntervals _mmi;
extern hidden struct MemoryIntervals _mmi;
unsigned FindMemoryInterval(const struct MemoryIntervals *, int) nosideeffect;
bool AreMemoryIntervalsOk(const struct MemoryIntervals *) nosideeffect;
void PrintMemoryIntervals(int, const struct MemoryIntervals *);
int TrackMemoryInterval(struct MemoryIntervals *, int, int, long, int, int);
unsigned FindMemoryInterval(const struct MemoryIntervals *,
int) nosideeffect hidden;
bool AreMemoryIntervalsOk(const struct MemoryIntervals *) nosideeffect hidden;
void PrintMemoryIntervals(int, const struct MemoryIntervals *) hidden;
int TrackMemoryInterval(struct MemoryIntervals *, int, int, long, int,
int) hidden;
int ReleaseMemoryIntervals(struct MemoryIntervals *, int, int,
void (*)(struct MemoryIntervals *, int, int));
void ReleaseMemoryNt(struct MemoryIntervals *, int, int);
int UntrackMemoryIntervals(void *, size_t);
void (*)(struct MemoryIntervals *, int, int)) hidden;
void ReleaseMemoryNt(struct MemoryIntervals *, int, int) hidden;
int UntrackMemoryIntervals(void *, size_t) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -1,5 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_STDIO_APPEND_INTERNAL_H_
#define COSMOPOLITAN_LIBC_STDIO_APPEND_INTERNAL_H_
#ifndef COSMOPOLITAN_LIBC_STDIO_APPEND_H_
#define COSMOPOLITAN_LIBC_STDIO_APPEND_H_
#include "libc/fmt/pflink.h"
#define APPEND_COOKIE 21578
@ -8,22 +8,23 @@
COSMOPOLITAN_C_START_
struct appendz {
size_t i; /* data size */
size_t n; /* allocation size */
size_t i;
size_t n;
};
int appendf(char **, const char *, ...);
int vappendf(char **, const char *, va_list);
int appends(char **, const char *);
int appendd(char **, const void *, size_t);
int appendw(char **, uint64_t);
struct appendz appendz(char *);
ssize_t appendr(char **, size_t);
ssize_t appendd(char **, const void *, size_t);
ssize_t appendw(char **, uint64_t);
ssize_t appends(char **, const char *);
ssize_t appendf(char **, const char *, ...);
ssize_t vappendf(char **, const char *, va_list);
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define appendf(BUF, FMT, ...) (appendf)(BUF, PFLINK(FMT), ##__VA_ARGS__)
#define vappendf(BUF, FMT, VA) (vappendf)(BUF, PFLINK(FMT), VA)
#define appendf(BUF, FMT, ...) appendf(BUF, PFLINK(FMT), ##__VA_ARGS__)
#define vappendf(BUF, FMT, VA) vappendf(BUF, PFLINK(FMT), VA)
#endif
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_STDIO_APPEND_INTERNAL_H_ */
#endif /* COSMOPOLITAN_LIBC_STDIO_APPEND_H_ */

View file

@ -26,11 +26,24 @@
#define W sizeof(size_t)
/**
* Appends raw data to buffer.
* Appends data to buffer, e.g.
*
* char *b = 0;
* appendd(&b, "hello", 5);
* free(b);
*
* The resulting buffer is guaranteed to be NUL-terminated, i.e.
* `!b[appendz(b).i]` will be the case.
*
* @param s may contain nul characters and may be null if `l` is zero
* @param l is byte length of `s`
* @return bytes appended (always `l`) or -1 if `ENOMEM`
* @see appendz(b).i to get buffer length
*/
int appendd(char **b, const void *s, size_t l) {
ssize_t appendd(char **b, const void *s, size_t l) {
char *p;
struct appendz z;
assert(b);
z = appendz((p = *b));
if (ROUNDUP(z.i + l + 1, 8) + W > z.n) {
if (!z.n) z.n = W * 2;

View file

@ -19,9 +19,16 @@
#include "libc/stdio/append.internal.h"
/**
* Appends formatted data to buffer.
* Appends formatted string to buffer, e.g.
*
* char *b = 0;
* appendf(&b, "hello %d\n", 123);
* free(b);
*
* @return bytes appended or -1 if `ENOMEM`
* @see appendz(b).i to get buffer length
*/
int(appendf)(char **b, const char *fmt, ...) {
ssize_t(appendf)(char **b, const char *fmt, ...) {
int n;
va_list va;
va_start(va, fmt);

71
libc/stdio/appendr.c Normal file
View file

@ -0,0 +1,71 @@
/*-*- 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/assert.h"
#include "libc/dce.h"
#include "libc/macros.internal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/append.internal.h"
#include "libc/str/str.h"
#define W sizeof(size_t)
/**
* Resets length of append buffer, e.g.
*
* char *b = 0;
* appends(&b, "hello");
* appendr(&b, 1);
* assert(!strcmp(b, "h"));
* appendr(&b, 0);
* assert(!strcmp(b, ""));
* free(b);
*
* If `i` is greater than the current length then the extra bytes are
* filled with NUL characters.
*
* The resulting buffer is guarranteed to be NUL-terminated, i.e.
* `!b[appendz(b).i]` will be the case.
*
* @return `i` or -1 if `ENOMEM`
* @see appendz(b).i to get buffer length
*/
ssize_t appendr(char **b, size_t i) {
char *p;
struct appendz z;
assert(b);
z = appendz((p = *b));
z.n = ROUNDUP(i + 1, 8) + W;
if ((p = realloc(p, z.n))) {
z.n = malloc_usable_size(p);
assert(!(z.n & (W - 1)));
*b = p;
} else {
return -1;
}
if (i > z.i) {
memset(p, z.i, i - z.i);
}
z.i = i;
p[z.i] = 0;
if (!IsTiny() && W == 8) {
z.i |= (size_t)APPEND_COOKIE << 48;
}
*(size_t *)(p + z.n - W) = z.i;
return i;
}

View file

@ -20,8 +20,18 @@
#include "libc/str/str.h"
/**
* Appends string to buffer.
* Appends string to buffer, e.g.
*
* char *b = 0;
* appends(&b, "hello");
* free(b);
*
* The resulting buffer is guaranteed to be NUL-terminated, i.e.
* `!b[appendz(b).i]` will be the case.
*
* @return bytes appended (always `strlen(s)`) or -1 if `ENOMEM`
* @see appendz(b).i to get buffer length
*/
int appends(char **b, const char *s) {
ssize_t appends(char **b, const char *s) {
return appendd(b, s, strlen(s));
}

View file

@ -19,15 +19,33 @@
#include "libc/bits/bits.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/stdio/append.internal.h"
#include "libc/stdio/stdio.h"
/**
* Appends string to buffer.
* Appends character or word-encoded string to buffer.
*
* Up to eight characters can be appended. For example:
*
* appendw(&s, 'h'|'i'<<8);
*
* Is equivalent to:
*
* appends(&s, "hi");
*
* The special case:
*
* appendw(&s, 0);
*
* Will append a single NUL character.
*
* The resulting buffer is guaranteed to be NUL-terminated, i.e.
* `!b[appendz(b).i]` will be the case.
*
* @return bytes appended or -1 if `ENOMEM`
*/
int appendw(char **b, uint64_t w) {
ssize_t appendw(char **b, uint64_t w) {
char t[8];
unsigned l;
if (!w) return 0;
unsigned n = 1;
WRITE64LE(t, w);
return appendd(b, t, (bsrl(w) >> 3) + 1);
if (w) n += bsrl(w) >> 3;
return appendd(b, t, n);
}

View file

@ -25,6 +25,9 @@
/**
* Returns size of append buffer.
*
* @return i is number of bytes stored in buffer
* @return n is number of bytes in allocation
*/
struct appendz appendz(char *p) {
struct appendz z;

View file

@ -55,6 +55,10 @@ o/$(MODE)/libc/stdio/fputc.o: \
OVERRIDE_CFLAGS += \
-O3
o//libc/stdio/appendw.o: \
OVERRIDE_CFLAGS += \
-O2
LIBC_STDIO_LIBS = $(foreach x,$(LIBC_STDIO_ARTIFACTS),$($(x)))
LIBC_STDIO_SRCS = $(foreach x,$(LIBC_STDIO_ARTIFACTS),$($(x)_SRCS))
LIBC_STDIO_HDRS = $(foreach x,$(LIBC_STDIO_ARTIFACTS),$($(x)_HDRS))

View file

@ -25,9 +25,9 @@
#define W sizeof(size_t)
/**
* Appends data to buffer.
* Appends formatted string to buffer.
*/
int(vappendf)(char **b, const char *f, va_list v) {
ssize_t(vappendf)(char **b, const char *f, va_list v) {
char *p;
int r, s;
va_list w;