Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
max_linear_memory_init_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 * (memory 1)
15 * (data (i32.const 0) "abcdefghijkl")
16 * )
17 */
18
19std::vector<uint8_t> full_12_data_wasm = {
20 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00,
21 0x01, 0x0b, 0x12, 0x01, 0x00, 0x41, 0x00, 0x0b, 0x0c, 0x61, 0x62, 0x63,
22 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c
23};
24
25/*
26 * (module
27 * (memory 1)
28 * (data (i32.const 12) "")
29 * )
30 */
31
32std::vector<uint8_t> empty_12_data_wasm = {
33 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00,
34 0x01, 0x0b, 0x06, 0x01, 0x00, 0x41, 0x0c, 0x0b, 0x00
35};
36
37/*
38 * (module
39 * (memory 1)
40 * (data (i32.const 13) "")
41 * )
42 */
43
44std::vector<uint8_t> empty_13_data_wasm = {
45 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00,
46 0x01, 0x0b, 0x06, 0x01, 0x00, 0x41, 0x0d, 0x0b, 0x00
47};
48
49/*
50 * (module
51 * (memory 1)
52 * (data (i32.const 11) "a")
53 * )
54 */
55
56std::vector<uint8_t> one_11_data_wasm = {
57 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00,
58 0x01, 0x0b, 0x07, 0x01, 0x00, 0x41, 0x0b, 0x0b, 0x01, 0x61
59};
60
61/*
62 * (module
63 * (memory 1)
64 * (data (i32.const 12) "a")
65 * )
66 */
67
68std::vector<uint8_t> one_12_data_wasm = {
69 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00,
70 0x01, 0x0b, 0x07, 0x01, 0x00, 0x41, 0x0c, 0x0b, 0x01, 0x61
71};
72
73/*
74 * (module
75 * (memory 1)
76 * (data (i32.const 0xFFFFFFFF) "ab")
77 * )
78 */
79
80std::vector<uint8_t> wrap_data_wasm = {
81 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x05, 0x03, 0x01, 0x00,
82 0x01, 0x0b, 0x08, 0x01, 0x00, 0x41, 0x7f, 0x0b, 0x02, 0x61, 0x62
83};
84
85struct empty_options {};
86struct dynamic_options {
87 std::uint32_t max_linear_memory_init;
88};
89struct static_options {
90 static constexpr std::uint32_t max_linear_memory_init = 12;
91};
92
93}
94
95BACKEND_TEST_CASE("Test max_linear_memory_init default", "[max_linear_memory_init_test]") {
97 backend_t backendfull12(full_12_data_wasm, &wa);
98 backend_t backendempty12(empty_12_data_wasm, &wa);
99 backend_t backendempty13(empty_13_data_wasm, &wa);
100 backend_t backendone11(one_11_data_wasm, &wa);
101 backend_t backendone12(one_12_data_wasm, &wa);
102 // This is well formed but should fail linking
103 CHECK_THROWS_AS(backend_t(wrap_data_wasm, &wa), wasm_memory_exception);
104}
105
106BACKEND_TEST_CASE("Test max_linear_memory_init static", "[max_linear_memory_init_test]") {
108 backend_t backendfull12(full_12_data_wasm, &wa);
109 backend_t backendempty12(empty_12_data_wasm, &wa);
110 CHECK_THROWS_AS(backend_t(empty_13_data_wasm, &wa), wasm_parse_exception);
111 backend_t backendone11(one_11_data_wasm, &wa);
112 CHECK_THROWS_AS(backend_t(one_12_data_wasm, &wa), wasm_parse_exception);
113 CHECK_THROWS_AS(backend_t(wrap_data_wasm, &wa), wasm_parse_exception);
114}
115
116BACKEND_TEST_CASE("Test max_linear_memory_init unlimited", "[max_linear_memory_init_test]") {
118 backend_t backendfull12(full_12_data_wasm, &wa);
119 backend_t backendempty12(empty_12_data_wasm, &wa);
120 backend_t backendempty13(empty_13_data_wasm, &wa);
121 backend_t backendone11(one_11_data_wasm, &wa);
122 backend_t backendone12(one_12_data_wasm, &wa);
123 CHECK_THROWS_AS(backend_t(wrap_data_wasm, &wa), wasm_memory_exception);
124}
125
126BACKEND_TEST_CASE("Test max_linear_memory_init dynamic", "[max_linear_memory_init_test]") {
128 backend_t backendfull12(full_12_data_wasm, nullptr, dynamic_options{12});
129 backend_t backendempty12(empty_12_data_wasm, nullptr, dynamic_options{12});
130 CHECK_THROWS_AS(backend_t(empty_13_data_wasm, nullptr, dynamic_options{12}), wasm_parse_exception);
131 backend_t backendone11(one_11_data_wasm, nullptr, dynamic_options{12});
132 CHECK_THROWS_AS(backend_t(one_12_data_wasm, nullptr, dynamic_options{12}), wasm_parse_exception);
133 CHECK_THROWS_AS(backend_t(wrap_data_wasm, nullptr, dynamic_options{12}), wasm_parse_exception);
134}
sysio::vm::backend< rhf_t > backend_t
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
wasm_allocator wa
Definition main.cpp:10
#define BACKEND_TEST_CASE(name, tags)
Definition utils.hpp:59