From 84071590b3d76a647fe9cd9055a089605fedc219 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 27 Aug 2021 09:02:03 -0500 Subject: [PATCH] py/gc: Avoid valgrind false positives. When you want to use the valgrind memory analysis tool on MicroPython, you can arrange to define MICROPY_DEBUG_VALGRIND to enable use of special valgrind macros. For now, this only fixes `gc_get_ptr` so that it never emits the diagnostic "Conditional jump or move depends on uninitialised value(s)". Signed-off-by: Jeff Epler --- py/gc.c | 9 +++++++++ py/mpconfig.h | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/py/gc.c b/py/gc.c index 120213783b..7ed60fe2cc 100644 --- a/py/gc.c +++ b/py/gc.c @@ -32,6 +32,10 @@ #include "py/gc.h" #include "py/runtime.h" +#if MICROPY_DEBUG_VALGRIND +#include +#endif + #if MICROPY_ENABLE_GC #if MICROPY_DEBUG_VERBOSE // print debugging info @@ -449,6 +453,11 @@ void gc_collect_start(void) { __attribute__((no_sanitize_address)) #endif static void *gc_get_ptr(void **ptrs, int i) { + #if MICROPY_DEBUG_VALGRIND + if (!VALGRIND_CHECK_MEM_IS_ADDRESSABLE(&ptrs[i], sizeof(*ptrs))) { + return NULL; + } + #endif return ptrs[i]; } diff --git a/py/mpconfig.h b/py/mpconfig.h index 765a8f45ce..564580c02d 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -510,6 +510,11 @@ #define MICROPY_DEBUG_VM_STACK_OVERFLOW (0) #endif +// Whether to enable extra instrumentation for valgrind +#ifndef MICROPY_DEBUG_VALGRIND +#define MICROPY_DEBUG_VALGRIND (0) +#endif + /*****************************************************************************/ /* Optimisations */