Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
host_functions_tests.cpp File Reference
#include <algorithm>
#include <vector>
#include <iterator>
#include <cstdlib>
#include <fstream>
#include <string>
#include <catch2/catch.hpp>
#include <sysio/vm/backend.hpp>
#include "wasm_config.hpp"
#include "utils.hpp"
#include "host_functions_tests_0.wasm.hpp"
#include "host_functions_tests_1.wasm.hpp"
Include dependency graph for host_functions_tests.cpp:

Go to the source code of this file.

Classes

struct  state_t
 
struct  init_backend< Functions, Host, Impl >
 
struct  my_host_functions
 
struct  has_stateful_conversion
 
struct  stateful_conversion
 
struct  host_functions_stateful_converter
 
struct  stateful_cnv
 
struct  test_exception
 
struct  host_functions_throw
 
struct  host_functions_exit< Impl >
 

Functions

void c_style_host_function_0 ()
 
void c_style_host_function_1 (int s)
 
void c_style_host_function_2 (int a, int b)
 
void c_style_host_function_3 (int a, float b)
 
void c_style_host_function_4 (const state_t &ss)
 
int next_ptr_offset ()
 
template<typename T , typename B >
void check_put (B &bkend, const std::string &name)
 
template<typename T , typename B >
void check_put_ptr (B &bkend, const std::string &name)
 
template<typename T , typename B >
void check_put_ref (B &bkend, const std::string &name)
 
template<typename T , typename B , typename F >
void check_get (B &bkend, const std::string &name, F getter)
 
template<typename T , typename B >
void check_get_ptr (B &bkend, const std::string &name)
 
template<typename T , typename B >
void check_get_ref (B &bkend, const std::string &name)
 
template<class Backend >
void test_parameters (Backend &&bkend)
 
 BACKEND_TEST_CASE ("Test host function parameters", "[host_functions_parameters]")
 
template<class Backend >
void test_results (Backend &&bkend)
 
 BACKEND_TEST_CASE ("Test host function results", "[host_functions_results]")
 
 BACKEND_TEST_CASE ("Test C-style host function system", "[C-style_host_functions_tests]")
 
 BACKEND_TEST_CASE ("Testing host functions", "[host_functions_test]")
 
 BACKEND_TEST_CASE ("Testing stateful ", "[host_functions_stateful_converter]")
 
 BACKEND_TEST_CASE ("Testing throwing host functions", "[host_functions_throw_test]")
 
 BACKEND_TEST_CASE ("Testing exiting host functions", "[host_functions_exit_test]")
 

Variables

int c_style_host_function_state = 0
 
wasm_code host_functions_tests_1_code
 
const std::vector< std::string > fun_prefixes = { "", "call.", "call_indirect." }
 
template<typename T >
std::vector< Ttest_values = { 0, 1, std::numeric_limits<T>::min(), std::numeric_limits<T>::max() }
 
template<>
std::vector< bool > test_values< bool > = { true, false }
 
wasm_allocator wa
 

Function Documentation

◆ BACKEND_TEST_CASE() [1/7]

BACKEND_TEST_CASE ( "Test C-style host function system" ,
"" [C-style_host_functions_tests] )

Definition at line 350 of file host_functions_tests.cpp.

350 {
354 rhf_t::add<&c_style_host_function_0>("env", "c_style_host_function_0");
355 rhf_t::add<&c_style_host_function_1>("env", "c_style_host_function_1");
356 rhf_t::add<&c_style_host_function_2>("env", "c_style_host_function_2");
357 rhf_t::add<&c_style_host_function_3>("env", "c_style_host_function_3");
358 rhf_t::add<&c_style_host_function_4>("env", "c_style_host_function_4");
359
361
362 bkend.call("env", "apply", (uint64_t)0, (uint64_t)0, (uint64_t)0);
364
365 bkend.call("env", "apply", (uint64_t)1, (uint64_t)2, (uint64_t)0);
367
368 bkend.call("env", "apply", (uint64_t)2, (uint64_t)1, (uint64_t)2);
370
371 float f = 2.4f;
372 bkend.call("env", "apply", (uint64_t)3, (uint64_t)2, (uint64_t)bit_cast<uint32_t>(f));
373 CHECK(c_style_host_function_state == 0x40199980);
374
375 bkend.call("env", "apply", (uint64_t)4, (uint64_t)5, (uint64_t)bit_cast<uint32_t>(f));
377}
bool call(host_t *host, uint32_t func_index, Args... args)
Definition backend.hpp:148
#define CHECK(cond)
Definition util.h:80
backend_t bkend(hello_wasm, ehm, &wa)
wasm_allocator wa
Definition main.cpp:10
int c_style_host_function_state
std::vector< uint8_t > host_functions_test_0_wasm
unsigned __int64 uint64_t
Definition stdint.h:136
static void add(const std::string &mod, const std::string &name)
T bit_cast(const U &u)
Definition utils.hpp:35
Here is the call graph for this function:

◆ BACKEND_TEST_CASE() [2/7]

BACKEND_TEST_CASE ( "Test host function parameters" ,
"" [host_functions_parameters] )

Definition at line 313 of file host_functions_tests.cpp.

Here is the call graph for this function:

◆ BACKEND_TEST_CASE() [3/7]

BACKEND_TEST_CASE ( "Test host function results" ,
"" [host_functions_results] )

Definition at line 344 of file host_functions_tests.cpp.

Here is the call graph for this function:

◆ BACKEND_TEST_CASE() [4/7]

BACKEND_TEST_CASE ( "Testing exiting host functions" ,
"" [host_functions_exit_test] )

Definition at line 540 of file host_functions_tests.cpp.

540 {
542 rhf_t::template add<&host_functions_exit<TestType>::test>("host", "test");
543 rhf_t::template add<&host_functions_exit<TestType>::test>("host", "test2");
544
546
547 auto code = read_wasm( host_wasm );
548 backend_t bkend( code, &wa );
550
551 CHECK(!bkend.call_with_return(host, "env", "test", UINT32_C(2)));
552}
auto & get_context()
Definition backend.hpp:250
auto call_with_return(host_t &host, const std::string_view &mod, const std::string_view &func, Args... args)
Definition backend.hpp:178
std::vector< uint8_t > read_wasm(const std::string &fname)
Definition utils.hpp:30
#define UINT32_C(val)
Definition stdint.h:283
Here is the call graph for this function:

◆ BACKEND_TEST_CASE() [5/7]

BACKEND_TEST_CASE ( "Testing host functions" ,
"" [host_functions_test] )

Definition at line 386 of file host_functions_tests.cpp.

386 {
391
393
394 auto code = read_wasm( host_wasm );
395 backend_t bkend( code, &wa );
396
397 CHECK(bkend.call_with_return("env", "test", UINT32_C(5))->to_i32() == 49);
398 CHECK(bkend.call_with_return("env", "test.indirect", UINT32_C(5), UINT32_C(0))->to_i32() == 47);
399 CHECK(bkend.call_with_return("env", "test.indirect", UINT32_C(5), UINT32_C(1))->to_i32() == 210);
400 CHECK(bkend.call_with_return("env", "test.indirect", UINT32_C(5), UINT32_C(2))->to_i32() == 49);
401 CHECK_THROWS_AS(bkend.call("env", "test.indirect", UINT32_C(5), UINT32_C(3)), std::exception);
402 CHECK(bkend.call_with_return("env", "test.local-call", UINT32_C(5))->to_i32() == 147);
403}
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
schedule config_dir_name data_dir_name p2p_port http_port file_size name host(p2p_endpoint)) FC_REFLECT(tn_node_def
Here is the call graph for this function:

◆ BACKEND_TEST_CASE() [6/7]

BACKEND_TEST_CASE ( "Testing stateful " ,
"" [host_functions_stateful_converter] )

Definition at line 430 of file host_functions_tests.cpp.

430 {
435
437
438 auto code = read_wasm( host_wasm );
439 backend_t bkend( code, &wa );
440
441 CHECK(bkend.call_with_return("env", "test", UINT32_C(5))->to_i32() == 49);
442 CHECK(bkend.call_with_return("env", "test.indirect", UINT32_C(5), UINT32_C(0))->to_i32() == 47);
443 CHECK(bkend.call_with_return("env", "test.indirect", UINT32_C(5), UINT32_C(1))->to_i32() == 210);
444 CHECK(bkend.call_with_return("env", "test.indirect", UINT32_C(5), UINT32_C(2))->to_i32() == 49);
445 CHECK_THROWS_AS(bkend.call("env", "test.indirect", UINT32_C(5), UINT32_C(3)), std::exception);
446 CHECK(bkend.call_with_return("env", "test.local-call", UINT32_C(5))->to_i32() == 147);
447}
Here is the call graph for this function:

◆ BACKEND_TEST_CASE() [7/7]

BACKEND_TEST_CASE ( "Testing throwing host functions" ,
"" [host_functions_throw_test] )

Definition at line 520 of file host_functions_tests.cpp.

520 {
525
527
528 auto code = read_wasm( host_wasm );
529 backend_t bkend( code, &wa );;
530
531 CHECK_THROWS_AS(bkend.call("env", "test", UINT32_C(2)), test_exception);
532}
Here is the call graph for this function:

◆ c_style_host_function_0()

void c_style_host_function_0 ( )

Definition at line 57 of file host_functions_tests.cpp.

57 {
59}

◆ c_style_host_function_1()

void c_style_host_function_1 ( int s)

Definition at line 60 of file host_functions_tests.cpp.

60 {
62}
char * s

◆ c_style_host_function_2()

void c_style_host_function_2 ( int a,
int b )

Definition at line 63 of file host_functions_tests.cpp.

63 {
65}
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181

◆ c_style_host_function_3()

void c_style_host_function_3 ( int a,
float b )

Definition at line 66 of file host_functions_tests.cpp.

66 {
68}

◆ c_style_host_function_4()

void c_style_host_function_4 ( const state_t & ss)

Definition at line 69 of file host_functions_tests.cpp.

69 {
71}
static const Segment ss(Segment::ss)

◆ check_get()

template<typename T , typename B , typename F >
void check_get ( B & bkend,
const std::string & name,
F getter )

Definition at line 252 of file host_functions_tests.cpp.

252 {
253 for(auto fun : fun_prefixes) {
254 fun += "get_" + name;
255 for(const T value : test_values<T>) {
256 global_test_value<T> = value;
257 CHECK(getter(bkend.call_with_return("env", fun)) == value);
258 }
259 }
260}
std::string name
const std::vector< std::string > fun_prefixes
std::vector< T > test_values
#define value
Definition pkcs11.h:157
#define T(meth, val, expected)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_get_ptr()

template<typename T , typename B >
void check_get_ptr ( B & bkend,
const std::string & name )

Definition at line 263 of file host_functions_tests.cpp.

263 {
264 for(auto fun : fun_prefixes) {
265 fun += "get_" + name;
266 int offset = next_ptr_offset();
267 global_test_value<T> = const_cast<T>(bkend.get_context().linear_memory() + offset);
268 CHECK(bkend.call_with_return("env", fun)->to_ui32() == offset);
269 }
270}
int next_ptr_offset()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_get_ref()

template<typename T , typename B >
void check_get_ref ( B & bkend,
const std::string & name )

Definition at line 273 of file host_functions_tests.cpp.

273 {
274 for(auto fun : fun_prefixes) {
275 fun += "get_" + name;
276 int offset = next_ptr_offset();
277 global_test_value<T> = const_cast<T>(*(bkend.get_context().linear_memory() + offset));
278 CHECK(bkend.call_with_return("env", fun)->to_ui32() == offset);
279 }
280}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_put()

template<typename T , typename B >
void check_put ( B & bkend,
const std::string & name )

Definition at line 219 of file host_functions_tests.cpp.

219 {
220 for(auto fun : fun_prefixes) {
221 fun += "put_" + name;
222 for(const T value : test_values<T>) {
223 bkend.call("env", fun, value);
224 CHECK(global_test_value<T> == value);
225 }
226 }
227}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_put_ptr()

template<typename T , typename B >
void check_put_ptr ( B & bkend,
const std::string & name )

Definition at line 230 of file host_functions_tests.cpp.

230 {
231 for(auto fun : fun_prefixes) {
232 fun += "put_" + name;
233 int offset = next_ptr_offset();
234 const T value = const_cast<T>(bkend.get_context().linear_memory() + offset);
235 bkend.call("env", fun, value);
236 CHECK(global_test_value<T> == value);
237 }
238}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_put_ref()

template<typename T , typename B >
void check_put_ref ( B & bkend,
const std::string & name )

Definition at line 241 of file host_functions_tests.cpp.

241 {
242 for(auto fun : fun_prefixes) {
243 fun += "put_" + name;
244 int offset = next_ptr_offset();
245 const auto value = &const_cast<T>(*(bkend.get_context().linear_memory() + offset));
246 bkend.call("env", fun, value);
247 CHECK(global_test_value<T>.val == value);
248 }
249}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ next_ptr_offset()

int next_ptr_offset ( )

Definition at line 213 of file host_functions_tests.cpp.

213 {
214 static int counter = 4;
215 return counter++;
216}
Here is the caller graph for this function:

◆ test_parameters()

template<class Backend >
void test_parameters ( Backend && bkend)

Definition at line 283 of file host_functions_tests.cpp.

283 {
284 for(auto fun : fun_prefixes) {
285 fun += "put_b";
286 bkend.call("env", fun, true);
287 CHECK(global_test_value<bool> == true);
288 bkend.call("env", fun, false);
289 CHECK(global_test_value<bool> == false);
290 // Extra tests for bool:
291 bkend.call("env", fun, 42);
292 CHECK(global_test_value<bool> == true);
293 bkend.call("env", fun, 0x10000);
294 CHECK(global_test_value<bool> == true);
295 }
296
298 check_put<uint32_t>(bkend, "ui32");
300 check_put<uint64_t>(bkend, "ui64");
301 check_put<float>(bkend, "f32");
302 check_put<double>(bkend, "f64");
311}
void check_put(B &bkend, const std::string &name)
void check_put_ref(B &bkend, const std::string &name)
void check_put_ptr(B &bkend, const std::string &name)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test_results()

template<class Backend >
void test_results ( Backend && bkend)

Definition at line 320 of file host_functions_tests.cpp.

320 {
321 for(auto fun : {/*"get_b",*/ "call.get_b", "call_indirect.get_b"}) {
322 global_test_value<bool> = false;
323 CHECK(bkend.call_with_return("env", fun)->to_ui32() == 0u);
324 global_test_value<bool> = true;
325 CHECK(bkend.call_with_return("env", fun)->to_ui32() == 1u);
326 }
327
328 check_get<int32_t>(bkend, "i32", [](auto x){ return x->to_i32(); });
329 check_get<uint32_t>(bkend, "ui32", [](auto x){ return x->to_ui32(); });
330 check_get<int64_t>(bkend, "i64", [](auto x){ return x->to_i64(); });
331 check_get<uint64_t>(bkend, "ui64", [](auto x){ return x->to_ui64(); });
332 check_get<float>(bkend, "f32", [](auto x){ return x->to_f32(); });
333 check_get<double>(bkend, "f64", [](auto x){ return x->to_f64(); });
342}
void check_get_ptr(B &bkend, const std::string &name)
void check_get(B &bkend, const std::string &name, F getter)
void check_get_ref(B &bkend, const std::string &name)
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ c_style_host_function_state

int c_style_host_function_state = 0

Definition at line 52 of file host_functions_tests.cpp.

◆ fun_prefixes

const std::vector<std::string> fun_prefixes = { "", "call.", "call_indirect." }

Definition at line 205 of file host_functions_tests.cpp.

205{ "", "call.", "call_indirect." };

◆ host_functions_tests_1_code

wasm_code host_functions_tests_1_code

◆ test_values

template<typename T >
std::vector<T> test_values = { 0, 1, std::numeric_limits<T>::min(), std::numeric_limits<T>::max() }

Definition at line 208 of file host_functions_tests.cpp.

208{ 0, 1, std::numeric_limits<T>::min(), std::numeric_limits<T>::max() };

◆ test_values< bool >

template<>
std::vector<bool> test_values< bool > = { true, false }

Definition at line 208 of file host_functions_tests.cpp.

211{ true, false };

◆ wa

wasm_allocator wa
extern

Definition at line 10 of file main.cpp.