Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
max_func_local_bytes_tests.cpp
Go to the documentation of this file.
1#include <sysio/vm/backend.hpp>
2
3#include "utils.hpp"
4#include <catch2/catch.hpp>
5
6using namespace sysio::vm;
7
8extern wasm_allocator wa;
9
10namespace {
11
12/*
13 * (module
14 * (type (func (param i64 i64)))
15 * )
16 */
17std::vector<uint8_t> unused_type_wasm = {
18 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60,
19 0x02, 0x7e, 0x7e, 0x00
20};
21
22/*
23 * (module
24 * (func (param i64 i64))
25 * )
26 */
27
28std::vector<uint8_t> param_16_wasm = {
29 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60,
30 0x02, 0x7e, 0x7e, 0x00, 0x03, 0x02, 0x01, 0x00, 0x0a, 0x04, 0x01, 0x02,
31 0x00, 0x0b
32};
33
34/*
35 * (module
36 * (func (local i64 i64))
37 * )
38 */
39
40std::vector<uint8_t> local_16_wasm = {
41 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
42 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x0a, 0x06, 0x01, 0x04, 0x01, 0x02,
43 0x7e, 0x0b
44};
45
46/*
47 * (module
48 * (func
49 * (i64.const 0)
50 * (i64.const 0)
51 * (drop)
52 * (i32.const 0)
53 * (i32.const 0)
54 * (drop)
55 * (drop)
56 * (drop)
57 * (return)
58 * (block
59 * (i64.const 0)
60 * (i64.const 0)
61 * (i64.const 0)
62 * (return)
63 * )
64 * )
65 * )
66 */
67
68std::vector<uint8_t> stack_16_wasm = {
69 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
70 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x0a, 0x1b, 0x01, 0x19, 0x00, 0x42,
71 0x00, 0x42, 0x00, 0x1a, 0x41, 0x00, 0x41, 0x00, 0x1a, 0x1a, 0x1a, 0x0f,
72 0x02, 0x40, 0x42, 0x00, 0x42, 0x00, 0x42, 0x00, 0x0f, 0x0b, 0x0b
73};
74
75/*
76 * (module
77 * (func (param i64) (local i64))
78 * )
79 */
80
81std::vector<uint8_t> mixed_16_wasm = {
82 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x05, 0x01, 0x60,
83 0x01, 0x7e, 0x00, 0x03, 0x02, 0x01, 0x00, 0x0a, 0x06, 0x01, 0x04, 0x01,
84 0x01, 0x7e, 0x0b
85};
86
87constexpr auto lp = max_func_local_bytes_flags_t::params | max_func_local_bytes_flags_t::locals;
88constexpr auto ps = max_func_local_bytes_flags_t::params | max_func_local_bytes_flags_t::stack;
89constexpr auto ls = max_func_local_bytes_flags_t::locals | max_func_local_bytes_flags_t::stack;
90constexpr auto lps = max_func_local_bytes_flags_t::params | max_func_local_bytes_flags_t::locals | max_func_local_bytes_flags_t::stack;
91
92struct empty_options {};
93template<max_func_local_bytes_flags_t F>
94struct empty_options_static_flags {
95 static constexpr auto max_func_local_bytes_flags = F;
96};
97struct empty_options_dynamic_flags {
98 max_func_local_bytes_flags_t max_func_local_bytes_flags;
99};
100template<int N, max_func_local_bytes_flags_t F>
101struct static_options {
102 static constexpr std::uint32_t max_func_local_bytes = N;
103 static constexpr auto max_func_local_bytes_flags = F;
104};
105template<int N>
106struct static_options_empty_flags {
107 static constexpr std::uint32_t max_func_local_bytes = N;
108};
109struct dynamic_options {
110 std::uint32_t max_func_local_bytes;
111 max_func_local_bytes_flags_t max_func_local_bytes_flags;
112};
113struct dynamic_options_empty_flags {
114 std::uint32_t max_func_local_bytes;
115};
116
117}
118
119BACKEND_TEST_CASE("Test max_func_local_bytes default", "[max_func_local_bytes_test]") {
121 backend_t backend_unused(unused_type_wasm, &wa);
122 backend_t backend_param(param_16_wasm, &wa);
123 backend_t backend_local(local_16_wasm, &wa);
124 backend_t backend_stack(stack_16_wasm, &wa);
125 backend_t backend_mixed(mixed_16_wasm, &wa);
126}
127
128BACKEND_TEST_CASE("Test max_func_local_bytes unlimited", "[max_func_local_bytes_test]") {
129 {
131 backend_t backend_unused(unused_type_wasm, &wa);
132 backend_t backend_param(param_16_wasm, &wa);
133 backend_t backend_local(local_16_wasm, &wa);
134 backend_t backend_stack(stack_16_wasm, &wa);
135 backend_t backend_mixed(mixed_16_wasm, &wa);
136 }
137 {
139 backend_t backend_unused(unused_type_wasm, &wa);
140 backend_t backend_param(param_16_wasm, &wa);
141 backend_t backend_local(local_16_wasm, &wa);
142 backend_t backend_stack(stack_16_wasm, &wa);
143 backend_t backend_mixed(mixed_16_wasm, &wa);
144 }
145 {
147 backend_t backend_unused(unused_type_wasm, &wa);
148 backend_t backend_param(param_16_wasm, &wa);
149 backend_t backend_local(local_16_wasm, &wa);
150 backend_t backend_stack(stack_16_wasm, &wa);
151 backend_t backend_mixed(mixed_16_wasm, &wa);
152 }
153 {
155 backend_t backend_unused(unused_type_wasm, &wa);
156 backend_t backend_param(param_16_wasm, &wa);
157 backend_t backend_local(local_16_wasm, &wa);
158 backend_t backend_stack(stack_16_wasm, &wa);
159 backend_t backend_mixed(mixed_16_wasm, &wa);
160 }
161 {
163 backend_t backend_unused(unused_type_wasm, nullptr, empty_options_dynamic_flags{lps});
164 backend_t backend_param(param_16_wasm, nullptr, empty_options_dynamic_flags{lps});
165 backend_t backend_local(local_16_wasm, nullptr, empty_options_dynamic_flags{lps});
166 backend_t backend_stack(stack_16_wasm, nullptr, empty_options_dynamic_flags{lps});
167 backend_t backend_mixed(mixed_16_wasm, nullptr, empty_options_dynamic_flags{lps});
168 }
169}
170
171BACKEND_TEST_CASE("Test max_func_local_bytes static fail", "[max_func_local_bytes_test]") {
172 {
174 backend_t backend_unused(unused_type_wasm, &wa);
175 CHECK_THROWS_AS(backend_t(param_16_wasm, &wa), wasm_parse_exception);
176 CHECK_THROWS_AS(backend_t(local_16_wasm, &wa), wasm_parse_exception);
177 backend_t backend_stack(stack_16_wasm, &wa);
178 CHECK_THROWS_AS(backend_t(mixed_16_wasm, &wa), wasm_parse_exception);
179 }
180 {
182 backend_t backend_param(param_16_wasm, &wa);
183 CHECK_THROWS_AS(backend_t(local_16_wasm, &wa), wasm_parse_exception);
184 CHECK_THROWS_AS(backend_t(stack_16_wasm, &wa), wasm_parse_exception);
185 }
186 {
188 CHECK_THROWS_AS(backend_t(param_16_wasm, &wa), wasm_parse_exception);
189 backend_t backend_local(local_16_wasm, &wa);
190 CHECK_THROWS_AS(backend_t(stack_16_wasm, &wa), wasm_parse_exception);
191 }
192 {
194 backend_t backend_param(param_16_wasm, &wa);
195 CHECK_THROWS_AS(backend_t(local_16_wasm, &wa), wasm_parse_exception);
196 CHECK_THROWS_AS(backend_t(stack_16_wasm, &wa), wasm_parse_exception);
197 }
198}
199
200BACKEND_TEST_CASE("Test max_func_local_bytes static pass", "[max_func_local_bytes_test]") {
201 {
203 backend_t backend_unused(unused_type_wasm, &wa);
204 backend_t backend_param(param_16_wasm, &wa);
205 backend_t backend_local(local_16_wasm, &wa);
206 backend_t backend_stack(stack_16_wasm, &wa);
207 backend_t backend_mixed(mixed_16_wasm, &wa);
208 }
209 {
211 backend_t backend_param(param_16_wasm, &wa);
212 backend_t backend_local(local_16_wasm, &wa);
213 backend_t backend_stack(stack_16_wasm, &wa);
214 }
215 {
217 backend_t backend_param(param_16_wasm, &wa);
218 backend_t backend_local(local_16_wasm, &wa);
219 backend_t backend_stack(stack_16_wasm, &wa);
220 }
221 {
223 backend_t backend_param(param_16_wasm, &wa);
224 backend_t backend_local(local_16_wasm, &wa);
225 backend_t backend_stack(stack_16_wasm, &wa);
226 }
227}
228
229BACKEND_TEST_CASE("Test max_func_local_bytes dynamic fail", "[max_func_local_bytes_test]") {
230 {
232 CHECK_THROWS_AS(backend_t(param_16_wasm, nullptr, dynamic_options{8, lp}), wasm_parse_exception);
233 CHECK_THROWS_AS(backend_t(param_16_wasm, nullptr, dynamic_options{8, ps}), wasm_parse_exception);
234 CHECK_THROWS_AS(backend_t(local_16_wasm, nullptr, dynamic_options{8, lp}), wasm_parse_exception);
235 CHECK_THROWS_AS(backend_t(local_16_wasm, nullptr, dynamic_options{8, ls}), wasm_parse_exception);
236 CHECK_THROWS_AS(backend_t(stack_16_wasm, nullptr, dynamic_options{8, ls}), wasm_parse_exception);
237 CHECK_THROWS_AS(backend_t(stack_16_wasm, nullptr, dynamic_options{8, ps}), wasm_parse_exception);
238 CHECK_THROWS_AS(backend_t(mixed_16_wasm, nullptr, dynamic_options{8, lp}), wasm_parse_exception);
239 }
240
241 {
243 CHECK_THROWS_AS(backend_t(local_16_wasm, nullptr, dynamic_options_empty_flags{8}), wasm_parse_exception);
244 CHECK_THROWS_AS(backend_t(stack_16_wasm, nullptr, dynamic_options_empty_flags{8}), wasm_parse_exception);
245 }
246}
247
248BACKEND_TEST_CASE("Test max_func_local_bytes dynamic pass", "[max_func_local_bytes_test]") {
249 {
251 backend_t backend_unused(unused_type_wasm, nullptr, dynamic_options{8, lp});
252
253 backend_t backend_param1(param_16_wasm, nullptr, dynamic_options{16, lp});
254 backend_t backend_param2(param_16_wasm, nullptr, dynamic_options{8, ls});
255 backend_t backend_param3(param_16_wasm, nullptr, dynamic_options{16, ps});
256 backend_t backend_local1(local_16_wasm, nullptr, dynamic_options{16, lp});
257 backend_t backend_local2(local_16_wasm, nullptr, dynamic_options{16, ls});
258 backend_t backend_local3(local_16_wasm, nullptr, dynamic_options{8, ps});
259 backend_t backend_stack1(stack_16_wasm, nullptr, dynamic_options{8, lp});
260 backend_t backend_stack3(stack_16_wasm, nullptr, dynamic_options{16, ps});
261
262 backend_t backend_mixed(mixed_16_wasm, nullptr, dynamic_options{16, lp});
263 }
264 {
266 backend_t backend_param2(param_16_wasm, nullptr, dynamic_options_empty_flags{8});
267 backend_t backend_local2(local_16_wasm, nullptr, dynamic_options_empty_flags{16});
268 backend_t backend_stack2(stack_16_wasm, nullptr, dynamic_options_empty_flags{16});
269 }
270}
sysio::vm::backend< rhf_t > backend_t
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
wasm_allocator wa
Definition main.cpp:10
max_func_local_bytes_flags_t
Definition options.hpp:7
const int N
Definition quantize.cpp:54
#define BACKEND_TEST_CASE(name, tags)
Definition utils.hpp:59