From: Doug Goldstein Date: Mon, 10 Mar 2014 01:20:22 +0000 (-0500) Subject: core: don't crash if pc is past flash X-Git-Tag: v1.2~10^2~1 X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=d9701a754e742fc3491d619fad928ac544b32b93;p=sx%2Fsimavr.git 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. --- 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;