Skip to content

Commit 15133cb

Browse files
committed
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>
1 parent 7c51cb2 commit 15133cb

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

py/gc.c

+9
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
@@ -349,6 +353,11 @@ void gc_collect_start(void) {
349353
__attribute__((no_sanitize_address))
350354
#endif
351355
static void *gc_get_ptr(void **ptrs, int i) {
356+
#if MICROPY_DEBUG_VALGRIND
357+
if (!VALGRIND_CHECK_MEM_IS_ADDRESSABLE(&ptrs[i], sizeof(*ptrs))) {
358+
return NULL;
359+
}
360+
#endif
352361
return ptrs[i];
353362
}
354363

py/mpconfig.h

+5
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,11 @@
479479
#define MICROPY_DEBUG_VM_STACK_OVERFLOW (0)
480480
#endif
481481

482+
// Whether to enable extra instrumentation for valgrind
483+
#ifndef MICROPY_DEBUG_VALGRIND
484+
#define MICROPY_DEBUG_VALGRIND (0)
485+
#endif
486+
482487
/*****************************************************************************/
483488
/* Optimisations */
484489

0 commit comments

Comments
 (0)