From fbfd9d4b61ee225084c8e0fafaa5736e09e136af Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Wed, 22 Jun 2016 13:53:22 +0200 Subject: [PATCH] split debug (=dangerous) things from trezor.utils to trezor.debug --- extmod/modtrezordebug/modtrezordebug.c | 71 ++++++++++++++++++++++++++ extmod/modtrezorutils/modtrezorutils.c | 71 -------------------------- src/trezor/debug.py | 9 ++++ src/trezor/utils.py | 6 --- vendor/micropython | 2 +- 5 files changed, 81 insertions(+), 78 deletions(-) create mode 100644 extmod/modtrezordebug/modtrezordebug.c delete mode 100644 extmod/modtrezorutils/modtrezorutils.c create mode 100644 src/trezor/debug.py diff --git a/extmod/modtrezordebug/modtrezordebug.c b/extmod/modtrezordebug/modtrezordebug.c new file mode 100644 index 0000000000..b492d3a985 --- /dev/null +++ b/extmod/modtrezordebug/modtrezordebug.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) Pavol Rusnak, SatoshiLabs + * + * Licensed under TREZOR License + * see LICENSE file for details + */ + +#include +#include +#include + +#include "py/nlr.h" +#include "py/runtime.h" +#include "py/binary.h" +#include "py/objstr.h" + +#if MICROPY_PY_TREZORDEBUG + +typedef struct _mp_obj_Debug_t { + mp_obj_base_t base; +} mp_obj_Debug_t; + +STATIC mp_obj_t mod_TrezorDebug_Debug_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 0, false); + mp_obj_Debug_t *o = m_new_obj(mp_obj_Debug_t); + o->base.type = type; + return MP_OBJ_FROM_PTR(o); +} + +/// def trezor.utils.memaccess(address: int, length: int) -> bytes +/// ''' +/// Creates a bytes object that can be used to access certain memory location. +/// ''' +STATIC mp_obj_t mod_TrezorDebug_Debug_memaccess(mp_obj_t self, mp_obj_t address, mp_obj_t length) { + uint32_t addr = mp_obj_get_int(address); + uint32_t len = mp_obj_get_int(length); + mp_obj_str_t *o = m_new_obj(mp_obj_str_t); + o->base.type = &mp_type_bytes; + o->len = len; + o->hash = 0; + o->data = (byte *)(uintptr_t)addr; + return MP_OBJ_FROM_PTR(o); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorDebug_Debug_memaccess_obj, mod_TrezorDebug_Debug_memaccess); + +STATIC const mp_rom_map_elem_t mod_TrezorDebug_Debug_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_memaccess), MP_ROM_PTR(&mod_TrezorDebug_Debug_memaccess_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(mod_TrezorDebug_Debug_locals_dict, mod_TrezorDebug_Debug_locals_dict_table); + +STATIC const mp_obj_type_t mod_TrezorDebug_Debug_type = { + { &mp_type_type }, + .name = MP_QSTR_Debug, + .make_new = mod_TrezorDebug_Debug_make_new, + .locals_dict = (void*)&mod_TrezorDebug_Debug_locals_dict, +}; + +STATIC const mp_rom_map_elem_t mp_module_TrezorDebug_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorDebug) }, + { MP_ROM_QSTR(MP_QSTR_Debug), MP_ROM_PTR(&mod_TrezorDebug_Debug_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_TrezorDebug_globals, mp_module_TrezorDebug_globals_table); + +const mp_obj_module_t mp_module_TrezorDebug = { + .base = { &mp_type_module }, + .name = MP_QSTR_TrezorDebug, + .globals = (mp_obj_dict_t*)&mp_module_TrezorDebug_globals, +}; + +#endif // MICROPY_PY_TREZORDEBUG diff --git a/extmod/modtrezorutils/modtrezorutils.c b/extmod/modtrezorutils/modtrezorutils.c deleted file mode 100644 index 8c9f81e919..0000000000 --- a/extmod/modtrezorutils/modtrezorutils.c +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) Pavol Rusnak, SatoshiLabs - * - * Licensed under TREZOR License - * see LICENSE file for details - */ - -#include -#include -#include - -#include "py/nlr.h" -#include "py/runtime.h" -#include "py/binary.h" -#include "py/objstr.h" - -#if MICROPY_PY_TREZORUTILS - -typedef struct _mp_obj_Utils_t { - mp_obj_base_t base; -} mp_obj_Utils_t; - -STATIC mp_obj_t mod_TrezorUtils_Utils_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { - mp_arg_check_num(n_args, n_kw, 0, 0, false); - mp_obj_Utils_t *o = m_new_obj(mp_obj_Utils_t); - o->base.type = type; - return MP_OBJ_FROM_PTR(o); -} - -/// def trezor.utils.memaccess(address: int, length: int) -> bytes -/// ''' -/// Creates a bytes object that can be used to access certain memory location. -/// ''' -STATIC mp_obj_t mod_TrezorUtils_Utils_memaccess(mp_obj_t self, mp_obj_t address, mp_obj_t length) { - uint32_t addr = mp_obj_get_int(address); - uint32_t len = mp_obj_get_int(length); - mp_obj_str_t *o = m_new_obj(mp_obj_str_t); - o->base.type = &mp_type_bytes; - o->len = len; - o->hash = 0; - o->data = (byte *)(uintptr_t)addr; - return MP_OBJ_FROM_PTR(o); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_TrezorUtils_Utils_memaccess_obj, mod_TrezorUtils_Utils_memaccess); - -STATIC const mp_rom_map_elem_t mod_TrezorUtils_Utils_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_memaccess), MP_ROM_PTR(&mod_TrezorUtils_Utils_memaccess_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(mod_TrezorUtils_Utils_locals_dict, mod_TrezorUtils_Utils_locals_dict_table); - -STATIC const mp_obj_type_t mod_TrezorUtils_Utils_type = { - { &mp_type_type }, - .name = MP_QSTR_Utils, - .make_new = mod_TrezorUtils_Utils_make_new, - .locals_dict = (void*)&mod_TrezorUtils_Utils_locals_dict, -}; - -STATIC const mp_rom_map_elem_t mp_module_TrezorUtils_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_TrezorUtils) }, - { MP_ROM_QSTR(MP_QSTR_Utils), MP_ROM_PTR(&mod_TrezorUtils_Utils_type) }, -}; - -STATIC MP_DEFINE_CONST_DICT(mp_module_TrezorUtils_globals, mp_module_TrezorUtils_globals_table); - -const mp_obj_module_t mp_module_TrezorUtils = { - .base = { &mp_type_module }, - .name = MP_QSTR_TrezorUtils, - .globals = (mp_obj_dict_t*)&mp_module_TrezorUtils_globals, -}; - -#endif // MICROPY_PY_TREZORUTILS diff --git a/src/trezor/debug.py b/src/trezor/debug.py new file mode 100644 index 0000000000..783e321047 --- /dev/null +++ b/src/trezor/debug.py @@ -0,0 +1,9 @@ +if not __debug__: + raise ImportError('This module can be loaded only in DEBUG mode') + +from TrezorDebug import Debug + +_utils = Debug() + +def memaccess(address, length): + return _utils.memaccess(address, length) diff --git a/src/trezor/utils.py b/src/trezor/utils.py index 42cf6bae5f..804e50eeef 100644 --- a/src/trezor/utils.py +++ b/src/trezor/utils.py @@ -1,13 +1,7 @@ import sys -from TrezorUtils import Utils - -_utils = Utils() type_gen = type((lambda: (yield))()) -def memaccess(address, length): - return _utils.memaccess(address, length) - def unimport_func(func): def inner(*args, **kwargs): mods = set(sys.modules) diff --git a/vendor/micropython b/vendor/micropython index e1a7215201..861b0136cd 160000 --- a/vendor/micropython +++ b/vendor/micropython @@ -1 +1 @@ -Subproject commit e1a7215201e2cc567a28b3ee12dddc9486747be4 +Subproject commit 861b0136cd68b3fe4431010588cabee96b016d41