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:
@ -46,10 +46,6 @@ o/$(MODE)/test/dsp/core/%.com.dbg: \
|
||||
$(APE)
|
||||
@$(APELINK)
|
||||
|
||||
# $(TEST_DSP_CORE_OBJS): \
|
||||
# OVERRIDE_CFLAGS += \
|
||||
# -fsanitize=address
|
||||
|
||||
.PHONY: o/$(MODE)/test/dsp/core
|
||||
o/$(MODE)/test/dsp/core: \
|
||||
$(TEST_DSP_CORE_BINS) \
|
||||
|
||||
@ -25,6 +25,7 @@
|
||||
#include "libc/math.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
@ -40,9 +41,9 @@ TEST(magikarp, testConvolve) {
|
||||
u"λλ λλ λλ λλ "
|
||||
u"λλ λλ λλ λλ ",
|
||||
cDecimate2xUint8x8(32 * 3,
|
||||
tgc(tunbing(u"λλλλ λλλλ λλλλ λλλλ "
|
||||
u"λλλλ λλλλ λλλλ λλλλ "
|
||||
u"λλλλ λλλλ λλλλ λλλλ ")),
|
||||
gc(xunbing(u"λλλλ λλλλ λλλλ λλλλ "
|
||||
u"λλλλ λλλλ λλλλ λλλλ "
|
||||
u"λλλλ λλλλ λλλλ λλλλ ")),
|
||||
K));
|
||||
}
|
||||
|
||||
@ -53,9 +54,9 @@ TEST(magikarp, testConvolveMin1) {
|
||||
u"λλ λλ λλ λλ "
|
||||
u"λλ λλ λλ λλ ",
|
||||
cDecimate2xUint8x8(32 * 3 - 1,
|
||||
tgc(tunbing(u"λλλλ λλλλ λλλλ λλλλ "
|
||||
u"λλλλ λλλλ λλλλ λλλλ "
|
||||
u"λλλλ λλλλ λλλλ λλλλ ")),
|
||||
gc(xunbing(u"λλλλ λλλλ λλλλ λλλλ "
|
||||
u"λλλλ λλλλ λλλλ λλλλ "
|
||||
u"λλλλ λλλλ λλλλ λλλλ ")),
|
||||
K));
|
||||
}
|
||||
|
||||
@ -71,14 +72,14 @@ TEST(magikarp, testConvolve2) {
|
||||
u"┴├┼╟╔╦═╧╤╙╒╫┘█▌▀"
|
||||
u"ßπστΘδφ∩±≤⌡≈∙√²λ",
|
||||
cDecimate2xUint8x8(256,
|
||||
tgc(tunbing(u" ☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼"
|
||||
u" !“#$%&‘()*+,-./0123456789:;<=>⁇"
|
||||
u"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
|
||||
u"`abcdefghijklmnopqrstuvwxyz{|}~⌂"
|
||||
u"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥€ƒ"
|
||||
u"áíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
|
||||
u"└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀"
|
||||
u"αßΓπΣσμτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■λ")),
|
||||
gc(xunbing(u" ☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼"
|
||||
u" !“#$%&‘()*+,-./0123456789:;<=>⁇"
|
||||
u"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
|
||||
u"`abcdefghijklmnopqrstuvwxyz{|}~⌂"
|
||||
u"ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥€ƒ"
|
||||
u"áíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐"
|
||||
u"└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀"
|
||||
u"αßΓπΣσμτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙×√ⁿ²■λ")),
|
||||
K));
|
||||
}
|
||||
|
||||
@ -147,8 +148,8 @@ noopopppqqqqqqqqqqqqqqqppoooonn",
|
||||
BENCH(magikarp, bench) { /* 30ms */
|
||||
unsigned char kMagkern[16] = {4, 12, 12, 4};
|
||||
signed char kMagikarp[16] = {-1, -3, 3, 17, 17, 3, -3, -1};
|
||||
unsigned char(*Me)[HDY][HDX] = tgc(tmalloc((HDX + 1) * (HDY + 1)));
|
||||
unsigned char(*Mo)[HDY][HDX] = tgc(tmalloc((HDX + 1) * (HDY + 1)));
|
||||
unsigned char(*Me)[HDY][HDX] = gc(malloc((HDX + 1) * (HDY + 1)));
|
||||
unsigned char(*Mo)[HDY][HDX] = gc(malloc((HDX + 1) * (HDY + 1)));
|
||||
if (X86_HAVE(AVX)) {
|
||||
EZBENCH2("Decimate2xUint8x8", donothing,
|
||||
cDecimate2xUint8x8((HDX * HDY - 16 - 8) / 2 / 8 * 8, (void *)Me,
|
||||
|
||||
@ -21,7 +21,9 @@
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/macros.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nexgen32e/x86feature.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
@ -54,12 +56,12 @@ const short kW3[64] forcealign(32) = {
|
||||
1730, 2041, 7707, 5096, 6876, 1324, 1242, 7, 0x7fff,
|
||||
};
|
||||
|
||||
#define TestIt(impl, index, value, n, array) \
|
||||
({ \
|
||||
short *a = memcpy(tgc(tmemalign(32, n * 2)), array, n * 2); \
|
||||
unsigned res = impl(array, n); \
|
||||
ASSERT_EQ(index, res); \
|
||||
ASSERT_EQ(value, a[res]); \
|
||||
#define TestIt(impl, index, value, n, array) \
|
||||
({ \
|
||||
short *a = memcpy(gc(memalign(32, n * 2)), array, n * 2); \
|
||||
unsigned res = impl(array, n); \
|
||||
ASSERT_EQ(index, res); \
|
||||
ASSERT_EQ(value, a[res]); \
|
||||
})
|
||||
|
||||
TEST(windex, testRealWorldPicks) {
|
||||
|
||||
Reference in New Issue
Block a user