Skip to content

Commit

Permalink
py/bc: Fix size calculation of UNWIND_JUMP opcode in mp_opcode_format.
Browse files Browse the repository at this point in the history
Prior to this patch mp_opcode_format would calculate the incorrect size of
the MP_BC_UNWIND_JUMP opcode, missing the additional byte.  But, because
opcodes below 0x10 are unused and treated as bytes in the .mpy load/save
and freezing code, this bug did not show any symptoms, since nested unwind
jumps would rarely (if ever) reach a depth of 16 (so the extra byte of this
opcode would be between 0x01 and 0x0f and be correctly loaded/saved/frozen
simply as an undefined opcode).

This patch fixes this bug by correctly accounting for the additional byte.
        .
  • Loading branch information
dpgeorge committed Sep 2, 2019
1 parent c348e79 commit b29fae0
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
6 changes: 4 additions & 2 deletions py/bc.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ continue2:;
#if MICROPY_PERSISTENT_CODE_LOAD || MICROPY_PERSISTENT_CODE_SAVE

// The following table encodes the number of bytes that a specific opcode
// takes up. There are 3 special opcodes that always have an extra byte:
// takes up. There are 4 special opcodes that always have an extra byte:
// MP_BC_UNWIND_JUMP
// MP_BC_MAKE_CLOSURE
// MP_BC_MAKE_CLOSURE_DEFARGS
// MP_BC_RAISE_VARARGS
Expand Down Expand Up @@ -402,7 +403,8 @@ uint mp_opcode_format(const byte *ip, size_t *opcode_size, bool count_var_uint)
ip += 3;
} else {
int extra_byte = (
*ip == MP_BC_RAISE_VARARGS
*ip == MP_BC_UNWIND_JUMP
|| *ip == MP_BC_RAISE_VARARGS
|| *ip == MP_BC_MAKE_CLOSURE
|| *ip == MP_BC_MAKE_CLOSURE_DEFARGS
);
Expand Down
73 changes: 73 additions & 0 deletions tests/basics/try_except_break.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# test deep unwind via break from nested try-except (22 of them)
while True:
print(1)
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
try:
print(2)
break
print(3)
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
except:
pass
print(4)
3 changes: 3 additions & 0 deletions tests/basics/try_except_break.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
1
2
4
4 changes: 3 additions & 1 deletion tools/mpy-tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def access(self, idx):
MP_OPCODE_OFFSET = 3

# extra bytes:
MP_BC_UNWIND_JUMP = 0x46
MP_BC_MAKE_CLOSURE = 0x62
MP_BC_MAKE_CLOSURE_DEFARGS = 0x63
MP_BC_RAISE_VARARGS = 0x5c
Expand Down Expand Up @@ -215,7 +216,8 @@ def mp_opcode_format(bytecode, ip, count_var_uint, opcode_format=make_opcode_for
ip += 3
else:
extra_byte = (
opcode == MP_BC_RAISE_VARARGS
opcode == MP_BC_UNWIND_JUMP
or opcode == MP_BC_RAISE_VARARGS
or opcode == MP_BC_MAKE_CLOSURE
or opcode == MP_BC_MAKE_CLOSURE_DEFARGS
)
Expand Down

0 comments on commit b29fae0

Please sign in to comment.