Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
wasm_sysio_validation.cpp
Go to the documentation of this file.
6#include "IR/Module.h"
7#include "IR/Operators.h"
8#include "WASM/WASM.h"
9
10namespace sysio { namespace chain { namespace wasm_validations {
11using namespace IR;
12
14 // just pass
15}
16
18 if ( m.memories.defs.size() && m.memories.defs[0].type.size.min > wasm_constraints::maximum_linear_memory/(64*1024) )
19 FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract initial memory size must be less than or equal to ${k}KiB",
21}
22
24 for ( const DataSegment& ds : m.dataSegments ) {
25 if ( ds.baseOffset.type != InitializerExpression::Type::i32_const )
26 FC_THROW_EXCEPTION( wasm_execution_error, "Smart contract has unexpected memory base offset type" );
27
28 if ( static_cast<uint32_t>( ds.baseOffset.i32 ) + ds.data.size() > wasm_constraints::maximum_linear_memory_init )
29 FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract data segments must lie in first ${k}KiB",
31 }
32}
33
35 if ( m.tables.defs.size() && m.tables.defs[0].type.size.min > wasm_constraints::maximum_table_elements )
36 FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract table limited to ${t} elements",
38}
39
41 unsigned mutable_globals_total_size = 0;
42 for(const GlobalDef& global_def : m.globals.defs) {
43 if(!global_def.type.isMutable)
44 continue;
45 switch(global_def.type.valueType) {
46 case ValueType::any:
47 case ValueType::num:
48 FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract has unexpected global definition value type");
49 case ValueType::i64:
50 case ValueType::f64:
51 mutable_globals_total_size += 4;
52 case ValueType::i32:
53 case ValueType::f32:
54 mutable_globals_total_size += 4;
55 }
56 }
57 if(mutable_globals_total_size > wasm_constraints::maximum_mutable_globals)
58 FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract has more than ${k} bytes of mutable globals",
60}
61
63 for(const FunctionDef& func : m.functions.defs) {
64 unsigned function_stack_usage = 0;
65 for(const ValueType& local : func.nonParameterLocalTypes)
66 function_stack_usage += getTypeBitWidth(local)/8;
67 for(const ValueType& params : m.types[func.type.index]->parameters)
68 function_stack_usage += getTypeBitWidth(params)/8;
69
70 if(function_stack_usage > wasm_constraints::maximum_func_local_bytes)
71 FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract function has more than ${k} bytes of stack usage",
73 }
74}
75
77 bool found_it = false;
78
79 for(const Export& exprt : m.exports) {
80 if(exprt.kind != ObjectKind::function)
81 continue;
82 if(exprt.name != "apply")
83 continue;
84 if(m.types[m.functions.getType(exprt.index).index] == FunctionType::get(ResultType::none, {ValueType::i64, ValueType::i64, ValueType::i64})) {
85 found_it = true;
86 break;
87 }
88 }
89
90 if(!found_it)
91 FC_THROW_EXCEPTION(wasm_execution_error, "Smart contract's apply function not exported; non-existent; or wrong type");
92}
93
96}}} // namespace sysio chain validation
Defines exception's used by fc.
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
ValueType
Definition Types.h:14
U8 getTypeBitWidth(ValueType type)
Definition Types.h:101
unsigned short uint16_t
Definition stdint.h:125
unsigned int uint32_t
Definition stdint.h:126
ObjectKind kind
Definition Module.h:86
std::string name
Definition Module.h:85
Uptr index
Definition Module.h:87
static IR_API const FunctionType * get(ResultType ret, const std::initializer_list< ValueType > &parameters)
Definition Types.cpp:38
GlobalType type
Definition Module.h:64
ValueType valueType
Definition Types.h:287
bool isMutable
Definition Types.h:288
IndexSpace< MemoryDef, MemoryType > memories
Definition Module.h:135
IndexSpace< TableDef, TableType > tables
Definition Module.h:134
IndexSpace< FunctionDef, IndexedFunctionType > functions
Definition Module.h:133
std::vector< const FunctionType * > types
Definition Module.h:131
IndexSpace< GlobalDef, GlobalType > globals
Definition Module.h:136
std::vector< DataSegment > dataSegments
Definition Module.h:139
std::vector< Export > exports
Definition Module.h:138