micropython/tests/cmdline
Damien George e1fb03f3e2 py: Fix VM crash with unwinding jump out of a finally block.
This patch fixes a bug in the VM when breaking within a try-finally.  The
bug has to do with executing a break within the finally block of a
try-finally statement.  For example:

    def f():
        for x in (1,):
            print('a', x)
            try:
                raise Exception
            finally:
                print(1)
                break
            print('b', x)
    f()

Currently in uPy the above code will print:

    a 1
    1
    1
    segmentation fault (core dumped)  micropython

Not only is there a seg fault, but the "1" in the finally block is printed
twice.  This is because when the VM executes a finally block it doesn't
really know if that block was executed due to a fall-through of the try (no
exception raised), or because an exception is active.  In particular, for
nested finallys the VM has no idea which of the nested ones have active
exceptions and which are just fall-throughs.  So when a break (or continue)
is executed it tries to unwind all of the finallys, when in fact only some
may be active.

It's questionable whether break (or return or continue) should be allowed
within a finally block, because they implicitly swallow any active
exception, but nevertheless it's allowed by CPython (although almost never
used in the standard library).  And uPy should at least not crash in such a
case.

The solution here relies on the fact that exception and finally handlers
always appear in the bytecode after the try body.

Note: there was a similar bug with a return in a finally block, but that
was previously fixed in b735208403
2019-03-05 16:05:05 +11:00
..
cmd_optimise.py tests/cmdline: Add test for -O option to check optimisation value. 2016-08-26 22:29:38 +10:00
cmd_optimise.py.exp tests/cmdline: Add test for -O option to check optimisation value. 2016-08-26 22:29:38 +10:00
cmd_parsetree.py tests: Add some more tests to improve coverage of py/parse.c. 2017-12-19 16:13:00 +11:00
cmd_parsetree.py.exp tests: Add some more tests to improve coverage of py/parse.c. 2017-12-19 16:13:00 +11:00
cmd_showbc.py tests/cmdline/cmd_showbc.py: Fix test to explicitly declare nonlocal. 2018-10-27 23:57:14 +11:00
cmd_showbc.py.exp py: Fix VM crash with unwinding jump out of a finally block. 2019-03-05 16:05:05 +11:00
cmd_verbose.py tests: Add ability to test uPy cmdline executable. 2015-03-13 10:58:34 +00:00
cmd_verbose.py.exp tests: Get cmdline verbose tests running again. 2016-09-20 11:33:19 +10:00
repl_autocomplete.py py/repl: Generalise REPL autocomplete to use qstr probing. 2018-02-19 16:12:44 +11:00
repl_autocomplete.py.exp py/repl: Generalise REPL autocomplete to use qstr probing. 2018-02-19 16:12:44 +11:00
repl_basic.py tests: Improve test coverage of py/compile.c. 2016-10-11 12:30:32 +11:00
repl_basic.py.exp tests: Improve test coverage of py/compile.c. 2016-10-11 12:30:32 +11:00
repl_cont.py py/repl: Check for an identifier char after the keyword. 2016-02-17 08:56:15 +00:00
repl_cont.py.exp py/repl: Check for an identifier char after the keyword. 2016-02-17 08:56:15 +00:00
repl_emacs_keys.py run-tests: Test REPL emacs keys, but only if present. 2015-08-06 00:03:44 +01:00
repl_emacs_keys.py.exp unix: Add exit and paste-mode hints to shell startup banner. 2015-10-12 00:19:00 +01:00