gc.c: Avoid valgrind false positives.

When you wish 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 <jepler@gmail.com>
This commit is contained in:
Jeff Epler 2021-08-27 09:02:03 -05:00
parent 7c51cb2307
commit 15133cbf59
2 changed files with 14 additions and 0 deletions

View File

@ -32,6 +32,10 @@
#include "py/gc.h" #include "py/gc.h"
#include "py/runtime.h" #include "py/runtime.h"
#if MICROPY_DEBUG_VALGRIND
#include <valgrind/memcheck.h>
#endif
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
#if MICROPY_DEBUG_VERBOSE // print debugging info #if MICROPY_DEBUG_VERBOSE // print debugging info
@ -349,6 +353,11 @@ void gc_collect_start(void) {
__attribute__((no_sanitize_address)) __attribute__((no_sanitize_address))
#endif #endif
static void *gc_get_ptr(void **ptrs, int i) { 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]; return ptrs[i];
} }

View File

@ -479,6 +479,11 @@
#define MICROPY_DEBUG_VM_STACK_OVERFLOW (0) #define MICROPY_DEBUG_VM_STACK_OVERFLOW (0)
#endif #endif
// Whether to enable extra instrumentation for valgrind
#ifndef MICROPY_DEBUG_VALGRIND
#define MICROPY_DEBUG_VALGRIND (0)
#endif
/*****************************************************************************/ /*****************************************************************************/
/* Optimisations */ /* Optimisations */