提交 db05664f 编写于 作者: M Matt Witherspoon

Place wasm_constraints in constexpr variables

Place some of the wasm_constraints in constexpr variables that can be referred to from elsewhere
上级 158f2e65
...@@ -6,6 +6,14 @@ namespace IR { ...@@ -6,6 +6,14 @@ namespace IR {
namespace eosio { namespace chain { 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 //Throws if something in the module violates
void validate_eosio_wasm_constraints(const IR::Module& m); void validate_eosio_wasm_constraints(const IR::Module& m);
......
...@@ -29,7 +29,7 @@ struct eosio_constraints_visitor : public nop_opcode_visitor { ...@@ -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 // 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 // of 1MiB where it's not a problem
void fail_large_offset(U32 offset) { 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"); 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); } void i32_load (LoadOrStoreImm<2> imm) override { fail_large_offset(imm.offset); }
...@@ -65,18 +65,18 @@ struct eosio_constraints_visitor : public nop_opcode_visitor { ...@@ -65,18 +65,18 @@ struct eosio_constraints_visitor : public nop_opcode_visitor {
}; };
void validate_eosio_wasm_constraints(const Module& m) { void validate_eosio_wasm_constraints(const Module& m) {
if(m.memories.defs.size() && m.memories.defs[0].type.size.min > 16) 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 1MiB"); 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) { for(const DataSegment& ds : m.dataSegments) {
if(ds.baseOffset.type != InitializerExpression::Type::i32_const) if(ds.baseOffset.type != InitializerExpression::Type::i32_const)
FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract has unexpected memory base offset type"); FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract has unexpected memory base offset type");
if(static_cast<uint32_t>(ds.baseOffset.i32) + ds.data.size() > 64*1024) if(static_cast<uint32_t>(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 64KiB"); 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) 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 1024 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; unsigned mutable_globals_total_size = 0;
for(const GlobalDef& global_def : m.globals.defs) { for(const GlobalDef& global_def : m.globals.defs) {
...@@ -94,8 +94,8 @@ void validate_eosio_wasm_constraints(const Module& m) { ...@@ -94,8 +94,8 @@ void validate_eosio_wasm_constraints(const Module& m) {
mutable_globals_total_size += 4; mutable_globals_total_size += 4;
} }
} }
if(mutable_globals_total_size > 1024) if(mutable_globals_total_size > wasm_constraints::maximum_mutable_globals)
FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract has more than 1KiB of 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 //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). // OperatorDecoderStream when the control stack is empty (since that would indicate unreachable code).
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册