Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
max_memory_offset_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 * (func (i32.const 0) (i32.load offset=0xFFFFFFFF) drop)
16 * )
17 */
18std::vector<uint8_t> max_load_offset_wasm = {
19 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
20 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x0a,
21 0x0e, 0x01, 0x0c, 0x00, 0x41, 0x00, 0x28, 0x02, 0xff, 0xff, 0xff, 0xff,
22 0x0f, 0x1a, 0x0b
23};
24
25/*
26 * (module
27 * (memory 1)
28 * (func (i32.const 0) (i32.const 0) (i32.store offset=0xFFFFFFFF))
29 * )
30 */
31std::vector<uint8_t> max_store_offset_wasm = {
32 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
33 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x0a,
34 0x0f, 0x01, 0x0d, 0x00, 0x41, 0x00, 0x41, 0x00, 0x36, 0x02, 0xff, 0xff,
35 0xff, 0xff, 0x0f, 0x0b
36};
37
38/*
39 * (module
40 * (memory 1)
41 * (func (i32.const 0) (i32.load offset=2) drop)
42 * )
43 */
44std::vector<uint8_t> load_offset_2_wasm = {
45 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
46 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x0a,
47 0x0a, 0x01, 0x08, 0x00, 0x41, 0x00, 0x28, 0x02, 0x02, 0x1a, 0x0b
48};
49
50/*
51 * (module
52 * (memory 1)
53 * (func (i32.const 0) (i32.const 0) (i32.store offset=2))
54 * )
55 */
56std::vector<uint8_t> store_offset_2_wasm = {
57 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60,
58 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01, 0x00, 0x01, 0x0a,
59 0x0b, 0x01, 0x09, 0x00, 0x41, 0x00, 0x41, 0x00, 0x36, 0x02, 0x02, 0x0b
60};
61
62struct empty_options {};
63struct static_options_1 {
64 static constexpr std::uint32_t max_memory_offset = 1;
65};
66struct static_options_2 {
67 static constexpr std::uint32_t max_memory_offset = 2;
68};
69struct dynamic_options {
70 std::uint32_t max_memory_offset;
71};
72
73}
74
75BACKEND_TEST_CASE("Test max_memory_offset default", "[max_memory_offset_test]") {
77 backend_t backend_load_max(max_load_offset_wasm, &wa);
78 backend_t backend_store_max(max_store_offset_wasm, &wa);
79 backend_t backend_load_2(load_offset_2_wasm, &wa);
80 backend_t backend_store_2(load_offset_2_wasm, &wa);
81}
82
83BACKEND_TEST_CASE("Test max_memory_offset unlimited", "[max_memory_offset_test]") {
85 backend_t backend_load_max(max_load_offset_wasm, &wa);
86 backend_t backend_store_max(max_store_offset_wasm, &wa);
87 backend_t backend_load_2(load_offset_2_wasm, &wa);
88 backend_t backend_store_2(load_offset_2_wasm, &wa);
89}
90
91BACKEND_TEST_CASE("Test max_memory_offset static fail", "[max_memory_offset_test]") {
93 CHECK_THROWS_AS(backend_t(max_load_offset_wasm, &wa), wasm_parse_exception);
94 CHECK_THROWS_AS(backend_t(max_store_offset_wasm, &wa), wasm_parse_exception);
95 CHECK_THROWS_AS(backend_t(load_offset_2_wasm, &wa), wasm_parse_exception);
96 CHECK_THROWS_AS(backend_t(store_offset_2_wasm, &wa), wasm_parse_exception);
97}
98
99BACKEND_TEST_CASE("Test max_memory_offset static pass", "[max_memory_offset_test]") {
101 CHECK_THROWS_AS(backend_t(max_load_offset_wasm, &wa), wasm_parse_exception);
102 CHECK_THROWS_AS(backend_t(max_store_offset_wasm, &wa), wasm_parse_exception);
103 backend_t backend_load_2(load_offset_2_wasm, &wa);
104 backend_t backend_store_2(store_offset_2_wasm, &wa);
105}
106
107BACKEND_TEST_CASE("Test max_memory_offset dynamic fail", "[max_memory_offset_test]") {
109 CHECK_THROWS_AS(backend_t(max_load_offset_wasm, nullptr, dynamic_options{1}), wasm_parse_exception);
110 CHECK_THROWS_AS(backend_t(max_store_offset_wasm, nullptr, dynamic_options{1}), wasm_parse_exception);
111 CHECK_THROWS_AS(backend_t(load_offset_2_wasm, nullptr, dynamic_options{1}), wasm_parse_exception);
112 CHECK_THROWS_AS(backend_t(store_offset_2_wasm, nullptr, dynamic_options{1}), wasm_parse_exception);
113}
114
115BACKEND_TEST_CASE("Test max_memory_offset dynamic pass", "[max_memory_offset_test]") {
117 CHECK_THROWS_AS(backend_t(max_load_offset_wasm, nullptr, dynamic_options{2}), wasm_parse_exception);
118 CHECK_THROWS_AS(backend_t(max_store_offset_wasm, nullptr, dynamic_options{2}), wasm_parse_exception);
119 backend_t backend_load_2(load_offset_2_wasm, nullptr, dynamic_options{2});
120 backend_t backend_store_2(store_offset_2_wasm, nullptr, dynamic_options{2});
121}
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