From 4c0747a25b39c0925dd0c94071a286a891291751 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 14 Mar 2018 16:05:47 +0100 Subject: [PATCH 1/3] Add hasWasmPreamble helper --- src/hera.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/hera.cpp b/src/hera.cpp index 6d9966461..c711c0068 100644 --- a/src/hera.cpp +++ b/src/hera.cpp @@ -56,6 +56,10 @@ struct hera_instance : evm_instance { namespace { +bool hasWasmPreamble(vector const& _input) { + return _input.size() >= 5 && _input[0] == 0 && _input[1] == 'a' && _input[2] == 's' && _input[3] == 'm' && _input[4] == 1; +} + #if HERA_METERING_CONTRACT || HERA_EVM2WASM vector callSystemContract( evm_context* context, @@ -259,7 +263,7 @@ evm_result evm_execute( vector _code(code, code + code_size); // ensure we can only handle WebAssembly version 1 - if (code_size < 5 || code[0] != 0 || code[1] != 'a' || code[2] != 's' || code[3] != 'm' || code[4] != 1) { + if (!hasWasmPreamble(_code)) { hera_instance* hera = static_cast(instance); #if HERA_EVM2WASM // Translate EVM bytecode to WASM From 89e5ea41348567b2cabd2f76ad8dc7d105e29fc9 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 14 Mar 2018 16:06:20 +0100 Subject: [PATCH 2/3] Run metering on CREATE output only if it returned wasm bytecode --- src/hera.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hera.cpp b/src/hera.cpp index c711c0068..e1b04af09 100644 --- a/src/hera.cpp +++ b/src/hera.cpp @@ -292,7 +292,7 @@ evm_result evm_execute( if (result.returnValue.size() > 0) { vector returnValue; - if (msg->kind == EVM_CREATE && !result.isRevert) { + if (msg->kind == EVM_CREATE && !result.isRevert && hasWasmPreamble(result.returnValue)) { // Meter the deployed code returnValue = sentinel(context, result.returnValue); heraAssert(returnValue.size() > 5, "Invalid contract or metering failed."); From 3fdfad7c83eece3ae880d0c9f0f8611ad518975d Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Wed, 14 Mar 2018 16:36:41 +0100 Subject: [PATCH 3/3] Ensure that wasm preamble has both fields completely --- src/hera.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/hera.cpp b/src/hera.cpp index e1b04af09..4e0cc4b52 100644 --- a/src/hera.cpp +++ b/src/hera.cpp @@ -57,7 +57,16 @@ struct hera_instance : evm_instance { namespace { bool hasWasmPreamble(vector const& _input) { - return _input.size() >= 5 && _input[0] == 0 && _input[1] == 'a' && _input[2] == 's' && _input[3] == 'm' && _input[4] == 1; + return + _input.size() >= 8 && + _input[0] == 0 && + _input[1] == 'a' && + _input[2] == 's' && + _input[3] == 'm' && + _input[4] == 1 && + _input[5] == 0 && + _input[6] == 0 && + _input[7] == 0; } #if HERA_METERING_CONTRACT || HERA_EVM2WASM