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:
16
third_party/chibicc/as.c
vendored
16
third_party/chibicc/as.c
vendored
@@ -522,6 +522,7 @@ static int AppendSection(struct As *a, int name, int flags, int type) {
|
||||
int i;
|
||||
APPEND(a->sections);
|
||||
i = a->sections.n - 1;
|
||||
CHECK_LT(i, SHN_LORESERVE);
|
||||
a->sections.p[i].name = name;
|
||||
a->sections.p[i].flags = flags;
|
||||
a->sections.p[i].type = type;
|
||||
@@ -1805,6 +1806,15 @@ static void OnSize(struct As *a, struct Slice s) {
|
||||
a->symbols.p[i].size = GetInt(a);
|
||||
}
|
||||
|
||||
static void OnComm(struct As *a, struct Slice s) {
|
||||
int i;
|
||||
i = GetSymbol(a, a->things.p[a->i++].i);
|
||||
ConsumeComma(a);
|
||||
a->symbols.p[i].size = GetInt(a);
|
||||
a->symbols.p[i].type = STT_COMMON;
|
||||
a->symbols.p[i].section = SHN_COMMON;
|
||||
}
|
||||
|
||||
static void OpVisibility(struct As *a, int visibility) {
|
||||
int i;
|
||||
for (;;) {
|
||||
@@ -3118,6 +3128,7 @@ static const struct Directive8 {
|
||||
{".balign", OnAlign}, //
|
||||
{".bss", OnBss}, //
|
||||
{".byte", OnByte}, //
|
||||
{".comm", OnComm}, //
|
||||
{".data", OnData}, //
|
||||
{".double", OnDouble}, //
|
||||
{".err", OnErr}, //
|
||||
@@ -3280,8 +3291,8 @@ static const struct Directive8 {
|
||||
{"fildll", OnFildll}, //
|
||||
{"fildq", OnFildq}, //
|
||||
{"filds", OnFilds}, //
|
||||
{"fistpq", OnFistpq}, //
|
||||
{"fistpll", OnFistpq}, //
|
||||
{"fistpq", OnFistpq}, //
|
||||
{"fisttpll", OnFisttpq}, //
|
||||
{"fisttpq", OnFisttpq}, //
|
||||
{"fisttps", OnFisttps}, //
|
||||
@@ -3906,6 +3917,9 @@ static void Objectify(struct As *a, int path) {
|
||||
a->symbols.p[i].ref = elfwriter_appendsym(
|
||||
elf, p, ELF64_ST_INFO(a->symbols.p[i].stb, a->symbols.p[i].type),
|
||||
a->symbols.p[i].stv, a->symbols.p[i].offset, a->symbols.p[i].size);
|
||||
if (a->symbols.p[i].section >= SHN_LORESERVE) {
|
||||
elfwriter_setsection(elf, a->symbols.p[i].ref, a->symbols.p[i].section);
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
for (i = 0; i < a->sections.n; ++i) {
|
||||
|
||||
7
third_party/chibicc/test/test.mk
vendored
7
third_party/chibicc/test/test.mk
vendored
@@ -21,8 +21,13 @@ THIRD_PARTY_CHIBICC_TEST_HDRS = $(filter %.h,$(THIRD_PARTY_CHIBICC_TEST_FILES))
|
||||
THIRD_PARTY_CHIBICC_TEST_TESTS = $(THIRD_PARTY_CHIBICC_TEST_COMS:%=%.ok)
|
||||
|
||||
THIRD_PARTY_CHIBICC_TEST_COMS = \
|
||||
$(THIRD_PARTY_CHIBICC_TEST_SRCS_TEST:%.c=o/$(MODE)/%.com) \
|
||||
$(THIRD_PARTY_CHIBICC_TEST_SRCS_TEST:%.c=o/$(MODE)/%.com)
|
||||
|
||||
# TODO(jart): make chibicc compiled chibicc work with asan runtime
|
||||
ifneq ($(MODE),dbg)
|
||||
THIRD_PARTY_CHIBICC_TEST_COMS += \
|
||||
$(THIRD_PARTY_CHIBICC_TEST_SRCS_TEST:%.c=o/$(MODE)/%2.com)
|
||||
endif
|
||||
|
||||
THIRD_PARTY_CHIBICC_TEST_OBJS = \
|
||||
$(THIRD_PARTY_CHIBICC_TEST_SRCS:%.c=o/$(MODE)/%.chibicc.o)
|
||||
|
||||
Reference in New Issue
Block a user