Make more fixes and improvements

- Remove PAGESIZE constant
- Fix realloc() documentation
- Fix ttyname_r() error reporting
- Make forking more reliable on Windows
- Make execvp() a few microseconds faster
- Make system() a few microseconds faster
- Tighten up the socket-related magic numbers
- Loosen restrictions on mmap() offset alignment
- Improve GetProgramExecutableName() with getenv("_")
- Use mkstemp() as basis for mktemp(), tmpfile(), tmpfd()
- Fix flakes in pthread_cancel_test, unix_test, fork_test
- Fix recently introduced futex stack overflow regression
- Let sockets be passed as stdio to subprocesses on Windows
- Improve security of bind() on Windows w/ SO_EXCLUSIVEADDRUSE
This commit is contained in:
Justine Tunney 2023-07-29 18:44:15 -07:00
parent 140a8a52e5
commit 18bb5888e1
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
311 changed files with 1239 additions and 2622 deletions

View file

@ -196,10 +196,13 @@ struct itimerval timer;
struct timespec signalled;
sigset_t mask;
char buf[4096];
sigset_t savemask;
char buf[PAGESIZE];
char tmpout[PATH_MAX];
char *g_tmpout;
const char *g_tmpout_original;
const char *const kSafeEnv[] = {
"ADDR2LINE", // needed by GetAddr2linePath
"HOME", // needed by ~/.runit.psk
@ -454,7 +457,13 @@ char *StripPrefix(char *s, char *p) {
}
}
void AddArg(char *s) {
void AddArg(char *actual) {
const char *s;
if (actual == g_tmpout) {
s = g_tmpout_original;
} else {
s = actual;
}
if (args.n) {
appendw(&command, ' ');
}
@ -487,7 +496,7 @@ void AddArg(char *s) {
appendw(&shortened, ' ');
appends(&shortened, s);
}
AddStr(&args, s);
AddStr(&args, actual);
}
static int GetBaseCpuFreqMhz(void) {
@ -810,6 +819,7 @@ char *MakeTmpOut(const char *path) {
int c;
char *p = tmpout;
char *e = tmpout + sizeof(tmpout) - 1;
g_tmpout_original = path;
p = stpcpy(p, kTmpPath);
while ((c = *path++)) {
if (c == '/') c = '_';
@ -820,6 +830,7 @@ char *MakeTmpOut(const char *path) {
*p++ = c;
}
*p = 0;
g_tmpout = tmpout;
return tmpout;
}

View file

@ -16,13 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "tool/build/lib/demangle.h"
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/iovec.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "tool/build/lib/demangle.h"
struct CxxFilt {
int pid;
@ -83,7 +83,7 @@ char *DemangleCxxFilt(char *p, size_t pn, const char *s, size_t sn) {
ssize_t rc;
size_t got;
struct iovec iov[2];
static char buf[PAGESIZE];
static char buf[4096];
if (!g_cxxfilt.pid) SpawnCxxFilt();
if (g_cxxfilt.pid == -1) return NULL;
buf[0] = '\n';

View file

@ -105,9 +105,9 @@
bool use_ftrace;
bool use_strace;
char *g_exepath;
unsigned char g_buf[4096];
volatile bool g_interrupted;
struct sockaddr_in g_servaddr;
unsigned char g_buf[PAGESIZE];
bool g_daemonize, g_sendready;
int g_timeout, g_bogusfd, g_servfd, g_clifd, g_exefd;

View file

@ -1770,7 +1770,6 @@ Keywords={
"FLT_MIN",
"FLT_MAX",
"__SAUCE__",
"PAGESIZE",
"FRAMESIZE",
"ARG_MAX",
"PATH_MAX",

View file

@ -176,7 +176,6 @@
(defconst cosmo-cpp-constants-cosmopolitan
'("__SAUCE__"
"PAGESIZE"
"APE_STACKSIZE"
"FRAMESIZE"
"ARG_MAX"

View file

@ -2134,6 +2134,10 @@ static void FreeStrings(struct Strings *l) {
l->n = 0;
}
static unsigned long roundup2pow(unsigned long x) {
return x > 1 ? 2ul << _bsrl(x - 1) : x ? 1 : 0;
}
static void IndexAssets(void) {
uint64_t cf;
struct Asset *p;
@ -2145,7 +2149,7 @@ static void IndexAssets(void) {
CHECK(READ32LE(zcdir) == kZipCdir64HdrMagic ||
READ32LE(zcdir) == kZipCdirHdrMagic);
n = GetZipCdirRecords(zcdir);
m = _roundup2pow(MAX(1, n) * HASH_LOAD_FACTOR);
m = roundup2pow(MAX(1, n) * HASH_LOAD_FACTOR);
p = xcalloc(m, sizeof(struct Asset));
for (cf = GetZipCdirOffset(zcdir); n--; cf += ZIP_CFILE_HDRSIZE(zmap + cf)) {
CHECK_EQ(kZipCfileHdrMagic, ZIP_CFILE_MAGIC(zmap + cf));
@ -6675,9 +6679,10 @@ static int MemoryMonitor(void *arg, int tid) {
addr = (char *)((int64_t)((uint64_t)mi[i].x << 32) >> 16);
color = 0;
appendf(&b, "\e[0m%lx", addr);
pages = (mi[i].size + PAGESIZE - 1) / PAGESIZE;
int pagesz = getauxval(AT_PAGESZ);
pages = (mi[i].size + pagesz - 1) / pagesz;
for (j = 0; j < pages; ++j) {
rc = mincore(addr + j * PAGESIZE, PAGESIZE, &rez);
rc = mincore(addr + j * pagesz, pagesz, &rez);
if (!rc) {
if (rez & 1) {
if (mi[i].flags & MAP_SHARED) {

View file

@ -35,6 +35,10 @@
#define W 64
#define C 253
static unsigned long roundup2pow(unsigned long x) {
return x > 1 ? 2ul << _bsrl(x - 1) : x ? 1 : 0;
}
void bf(int fd) {
ssize_t rc;
uint64_t w;
@ -44,7 +48,7 @@ void bf(int fd) {
char ibuf[W], obuf[12 + 1 + W * 6 + 1 + W * (2 + 11) + 11 + 1];
o = 0;
if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode) && st.st_size) {
bits = _bsr(_roundup2pow(st.st_size));
bits = _bsr(roundup2pow(st.st_size));
bits = ROUNDUP(bits, 4);
} else {
bits = 48;

View file

@ -18,13 +18,12 @@
*/
#include "dsp/tty/quant.h"
#include "libc/intrin/hilbert.h"
#include "libc/intrin/morton.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/mem/mem.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "tool/viz/lib/graphic.h"
#include "tool/viz/lib/knobs.h"

View file

@ -27,6 +27,7 @@
#include "dsp/scale/scale.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/sigset.h"
#include "libc/intrin/bsr.h"
#include "libc/intrin/pmulhrsw.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
@ -80,6 +81,14 @@ struct YCbCr {
} luma, chroma;
};
static unsigned long roundup2pow(unsigned long x) {
return x > 1 ? 2ul << _bsrl(x - 1) : x ? 1 : 0;
}
static unsigned long rounddown2pow(unsigned long x) {
return x ? 1ul << _bsrl(x) : 0;
}
/**
* Computes magnums for YCbCr decoding.
*
@ -95,8 +104,8 @@ void YCbCrComputeCoefficients(int swing, double gamma,
double x;
double f1[6][3];
long longs[6][6];
long bitlimit = _roundup2pow(swing);
long wordoffset = _rounddown2pow((bitlimit - swing) / 2);
long bitlimit = roundup2pow(swing);
long wordoffset = rounddown2pow((bitlimit - swing) / 2);
long chromaswing = swing + 2 * (bitlimit / 2. - swing / 2. - wordoffset);
long lumamin = wordoffset;
long lumamax = wordoffset + swing;

View file

@ -258,6 +258,12 @@ static char16_t statusline16[256];
RES = (B11 | r0) & r1 & ~r2; \
} while (0)
static void SwapBoards(void) {
uint64_t *t = board;
board = board2;
board2 = t;
}
static void Step(void) {
long y, x, yn, xn;
yn = byn >> 3;
@ -275,7 +281,7 @@ static void Step(void) {
board[(y + 1 < yn ? y + 1 : 0) * xn + (x + 1 < xn ? x + 1 : 0)]);
}
}
xchg(&board, &board2);
SwapBoards();
++generation;
}
@ -593,7 +599,7 @@ static int LoadFile(const char *path) {
if ((c = ReadChar(f)) == -1) goto ReadError;
}
if (yn > byn || xn > bxn) goto ReadError;
xchg(&board, &board2);
SwapBoards();
bzero(board, (byn * bxn) >> 3);
yo = byn / 2 - yn / 2;
xo = bxn / 2 - xn / 2;
@ -640,7 +646,7 @@ static int LoadFile(const char *path) {
return 0;
ReadError:
fclose(f);
xchg(&board, &board2);
SwapBoards();
return -1;
}

View file

@ -31,8 +31,8 @@
#include "libc/fmt/itoa.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/bsf.h"
#include "libc/intrin/bsr.h"
#include "libc/intrin/hilbert.h"
#include "libc/intrin/morton.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
@ -188,6 +188,10 @@ static void LeaveScreen(void) {
Write("\e[H\e[J");
}
static unsigned long rounddown2pow(unsigned long x) {
return x ? 1ul << _bsrl(x) : 0;
}
static void GetTtySize(void) {
struct winsize wsize;
wsize.ws_row = tyn + 1;
@ -195,8 +199,8 @@ static void GetTtySize(void) {
tcgetwinsize(out, &wsize);
tyn = MAX(2, wsize.ws_row) - 1;
txn = MAX(17, wsize.ws_col) - 16;
tyn = _rounddown2pow(tyn);
txn = _rounddown2pow(txn);
tyn = rounddown2pow(tyn);
txn = rounddown2pow(txn);
tyn = MIN(tyn, txn);
}
@ -278,11 +282,30 @@ static void SetupCanvas(void) {
}
displaysize = ROUNDUP(ROUNDUP((tyn * txn) << zoom, 16), 1ul << zoom);
canvassize = ROUNDUP(displaysize, FRAMESIZE);
buffersize = ROUNDUP(tyn * txn * 16 + PAGESIZE, FRAMESIZE);
buffersize = ROUNDUP(tyn * txn * 16 + 4096, FRAMESIZE);
canvas = Allocate(canvassize);
buffer = Allocate(buffersize);
}
/**
* Interleaves bits.
* @see https://en.wikipedia.org/wiki/Z-order_curve
* @see unmorton()
*/
static unsigned long morton(unsigned long y, unsigned long x) {
x = (x | x << 020) & 0x0000FFFF0000FFFF;
x = (x | x << 010) & 0x00FF00FF00FF00FF;
x = (x | x << 004) & 0x0F0F0F0F0F0F0F0F;
x = (x | x << 002) & 0x3333333333333333;
x = (x | x << 001) & 0x5555555555555555;
y = (y | y << 020) & 0x0000FFFF0000FFFF;
y = (y | y << 010) & 0x00FF00FF00FF00FF;
y = (y | y << 004) & 0x0F0F0F0F0F0F0F0F;
y = (y | y << 002) & 0x3333333333333333;
y = (y | y << 001) & 0x5555555555555555;
return x | y << 1;
}
static long IndexSquare(long y, long x) {
switch (order) {
case LINEAR:

View file

@ -371,7 +371,7 @@ static bool CloseSpeaker(void) {
}
static void ResizeVtFrame(struct VtFrame *f, size_t yn, size_t xn) {
BALLOC(&f->b, PAGESIZE, 64 + yn * (xn * 32 + 8), __FUNCTION__);
BALLOC(&f->b, 4096, 64 + yn * (xn * 32 + 8), __FUNCTION__);
f->i = f->n = 0;
}
@ -837,7 +837,8 @@ static void OnVideo(plm_t *mpeg, plm_frame_t *pf, void *user) {
} else {
TranscodeVideo(pf);
if (!f1_->n) {
xchg(&f1_, &f2_);
struct VtFrame *t = f1_;
f1_ = f2_, f2_ = t;
f1_start_ = decode_start_;
} else {
f2_start_ = decode_start_;
@ -872,7 +873,7 @@ static void OpenVideo(void) {
static ssize_t WriteVideoCall(void) {
size_t amt;
ssize_t rc;
amt = min(PAGESIZE * 4, f1_->n - f1_->i);
amt = min(4096 * 4, f1_->n - f1_->i);
if ((rc = write(outfd_, f1_->bytes + f1_->i, amt)) != -1) {
if ((f1_->i += rc) == f1_->n) {
if (plm_get_audio_enabled(plm_)) {
@ -883,7 +884,8 @@ static ssize_t WriteVideoCall(void) {
}
f1_start_ = f2_start_;
f1_->i = f1_->n = 0;
xchg(&f1_, &f2_);
struct VtFrame *t = f1_;
f1_ = f2_, f2_ = t;
RecordFactThatFrameWasFullyRendered();
}
}