From 15133cbf59d11da039d8df1eb8c4d3ce40ad851a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 27 Aug 2021 09:02:03 -0500 Subject: [PATCH 1/2] 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 --- py/gc.c | 9 +++++++++ py/mpconfig.h | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/py/gc.c b/py/gc.c index 8284c435ba94c..4d48a02a148b9 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 @@ -349,6 +353,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 d40637dfaf6b4..f679f1e33f147 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -479,6 +479,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 */ From 5a162eb168b8518e20c04f12b5a3a8977d9d0020 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 27 Aug 2021 09:04:32 -0500 Subject: [PATCH 2/2] gc.c: Ensure a gap of one byte before the finaliser table. .. or, for !MICROPY_ENABLE_FINALISER, before the first block of the pool. Closes: adafruit/circuitpython#5021 Closes: micropython/micropython#7116 Signed-off-by: Jeff Epler --- py/gc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/py/gc.c b/py/gc.c index 4d48a02a148b9..35bdf21fb2666 100644 --- a/py/gc.c +++ b/py/gc.c @@ -122,7 +122,7 @@ void gc_init(void *start, void *end) { // => T = A * (1 + BLOCKS_PER_ATB / BLOCKS_PER_FTB + BLOCKS_PER_ATB * BYTES_PER_BLOCK) size_t total_byte_len = (byte *)end - (byte *)start; #if MICROPY_ENABLE_FINALISER - MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len * MP_BITS_PER_BYTE / (MP_BITS_PER_BYTE + MP_BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + MP_BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK); + MP_STATE_MEM(gc_alloc_table_byte_len) = (total_byte_len - 1) * MP_BITS_PER_BYTE / (MP_BITS_PER_BYTE + MP_BITS_PER_BYTE * BLOCKS_PER_ATB / BLOCKS_PER_FTB + MP_BITS_PER_BYTE * BLOCKS_PER_ATB * BYTES_PER_BLOCK); #else MP_STATE_MEM(gc_alloc_table_byte_len) = total_byte_len / (1 + MP_BITS_PER_BYTE / 2 * BYTES_PER_BLOCK); #endif @@ -131,7 +131,7 @@ void gc_init(void *start, void *end) { #if MICROPY_ENABLE_FINALISER size_t gc_finaliser_table_byte_len = (MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB + BLOCKS_PER_FTB - 1) / BLOCKS_PER_FTB; - MP_STATE_MEM(gc_finaliser_table_start) = MP_STATE_MEM(gc_alloc_table_start) + MP_STATE_MEM(gc_alloc_table_byte_len); + MP_STATE_MEM(gc_finaliser_table_start) = MP_STATE_MEM(gc_alloc_table_start) + MP_STATE_MEM(gc_alloc_table_byte_len) + 1; #endif size_t gc_pool_block_len = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy