Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
allow_code_after_function_end_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 0)
15 * (global i32 (i32.const 0))
16 * (func (export "apply") (param i64 i64 i64))
17 * (block unreachable)
18 * (loop unreachable)
19 * (i32.const 0)
20 * (nop)
21 * (local.get 0)
22 * (global.get 0)
23 * (memory.size)
24 * )
25 */
26std::vector<uint8_t> code_after_end_wasm = {
27 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60,
28 0x03, 0x7e, 0x7e, 0x7e, 0x00, 0x03, 0x02, 0x01, 0x00, 0x05, 0x03, 0x01,
29 0x00, 0x00, 0x06, 0x06, 0x01, 0x7f, 0x00, 0x41, 0x00, 0x0b, 0x07, 0x09,
30 0x01, 0x05, 0x61, 0x70, 0x70, 0x6c, 0x79, 0x00, 0x00, 0x0a, 0x15, 0x01,
31 0x13, 0x00, 0x0b, 0x02, 0x40, 0x00, 0x0b, 0x03, 0x40, 0x00, 0x0b, 0x41,
32 0x00, 0x01, 0x20, 0x00, 0x23, 0x00, 0x3f, 0x00
33};
34
35/*
36 * (module
37 * (memory 0)
38 * (table 1 funcref)
39 * (elem (i32.const 0) 0)
40 * (global (mut i32) (i32.const 0))
41 * (func (export "apply") (param i64 i64 i64))
42 * ...
43 * )
44 */
45std::vector<uint8_t> make_wasm(std::initializer_list<uint8_t> il) {
46 std::vector<uint8_t> result = {
47 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, 0x60,
48 0x03, 0x7e, 0x7e, 0x7e, 0x00, 0x03, 0x02, 0x01, 0x00, 0x04, 0x04, 0x01,
49 0x70, 0x00, 0x01, 0x05, 0x03, 0x01, 0x00, 0x00, 0x06, 0x06, 0x01, 0x7f,
50 0x01, 0x41, 0x00, 0x0b, 0x07, 0x09, 0x01, 0x05, 0x61, 0x70, 0x70, 0x6c,
51 0x79, 0x00, 0x00, 0x09, 0x07, 0x01, 0x00, 0x41, 0x00, 0x0b, 0x01, 0x00,
52 0x0a
53 };
54 result.push_back(il.size() + 4);
55 result.push_back(1);
56 result.push_back(il.size() + 2);
57 result.push_back(0);
58 result.push_back(0x0b);
59 result.insert(result.end(), il);
60 return result;
61}
62
63std::vector<std::vector<uint8_t>> fail_wasms = {
64 make_wasm({0x00}), // unreachable
65 make_wasm({0x0b}), // end
66 make_wasm({0x0f}), // return
67 make_wasm({0x41, 0x00, 0x04, 0x40, 0x0b}), // if
68 make_wasm({0x05}), // else
69 make_wasm({0x0C, 0x00}), // br
70 make_wasm({0x41, 0x00, 0x0D, 0x00}), // br_if
71 make_wasm({0x41, 0x00, 0x0E, 0x00, 0x00}), // br_table
72 make_wasm({0x42, 0x00, 0x42, 0x00, 0x42, 0x00, 0x10, 0x00}), // call
73 make_wasm({0x42, 0x00, 0x42, 0x00, 0x42, 0x00, 0x41, 0x00, 0x11, 0x00, 0x00}), // call_indirect
74 make_wasm({0x41, 0x00, 0x1A}), // drop
75 make_wasm({0x41, 0x00, 0x41, 0x00, 0x41, 0x00, 0x1B}), // select
76 make_wasm({0x42, 0x00, 0x21, 0x00}), // local.set
77 make_wasm({0x42, 0x00, 0x22, 0x00}), // local.tee
78 make_wasm({0x41, 0x00, 0x24, 0x00}), // global.set
79 make_wasm({0x41, 0x00, 0x28, 0x00, 0x00}), // i32.load
80 make_wasm({0x41, 0x00, 0x41, 0x41, 0x36, 0x00, 0x00}), // i32.store
81 make_wasm({0x41, 0x00, 0x40, 0x00}), // memory.grow
82 make_wasm({0x41, 0x00, 0x67}), // i32.clz
83 make_wasm({0x41, 0x00, 0x41, 0x00, 0x6a}), // i32.add
84 make_wasm({0x41, 0x00, 0xac}), // i64.extend_i32
85 make_wasm({0x25}), // illegal instruction
86 make_wasm({0xff}) // illegal instruction
87};
88
89struct empty_options {};
90struct static_options_false {
91 static constexpr bool allow_code_after_function_end = false;
92};
93struct static_options_true {
94 static constexpr bool allow_code_after_function_end = true;
95};
96struct dynamic_options {
97 bool allow_code_after_function_end;
98};
99
100}
101
102BACKEND_TEST_CASE("Test allow_code_after_function_end default", "[allow_code_after_function_end_test]") {
104 CHECK_THROWS_AS(backend_t(code_after_end_wasm, &wa), wasm_parse_exception);
105}
106
107BACKEND_TEST_CASE("Test allow_code_after_function_end unlimited", "[allow_code_after_function_end_test]") {
109 CHECK_THROWS_AS(backend_t(code_after_end_wasm, &wa), wasm_parse_exception);
110}
111
112BACKEND_TEST_CASE("Test allow_code_after_function_end static fail", "[allow_code_after_function_end_test]") {
114 CHECK_THROWS_AS(backend_t(code_after_end_wasm, &wa), wasm_parse_exception);
115}
116
117BACKEND_TEST_CASE("Test allow_code_after_function_end static pass", "[allow_code_after_function_end_test]") {
119 backend_t backend(code_after_end_wasm, &wa);
120 for(auto wasm : fail_wasms) {
121 CHECK_THROWS_AS(backend_t(wasm, &wa), wasm_parse_exception);
122 }
123}
124
125BACKEND_TEST_CASE("Test allow_code_after_function_end dynamic fail", "[allow_code_after_function_end_test]") {
127 CHECK_THROWS_AS(backend_t(code_after_end_wasm, nullptr, dynamic_options{false}), wasm_parse_exception);
128}
129
130BACKEND_TEST_CASE("Test allow_code_after_function_end dynamic pass", "[allow_code_after_function_end_test]") {
132 backend_t backend(code_after_end_wasm, nullptr, dynamic_options{true});
133 for(auto wasm : fail_wasms) {
134 CHECK_THROWS_AS(backend_t(wasm, nullptr, dynamic_options{true}), wasm_parse_exception);
135 }
136}
wasm_allocator wa
Definition main.cpp:10
sysio::vm::backend< rhf_t > backend_t
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
#define BACKEND_TEST_CASE(name, tags)
Definition utils.hpp:59