Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
max_section_elements_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))
15 * (type (func (result i32)))
16 * )
17 */
18std::vector<uint8_t> types_2_wasm = {
19 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x60,
20 0x00, 0x00, 0x60, 0x00, 0x01, 0x7f
21};
22
23/*
24 * (module
25 * (func (import "env" "f0"))
26 * (func (import "env" "f1"))
27 * )
28 */
29std::vector<uint8_t> imports_2_wasm = {
30 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
31 0x00, 0x00, 0x02, 0x13, 0x02, 0x03, 0x65, 0x6e, 0x76, 0x02, 0x66, 0x30,
32 0x00, 0x00, 0x03, 0x65, 0x6e, 0x76, 0x02, 0x66, 0x31, 0x00, 0x00
33};
34
35/*
36 * (module
37 * (func)
38 * (func)
39 * )
40 */
41std::vector<uint8_t> functions_2_wasm = {
42 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
43 0x00, 0x00, 0x03, 0x03, 0x02, 0x00, 0x00, 0x0a, 0x07, 0x02, 0x02, 0x00,
44 0x0b, 0x02, 0x00, 0x0b
45};
46
47/*
48 * (module
49 * (global i32 (i32.const 0))
50 * (global i32 (i32.const 0))
51 * )
52 */
53std::vector<uint8_t> globals_2_wasm = {
54 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x06, 0x0b, 0x02, 0x7f,
55 0x00, 0x41, 0x00, 0x0b, 0x7f, 0x00, 0x41, 0x00, 0x0b
56};
57
58/*
59 * (module
60 * (func (export "f1"))
61 * (func (export "f2"))
62 * )
63 */
64std::vector<uint8_t> exports_2_wasm = {
65 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
66 0x00, 0x00, 0x03, 0x03, 0x02, 0x00, 0x00, 0x07, 0x0b, 0x02, 0x02, 0x66,
67 0x31, 0x00, 0x00, 0x02, 0x66, 0x32, 0x00, 0x01, 0x0a, 0x07, 0x02, 0x02,
68 0x00, 0x0b, 0x02, 0x00, 0x0b
69};
70
71/*
72 * (module
73 * (table 1 anyfunc)
74 * (elem (i32.const 0))
75 * (elem (i32.const 0))
76 * )
77 */
78std::vector<uint8_t> elems_2_wasm = {
79 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x04, 0x04, 0x01, 0x70,
80 0x00, 0x01, 0x09, 0x0b, 0x02, 0x00, 0x41, 0x00, 0x0b, 0x00, 0x00, 0x41,
81 0x00, 0x0b, 0x00
82};
83
84/*
85 * (module
86 * (memory 1)
87 * (data (i32.const 0) "")
88 * (data (i32.const 0) "")
89 * )
90 */
91std::vector<uint8_t> data_2_wasm = {
92 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00,
93 0x01, 0x0b, 0x0b, 0x02, 0x00, 0x41, 0x00, 0x0b, 0x00, 0x00, 0x41, 0x00,
94 0x0b, 0x00
95};
96
97struct empty_options {};
98struct dynamic_options {
99 std::uint32_t max_section_elements;
100};
101struct static_options_1 {
102 static constexpr std::uint32_t max_section_elements = 1;
103};
104struct static_options_2 {
105 static constexpr std::uint32_t max_section_elements = 2;
106};
107}
108
109#define SECTION_TEST_CASE_DEFS(name) \
110namespace { \
111struct name ## _dynamic_options { \
112 std::uint32_t max_ ## name ## _section_elements; \
113}; \
114struct name ## _static_options_1 { \
115 static constexpr std::uint32_t max_ ## name ## _section_elements = 1; \
116}; \
117struct name ## _static_options_2 { \
118 static constexpr std::uint32_t max_ ## name ## _section_elements = 2; \
119}; \
120}
121
122#define SECTION_TEST_CASE_IMPL(name, wasm) \
123BACKEND_TEST_CASE("Test max_" #name "_section_elements", "[max_section_elements_test]") { \
124 backend<std::nullptr_t, TestType> backend_default(wasm, &wa); \
125 backend<std::nullptr_t, TestType, empty_options> backend_empty(wasm, &wa); \
126 CHECK_THROWS_AS((backend<std::nullptr_t, TestType, static_options_1>(wasm, &wa)), wasm_parse_exception); \
127 backend<std::nullptr_t, TestType, static_options_2> backend_static_fallback(wasm, &wa); \
128 CHECK_THROWS_AS((backend<std::nullptr_t, TestType, dynamic_options>(wasm, nullptr, {1})), wasm_parse_exception); \
129 backend<std::nullptr_t, TestType, dynamic_options> backend_dynamic_fallback(wasm, nullptr, {2}); \
130 \
131 CHECK_THROWS_AS((backend<std::nullptr_t, TestType, name ## _static_options_1>(wasm, &wa)), wasm_parse_exception); \
132 backend<std::nullptr_t, TestType, name ## _static_options_2> backend_static(wasm, &wa); \
133 CHECK_THROWS_AS((backend<std::nullptr_t, TestType, name ## _dynamic_options>(wasm, nullptr, {1})), wasm_parse_exception); \
134 backend<std::nullptr_t, TestType, name ## _dynamic_options> backend_dynamic(wasm, nullptr, {2}); \
135}
136
137#define SECTION_TEST_CASE(name, wasm) \
138 SECTION_TEST_CASE_DEFS(name) \
139 SECTION_TEST_CASE_IMPL(name, wasm)
140
141SECTION_TEST_CASE(type, types_2_wasm)
142SECTION_TEST_CASE(import, imports_2_wasm)
143SECTION_TEST_CASE(function, functions_2_wasm)
144SECTION_TEST_CASE(global, globals_2_wasm)
145SECTION_TEST_CASE(export, exports_2_wasm)
146SECTION_TEST_CASE(element, elems_2_wasm)
147SECTION_TEST_CASE(data, data_2_wasm)
#define SECTION_TEST_CASE(name, wasm)
wasm_allocator wa
Definition main.cpp:10