Fix several places where an exception could be chained wrongly

If an exception's chain or context can refer to a pointer from a different
VM, a crash would typically result.

This couldn't turn up on UNIX testing because the VM is never torn
down and rebuilt like it is on hardware.

Because in the 'static' case the GeneratorObject is now fully initialized
whenever it's raised, the initialization can be dropped, which reduces
the flash size slightly.
Closes: #7565
This commit is contained in:
Jeff Epler 2023-02-10 08:44:58 -06:00
parent fc919d24e1
commit 0d957fe15c
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
4 changed files with 18 additions and 10 deletions

View File

@ -155,6 +155,12 @@ void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kin
mp_obj_tuple_print(print, MP_OBJ_FROM_PTR(o->args), kind);
}
void mp_obj_exception_initialize0(mp_obj_exception_t *o_exc, const mp_obj_type_t *type) {
o_exc->base.type = type;
o_exc->args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;
mp_obj_exception_clear_traceback(o_exc);
}
mp_obj_t mp_obj_exception_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, MP_OBJ_FUN_ARGS_MAX, false);
@ -583,6 +589,12 @@ void mp_obj_exception_clear_traceback(mp_obj_t self_in) {
// just set the traceback to the empty traceback object
// we don't want to call any memory management functions here
self->traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
#if MICROPY_CPYTHON_EXCEPTION_CHAIN
self->cause = 0;
self->context = 0;
self->suppress_context = false;
self->marked = false;
#endif
}
void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block) {

View File

@ -43,6 +43,7 @@ typedef struct _mp_obj_exception_t {
void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
void mp_obj_exception_initialize0(mp_obj_exception_t *o_exc, const mp_obj_type_t *type);
mp_obj_exception_t *mp_obj_exception_get_native(mp_obj_t self_in);
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \

View File

@ -40,10 +40,11 @@
// Instance of GeneratorExit exception - needed by generator.close()
#if MICROPY_CONST_GENERATOREXIT_OBJ
const
mp_obj_exception_t mp_static_GeneratorExit_obj = {{&mp_type_GeneratorExit}, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj, (mp_obj_traceback_t *)&mp_const_empty_traceback_obj};
#else
static
mp_obj_exception_t mp_static_GeneratorExit_obj;
#endif
mp_obj_exception_t mp_static_GeneratorExit_obj = {{&mp_type_GeneratorExit}, (mp_obj_tuple_t *)&mp_const_empty_tuple_obj, (mp_obj_traceback_t *)&mp_const_empty_traceback_obj};
/******************************************************************************/
/* generator wrapper */
@ -370,9 +371,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_ins
static mp_obj_t generatorexit(void) {
#if MICROPY_CPYTHON_EXCEPTION_CHAIN
MP_STATIC_ASSERT(!MICROPY_CONST_GENERATOREXIT_OBJ);
mp_static_GeneratorExit_obj.context = NULL;
mp_static_GeneratorExit_obj.cause = NULL;
mp_static_GeneratorExit_obj.suppress_context = false;
mp_obj_exception_initialize0(&mp_static_GeneratorExit_obj, &mp_type_GeneratorExit);
#endif
return MP_OBJ_FROM_PTR(&mp_static_GeneratorExit_obj);
}

View File

@ -78,14 +78,10 @@ void mp_init(void) {
#if MICROPY_KBD_EXCEPTION
// initialise the exception object for raising KeyboardInterrupt
MP_STATE_VM(mp_kbd_exception).base.type = &mp_type_KeyboardInterrupt;
MP_STATE_VM(mp_kbd_exception).args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;
MP_STATE_VM(mp_kbd_exception).traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
mp_obj_exception_initialize0(&MP_STATE_VM(mp_kbd_exception), &mp_type_KeyboardInterrupt);
#endif
MP_STATE_VM(mp_reload_exception).base.type = &mp_type_ReloadException;
MP_STATE_VM(mp_reload_exception).args = (mp_obj_tuple_t *)&mp_const_empty_tuple_obj;
MP_STATE_VM(mp_reload_exception).traceback = (mp_obj_traceback_t *)&mp_const_empty_traceback_obj;
mp_obj_exception_initialize0(&MP_STATE_VM(mp_reload_exception), &mp_type_ReloadException);
// call port specific initialization if any
#ifdef MICROPY_PORT_INIT_FUNC