AddressSanitizer working, fixed major bug thanks to this in OS_ListDir
This commit is contained in:
@@ -3,7 +3,7 @@ call ../misc/compile_setup.bat
|
|||||||
|
|
||||||
mkdir build
|
mkdir build
|
||||||
cd build
|
cd build
|
||||||
cl -Fe:bld.exe ../code/build_main.cpp -WX -W3 -wd4200 -diagnostics:column -nologo -Zi -D_CRT_SECURE_NO_WARNINGS /MD
|
cl -Fe:bld.exe ../code/build_main.cpp -WX -W3 -wd4200 -diagnostics:column -nologo -Zi -D_CRT_SECURE_NO_WARNINGS /fsanitize=address
|
||||||
cd ..
|
cd ..
|
||||||
build\bld.exe
|
build\bld.exe
|
||||||
|
|
||||||
|
|||||||
2
build.sh
2
build.sh
@@ -1,3 +1,3 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
gcc -o bld code/build_main.cpp -g
|
gcc -o bld code/build_main.cpp -g
|
||||||
./bld
|
./bld cc=clang
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "code/build_lib.cpp"
|
#include "code/build_lib.cpp"
|
||||||
|
|
||||||
int CompileFiles(Strs cc, Strs files);
|
void CompileFiles(Strs cc, Strs files);
|
||||||
|
int ReturnValue = 0;
|
||||||
|
|
||||||
int Main() {
|
int Main() {
|
||||||
Strs cc = CMDLine.get("cc"_s, ON_WINDOWS("cl"_s) ON_MAC("clang"_s) ON_LINUX("gcc"_s));
|
Strs cc = CMDLine.get("cc"_s, ON_WINDOWS("cl"_s) ON_MAC("clang"_s) ON_LINUX("gcc"_s));
|
||||||
@@ -12,10 +13,10 @@ int Main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return ReturnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CompileFiles(Strs cc, Strs files) {
|
void CompileFiles(Strs cc, Strs files) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
Str exe = FilenameWithoutExt(files[0]);
|
Str exe = FilenameWithoutExt(files[0]);
|
||||||
Str filestr = Merge(files);
|
Str filestr = Merge(files);
|
||||||
@@ -28,6 +29,11 @@ int CompileFiles(Strs cc, Strs files) {
|
|||||||
else {
|
else {
|
||||||
result = OS_SystemF("cl -Fe:%Q.exe %Q -Zi -WX -W3 -wd4200 -diagnostics:column -nologo -D_CRT_SECURE_NO_WARNINGS -fsanitize=address -RTC1", exe, filestr);
|
result = OS_SystemF("cl -Fe:%Q.exe %Q -Zi -WX -W3 -wd4200 -diagnostics:column -nologo -D_CRT_SECURE_NO_WARNINGS -fsanitize=address -RTC1", exe, filestr);
|
||||||
}
|
}
|
||||||
if (result == 0) result = OS_SystemF(IF_WINDOWS_ELSE("", "./") "%Q.exe", exe);
|
|
||||||
return result;
|
if (result == 0) {
|
||||||
|
result = OS_SystemF(IF_WINDOWS_ELSE("", "./") "%Q.exe", exe);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ReturnValue = result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
42
code/arena.c
42
code/arena.c
@@ -33,38 +33,11 @@ MA_API void MA_MemoryCopy(void *dst, void *src, size_t size) {
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Marks memory region [addr, addr+size) as unaddressable.
|
#if defined(__SANITIZE_ADDRESS__)
|
||||||
// This memory must be previously allocated by the user program. Accessing
|
#include <sanitizer/asan_interface.h>
|
||||||
// addresses in this region from instrumented code is forbidden until
|
#else
|
||||||
// this region is unpoisoned. This function is not guaranteed to poison
|
#define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
|
||||||
// the whole region - it may poison only subregion of [addr, addr+size) due
|
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size))
|
||||||
// to ASan alignment restrictions.
|
|
||||||
// Method is NOT thread-safe in the sense that no two threads can
|
|
||||||
// (un)poison memory in the same memory region simultaneously.
|
|
||||||
void __asan_poison_memory_region(void const volatile *addr, size_t size);
|
|
||||||
// Marks memory region [addr, addr+size) as addressable.
|
|
||||||
// This memory must be previously allocated by the user program. Accessing
|
|
||||||
// addresses in this region is allowed until this region is poisoned again.
|
|
||||||
// This function may unpoison a superregion of [addr, addr+size) due to
|
|
||||||
// ASan alignment restrictions.
|
|
||||||
// Method is NOT thread-safe in the sense that no two threads can
|
|
||||||
// (un)poison memory in the same memory region simultaneously.
|
|
||||||
void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
|
|
||||||
|
|
||||||
// User code should use macros instead of functions.
|
|
||||||
#if defined(__clang__)
|
|
||||||
#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)
|
|
||||||
#define ASAN_POISON_MEMORY_REGION(addr, size) \
|
|
||||||
__asan_poison_memory_region((addr), (size))
|
|
||||||
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
|
|
||||||
__asan_unpoison_memory_region((addr), (size))
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#ifndef ASAN_POISON_MEMORY_REGION
|
|
||||||
#define ASAN_POISON_MEMORY_REGION(addr, size) \
|
|
||||||
((void)(addr), (void)(size))
|
|
||||||
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
|
|
||||||
((void)(addr), (void)(size))
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MA_API size_t MA_GetAlignOffset(size_t size, size_t align) {
|
MA_API size_t MA_GetAlignOffset(size_t size, size_t align) {
|
||||||
@@ -107,7 +80,7 @@ MA_API void MA_PopToPos(MA_Arena *arena, size_t pos) {
|
|||||||
pos = MA_CLAMP(pos, arena->base_len, arena->len);
|
pos = MA_CLAMP(pos, arena->base_len, arena->len);
|
||||||
size_t size = arena->len - pos;
|
size_t size = arena->len - pos;
|
||||||
arena->len = pos;
|
arena->len = pos;
|
||||||
ASAN_POISON_MEMORY_REGION(arena->memory.data + pos, size);
|
ASAN_POISON_MEMORY_REGION(arena->memory.data + arena->len, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
MA_API void MA_PopSize(MA_Arena *arena, size_t size) {
|
MA_API void MA_PopSize(MA_Arena *arena, size_t size) {
|
||||||
@@ -158,7 +131,7 @@ MA_API void *MA_PushSizeNonZeroed(MA_Arena *a, size_t size) {
|
|||||||
uint8_t *result = a->memory.data + aligned_len;
|
uint8_t *result = a->memory.data + aligned_len;
|
||||||
a->len += size_with_alignment;
|
a->len += size_with_alignment;
|
||||||
MA_ASSERT(a->len <= a->memory.commit);
|
MA_ASSERT(a->len <= a->memory.commit);
|
||||||
ASAN_UNPOISON_MEMORY_REGION(result, a->len);
|
ASAN_UNPOISON_MEMORY_REGION(result, size);
|
||||||
return (void *)result;
|
return (void *)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,6 +178,7 @@ MA_API void MA_InitFromBuffer(MA_Arena *arena, void *buffer, size_t size) {
|
|||||||
arena->memory.commit = size;
|
arena->memory.commit = size;
|
||||||
arena->memory.reserve = size;
|
arena->memory.reserve = size;
|
||||||
arena->alignment = MA_DEFAULT_ALIGNMENT;
|
arena->alignment = MA_DEFAULT_ALIGNMENT;
|
||||||
|
ASAN_POISON_MEMORY_REGION(arena->memory.data, arena->memory.reserve);
|
||||||
}
|
}
|
||||||
|
|
||||||
MA_API MA_Arena MA_MakeFromBuffer(void *buffer, size_t size) {
|
MA_API MA_Arena MA_MakeFromBuffer(void *buffer, size_t size) {
|
||||||
|
|||||||
@@ -171,11 +171,11 @@ OS_API S8_List OS_ListDir(MA_Arena *arena, S8_String path, unsigned flags) {
|
|||||||
S8_String filename = UTF_CreateStringFromWidechar(scratch.arena, ffd.cFileName, S8_WideLength(ffd.cFileName));
|
S8_String filename = UTF_CreateStringFromWidechar(scratch.arena, ffd.cFileName, S8_WideLength(ffd.cFileName));
|
||||||
S8_String rel_abs_path = S8_Format(scratch.arena, "%Q/%Q%Q", it->string, filename, dir ? S8_Lit("/") : S8_Lit(""));
|
S8_String rel_abs_path = S8_Format(scratch.arena, "%Q/%Q%Q", it->string, filename, dir ? S8_Lit("/") : S8_Lit(""));
|
||||||
if (flags & OS_RELATIVE_PATHS) {
|
if (flags & OS_RELATIVE_PATHS) {
|
||||||
S8_AddNode(arena, &result, rel_abs_path);
|
S8_Add(arena, &result, rel_abs_path);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
S8_String abs_path = OS_GetAbsolutePath(arena, rel_abs_path);
|
S8_String abs_path = OS_GetAbsolutePath(arena, rel_abs_path);
|
||||||
S8_AddNode(arena, &result, abs_path);
|
S8_Add(arena, &result, abs_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dir && flags & OS_RECURSIVE) {
|
if (dir && flags & OS_RECURSIVE) {
|
||||||
|
|||||||
12
code/io.c
12
code/io.c
@@ -58,15 +58,15 @@ IO_API bool IO__FatalErrorf(const char *file, int line, const char *msg, ...) {
|
|||||||
char buff2[2048];
|
char buff2[2048];
|
||||||
char *result = buff2;
|
char *result = buff2;
|
||||||
char *b = 0;
|
char *b = 0;
|
||||||
int size = IO_SNPRINTF(buff2, sizeof(buff2), "%s(%d): error: %s \n", file, line, user_message);
|
int size2 = IO_SNPRINTF(buff2, sizeof(buff2), "%s(%d): error: %s \n", file, line, user_message);
|
||||||
if (size >= sizeof(buff2)) {
|
if (size2 >= sizeof(buff2)) {
|
||||||
size += 4;
|
size2 += 4;
|
||||||
b = (char *)IO_ALLOCATE(size);
|
b = (char *)IO_ALLOCATE(size2);
|
||||||
size = IO_SNPRINTF(b, size, "%s(%d): error: %s \n", file, line, user_message);
|
size2 = IO_SNPRINTF(b, size2, "%s(%d): error: %s \n", file, line, user_message);
|
||||||
result = b;
|
result = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = IO_OutputError(result, size);
|
ret = IO_OutputError(result, size2);
|
||||||
if (ret == IO_ErrorResult_Exit) {
|
if (ret == IO_ErrorResult_Exit) {
|
||||||
IO_Exit(1);
|
IO_Exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,6 +158,10 @@ PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC):
|
|||||||
#if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__
|
#if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__
|
||||||
#define STBSP__ASAN __attribute__((__no_sanitize_address__))
|
#define STBSP__ASAN __attribute__((__no_sanitize_address__))
|
||||||
#endif
|
#endif
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#ifdef __SANITIZE_ADDRESS__
|
||||||
|
#define STBSP__ASAN __declspec(no_sanitize_address)
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef STBSP__ASAN
|
#ifndef STBSP__ASAN
|
||||||
|
|||||||
@@ -486,6 +486,13 @@ S8_API S8_Node *S8_AddNode(S8_Allocator allocator, S8_List *list, S8_String stri
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
S8_API S8_Node *S8_Add(S8_Allocator allocator, S8_List *list, S8_String string) {
|
||||||
|
S8_String copy = S8_Copy(allocator, string);
|
||||||
|
S8_Node *node = S8_CreateNode(allocator, copy);
|
||||||
|
S8_AddExistingNode(list, node);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
S8_API S8_String S8_AddF(S8_Allocator allocator, S8_List *list, const char *str, ...) {
|
S8_API S8_String S8_AddF(S8_Allocator allocator, S8_List *list, const char *str, ...) {
|
||||||
S8_FORMAT(allocator, str, result);
|
S8_FORMAT(allocator, str, result);
|
||||||
S8_AddNode(allocator, list, result);
|
S8_AddNode(allocator, list, result);
|
||||||
|
|||||||
@@ -114,4 +114,5 @@ S8_API S8_List S8_MakeList(S8_Allocator allocator, S8_String a);
|
|||||||
S8_API S8_List S8_CopyList(S8_Allocator allocator, S8_List a);
|
S8_API S8_List S8_CopyList(S8_Allocator allocator, S8_List a);
|
||||||
S8_API S8_List S8_ConcatLists(S8_Allocator allocator, S8_List a, S8_List b);
|
S8_API S8_List S8_ConcatLists(S8_Allocator allocator, S8_List a, S8_List b);
|
||||||
S8_API S8_Node *S8_AddNode(S8_Allocator allocator, S8_List *list, S8_String string);
|
S8_API S8_Node *S8_AddNode(S8_Allocator allocator, S8_List *list, S8_String string);
|
||||||
|
S8_API S8_Node *S8_Add(S8_Allocator allocator, S8_List *list, S8_String string);
|
||||||
S8_API S8_String S8_AddF(S8_Allocator allocator, S8_List *list, const char *str, ...);
|
S8_API S8_String S8_AddF(S8_Allocator allocator, S8_List *list, const char *str, ...);
|
||||||
|
|||||||
@@ -18,6 +18,15 @@ void TestBootstrapArenaClear() {
|
|||||||
for (int i = 0; i < 1024; i += 1) {
|
for (int i = 0; i < 1024; i += 1) {
|
||||||
IO_Assert(vals[i] == i);
|
IO_Assert(vals[i] == i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t len = arena->len;
|
||||||
|
MA_PopSize(arena, 512);
|
||||||
|
IO_Assert(len == arena->len + 512);
|
||||||
|
((char *)arena->memory.data)[arena->len - 1] = 0;
|
||||||
|
|
||||||
|
MA_PopToPos(arena, 512);
|
||||||
|
IO_Assert(arena->len == 512);
|
||||||
|
((char *)arena->memory.data)[arena->len - 1] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestScratch() {
|
void TestScratch() {
|
||||||
|
|||||||
Reference in New Issue
Block a user