From 50a9252df39143da2317d7055201fc0b493fbb58 Mon Sep 17 00:00:00 2001 From: Cameron Reikes Date: Sat, 6 May 2023 15:00:23 -0700 Subject: [PATCH] Get md_stb_sprintf running under ubsan, with zig compiler --- .gitignore | 3 +++ codegen.c | 14 +++++++++++--- run_codegen.bat | 2 +- thirdparty/md_stb_sprintf.h | 13 ++++++++++--- thirdparty/stb_ds.h | 6 +++--- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 2d45464..6d67000 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Visual studio stuff .vs/ +# ctags +tags + # Clion stuff .idea/ cmake-build-debug/ diff --git a/codegen.c b/codegen.c index 55dd387..e8f92b0 100644 --- a/codegen.c +++ b/codegen.c @@ -6,9 +6,17 @@ #pragma warning(disable : 4996) // nonsense about fopen being insecure +#if defined(__clang__) +#define no_ubsan __attribute__((no_sanitize("undefined"))) +#else +#define no_ubsan +#endif + #pragma warning(push) #pragma warning(disable : 4244) // loss of data warning #pragma warning(disable : 4101) // unreferenced local variable +#define STBSP_ADD_TO_FUNCTIONS no_ubsan +#define MD_FUNCTION no_ubsan #include "md.h" #include "md.c" #pragma warning(pop) @@ -51,7 +59,7 @@ MD_String8 asset_file_path(MD_String8 filename) { } char *nullterm(MD_String8 s) { - char *to_return = malloc(s.size + 1); + char *to_return = MD_ArenaPush(cg_arena, s.size + 1); memcpy(to_return, s.str, s.size); to_return[s.size] = '\0'; return to_return; @@ -154,7 +162,7 @@ int main(int argc, char **argv) MD_String8 filepath = ChildValue(node, MD_S8Lit("filepath")); filepath = asset_file_path(filepath); assert(filepath.str != 0); // MD_S8Fmt(cg_arena, "No filepath specified for sound '%.*s'", MD_S8VArg(node->string))); - FILE *asset_file = fopen(filepath.str, "r"); + FILE *asset_file = fopen(nullterm(filepath), "r"); assert(asset_file); // MD_S8Fmt(cg_arena, "Could not open filepath %.*s for asset '%.*s'", MD_S8VArg(filepath), MD_S8VArg(node->string))); fclose(asset_file); @@ -167,7 +175,7 @@ int main(int argc, char **argv) MD_String8 filepath = ChildValue(node, MD_S8Lit("filepath")); filepath = asset_file_path(filepath); assert(filepath.str != 0); // , MD_S8Fmt(cg_arena, "No filepath specified for image '%.*s'", MD_S8VArg(node->string))); - FILE *asset_file = fopen(filepath.str, "r"); + FILE *asset_file = fopen(nullterm(filepath), "r"); assert(asset_file); // , MD_S8Fmt(cg_arena, "Could not open filepath %.*s for asset '%.*s'", MD_S8VArg(filepath), MD_S8VArg(node->string))); fclose(asset_file); diff --git a/run_codegen.bat b/run_codegen.bat index f7672d6..211bf6d 100644 --- a/run_codegen.bat +++ b/run_codegen.bat @@ -36,7 +36,7 @@ thirdparty\sokol-shdc.exe --input quad.glsl --output gen\quad-sapp.glsl.h --slan @REM metadesk codegen @REM cl /Ithirdparty /W3 /Zi /WX codegen.c || goto :error -zig -Ithirdparty -gfull -gcodeview codegen.c -o codegen.exe || goto error +zig cc -Ithirdparty -gfull -gcodeview codegen.c -o codegen.exe || goto error codegen || goto :error @REM cl /Ithirdparty /Igen /W3 /Zi /WX maketraining.c || goto :error diff --git a/thirdparty/md_stb_sprintf.h b/thirdparty/md_stb_sprintf.h index c5b5fcc..1567671 100644 --- a/thirdparty/md_stb_sprintf.h +++ b/thirdparty/md_stb_sprintf.h @@ -165,19 +165,26 @@ PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC): #define STBSP__ASAN #endif + #ifdef STB_SPRINTF_STATIC #define STBSP__PUBLICDEC static -#define STBSP__PUBLICDEF static STBSP__ASAN +#define STBSP__NEW_PUBLICDEF static STBSP__ASAN #else #ifdef __cplusplus #define STBSP__PUBLICDEC extern "C" -#define STBSP__PUBLICDEF extern "C" STBSP__ASAN +#define STBSP__NEW_PUBLICDEF extern "C" STBSP__ASAN #else #define STBSP__PUBLICDEC extern -#define STBSP__PUBLICDEF STBSP__ASAN +#define STBSP__NEW_PUBLICDEF STBSP__ASAN +#endif #endif + +#ifndef STBSP_ADD_TO_FUNCTIONS +#define STBSP_ADD_TO_FUNCTIONS #endif +#define STBSP__PUBLICDEF STBSP_ADD_TO_FUNCTIONS STBSP__NEW_PUBLICDEF + #include // for va_list() #include // size_t, ptrdiff_t diff --git a/thirdparty/stb_ds.h b/thirdparty/stb_ds.h index 974d080..0776002 100644 --- a/thirdparty/stb_ds.h +++ b/thirdparty/stb_ds.h @@ -1114,12 +1114,12 @@ static size_t stbds_siphash_bytes(void *p, size_t len, size_t seed) } #if defined(__clang__) -#define no_ubsan_overflow __attribute__((no_sanitize("undefined"))) +#define no_ubsan __attribute__((no_sanitize("undefined"))) #else -#define no_ubsan_overflow +#define no_ubsan #endif -size_t no_ubsan_overflow stbds_hash_bytes(void *p, size_t len, size_t seed) +size_t no_ubsan stbds_hash_bytes(void *p, size_t len, size_t seed) { #ifdef STBDS_SIPHASH_2_4 return stbds_siphash_bytes(p,len,seed);