py/vm: Fix handling of unwind jump out of active finally.
Prior to this commit, when unwinding through an active finally the stack was not being correctly popped/folded, which resulting in the VM crashing for complicated unwinding of nested finallys. This should be fixed with this commit, and more tests for return/break/ continue within a finally have been added to exercise this.
This commit is contained in:
parent
0096041c99
commit
82c494a97e
104
py/vm.c
104
py/vm.c
|
@ -109,6 +109,21 @@
|
||||||
exc_sp--; /* pop back to previous exception handler */ \
|
exc_sp--; /* pop back to previous exception handler */ \
|
||||||
CLEAR_SYS_EXC_INFO() /* just clear sys.exc_info(), not compliant, but it shouldn't be used in 1st place */
|
CLEAR_SYS_EXC_INFO() /* just clear sys.exc_info(), not compliant, but it shouldn't be used in 1st place */
|
||||||
|
|
||||||
|
#define CANCEL_ACTIVE_FINALLY(sp) do { \
|
||||||
|
if (mp_obj_is_small_int(sp[-1])) { \
|
||||||
|
/* Stack: (..., prev_dest_ip, prev_cause, dest_ip) */ \
|
||||||
|
/* Cancel the unwind through the previous finally, replace with current one */ \
|
||||||
|
sp[-2] = sp[0]; \
|
||||||
|
sp -= 2; \
|
||||||
|
} else { \
|
||||||
|
assert(sp[-1] == mp_const_none || mp_obj_is_exception_instance(sp[-1])); \
|
||||||
|
/* Stack: (..., None/exception, dest_ip) */ \
|
||||||
|
/* Silence the finally's exception value (may be None or an exception) */ \
|
||||||
|
sp[-1] = sp[0]; \
|
||||||
|
--sp; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#if MICROPY_PY_SYS_SETTRACE
|
#if MICROPY_PY_SYS_SETTRACE
|
||||||
|
|
||||||
#define FRAME_SETUP() do { \
|
#define FRAME_SETUP() do { \
|
||||||
|
@ -698,21 +713,28 @@ unwind_jump:;
|
||||||
while ((unum & 0x7f) > 0) {
|
while ((unum & 0x7f) > 0) {
|
||||||
unum -= 1;
|
unum -= 1;
|
||||||
assert(exc_sp >= exc_stack);
|
assert(exc_sp >= exc_stack);
|
||||||
if (MP_TAGPTR_TAG1(exc_sp->val_sp) && exc_sp->handler > ip) {
|
|
||||||
// Getting here the stack looks like:
|
if (MP_TAGPTR_TAG1(exc_sp->val_sp)) {
|
||||||
// (..., X, dest_ip)
|
if (exc_sp->handler > ip) {
|
||||||
// where X is pointed to by exc_sp->val_sp and in the case
|
// Found a finally handler that isn't active; run it.
|
||||||
// of a "with" block contains the context manager info.
|
// Getting here the stack looks like:
|
||||||
// We're going to run "finally" code as a coroutine
|
// (..., X, dest_ip)
|
||||||
// (not calling it recursively). Set up a sentinel
|
// where X is pointed to by exc_sp->val_sp and in the case
|
||||||
// on the stack so it can return back to us when it is
|
// of a "with" block contains the context manager info.
|
||||||
// done (when WITH_CLEANUP or END_FINALLY reached).
|
assert(&sp[-1] == MP_TAGPTR_PTR(exc_sp->val_sp));
|
||||||
// The sentinel is the number of exception handlers left to
|
// We're going to run "finally" code as a coroutine
|
||||||
// unwind, which is a non-negative integer.
|
// (not calling it recursively). Set up a sentinel
|
||||||
PUSH(MP_OBJ_NEW_SMALL_INT(unum));
|
// on the stack so it can return back to us when it is
|
||||||
ip = exc_sp->handler; // get exception handler byte code address
|
// done (when WITH_CLEANUP or END_FINALLY reached).
|
||||||
exc_sp--; // pop exception handler
|
// The sentinel is the number of exception handlers left to
|
||||||
goto dispatch_loop; // run the exception handler
|
// unwind, which is a non-negative integer.
|
||||||
|
PUSH(MP_OBJ_NEW_SMALL_INT(unum));
|
||||||
|
ip = exc_sp->handler;
|
||||||
|
goto dispatch_loop;
|
||||||
|
} else {
|
||||||
|
// Found a finally handler that is already active; cancel it.
|
||||||
|
CANCEL_ACTIVE_FINALLY(sp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
POP_EXC_BLOCK();
|
POP_EXC_BLOCK();
|
||||||
}
|
}
|
||||||
|
@ -740,9 +762,9 @@ unwind_jump:;
|
||||||
// if TOS is None, just pops it and continues
|
// if TOS is None, just pops it and continues
|
||||||
// if TOS is an integer, finishes coroutine and returns control to caller
|
// if TOS is an integer, finishes coroutine and returns control to caller
|
||||||
// if TOS is an exception, reraises the exception
|
// if TOS is an exception, reraises the exception
|
||||||
|
assert(exc_sp >= exc_stack);
|
||||||
|
POP_EXC_BLOCK();
|
||||||
if (TOP() == mp_const_none) {
|
if (TOP() == mp_const_none) {
|
||||||
assert(exc_sp >= exc_stack);
|
|
||||||
POP_EXC_BLOCK();
|
|
||||||
sp--;
|
sp--;
|
||||||
} else if (mp_obj_is_small_int(TOP())) {
|
} else if (mp_obj_is_small_int(TOP())) {
|
||||||
// We finished "finally" coroutine and now dispatch back
|
// We finished "finally" coroutine and now dispatch back
|
||||||
|
@ -1113,28 +1135,32 @@ unwind_jump:;
|
||||||
unwind_return:
|
unwind_return:
|
||||||
// Search for and execute finally handlers that aren't already active
|
// Search for and execute finally handlers that aren't already active
|
||||||
while (exc_sp >= exc_stack) {
|
while (exc_sp >= exc_stack) {
|
||||||
if (MP_TAGPTR_TAG1(exc_sp->val_sp) && exc_sp->handler > ip) {
|
if (MP_TAGPTR_TAG1(exc_sp->val_sp)) {
|
||||||
// Found a finally handler that isn't active.
|
if (exc_sp->handler > ip) {
|
||||||
// Getting here the stack looks like:
|
// Found a finally handler that isn't active; run it.
|
||||||
// (..., X, [iter0, iter1, ...,] ret_val)
|
// Getting here the stack looks like:
|
||||||
// where X is pointed to by exc_sp->val_sp and in the case
|
// (..., X, [iter0, iter1, ...,] ret_val)
|
||||||
// of a "with" block contains the context manager info.
|
// where X is pointed to by exc_sp->val_sp and in the case
|
||||||
// There may be 0 or more for-iterators between X and the
|
// of a "with" block contains the context manager info.
|
||||||
// return value, and these must be removed before control can
|
// There may be 0 or more for-iterators between X and the
|
||||||
// pass to the finally code. We simply copy the ret_value down
|
// return value, and these must be removed before control can
|
||||||
// over these iterators, if they exist. If they don't then the
|
// pass to the finally code. We simply copy the ret_value down
|
||||||
// following is a null operation.
|
// over these iterators, if they exist. If they don't then the
|
||||||
mp_obj_t *finally_sp = MP_TAGPTR_PTR(exc_sp->val_sp);
|
// following is a null operation.
|
||||||
finally_sp[1] = sp[0];
|
mp_obj_t *finally_sp = MP_TAGPTR_PTR(exc_sp->val_sp);
|
||||||
sp = &finally_sp[1];
|
finally_sp[1] = sp[0];
|
||||||
// We're going to run "finally" code as a coroutine
|
sp = &finally_sp[1];
|
||||||
// (not calling it recursively). Set up a sentinel
|
// We're going to run "finally" code as a coroutine
|
||||||
// on a stack so it can return back to us when it is
|
// (not calling it recursively). Set up a sentinel
|
||||||
// done (when WITH_CLEANUP or END_FINALLY reached).
|
// on a stack so it can return back to us when it is
|
||||||
PUSH(MP_OBJ_NEW_SMALL_INT(-1));
|
// done (when WITH_CLEANUP or END_FINALLY reached).
|
||||||
ip = exc_sp->handler;
|
PUSH(MP_OBJ_NEW_SMALL_INT(-1));
|
||||||
POP_EXC_BLOCK();
|
ip = exc_sp->handler;
|
||||||
goto dispatch_loop;
|
goto dispatch_loop;
|
||||||
|
} else {
|
||||||
|
// Found a finally handler that is already active; cancel it.
|
||||||
|
CANCEL_ACTIVE_FINALLY(sp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
POP_EXC_BLOCK();
|
POP_EXC_BLOCK();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
def foo(x):
|
||||||
|
for i in range(x):
|
||||||
|
for j in range(x):
|
||||||
|
try:
|
||||||
|
print(x, i, j, 1)
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
print(x, i, j, 2)
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
1 / 0
|
||||||
|
finally:
|
||||||
|
print(x, i, j, 3)
|
||||||
|
break
|
||||||
|
finally:
|
||||||
|
print(x, i, j, 4)
|
||||||
|
break
|
||||||
|
print(foo(4))
|
|
@ -0,0 +1,17 @@
|
||||||
|
def foo(x):
|
||||||
|
for i in range(x):
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
print(x, i)
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
1 / 0
|
||||||
|
finally:
|
||||||
|
return 42
|
||||||
|
finally:
|
||||||
|
print('continue')
|
||||||
|
continue
|
||||||
|
print(foo(4))
|
|
@ -0,0 +1,9 @@
|
||||||
|
4 0
|
||||||
|
continue
|
||||||
|
4 1
|
||||||
|
continue
|
||||||
|
4 2
|
||||||
|
continue
|
||||||
|
4 3
|
||||||
|
continue
|
||||||
|
None
|
|
@ -0,0 +1,17 @@
|
||||||
|
def foo(x):
|
||||||
|
for i in range(x):
|
||||||
|
try:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
print(x, i)
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
1 / 0
|
||||||
|
finally:
|
||||||
|
return 42
|
||||||
|
finally:
|
||||||
|
print('return')
|
||||||
|
return 43
|
||||||
|
print(foo(4))
|
Loading…
Reference in New Issue