From a94bc82a3c9054aa8c8ff773da79ebc537f2fee9 Mon Sep 17 00:00:00 2001 From: derselbst Date: Sat, 5 Oct 2019 16:43:53 +0200 Subject: [PATCH] Initialize allocated memory to garbage for debug builds. --- src/utils/fluid_sys.c | 23 +++++++++++++++++++++++ src/utils/fluid_sys.h | 2 ++ src/utils/fluidsynth_priv.h | 9 +++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/utils/fluid_sys.c b/src/utils/fluid_sys.c index 01ac1a2a..ec92d270 100644 --- a/src/utils/fluid_sys.c +++ b/src/utils/fluid_sys.c @@ -195,6 +195,29 @@ fluid_log(int level, const char *fmt, ...) return FLUID_FAILED; } +void* fluid_alloc(size_t len) +{ + void* ptr = malloc(len); + +#if defined(DEBUG) && !defined(_MSC_VER) + // garbage initialize allocated memory for debug builds to ease reproducing + // bugs like 44453ff23281b3318abbe432fda90888c373022b . + // + // MSVC++ already garbage initializes allocated memory by itself (debug-heap). + // + // 0xCC because + // * it makes pointers reliably crash when dereferencing them, + // * floating points are still some valid but insanely huge negative number, and + // * if for whatever reason this allocated memory is executed, it'll trigger + // INT3 (...at least on x86) + if(ptr != NULL) + { + memset(ptr, 0xCC, len); + } +#endif + return ptr; +} + /** * Convenience wrapper for free() that satisfies at least C90 requirements. * Especially useful when using fluidsynth with programming languages that do not provide malloc() and free(). diff --git a/src/utils/fluid_sys.h b/src/utils/fluid_sys.h index 65c088ca..6912f58b 100644 --- a/src/utils/fluid_sys.h +++ b/src/utils/fluid_sys.h @@ -739,4 +739,6 @@ static FLUID_INLINE void *fluid_align_ptr(const void *ptr, unsigned int alignmen #define FLUID_DEFAULT_ALIGNMENT (64U) +void* fluid_alloc(size_t len); + #endif /* _FLUID_SYS_H */ diff --git a/src/utils/fluidsynth_priv.h b/src/utils/fluidsynth_priv.h index 2f5cc41f..32e166d9 100644 --- a/src/utils/fluidsynth_priv.h +++ b/src/utils/fluidsynth_priv.h @@ -126,12 +126,13 @@ typedef void (*fluid_rvoice_function_t)(void *obj, const fluid_rvoice_param_t pa * SYSTEM INTERFACE */ -#define FLUID_MALLOC(_n) malloc(_n) +#define FLUID_MALLOC(_n) fluid_alloc(_n) #define FLUID_REALLOC(_p,_n) realloc(_p,_n) -#define FLUID_NEW(_t) (_t*)malloc(sizeof(_t)) -#define FLUID_ARRAY_ALIGNED(_t,_n,_a) (_t*)malloc((_n)*sizeof(_t) + ((unsigned int)_a - 1u)) -#define FLUID_ARRAY(_t,_n) FLUID_ARRAY_ALIGNED(_t,_n,1u) #define FLUID_FREE(_p) fluid_free(_p) + +#define FLUID_NEW(_t) (_t*)FLUID_MALLOC(sizeof(_t)) +#define FLUID_ARRAY_ALIGNED(_t,_n,_a) (_t*)FLUID_MALLOC((_n)*sizeof(_t) + ((unsigned int)_a - 1u)) +#define FLUID_ARRAY(_t,_n) FLUID_ARRAY_ALIGNED(_t,_n,1u) #define FLUID_FOPEN(_f,_m) fopen(_f,_m) #define FLUID_FCLOSE(_f) fclose(_f) #define FLUID_FREAD(_p,_s,_n,_f) fread(_p,_s,_n,_f)