Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
parse_tests.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <vector>
3#include <iterator>
4#include <cstdlib>
5#include <fstream>
6#include <string>
7
8#include "utils.hpp"
9
10#include <sysio/vm/backend.hpp>
11
12using namespace sysio;
13using namespace sysio::vm;
14
15TEST_CASE("small parse test", "[small_parse_test]") {
16 module mod;
17 binary_parser bp{mod.allocator};
18 wasm_code error = { 0x01, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00 };
19 wasm_code_ptr error_ptr( error.data(), 4 );
20 auto n = bp.parse_magic( error_ptr );
21 CHECK(n != constants::magic);
22 wasm_code correct = { 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00 };
23 wasm_code_ptr correct_ptr( correct.data(), 4 );
24 n = bp.parse_magic( correct_ptr );
25 CHECKL(n == constants::magic);
26}
27/*
28BOOST_AUTO_TEST_CASE(actual_wasm_test) {
29 try {
30 static const std::vector<std::pair <std::vector<uint8_t>, std::vector<uint8_t>>> system_contract_types = {
31 {{types::i32,types::i32}, {}},
32 {{types::i32,types::i32,types::i64},{}},
33 {{types::i32},{}},
34 {{types::i32,types::i64,types::i64},{}},
35 {{types::i32,types::i64,types::i64,types::i32},{}},
36 {{types::i32,types::i64,types::i32,types::i32,types::i32},{}},
37 {{types::i32,types::i64},{}},
38 {{types::i32,types::i64,types::i64,types::i32,types::i32},{}},
39 {{types::i32,types::i64,types::i32},{}},
40 {{types::i32,types::i64,types::i64,types::i32,types::i32,types::i32},{}},
41 {{},{}},
42 {{types::i64,types::i64},{}},
43 {{types::i64},{}},
44 {{},{types::i64}},
45 {{types::i64,types::i64,types::i64,types::i64},{types::i32}},
46 {{types::i32,types::i64,types::i32,types::i32},{}},
47 {{types::i64,types::i64,types::i64,types::i32,types::i64},{types::i32}},
48 {{types::i32,types::i32,types::i32},{types::i32}},
49 {{types::i32,types::i32},{types::i64}},
50 {{types::i64,types::i64,types::i64,types::i64,types::i32,types::i32},{types::i32}},
51 {{types::i64,types::i64,types::i64,types::i64},{}},
52 {{types::i32,types::i32},{types::i32}},
53 {{types::i32},{types::i32}},
54 {{types::i64,types::i32},{}},
55 {{types::i64},{types::i32}},
56 {{types::i64,types::i64,types::i64,types::i64,types::i32},{types::i32}},
57 {{},{types::i32}},
58 {{types::i64,types::i64,types::i64,types::i32,types::i32},{types::i32}},
59 {{types::i32,types::i64,types::i64,types::i64,types::i64},{}},
60 {{types::i64,types::i64},{types::i32}},
61 {{types::i32,types::f64},{}},
62 {{types::i32,types::f32},{}},
63 {{types::i64,types::i64},{types::f64}},
64 {{types::i64,types::i64},{types::f32}},
65 {{types::i32,types::i32,types::i32},{}},
66 {{types::i32,types::i64,types::i32},{types::i32}},
67 {{types::i64,types::i64,types::i32,types::i32},{}},
68 {{types::i32,types::i32,types::i32,types::i64},{}},
69 {{types::i32,types::i32,types::i32,types::i32},{}},
70 {{types::i32,types::i32,types::i32,types::i32,types::i32},{}},
71 {{types::i32,types::i32,types::i64,types::i32},{}},
72 {{types::i32,types::i64},{types::i32}},
73 {{types::i64},{types::i64}},
74 {{types::i64,types::i64,types::i64},{}},
75 {{types::i32,types::i32,types::i32,types::i32,types::i32,types::i32},{types::i32}},
76 {{types::i32,types::i32,types::i32,types::i32},{types::i32}},
77 {{types::i32,types::i32,types::i32,types::i32,types::i32},{types::i32}},
78 {{types::i32,types::i32,types::i32,types::i32,types::i32,types::i32,types::i32,types::i32},{}},
79 {{types::f64},{types::f64}},
80 {{types::f64,types::f64},{types::f64}},
81 {{types::f64,types::i32},{types::f64}}
82 };
83
84 {
85 memory_manager::set_memory_limits( 32*1024*1024 );
86 binary_parser bp;
87 module mod;
88 wasm_code code = read_wasm( "wasms/system.wasm" );
89 wasm_code_ptr code_ptr(code.data(), 0);
90 bp.parse_module( code, mod );
91
92 for ( int i=0; i < mod.types.size(); i++ ) {
93 auto& ft = mod.types.at(i);
94 BOOST_CHECK_EQUAL( ft.form, types::func );
95 BOOST_CHECK_EQUAL( ft.param_count, std::get<0>(system_contract_types[i]).size() );
96 for ( int j=0; j < ft.param_types.size(); j++ ) {
97 auto type = ft.param_types.at(j);
98 BOOST_CHECK_EQUAL( std::get<0>(system_contract_types[i])[j++], type );
99 }
100 if ( ft.return_count )
101 BOOST_CHECK_EQUAL ( std::get<1>(system_contract_types[i])[0], ft.return_type );
102 }
103
104 for ( int i=0; i < mod.imports.size(); i++ ) {
105 BOOST_CHECK_EQUAL( mod.imports.at(i).kind, external_kind::Function );
106 }
107
108 for ( int i=0; i < mod.functions.size(); i++ ) {
109 //std::cout << "FUNC " << mod.imports.size()+i << " : " << mod.functions.at(i) << "\n";
110 }
111
112 BOOST_CHECK_EQUAL( mod.tables.at(0).element_type, 0x70 );
113 BOOST_CHECK_EQUAL( mod.tables.at(0).limits.flags, true );
114 BOOST_CHECK_EQUAL( mod.tables.at(0).limits.initial, 0x1A );
115 BOOST_CHECK_EQUAL( mod.tables.at(0).limits.maximum, 0x1A );
116
117 BOOST_CHECK_EQUAL( mod.memories.at(0).limits.flags, false );
118 BOOST_CHECK_EQUAL( mod.memories.at(0).limits.initial, 0x01 );
119
120 BOOST_CHECK_EQUAL( mod.globals.at(0).type.content_type, types::i32 );
121 BOOST_CHECK_EQUAL( mod.globals.at(1).type.content_type, types::i32 );
122 BOOST_CHECK_EQUAL( mod.globals.at(2).type.content_type, types::i32 );
123
124 BOOST_CHECK_EQUAL( mod.globals.at(0).type.mutability, true );
125 BOOST_CHECK_EQUAL( mod.globals.at(1).type.mutability, false );
126 BOOST_CHECK_EQUAL( mod.globals.at(2).type.mutability, false );
127
128 BOOST_CHECK_EQUAL( mod.globals.at(0).init.opcode, opcodes::i32_const );
129 BOOST_CHECK_EQUAL( mod.globals.at(1).init.opcode, opcodes::i32_const );
130 BOOST_CHECK_EQUAL( mod.globals.at(2).init.opcode, opcodes::i32_const );
131
132 BOOST_CHECK_EQUAL( mod.globals.at(0).init.value.i32, 8192 );
133 BOOST_CHECK_EQUAL( mod.globals.at(1).init.value.i32, 12244 );
134 BOOST_CHECK_EQUAL( mod.globals.at(2).init.value.i32, 12244 );
135
136 BOOST_CHECK( memcmp((char*)mod.exports.at(0).field_str.raw(), "memory", mod.exports.at(0).field_len) == 0 &&
137 mod.exports.at(0).kind == external_kind::Memory );
138 BOOST_CHECK( memcmp((char*)mod.exports.at(1).field_str.raw(), "__heap_base", mod.exports.at(1).field_len) == 0 &&
139 mod.exports.at(1).kind == external_kind::Global );
140 BOOST_CHECK( memcmp((char*)mod.exports.at(2).field_str.raw(), "__data_end", mod.exports.at(2).field_len) == 0 &&
141 mod.exports.at(2).kind == external_kind::Global );
142 BOOST_CHECK( memcmp((char*)mod.exports.at(3).field_str.raw(), "apply", mod.exports.at(3).field_len) == 0 &&
143 mod.exports.at(3).kind == external_kind::Function );
144 BOOST_CHECK( memcmp((char*)mod.exports.at(4).field_str.raw(), "_ZdlPv", mod.exports.at(4).field_len) == 0 &&
145 mod.exports.at(4).kind == external_kind::Function );
146 BOOST_CHECK( memcmp((char*)mod.exports.at(5).field_str.raw(), "_Znwj", mod.exports.at(5).field_len) == 0 &&
147 mod.exports.at(5).kind == external_kind::Function );
148 BOOST_CHECK( memcmp((char*)mod.exports.at(6).field_str.raw(), "_Znaj", mod.exports.at(6).field_len) == 0 &&
149 mod.exports.at(6).kind == external_kind::Function );
150 BOOST_CHECK( memcmp((char*)mod.exports.at(7).field_str.raw(), "_ZdaPv", mod.exports.at(7).field_len) == 0 &&
151 mod.exports.at(7).kind == external_kind::Function );
152
153#if 0
154 code_ptr.add_bounds( constants::id_size);
155 id = bp.parse_section_id( code_ptr );
156 BOOST_CHECK_EQUAL( id, section_id::start_section );
157
158 code_ptr.add_bounds( constants::varuint32_size );
159 len = bp.parse_section_payload_len( code_ptr );
160 code_ptr.fit_bounds();
161
162 code_ptr.add_bounds( len );
163 bp.parse_start_section( code_ptr, mod.start );
164#endif
165
166 uint32_t indices[] = {73, 60, 169, 92, 80, 124, 131, 152, 176, 177, 178, 164, 153, 122, 136, 156, 158, 83, 184, 123, 129, 185, 154, 155, 121};
167
168 for (int i=0; i < mod.elements.size(); i++) {
169 for (int j=0; j < mod.elements[i].elems.size(); j++)
170 BOOST_CHECK_EQUAL(mod.elements[i].elems[j], indices[j]);
171 }
172 }
173
174 } FC_LOG_AND_RETHROW()
175}
176BOOST_AUTO_TEST_SUITE_END()
177
#define CHECK(cond)
Definition util.h:80
#define TEST_CASE(...)
Definition catch.hpp:222
std::vector< uint8_t > wasm_code
Definition types.hpp:147