From db05664f5792cac82eaeeaaf27c4b35db5018a67 Mon Sep 17 00:00:00 2001 From: Matt Witherspoon <32485495+spoonincode@users.noreply.github.com> Date: Wed, 7 Feb 2018 12:45:20 -0500 Subject: [PATCH] Place wasm_constraints in constexpr variables Place some of the wasm_constraints in constexpr variables that can be referred to from elsewhere --- .../eosio/chain/wasm_eosio_constraints.hpp | 8 ++++++++ libraries/chain/wasm_eosio_constraints.cpp | 18 +++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/libraries/chain/include/eosio/chain/wasm_eosio_constraints.hpp b/libraries/chain/include/eosio/chain/wasm_eosio_constraints.hpp index 323f3cd14..c3e38b979 100644 --- a/libraries/chain/include/eosio/chain/wasm_eosio_constraints.hpp +++ b/libraries/chain/include/eosio/chain/wasm_eosio_constraints.hpp @@ -6,6 +6,14 @@ namespace IR { namespace eosio { namespace chain { +namespace wasm_constraints { + //Be aware that some of these are required to be a multiple of some internal number + constexpr unsigned maximum_linear_memory = 1024*1024; //bytes + constexpr unsigned maximum_mutable_globals = 1024; //bytes + constexpr unsigned maximum_table_elements = 1024; //elements + constexpr unsigned maximum_linear_memory_init = 64*1024; //bytes +} + //Throws if something in the module violates void validate_eosio_wasm_constraints(const IR::Module& m); diff --git a/libraries/chain/wasm_eosio_constraints.cpp b/libraries/chain/wasm_eosio_constraints.cpp index 515448558..d755c8b5a 100644 --- a/libraries/chain/wasm_eosio_constraints.cpp +++ b/libraries/chain/wasm_eosio_constraints.cpp @@ -29,7 +29,7 @@ struct eosio_constraints_visitor : public nop_opcode_visitor { // an 8 byte data type, that's fine. There will be enough of a guard on the end // of 1MiB where it's not a problem void fail_large_offset(U32 offset) { - if(offset >= 1024*1024) + if(offset >= wasm_constraints::maximum_linear_memory) FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract used an invalid large memory store/load offset"); } void i32_load (LoadOrStoreImm<2> imm) override { fail_large_offset(imm.offset); } @@ -65,18 +65,18 @@ struct eosio_constraints_visitor : public nop_opcode_visitor { }; void validate_eosio_wasm_constraints(const Module& m) { - if(m.memories.defs.size() && m.memories.defs[0].type.size.min > 16) - FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract initial memory size must be less than or equal to 1MiB"); + if(m.memories.defs.size() && m.memories.defs[0].type.size.min > wasm_constraints::maximum_linear_memory/(64*1024)) + FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract initial memory size must be less than or equal to ${k}KiB", ("k", wasm_constraints::maximum_linear_memory/1024)); for(const DataSegment& ds : m.dataSegments) { if(ds.baseOffset.type != InitializerExpression::Type::i32_const) FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract has unexpected memory base offset type"); - if(static_cast(ds.baseOffset.i32) + ds.data.size() > 64*1024) - FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract data segments must lie in first 64KiB"); + if(static_cast(ds.baseOffset.i32) + ds.data.size() > wasm_constraints::maximum_linear_memory_init) + FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract data segments must lie in first ${k}KiB", ("k", wasm_constraints::maximum_linear_memory_init/1024)); } - if(m.tables.defs.size() && m.tables.defs[0].type.size.min > 1024) - FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract table limited to 1024 elements"); + if(m.tables.defs.size() && m.tables.defs[0].type.size.min > wasm_constraints::maximum_table_elements) + FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract table limited to ${t} elements", ("t", wasm_constraints::maximum_table_elements)); unsigned mutable_globals_total_size = 0; for(const GlobalDef& global_def : m.globals.defs) { @@ -94,8 +94,8 @@ void validate_eosio_wasm_constraints(const Module& m) { mutable_globals_total_size += 4; } } - if(mutable_globals_total_size > 1024) - FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract has more than 1KiB of mutable globals"); + if(mutable_globals_total_size > wasm_constraints::maximum_mutable_globals) + FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract has more than ${k} bytes of mutable globals", ("k", wasm_constraints::maximum_mutable_globals)); //Some of the OperatorDecoderStream users inside of WAVM track the control stack and quit parsing from // OperatorDecoderStream when the control stack is empty (since that would indicate unreachable code). -- GitLab