From f4ac8b1514bcce3ed022e65c074f7d41cbad5280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 31 Mar 2021 22:06:29 +0200 Subject: [PATCH 1/2] baseline: Use "infinite" interpreter loop Guaranteed to terminate because code must end with STOP. --- lib/evmone/baseline.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/evmone/baseline.cpp b/lib/evmone/baseline.cpp index d17396f6a4..a1b32bda59 100644 --- a/lib/evmone/baseline.cpp +++ b/lib/evmone/baseline.cpp @@ -112,7 +112,7 @@ evmc_result baseline_execute(ExecutionState& state) noexcept const auto code_end = code + code_size; auto* pc = code; - while (pc != code_end) + while (true) // Guaranteed to terminate because code must end with STOP. { const auto op = *pc; From 532eb14b795577210f5d2f8d348dacea25d27567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 31 Mar 2021 22:09:33 +0200 Subject: [PATCH 2/2] baseline: Do not check for out-of-buffer push load This cannot overflow code buffer because code ends with valid STOP instruction. --- lib/evmone/baseline.cpp | 73 +++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/lib/evmone/baseline.cpp b/lib/evmone/baseline.cpp index a1b32bda59..64fb1645c3 100644 --- a/lib/evmone/baseline.cpp +++ b/lib/evmone/baseline.cpp @@ -41,14 +41,10 @@ const uint8_t* op_jump(ExecutionState& state, const JumpdestMap& jumpdest_map) n } template -inline const uint8_t* load_push( - ExecutionState& state, const uint8_t* code, const uint8_t* code_end) noexcept +inline const uint8_t* load_push(ExecutionState& state, const uint8_t* code) noexcept { - // TODO: Also last full push can be ignored. - if (code + Len > code_end) // Trimmed push data can be ignored. - return code_end; - uint8_t buffer[Len]; + // This cannot overflow code buffer because code ends with valid STOP instruction. std::memcpy(buffer, code, Len); state.stack.push(intx::be::load(buffer)); return code + Len; @@ -110,7 +106,6 @@ evmc_result baseline_execute(ExecutionState& state) noexcept const auto instruction_metrics = evmc_get_instruction_metrics_table(rev); const auto jumpdest_map = build_jumpdest_map(code, code_size); - const auto code_end = code + code_size; auto* pc = code; while (true) // Guaranteed to terminate because code must end with STOP. { @@ -400,100 +395,100 @@ evmc_result baseline_execute(ExecutionState& state) noexcept break; case OP_PUSH1: - pc = load_push<1>(state, pc + 1, code_end); + pc = load_push<1>(state, pc + 1); continue; case OP_PUSH2: - pc = load_push<2>(state, pc + 1, code_end); + pc = load_push<2>(state, pc + 1); continue; case OP_PUSH3: - pc = load_push<3>(state, pc + 1, code_end); + pc = load_push<3>(state, pc + 1); continue; case OP_PUSH4: - pc = load_push<4>(state, pc + 1, code_end); + pc = load_push<4>(state, pc + 1); continue; case OP_PUSH5: - pc = load_push<5>(state, pc + 1, code_end); + pc = load_push<5>(state, pc + 1); continue; case OP_PUSH6: - pc = load_push<6>(state, pc + 1, code_end); + pc = load_push<6>(state, pc + 1); continue; case OP_PUSH7: - pc = load_push<7>(state, pc + 1, code_end); + pc = load_push<7>(state, pc + 1); continue; case OP_PUSH8: - pc = load_push<8>(state, pc + 1, code_end); + pc = load_push<8>(state, pc + 1); continue; case OP_PUSH9: - pc = load_push<9>(state, pc + 1, code_end); + pc = load_push<9>(state, pc + 1); continue; case OP_PUSH10: - pc = load_push<10>(state, pc + 1, code_end); + pc = load_push<10>(state, pc + 1); continue; case OP_PUSH11: - pc = load_push<11>(state, pc + 1, code_end); + pc = load_push<11>(state, pc + 1); continue; case OP_PUSH12: - pc = load_push<12>(state, pc + 1, code_end); + pc = load_push<12>(state, pc + 1); continue; case OP_PUSH13: - pc = load_push<13>(state, pc + 1, code_end); + pc = load_push<13>(state, pc + 1); continue; case OP_PUSH14: - pc = load_push<14>(state, pc + 1, code_end); + pc = load_push<14>(state, pc + 1); continue; case OP_PUSH15: - pc = load_push<15>(state, pc + 1, code_end); + pc = load_push<15>(state, pc + 1); continue; case OP_PUSH16: - pc = load_push<16>(state, pc + 1, code_end); + pc = load_push<16>(state, pc + 1); continue; case OP_PUSH17: - pc = load_push<17>(state, pc + 1, code_end); + pc = load_push<17>(state, pc + 1); continue; case OP_PUSH18: - pc = load_push<18>(state, pc + 1, code_end); + pc = load_push<18>(state, pc + 1); continue; case OP_PUSH19: - pc = load_push<19>(state, pc + 1, code_end); + pc = load_push<19>(state, pc + 1); continue; case OP_PUSH20: - pc = load_push<20>(state, pc + 1, code_end); + pc = load_push<20>(state, pc + 1); continue; case OP_PUSH21: - pc = load_push<21>(state, pc + 1, code_end); + pc = load_push<21>(state, pc + 1); continue; case OP_PUSH22: - pc = load_push<22>(state, pc + 1, code_end); + pc = load_push<22>(state, pc + 1); continue; case OP_PUSH23: - pc = load_push<23>(state, pc + 1, code_end); + pc = load_push<23>(state, pc + 1); continue; case OP_PUSH24: - pc = load_push<24>(state, pc + 1, code_end); + pc = load_push<24>(state, pc + 1); continue; case OP_PUSH25: - pc = load_push<25>(state, pc + 1, code_end); + pc = load_push<25>(state, pc + 1); continue; case OP_PUSH26: - pc = load_push<26>(state, pc + 1, code_end); + pc = load_push<26>(state, pc + 1); continue; case OP_PUSH27: - pc = load_push<27>(state, pc + 1, code_end); + pc = load_push<27>(state, pc + 1); continue; case OP_PUSH28: - pc = load_push<28>(state, pc + 1, code_end); + pc = load_push<28>(state, pc + 1); continue; case OP_PUSH29: - pc = load_push<29>(state, pc + 1, code_end); + pc = load_push<29>(state, pc + 1); continue; case OP_PUSH30: - pc = load_push<30>(state, pc + 1, code_end); + pc = load_push<30>(state, pc + 1); continue; case OP_PUSH31: - pc = load_push<31>(state, pc + 1, code_end); + pc = load_push<31>(state, pc + 1); continue; case OP_PUSH32: - pc = load_push<32>(state, pc + 1, code_end); + pc = load_push<32>(state, pc + 1); continue; case OP_DUP1: