Make C memory safe like Rust

This change enables Address Sanitizer systemically w/ `make MODE=dbg`.
Our version of Rust's `unsafe` keyword is named `noasan` which is used
for two functions that do aligned memory chunking, like `strcpy.c` and
we need to fix the tiny DEFLATE code, but that's it everything else is
fabulous you can have all the fischer price security blankets you need

Best of all is we're now able to use the ASAN data in Blinkenlights to
colorize the memory dumps. See the screenshot below of a test program:

  https://justine.lol/blinkenlights/asan.png

Which is operating on float arrays stored on the stack, with red areas
indicating poisoned memory, and the green areas indicate valid memory.
This commit is contained in:
Justine Tunney 2021-02-01 03:33:13 -08:00
parent fdc3fa9148
commit 1ff9ab95ac
153 changed files with 2545 additions and 2077 deletions

View file

@ -34,6 +34,7 @@
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/itoa.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/pcmpeqb.h"
#include "libc/intrin/pmovmskb.h"
#include "libc/log/check.h"
@ -441,6 +442,25 @@ static int VirtualBing(int64_t v) {
return rc;
}
static int VirtualShadow(int64_t v) {
int rc;
char *p;
jmp_buf busted;
if (0x7fff8000 <= v && v < 0x100080000000) return -2;
onbusted = busted;
if ((p = FindReal(m, (v >> 3) + 0x7fff8000))) {
if (!setjmp(busted)) {
rc = p[0] & 0xff;
} else {
rc = -1;
}
} else {
rc = -1;
}
onbusted = NULL;
return rc;
}
static void ScrollOp(struct Panel *p, long op) {
long n;
opline = op;
@ -1260,7 +1280,8 @@ static void DrawMemoryZoomed(struct Panel *p, struct MemoryView *view,
static void DrawMemoryUnzoomed(struct Panel *p, struct MemoryView *view,
long histart, long hiend) {
long i, j, k, c;
long i, j, k;
int c, s, x, sc;
bool high, changed;
high = false;
for (i = 0; i < p->bottom - p->top; ++i) {
@ -1268,6 +1289,35 @@ static void DrawMemoryUnzoomed(struct Panel *p, struct MemoryView *view,
for (j = 0; j < DUMPWIDTH; ++j) {
k = (view->start + i) * DUMPWIDTH + j;
c = VirtualBing(k);
s = VirtualShadow(k);
if (s != -1) {
if (s == -2) {
/* grey for shadow memory */
x = 235;
} else {
sc = (signed char)s;
if (sc > 7) {
x = 129; /* PURPLE: shadow corruption */
} else if (sc == kAsanHeapFree) {
x = 17; /* NAVYBLUE: heap freed */
} else if (sc == kAsanRelocated) {
x = 18; /* DARKBLUE: heap relocated */
} else if (sc == kAsanHeapUnderrun || sc == kAsanAllocaUnderrun) {
x = 53; /* RED+PURPLETINGE: heap underrun */
} else if (sc == kAsanHeapOverrun || sc == kAsanAllocaOverrun) {
x = 52; /* RED: heap overrun */
} else if (sc < 0) {
x = 52; /* RED: uncategorized invalid */
} else if (sc > 0 && (k & 7) >= sc) {
x = 54; /* REDPURP: invalid address (skew) */
} else if (!sc || (sc > 0 && (k & 7) < sc)) {
x = 22; /* GREEN: valid address */
} else {
abort();
}
}
AppendFmt(&p->lines[i], "\e[38;5;253;48;5;%dm", x);
}
changed = histart <= k && k < hiend;
if (changed && !high) {
high = true;
@ -1277,6 +1327,9 @@ static void DrawMemoryUnzoomed(struct Panel *p, struct MemoryView *view,
high = false;
}
AppendWide(&p->lines[i], c);
if (s != -1) {
AppendStr(&p->lines[i], "\e[39;49m");
}
}
if (high) {
AppendStr(&p->lines[i], "\e[27m");

View file

@ -671,7 +671,7 @@ void CleanupTerminal(void) {
}
void StartInteractive(void) {
if (!interactive && !isterminalinarticulate() && isatty(fileno(stdin)) &&
if (!interactive && !IsTerminalInarticulate() && isatty(fileno(stdin)) &&
isatty(fileno(stdout)) && cancolor()) {
interactive = true;
}

View file

@ -68,6 +68,10 @@ o/tiny/tool/build/emubin/mdatest.bin.dbg: \
o/tiny/tool/build/emubin/mdatest.real.o
@$(ELFLINK) -z max-page-size=0x10 -T tool/build/emucrt/real.lds
$(TOOL_BUILD_EMUBIN_OBJS): \
OVERRIDE_CFLAGS += \
$(NO_MAGIC)
.PHONY: o/$(MODE)/tool/build/emubin
o/$(MODE)/tool/build/emubin: \
$(TOOL_BUILD_EMUBIN_BINS) \

View file

@ -61,12 +61,6 @@ $(TOOL_BUILD_LIB_A).pkg: \
$(TOOL_BUILD_LIB_A_OBJS) \
$(foreach x,$(TOOL_BUILD_LIB_A_DIRECTDEPS),$($(x)_A).pkg)
# ifeq (,$(MODE))
# $(TOOL_BUILD_LIB_A_OBJS): \
# OVERRIDE_CFLAGS += \
# -fsanitize=address
# endif
o/$(MODE)/tool/build/lib/ssefloat.o: \
TARGET_ARCH += \
-msse3

View file

@ -449,6 +449,7 @@ static void OpCmpxchg16b(struct Machine *m, uint32_t rde) {
static void OpRdrand(struct Machine *m, uint32_t rde) {
WriteRegister(rde, RegRexbRm(m, rde), rand64());
m->flags = SetFlag(m->flags, FLAGS_CF, true);
}
static void OpRdseed(struct Machine *m, uint32_t rde) {

View file

@ -169,6 +169,7 @@
"nodebuginfo"
"frownedupon"
"wontreturn"
"noasan"
"initarray"
"mayalias"
"noinstrument"

View file

@ -152,6 +152,7 @@
"__PG__"
"__MFENTRY__"
"__MNO_VZEROUPPER__"
"__FSANITIZE_ADDRESS__"
"__FSANITIZE_UNDEFINED__"
"__MNOP_MCOUNT__"
"__MRECORD_MCOUNT__"))

View file

@ -440,8 +440,8 @@
;; -ffast-math -funsafe-math-optimizations -fsched2-use-superblocks -fjump-tables
(cond ((not (eq 0 (logand 8 arg)))
(cosmo--assembly (setq arg (logand (lognot 8)))
"SILENT=0 COPTS='-Os'"))
(t (cosmo--assembly arg "SILENT=0 COPTS='-Os' TARGET_ARCH='-mdispatch-scheduler' CPPFLAGS='-DSTACK_FRAME_UNLIMITED'"))))
"SILENT=0 OVERRIDE_COPTS='-fverbose-asm -fsanitize=address'"))
(t (cosmo--assembly arg "SILENT=0 OVERRIDE_COPTS='-fverbose-asm -fsanitize=address' CPPFLAGS='-DSTACK_FRAME_UNLIMITED'"))))
(defun cosmo-assembly-native (arg)
(interactive "P")

View file

@ -27,7 +27,6 @@ TOOL_NET_DIRECTDEPS = \
LIBC_FMT \
LIBC_INTRIN \
LIBC_LOG \
LIBC_LOG_ASAN \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_RAND \
@ -85,12 +84,6 @@ o/$(MODE)/tool/net/greenbean.com.dbg: \
$(APE)
@$(APELINK)
# ifeq (,$(MODE))
# $(TOOL_NET_OBJS): \
# OVERRIDE_CFLAGS += \
# -fsanitize=address
# endif
.PHONY: o/$(MODE)/tool/net
o/$(MODE)/tool/net: \
$(TOOL_NET_BINS) \

View file

@ -89,12 +89,6 @@ $(TOOL_VIZ_LIB_A).pkg: \
$(TOOL_VIZ_LIB_A_OBJS) \
$(foreach x,$(TOOL_VIZ_LIB_A_DIRECTDEPS),$($(x)_A).pkg)
# ifeq (,$(MODE))
# $(TOOL_VIZ_LIB_A_OBJS): \
# OVERRIDE_CFLAGS += \
# -fsanitize=address
# endif
$(TOOL_VIZ_LIB_A_OBJS): tool/viz/lib/vizlib.mk
TOOL_VIZ_LIB_LIBS = $(foreach x,$(TOOL_VIZ_LIB_ARTIFACTS),$($(x)))

View file

@ -1467,7 +1467,7 @@ int main(int argc, char *argv[]) {
sigaddset(&wut, SIGCHLD);
sigaddset(&wut, SIGPIPE);
sigprocmask(SIG_SETMASK, &wut, NULL);
if (!NoDebug()) showcrashreports();
showcrashreports();
fullclear_ = true;
GetOpts(argc, argv);
if (!tuned_) PickDefaults();

View file

@ -84,12 +84,6 @@ $(TOOL_VIZ_OBJS): \
$(BUILD_FILES) \
tool/viz/viz.mk
# ifeq (,$(MODE))
# $(TOOL_VIZ_OBJS): \
# OVERRIDE_CFLAGS += \
# -fsanitize=address
# endif
.PHONY: o/$(MODE)/tool/viz
o/$(MODE)/tool/viz: \
o/$(MODE)/tool/viz/lib \

View file

@ -0,0 +1,77 @@
/*-*- 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/math.h"
#include "libc/stdio/stdio.h"
#define N 8
#define SQR(X) ((X) * (X))
static const uint8_t kXtermCube[6] = {0, 0137, 0207, 0257, 0327, 0377};
static int rgb2xterm256(int r, int g, int b) {
int cerr, gerr, ir, ig, ib, gray, grai, cr, cg, cb, gv;
gray = round(r * .299 + g * .587 + b * .114);
grai = gray > 238 ? 23 : (gray - 3) / 10;
ir = r < 48 ? 0 : r < 115 ? 1 : (r - 35) / 40;
ig = g < 48 ? 0 : g < 115 ? 1 : (g - 35) / 40;
ib = b < 48 ? 0 : b < 115 ? 1 : (b - 35) / 40;
cr = kXtermCube[ir];
cg = kXtermCube[ig];
cb = kXtermCube[ib];
gv = 8 + 10 * grai;
cerr = SQR(cr - r) + SQR(cg - g) + SQR(cb - b);
gerr = SQR(gv - r) + SQR(gv - g) + SQR(gv - b);
if (cerr <= gerr) {
return 16 + 36 * ir + 6 * ig + ib;
} else {
return 232 + grai;
}
}
int main(int argc, char *argv[]) {
double d;
int i, j, x;
int r, g, b;
double G[N][3];
double rgb[2][3] = {
{1, 0, 0},
{0, 1, 0},
};
for (i = 0; i < N; ++i) {
for (j = 0; j < 3; ++j) {
d = (rgb[1][j] - rgb[0][j]) / (N - 1);
G[i][j] = rgb[0][j] + d * i;
}
}
for (i = 0; i < N; ++i) {
r = round(G[i][0] * 255);
g = round(G[i][1] * 255);
b = round(G[i][2] * 255);
x = rgb2xterm256(r, g, b);
printf("\e[38;5;232;48;5;%dmabcdefg \e[0m %3d "
"\e[38;5;232;48;2;%d;%d;%dmabcdgefg \e[0m "
"%3d %3d %3d %f %f %f\n",
x, x, r, g, b, r, g, b, G[i][0], G[i][1], G[i][2]);
}
return 0;
}