Skip to content

Commit 8e39ed4

Browse files
jeplerdpgeorge
authored andcommitted
py/gc: 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 <[email protected]>
1 parent 2283b6d commit 8e39ed4

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

py/gc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "py/gc.h"
3333
#include "py/runtime.h"
3434

35+
#if MICROPY_DEBUG_VALGRIND
36+
#include <valgrind/memcheck.h>
37+
#endif
38+
3539
#if MICROPY_ENABLE_GC
3640

3741
#if MICROPY_DEBUG_VERBOSE // print debugging info
@@ -449,6 +453,11 @@ void gc_collect_start(void) {
449453
__attribute__((no_sanitize_address))
450454
#endif
451455
static void *gc_get_ptr(void **ptrs, int i) {
456+
#if MICROPY_DEBUG_VALGRIND
457+
if (!VALGRIND_CHECK_MEM_IS_ADDRESSABLE(&ptrs[i], sizeof(*ptrs))) {
458+
return NULL;
459+
}
460+
#endif
452461
return ptrs[i];
453462
}
454463

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@
510510
#define MICROPY_DEBUG_VM_STACK_OVERFLOW (0)
511511
#endif
512512

513+
// Whether to enable extra instrumentation for valgrind
514+
#ifndef MICROPY_DEBUG_VALGRIND
515+
#define MICROPY_DEBUG_VALGRIND (0)
516+
#endif
517+
513518
/*****************************************************************************/
514519
/* Optimisations */
515520

0 commit comments

Comments
 (0)