mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
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:
parent
fdc3fa9148
commit
1ff9ab95ac
153 changed files with 2545 additions and 2077 deletions
|
@ -233,71 +233,6 @@ COSMOPOLITAN_C_START_
|
|||
#define EXPECT_LGBL_LT(VAL, GOT) \
|
||||
expectLongDoubleLessThan(VAL, GOT, #VAL " < " #GOT, false)
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § testing library » hardware-accelerated memory safety ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
||||
typedef void (*testfn_t)(void);
|
||||
|
||||
struct TestFixture {
|
||||
const char *group;
|
||||
const char *name;
|
||||
testfn_t fn;
|
||||
};
|
||||
|
||||
struct TestAllocation {
|
||||
void *mapaddr;
|
||||
size_t mapsize;
|
||||
void *useraddr;
|
||||
size_t usersize;
|
||||
};
|
||||
|
||||
struct TestMemoryStack {
|
||||
size_t i;
|
||||
size_t n;
|
||||
struct TestAllocation *p;
|
||||
};
|
||||
|
||||
extern struct TestMemoryStack g_testmem;
|
||||
|
||||
void tfree(void *) paramsnonnull();
|
||||
void *tmalloc(size_t) returnsnonnull returnspointerwithnoaliases nodiscard
|
||||
attributeallocsize((1)) returnsaligned((1));
|
||||
void *tmemalign(size_t,
|
||||
size_t) returnsnonnull returnspointerwithnoaliases nodiscard
|
||||
attributeallocsize((2)) libcesque attributeallocalign((1));
|
||||
char *tstrdup(const char *) returnsnonnull returnspointerwithnoaliases nodiscard
|
||||
returnsaligned((1));
|
||||
void *tunbing(const char16_t *)
|
||||
paramsnonnull() returnsnonnull returnspointerwithnoaliases nodiscard
|
||||
returnsaligned((1));
|
||||
void *tunbinga(size_t, const char16_t *)
|
||||
paramsnonnull() returnsnonnull returnspointerwithnoaliases nodiscard
|
||||
attributeallocalign((1));
|
||||
void testlib_clearxmmregisters(void);
|
||||
|
||||
#define tgc(TMEM) \
|
||||
({ \
|
||||
void *Res; \
|
||||
/* volatile b/c testmem only lifo atm */ \
|
||||
asm volatile("" ::: "memory"); \
|
||||
Res = defer((tfree), (TMEM)); \
|
||||
asm volatile("" ::: "memory"); \
|
||||
Res; \
|
||||
})
|
||||
|
||||
#define tfree(P) \
|
||||
do { \
|
||||
__tfree_check(P); \
|
||||
(tfree)(P); \
|
||||
} while (0)
|
||||
|
||||
#define __tfree_check(P) \
|
||||
ASSERT_BETWEEN(g_testmem.p[g_testmem.i - 1].useraddr, \
|
||||
((char *)g_testmem.p[g_testmem.i - 1].useraddr + \
|
||||
g_testmem.p[g_testmem.i - 1].usersize), \
|
||||
P)
|
||||
|
||||
/*───────────────────────────────────────────────────────────────────────────│─╗
|
||||
│ cosmopolitan § testing library » implementation details ─╬─│┼
|
||||
╚────────────────────────────────────────────────────────────────────────────│*/
|
||||
|
@ -310,6 +245,14 @@ void testlib_clearxmmregisters(void);
|
|||
const char *file; \
|
||||
int line
|
||||
|
||||
typedef void (*testfn_t)(void);
|
||||
|
||||
struct TestFixture {
|
||||
const char *group;
|
||||
const char *name;
|
||||
testfn_t fn;
|
||||
};
|
||||
|
||||
extern char g_fixturename[256];
|
||||
extern bool g_testlib_shoulddebugbreak; /* set by testmain */
|
||||
extern unsigned g_testlib_ran; /* set by wrappers */
|
||||
|
@ -374,6 +317,7 @@ void testlib_formatbinaryasglyphs(const char16_t *, const void *, size_t,
|
|||
char **, char **);
|
||||
bool testlib_almostequallongdouble(long double, long double);
|
||||
void testlib_incrementfailed(void);
|
||||
void testlib_clearxmmregisters(void);
|
||||
|
||||
forceinline void testlib_ontest() {
|
||||
YOINK(__testcase_start);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue