Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
implementation_limits_tests.cpp
Go to the documentation of this file.
1#include <algorithm>
2#include <vector>
3#include <iterator>
4#include <cstdlib>
5#include <fstream>
6#include <string>
7
8#include <catch2/catch.hpp>
9
10#include <sysio/vm/backend.hpp>
11#include "wasm_config.hpp"
12#include "utils.hpp"
13
14using namespace sysio;
15using namespace sysio::vm;
16
17void host_call() {}
18
20
21namespace {
22
23wasm_code implementation_limits_wasm_code{
26
27struct dynamic_options {
28 std::uint32_t max_call_depth;
29};
30
31}
32
33BACKEND_TEST_CASE( "Test call depth", "[call_depth]") {
37
38 rhf_t::add<&host_call>("env", "host.call");
39
40 backend_t bkend(implementation_limits_wasm_code, get_wasm_allocator());
41
43
44 CHECK(!bkend.call_with_return("env", "call", (uint32_t)250));
45 CHECK_THROWS_AS(bkend.call("env", "call", (uint32_t)251), std::exception);
46 CHECK(!bkend.call_with_return("env", "call.indirect", (uint32_t)250));
47 CHECK_THROWS_AS(bkend.call("env", "call.indirect", (uint32_t)251), std::exception);
48 // The host call is added to the recursive function, so we have one fewer frames
49 CHECK(!bkend.call_with_return("env", "call.host", (uint32_t)249));
50 CHECK_THROWS_AS(bkend.call("env", "call.host", (uint32_t)250), std::exception);
51 CHECK(!bkend.call_with_return("env", "call.indirect.host", (uint32_t)249));
52 CHECK_THROWS_AS(bkend.call("env", "call.indirect.host", (uint32_t)250), std::exception);
53}
54
55BACKEND_TEST_CASE( "Test call depth dynamic", "[call_depth]") {
59 rhf_t::add<&host_call>("env", "host.call");
60
61 backend_t bkend(implementation_limits_wasm_code, nullptr, dynamic_options{151});
63 bkend.initialize(nullptr);
64
66
67 CHECK(!bkend.call_with_return("env", "call", (uint32_t)150));
68 CHECK_THROWS_AS(bkend.call("env", "call", (uint32_t)151), std::exception);
69 CHECK(!bkend.call_with_return("env", "call.indirect", (uint32_t)150));
70 CHECK_THROWS_AS(bkend.call("env", "call.indirect", (uint32_t)151), std::exception);
71 // The host call is added to the recursive function, so we have one fewer frames
72 CHECK(!bkend.call_with_return("env", "call.host", (uint32_t)149));
73 CHECK_THROWS_AS(bkend.call("env", "call.host", (uint32_t)150), std::exception);
74 CHECK(!bkend.call_with_return("env", "call.indirect.host", (uint32_t)149));
75 CHECK_THROWS_AS(bkend.call("env", "call.indirect.host", (uint32_t)150), std::exception);
76
77 bkend.initialize(nullptr, dynamic_options{51});
78
79 CHECK(!bkend.call_with_return("env", "call", (uint32_t)50));
80 CHECK_THROWS_AS(bkend.call("env", "call", (uint32_t)51), std::exception);
81 CHECK(!bkend.call_with_return("env", "call.indirect", (uint32_t)50));
82 CHECK_THROWS_AS(bkend.call("env", "call.indirect", (uint32_t)51), std::exception);
83 // The host call is added to the recursive function, so we have one fewer frames
84 CHECK(!bkend.call_with_return("env", "call.host", (uint32_t)49));
85 CHECK_THROWS_AS(bkend.call("env", "call.host", (uint32_t)50), std::exception);
86 CHECK(!bkend.call_with_return("env", "call.indirect.host", (uint32_t)49));
87 CHECK_THROWS_AS(bkend.call("env", "call.indirect.host", (uint32_t)50), std::exception);
88
89 // Very large call depth requires dynamically allocating a new stack
90 bkend.initialize(nullptr, dynamic_options{1024*1024});
91
92 CHECK(!bkend.call_with_return("env", "call", (uint32_t)1024*1024 - 1));
93}
wasm_allocator wa
Definition main.cpp:10
bool call(host_t *host, uint32_t func_index, Args... args)
Definition backend.hpp:148
backend & initialize(host_t *host, const Options &new_options)
Definition backend.hpp:118
auto call_with_return(host_t &host, const std::string_view &mod, const std::string_view &func, Args... args)
Definition backend.hpp:178
module & get_module()
Definition backend.hpp:248
void set_wasm_allocator(wasm_allocator *alloc)
Definition backend.hpp:242
#define CHECK(cond)
Definition util.h:80
backend_t bkend(hello_wasm, ehm, &wa)
unsigned char implementation_limits_wasm[]
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
std::vector< uint8_t > wasm_code
Definition types.hpp:147
unsigned int uint32_t
Definition stdint.h:126
static void add(const std::string &mod, const std::string &name)
#define BACKEND_TEST_CASE(name, tags)
Definition utils.hpp:59
sysio::vm::wasm_allocator * get_wasm_allocator()
Definition utils.hpp:50