What changed, and why it matters
This commit adds diagnostic counters that report how the device's memory garbage collector is behaving. It does not change how user funds, keys, or transactions are handled. It is a debugging/metrics feature only, gated so it is active only on non-optimized (debug) builds.
No security action required. Treat as a normal feature/debugging improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change enables MICROPY_PY_GC_STATS on non-PYOPT builds and exposes three new GC statistics (count, used_min, used_max) through the existing trezorutils.get_gc_info() MicroPython module. It also resets those counters when the stats are read, and updates a Python debug-link helper to log the new values. No security-sensitive logic is modified.
Changed components
core/embed/projects/firmware/src/stm32/mpconfigport.hcore/embed/projects/firmware/src/unix/mpconfigport.hcore/embed/upymod/modtrezorutils/modtrezorutils.ccore/mocks/generated/trezorutils.pyipython/src/trezorlib/debuglink.pyInspect captured patch +27 / −2
### core/embed/projects/firmware/src/stm32/mpconfigport.h
@@ -166,6 +166,7 @@
// allocate traceback data only on debug builds
#define MICROPY_PY_SYS_TRACEBACK_DISABLE (PYOPT)
+#define MICROPY_PY_GC_STATS (!PYOPT)
#define MICROPY_PY_TREZORCONFIG (1)
#define MICROPY_PY_TREZORCRYPTO (1)
### core/embed/projects/firmware/src/unix/mpconfigport.h
@@ -181,6 +181,7 @@
// allocate traceback data only on debug builds
#define MICROPY_PY_SYS_TRACEBACK_DISABLE (PYOPT)
+#define MICROPY_PY_GC_STATS (!PYOPT)
// Debugging and interactive functionality.
#define MICROPY_DEBUG_PRINTERS (1)
### core/embed/upymod/modtrezorutils/modtrezorutils.c
@@ -494,18 +494,31 @@ static MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_clear_gc_info_obj,
/// def get_gc_info() -> dict[str, int]:
/// """
/// Get GC heap stats, updated by `update_gc_info`.
+/// `used_{min,max}` are measured after each collection (since
+/// the last call), so they exclude garbage freed by that collection.
/// """
static mp_obj_t mod_trezorutils_get_gc_info() {
- mp_obj_t result = mp_obj_new_dict(4);
+ mp_obj_t result = mp_obj_new_dict(7);
+ mp_obj_dict_store(result, MP_OBJ_NEW_QSTR(MP_QSTR_count),
+ mp_obj_new_int_from_uint(MP_STATE_MEM(gc_count)));
mp_obj_dict_store(result, MP_OBJ_NEW_QSTR(MP_QSTR_total),
mp_obj_new_int_from_uint(current_gc_info.total));
mp_obj_dict_store(result, MP_OBJ_NEW_QSTR(MP_QSTR_used),
mp_obj_new_int_from_uint(current_gc_info.used));
+ mp_obj_dict_store(result, MP_OBJ_NEW_QSTR(MP_QSTR_used_max),
+ mp_obj_new_int_from_uint(MP_STATE_MEM(gc_used_blocks_max) *
+ MICROPY_BYTES_PER_GC_BLOCK));
+ mp_obj_dict_store(result, MP_OBJ_NEW_QSTR(MP_QSTR_used_min),
+ mp_obj_new_int_from_uint(MP_STATE_MEM(gc_used_blocks_min) *
+ MICROPY_BYTES_PER_GC_BLOCK));
mp_obj_dict_store(result, MP_OBJ_NEW_QSTR(MP_QSTR_free),
mp_obj_new_int_from_uint(current_gc_info.free));
mp_obj_dict_store(result, MP_OBJ_NEW_QSTR(MP_QSTR_max_free),
mp_obj_new_int_from_uint(current_gc_info.max_free *
MICROPY_BYTES_PER_GC_BLOCK));
+ MP_STATE_MEM(gc_count) = 0;
+ MP_STATE_MEM(gc_used_blocks_max) = 0;
+ MP_STATE_MEM(gc_used_blocks_min) = 0;
return result;
}
static MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_get_gc_info_obj,
### core/mocks/generated/trezorutils.pyi
@@ -155,6 +155,8 @@ if __debug__:
def get_gc_info() -> dict[str, int]:
"""
Get GC heap stats, updated by `update_gc_info`.
+ `used_{min,max}` are measured after each collection (since
+ the last call), so they exclude garbage freed by that collection.
"""
if __debug__:
def update_gc_info() -> None:
### python/src/trezorlib/debuglink.py
@@ -991,10 +991,18 @@ def check_gc_info(self, fail_on_gc_leak: bool = True) -> None:
)
info = dict(sorted((item.name, item.value) for item in resp.items))
if info["total"]:
+ # older firmware doesn't report GC stats
+ stats = "".join(
+ f" {key}={info[key]}"
+ for key in ("count", "used_min", "used_max")
+ if key in info
+ )
LOG.debug(
- "GC info: free=%.2f%% max_free=%.2f%%",
+ "GC info: free=%.2f%% max_free=%.2f%% total=%d%s",
100 * info["free"] / info["total"],
100 * info["max_free"] / info["total"],
+ info["total"],
+ stats,
)
prev_info = self.prev_gc_infoWhy this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.