From d3cac18d4914af4549437ef3fd0d62f1fc05ff27 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 27 Feb 2018 16:17:10 +1100 Subject: [PATCH] tests/unix: Add coverage test for VM executing invalid bytecode. --- ports/unix/coverage.c | 18 ++++++++++++++++++ tests/unix/extra_coverage.py.exp | 2 ++ 2 files changed, 20 insertions(+) diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 25376edc02..f0d9406c61 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -11,6 +11,7 @@ #include "py/formatfloat.h" #include "py/stream.h" #include "py/binary.h" +#include "py/bc.h" #if defined(MICROPY_UNIX_COVERAGE) @@ -350,6 +351,23 @@ STATIC mp_obj_t extra_coverage(void) { mp_printf(&mp_plat_print, "%.0lf\n", dar[0]); } + // VM + { + mp_printf(&mp_plat_print, "# VM\n"); + + // call mp_execute_bytecode with invalide bytecode (should raise NotImplementedError) + mp_obj_fun_bc_t fun_bc; + fun_bc.bytecode = (const byte*)"\x01"; // just needed for n_state + mp_code_state_t *code_state = m_new_obj_var(mp_code_state_t, mp_obj_t, 1); + code_state->fun_bc = &fun_bc; + code_state->ip = (const byte*)"\x00"; // just needed for an invalid opcode + code_state->sp = &code_state->state[0]; + code_state->exc_sp = NULL; + code_state->old_globals = NULL; + mp_vm_return_kind_t ret = mp_execute_bytecode(code_state, MP_OBJ_NULL); + mp_printf(&mp_plat_print, "%d %d\n", ret, mp_obj_get_type(code_state->state[0]) == &mp_type_NotImplementedError); + } + // scheduler { mp_printf(&mp_plat_print, "# scheduler\n"); diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 71b6867ca6..a030155ba8 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -58,6 +58,8 @@ Warning: test # binary 122 456 +# VM +2 1 # scheduler sched(0)=1 sched(1)=1