mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-04-23 02:29:10 +00:00
feat(core): check for sys.modules
and main
globals reallocations
Rewrite the static comparison in `utils.unimport_end()` in C. [no changelog]
This commit is contained in:
parent
ec87d2d21d
commit
e6f96974de
core
@ -302,6 +302,27 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_enable_oom_dump_obj,
|
||||
mod_trezorutils_enable_oom_dump);
|
||||
#endif // MICROPY_OOM_CALLBACK
|
||||
|
||||
/// if __debug__:
|
||||
/// def check_reallocs() -> None:
|
||||
/// """
|
||||
/// Assert that `sys.modules` and `main`'s globals are never
|
||||
/// reallocated.
|
||||
/// """
|
||||
STATIC mp_obj_t mod_trezorutils_check_reallocs(void) {
|
||||
mp_obj_dict_t *modules = &MP_STATE_VM(mp_loaded_modules_dict);
|
||||
if (modules->map.alloc > MICROPY_LOADED_MODULES_DICT_SIZE) {
|
||||
mp_raise_msg(&mp_type_AssertionError, "sys.modules dict is reallocated");
|
||||
}
|
||||
mp_obj_t main = mp_obj_dict_get(modules, MP_OBJ_NEW_QSTR(MP_QSTR_main));
|
||||
size_t main_map_alloc = mp_obj_module_get_globals(main)->map.alloc;
|
||||
if (main_map_alloc > MICROPY_MAIN_DICT_SIZE) {
|
||||
mp_raise_msg(&mp_type_AssertionError, "main globals dict is reallocated");
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_trezorutils_check_reallocs_obj,
|
||||
mod_trezorutils_check_reallocs);
|
||||
|
||||
#endif // !PYOPT
|
||||
|
||||
/// def reboot_to_bootloader(
|
||||
@ -520,6 +541,8 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = {
|
||||
{MP_ROM_QSTR(MP_QSTR_enable_oom_dump),
|
||||
MP_ROM_PTR(&mod_trezorutils_enable_oom_dump_obj)},
|
||||
#endif
|
||||
{MP_ROM_QSTR(MP_QSTR_check_reallocs),
|
||||
MP_ROM_PTR(&mod_trezorutils_check_reallocs_obj)},
|
||||
#endif
|
||||
{MP_ROM_QSTR(MP_QSTR_sd_hotswap_enabled),
|
||||
MP_ROM_PTR(&mod_trezorutils_sd_hotswap_enabled_obj)},
|
||||
|
@ -104,6 +104,12 @@ if __debug__:
|
||||
"""
|
||||
Dump GC info in case of an OOM.
|
||||
"""
|
||||
if __debug__:
|
||||
def check_reallocs() -> None:
|
||||
"""
|
||||
Assert that `sys.modules` and `main`'s globals are never
|
||||
reallocated.
|
||||
"""
|
||||
|
||||
|
||||
# upymod/modtrezorutils/modtrezorutils.c
|
||||
|
@ -37,7 +37,7 @@ from trezorutils import ( # noqa: F401
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if __debug__:
|
||||
from trezorutils import LOG_STACK_USAGE
|
||||
from trezorutils import LOG_STACK_USAGE, check_reallocs
|
||||
|
||||
if LOG_STACK_USAGE:
|
||||
from trezorutils import estimate_unused_stack, zero_unused_stack # noqa: F401
|
||||
@ -67,10 +67,9 @@ def unimport_begin() -> set[str]:
|
||||
|
||||
|
||||
def unimport_end(mods: set[str], collect: bool = True) -> None:
|
||||
# static check that the size of sys.modules never grows above value of
|
||||
# MICROPY_LOADED_MODULES_DICT_SIZE, so that the sys.modules dict is never
|
||||
# reallocated at run-time
|
||||
assert len(sys.modules) <= 160, "Please bump preallocated size in mpconfigport.h"
|
||||
if __debug__:
|
||||
# Assert that `sys.modules` and `__main__` dict are never reallocated at run-time
|
||||
check_reallocs()
|
||||
|
||||
for mod in sys.modules: # pylint: disable=consider-using-dict-items
|
||||
if mod not in mods:
|
||||
|
Loading…
Reference in New Issue
Block a user