From d9701a754e742fc3491d619fad928ac544b32b93 Mon Sep 17 00:00:00 2001 From: Doug Goldstein Date: Sun, 9 Mar 2014 20:20:22 -0500 Subject: [PATCH] core: don't crash if pc is past flash If we somehow had bad code that pointed us past the end of the flash it would segfault simavr rather than catching the bad case, this fixes and that wraps the check in an unlikely() branch hint since its very unlikely we will take that case. --- simavr/sim/sim_core.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/simavr/sim/sim_core.c b/simavr/sim/sim_core.c index 538caa4..840977a 100644 --- a/simavr/sim/sim_core.c +++ b/simavr/sim/sim_core.c @@ -496,6 +496,15 @@ avr_flashaddr_t avr_run_one(avr_t * avr) avr->trace_data->touched[0] = avr->trace_data->touched[1] = avr->trace_data->touched[2] = 0; #endif + /* Ensure we don't crash simavr due to a bad instruction reading past + * the end of the flash. + */ + if (unlikely(avr->pc >= avr->flashend)) { + STATE("CRASH\n"); + crash(avr); + return 0; + } + uint32_t opcode = (avr->flash[avr->pc + 1] << 8) | avr->flash[avr->pc]; avr_flashaddr_t new_pc = avr->pc + 2; // future "default" pc int cycle = 1; -- 2.39.5