From 29738fef56f03a8bcee89f9d0e459efb18a45839 Mon Sep 17 00:00:00 2001 From: Nikhil Parasaram Date: Sat, 6 Nov 2021 14:53:33 +0000 Subject: [PATCH] Fix issues with out of index code (#1545) --- mythril/laser/ethereum/state/global_state.py | 2 +- .../laser/plugin/plugins/coverage/coverage_plugin.py | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/mythril/laser/ethereum/state/global_state.py b/mythril/laser/ethereum/state/global_state.py index 36aa26b2..22d5d414 100644 --- a/mythril/laser/ethereum/state/global_state.py +++ b/mythril/laser/ethereum/state/global_state.py @@ -97,7 +97,7 @@ class GlobalState: instructions = self.environment.code.instruction_list try: return instructions[self.mstate.pc] - except KeyError: + except IndexError: return {"address": self.mstate.pc, "opcode": "STOP"} @property diff --git a/mythril/laser/plugin/plugins/coverage/coverage_plugin.py b/mythril/laser/plugin/plugins/coverage/coverage_plugin.py index b9f1009c..523d27fa 100644 --- a/mythril/laser/plugin/plugins/coverage/coverage_plugin.py +++ b/mythril/laser/plugin/plugins/coverage/coverage_plugin.py @@ -49,7 +49,10 @@ class InstructionCoveragePlugin(LaserPlugin): def stop_sym_exec_hook(): # Print results for code, code_cov in self.coverage.items(): - cov_percentage = sum(code_cov[1]) / float(code_cov[0]) * 100 + if sum(code_cov[1]) == 0 and code_cov[0] == 0: + cov_percentage = 0 + else: + cov_percentage = sum(code_cov[1]) / float(code_cov[0]) * 100 log.info( "Achieved {:.2f}% coverage for code: {}".format( @@ -70,7 +73,10 @@ class InstructionCoveragePlugin(LaserPlugin): number_of_instructions, [False] * number_of_instructions, ) - + if global_state.mstate.pc >= len(self.coverage[code][1]): + # Instruction beyond the instruction list are considered as STOP by EVM + # and can be ignored + return self.coverage[code][1][global_state.mstate.pc] = True @symbolic_vm.laser_hook("start_sym_trans")