diff --git a/build.bat b/build.bat index edca8de..b7cec37 100644 --- a/build.bat +++ b/build.bat @@ -3,7 +3,7 @@ call ../misc/compile_setup.bat mkdir 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 .. build\bld.exe diff --git a/build.sh b/build.sh index 649d62c..a9f57a4 100644 --- a/build.sh +++ b/build.sh @@ -1,3 +1,3 @@ #!/usr/bin/env bash -gcc -o bld code/build_main.cpp -g -./bld +gcc -o bld code/build_main.cpp -g +./bld cc=clang diff --git a/build_file.cpp b/build_file.cpp index 0b908ea..e53b810 100644 --- a/build_file.cpp +++ b/build_file.cpp @@ -1,6 +1,7 @@ #include "code/build_lib.cpp" -int CompileFiles(Strs cc, Strs files); +void CompileFiles(Strs cc, Strs files); +int ReturnValue = 0; int Main() { 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; Str exe = FilenameWithoutExt(files[0]); Str filestr = Merge(files); @@ -28,6 +29,11 @@ int CompileFiles(Strs cc, Strs files) { 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); } - 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; + } } diff --git a/code/arena.c b/code/arena.c index 13d2b86..c787712 100644 --- a/code/arena.c +++ b/code/arena.c @@ -33,38 +33,11 @@ MA_API void MA_MemoryCopy(void *dst, void *src, size_t size) { #endif #endif -// Marks memory region [addr, addr+size) as unaddressable. -// This memory must be previously allocated by the user program. Accessing -// addresses in this region from instrumented code is forbidden until -// this region is unpoisoned. This function is not guaranteed to poison -// the whole region - it may poison only subregion 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_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)) +#if defined(__SANITIZE_ADDRESS__) + #include +#else + #define ASAN_POISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) + #define ASAN_UNPOISON_MEMORY_REGION(addr, size) ((void)(addr), (void)(size)) #endif 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); size_t size = 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) { @@ -158,7 +131,7 @@ MA_API void *MA_PushSizeNonZeroed(MA_Arena *a, size_t size) { uint8_t *result = a->memory.data + aligned_len; a->len += size_with_alignment; MA_ASSERT(a->len <= a->memory.commit); - ASAN_UNPOISON_MEMORY_REGION(result, a->len); + ASAN_UNPOISON_MEMORY_REGION(result, size); 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.reserve = size; 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) { diff --git a/code/filesystem.c b/code/filesystem.c index 4e9b454..a226560 100644 --- a/code/filesystem.c +++ b/code/filesystem.c @@ -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 rel_abs_path = S8_Format(scratch.arena, "%Q/%Q%Q", it->string, filename, dir ? S8_Lit("/") : S8_Lit("")); if (flags & OS_RELATIVE_PATHS) { - S8_AddNode(arena, &result, rel_abs_path); + S8_Add(arena, &result, rel_abs_path); } else { 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) { diff --git a/code/io.c b/code/io.c index ff8e4f5..eddbb64 100644 --- a/code/io.c +++ b/code/io.c @@ -58,15 +58,15 @@ IO_API bool IO__FatalErrorf(const char *file, int line, const char *msg, ...) { char buff2[2048]; char *result = buff2; char *b = 0; - int size = IO_SNPRINTF(buff2, sizeof(buff2), "%s(%d): error: %s \n", file, line, user_message); - if (size >= sizeof(buff2)) { - size += 4; - b = (char *)IO_ALLOCATE(size); - size = IO_SNPRINTF(b, size, "%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 (size2 >= sizeof(buff2)) { + size2 += 4; + b = (char *)IO_ALLOCATE(size2); + size2 = IO_SNPRINTF(b, size2, "%s(%d): error: %s \n", file, line, user_message); result = b; } - ret = IO_OutputError(result, size); + ret = IO_OutputError(result, size2); if (ret == IO_ErrorResult_Exit) { IO_Exit(1); } diff --git a/code/stb_sprintf.h b/code/stb_sprintf.h index 5b626c9..5b91110 100644 --- a/code/stb_sprintf.h +++ b/code/stb_sprintf.h @@ -158,6 +158,10 @@ PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC): #if defined(__SANITIZE_ADDRESS__) && __SANITIZE_ADDRESS__ #define STBSP__ASAN __attribute__((__no_sanitize_address__)) #endif + #elif defined(_MSC_VER) + #ifdef __SANITIZE_ADDRESS__ + #define STBSP__ASAN __declspec(no_sanitize_address) + #endif #endif #ifndef STBSP__ASAN diff --git a/code/string.c b/code/string.c index 015696b..fa921f4 100644 --- a/code/string.c +++ b/code/string.c @@ -486,6 +486,13 @@ S8_API S8_Node *S8_AddNode(S8_Allocator allocator, S8_List *list, S8_String stri 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_FORMAT(allocator, str, result); S8_AddNode(allocator, list, result); diff --git a/code/string.h b/code/string.h index ca3dd3c..1c75c78 100644 --- a/code/string.h +++ b/code/string.h @@ -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_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_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, ...); diff --git a/test/test_arena.cpp b/test/test_arena.cpp index 8f658f0..ad3c3ff 100644 --- a/test/test_arena.cpp +++ b/test/test_arena.cpp @@ -18,6 +18,15 @@ void TestBootstrapArenaClear() { for (int i = 0; i < 1024; i += 1) { 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() {