Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
sysio::vm::execution_context_base< Derived, Host > Class Template Reference

#include <execution_context.hpp>

Collaboration diagram for sysio::vm::execution_context_base< Derived, Host >:

Public Member Functions

Derived & derived ()
 
 execution_context_base (module &m)
 
int32_t grow_linear_memory (int32_t pages)
 
int32_t current_linear_memory () const
 
void exit (std::error_code err=std::error_code())
 
moduleget_module ()
 
void set_wasm_allocator (wasm_allocator *alloc)
 
auto get_wasm_allocator ()
 
char * linear_memory ()
 
auto & get_operand_stack ()
 
const auto & get_operand_stack () const
 
auto get_interface ()
 
void set_max_pages (std::uint32_t max_pages)
 
std::error_code get_error_code () const
 
void reset ()
 
template<typename Visitor , typename... Args>
std::optional< operand_stack_elemexecute (host_type *host, Visitor &&visitor, const std::string_view func, Args... args)
 
template<typename Visitor , typename... Args>
void execute_start (host_type *host, Visitor &&visitor)
 
Derived & derived ()
 
 execution_context_base (module &m)
 
int32_t grow_linear_memory (int32_t pages)
 
int32_t current_linear_memory () const
 
void exit (std::error_code err=std::error_code())
 
moduleget_module ()
 
void set_wasm_allocator (wasm_allocator *alloc)
 
auto get_wasm_allocator ()
 
char * linear_memory ()
 
auto & get_operand_stack ()
 
const auto & get_operand_stack () const
 
auto get_interface ()
 
void set_max_pages (std::uint32_t max_pages)
 
std::error_code get_error_code () const
 
void reset ()
 
template<typename Visitor , typename... Args>
std::optional< operand_stack_elemexecute (host_type *host, Visitor &&visitor, const std::string_view func, Args... args)
 
template<typename Visitor , typename... Args>
void execute_start (host_type *host, Visitor &&visitor)
 

Static Protected Member Functions

template<typename... Args>
static void type_check_args (const func_type &ft, Args &&...)
 
static void handle_signal (int sig)
 
template<typename... Args>
static void type_check_args (const func_type &ft, Args &&...)
 
static void handle_signal (int sig)
 

Protected Attributes

char * _linear_memory = nullptr
 
module_mod
 
wasm_allocator_wasm_alloc
 
uint32_t _max_pages = max_pages
 
detail::host_invoker_t< Host > _rhf
 
std::error_code _error_code
 
operand_stack _os
 

Detailed Description

template<typename Derived, typename Host>
class sysio::vm::execution_context_base< Derived, Host >

Definition at line 84 of file execution_context.hpp.

Constructor & Destructor Documentation

◆ execution_context_base() [1/2]

template<typename Derived , typename Host >
sysio::vm::execution_context_base< Derived, Host >::execution_context_base ( module & m)
inline

Definition at line 88 of file execution_context.hpp.

◆ execution_context_base() [2/2]

template<typename Derived , typename Host >
sysio::vm::execution_context_base< Derived, Host >::execution_context_base ( module & m)
inline

Definition at line 88 of file execution_context.hpp.

88: _mod(m) {}

Member Function Documentation

◆ current_linear_memory() [1/2]

template<typename Derived , typename Host >
int32_t sysio::vm::execution_context_base< Derived, Host >::current_linear_memory ( ) const
inline

Definition at line 105 of file execution_context.hpp.

Here is the call graph for this function:

◆ current_linear_memory() [2/2]

template<typename Derived , typename Host >
int32_t sysio::vm::execution_context_base< Derived, Host >::current_linear_memory ( ) const
inline

Definition at line 105 of file execution_context.hpp.

105{ return _wasm_alloc->get_current_page(); }
Here is the call graph for this function:

◆ derived() [1/2]

template<typename Derived , typename Host >
Derived & sysio::vm::execution_context_base< Derived, Host >::derived ( )
inline

Definition at line 87 of file execution_context.hpp.

87{ return static_cast<Derived&>(*this); }

◆ derived() [2/2]

template<typename Derived , typename Host >
Derived & sysio::vm::execution_context_base< Derived, Host >::derived ( )
inline

Definition at line 87 of file execution_context.hpp.

87{ return static_cast<Derived&>(*this); }

◆ execute() [1/2]

template<typename Derived , typename Host >
template<typename Visitor , typename... Args>
std::optional< operand_stack_elem > sysio::vm::execution_context_base< Derived, Host >::execute ( host_type * host,
Visitor && visitor,
const std::string_view func,
Args... args )
inline

Definition at line 151 of file execution_context.hpp.

152 {
154 return derived().execute(host, std::forward<Visitor>(visitor), func_index, std::forward<Args>(args)...);
155 }
unsigned int uint32_t
Definition stdint.h:126
uint32_t get_exported_function(const std::string_view str)
Definition types.hpp:227

◆ execute() [2/2]

template<typename Derived , typename Host >
template<typename Visitor , typename... Args>
std::optional< operand_stack_elem > sysio::vm::execution_context_base< Derived, Host >::execute ( host_type * host,
Visitor && visitor,
const std::string_view func,
Args... args )
inline

Definition at line 151 of file execution_context.hpp.

152 {
154 return derived().execute(host, std::forward<Visitor>(visitor), func_index, std::forward<Args>(args)...);
155 }

◆ execute_start() [1/2]

template<typename Derived , typename Host >
template<typename Visitor , typename... Args>
void sysio::vm::execution_context_base< Derived, Host >::execute_start ( host_type * host,
Visitor && visitor )
inline

Definition at line 158 of file execution_context.hpp.

158 {
159 if (_mod.start != std::numeric_limits<uint32_t>::max())
160 derived().execute(host, std::forward<Visitor>(visitor), _mod.start);
161 }
uint32_t start
Definition types.hpp:168

◆ execute_start() [2/2]

template<typename Derived , typename Host >
template<typename Visitor , typename... Args>
void sysio::vm::execution_context_base< Derived, Host >::execute_start ( host_type * host,
Visitor && visitor )
inline

Definition at line 158 of file execution_context.hpp.

158 {
159 if (_mod.start != std::numeric_limits<uint32_t>::max())
160 derived().execute(host, std::forward<Visitor>(visitor), _mod.start);
161 }

◆ exit() [1/2]

template<typename Derived , typename Host >
void sysio::vm::execution_context_base< Derived, Host >::exit ( std::error_code err = std::error_code())
inline

Definition at line 106 of file execution_context.hpp.

106 {
107 // FIXME: system_error?
108 _error_code = err;
109 throw wasm_exit_exception{"Exiting"};
110 }

◆ exit() [2/2]

template<typename Derived , typename Host >
void sysio::vm::execution_context_base< Derived, Host >::exit ( std::error_code err = std::error_code())
inline

Definition at line 106 of file execution_context.hpp.

106 {
107 // FIXME: system_error?
108 _error_code = err;
109 throw wasm_exit_exception{"Exiting"};
110 }

◆ get_error_code() [1/2]

template<typename Derived , typename Host >
std::error_code sysio::vm::execution_context_base< Derived, Host >::get_error_code ( ) const
inline

Definition at line 121 of file execution_context.hpp.

121{ return _error_code; }

◆ get_error_code() [2/2]

template<typename Derived , typename Host >
std::error_code sysio::vm::execution_context_base< Derived, Host >::get_error_code ( ) const
inline

Definition at line 121 of file execution_context.hpp.

121{ return _error_code; }

◆ get_interface() [1/2]

template<typename Derived , typename Host >
auto sysio::vm::execution_context_base< Derived, Host >::get_interface ( )
inline

Definition at line 118 of file execution_context.hpp.

118{ return execution_interface{ _linear_memory, &_os }; }

◆ get_interface() [2/2]

template<typename Derived , typename Host >
auto sysio::vm::execution_context_base< Derived, Host >::get_interface ( )
inline

Definition at line 118 of file execution_context.hpp.

118{ return execution_interface{ _linear_memory, &_os }; }

◆ get_module() [1/2]

template<typename Derived , typename Host >
module & sysio::vm::execution_context_base< Derived, Host >::get_module ( )
inline

Definition at line 112 of file execution_context.hpp.

112{ return _mod; }

◆ get_module() [2/2]

template<typename Derived , typename Host >
module & sysio::vm::execution_context_base< Derived, Host >::get_module ( )
inline

Definition at line 112 of file execution_context.hpp.

112{ return _mod; }

◆ get_operand_stack() [1/4]

template<typename Derived , typename Host >
auto & sysio::vm::execution_context_base< Derived, Host >::get_operand_stack ( )
inline

Definition at line 116 of file execution_context.hpp.

116{ return _os; }

◆ get_operand_stack() [2/4]

template<typename Derived , typename Host >
auto & sysio::vm::execution_context_base< Derived, Host >::get_operand_stack ( )
inline

Definition at line 116 of file execution_context.hpp.

116{ return _os; }

◆ get_operand_stack() [3/4]

template<typename Derived , typename Host >
const auto & sysio::vm::execution_context_base< Derived, Host >::get_operand_stack ( ) const
inline

Definition at line 117 of file execution_context.hpp.

117{ return _os; }

◆ get_operand_stack() [4/4]

template<typename Derived , typename Host >
const auto & sysio::vm::execution_context_base< Derived, Host >::get_operand_stack ( ) const
inline

Definition at line 117 of file execution_context.hpp.

117{ return _os; }

◆ get_wasm_allocator() [1/2]

template<typename Derived , typename Host >
auto sysio::vm::execution_context_base< Derived, Host >::get_wasm_allocator ( )
inline

Definition at line 114 of file execution_context.hpp.

114{ return _wasm_alloc; }

◆ get_wasm_allocator() [2/2]

template<typename Derived , typename Host >
auto sysio::vm::execution_context_base< Derived, Host >::get_wasm_allocator ( )
inline

Definition at line 114 of file execution_context.hpp.

114{ return _wasm_alloc; }

◆ grow_linear_memory() [1/2]

template<typename Derived , typename Host >
int32_t sysio::vm::execution_context_base< Derived, Host >::grow_linear_memory ( int32_t pages)
inline

Definition at line 90 of file execution_context.hpp.

90 {
92 if (pages < 0) {
93 if (sz + pages < 0)
94 return -1;
95 _wasm_alloc->free<char>(-pages);
96 } else {
97 if (!_mod.memories.size() || _max_pages - sz < static_cast<uint32_t>(pages) ||
98 (_mod.memories[0].limits.flags && (static_cast<int32_t>(_mod.memories[0].limits.maximum) - sz < pages)))
99 return -1;
100 _wasm_alloc->alloc<char>(pages);
101 }
102 return sz;
103 }
void free(std::size_t size)
void alloc(size_t size=1)
signed int int32_t
Definition stdint.h:123
guarded_vector< memory_type > memories
Definition types.hpp:173
Here is the call graph for this function:

◆ grow_linear_memory() [2/2]

template<typename Derived , typename Host >
int32_t sysio::vm::execution_context_base< Derived, Host >::grow_linear_memory ( int32_t pages)
inline

Definition at line 90 of file execution_context.hpp.

90 {
92 if (pages < 0) {
93 if (sz + pages < 0)
94 return -1;
95 _wasm_alloc->free<char>(-pages);
96 } else {
97 if (!_mod.memories.size() || _max_pages - sz < static_cast<uint32_t>(pages) ||
98 (_mod.memories[0].limits.flags && (static_cast<int32_t>(_mod.memories[0].limits.maximum) - sz < pages)))
99 return -1;
100 _wasm_alloc->alloc<char>(pages);
101 }
102 return sz;
103 }
Here is the call graph for this function:

◆ handle_signal() [1/2]

template<typename Derived , typename Host >
static void sysio::vm::execution_context_base< Derived, Host >::handle_signal ( int sig)
inlinestaticprotected

Definition at line 172 of file execution_context.hpp.

172 {
173 switch(sig) {
174 case SIGSEGV:
175 case SIGBUS:
176 case SIGFPE:
177 break;
178 default:
179 /* TODO fix this */
180 assert(!"??????");
181 }
182 throw wasm_memory_exception{ "wasm memory out-of-bounds" };
183 }

◆ handle_signal() [2/2]

template<typename Derived , typename Host >
static void sysio::vm::execution_context_base< Derived, Host >::handle_signal ( int sig)
inlinestaticprotected

Definition at line 172 of file execution_context.hpp.

172 {
173 switch(sig) {
174 case SIGSEGV:
175 case SIGBUS:
176 case SIGFPE:
177 break;
178 default:
179 /* TODO fix this */
180 assert(!"??????");
181 }
182 throw wasm_memory_exception{ "wasm memory out-of-bounds" };
183 }

◆ linear_memory() [1/2]

template<typename Derived , typename Host >
char * sysio::vm::execution_context_base< Derived, Host >::linear_memory ( )
inline

Definition at line 115 of file execution_context.hpp.

115{ return _linear_memory; }

◆ linear_memory() [2/2]

template<typename Derived , typename Host >
char * sysio::vm::execution_context_base< Derived, Host >::linear_memory ( )
inline

Definition at line 115 of file execution_context.hpp.

115{ return _linear_memory; }

◆ reset() [1/2]

template<typename Derived , typename Host >
void sysio::vm::execution_context_base< Derived, Host >::reset ( )
inline

Definition at line 123 of file execution_context.hpp.

123 {
124 SYS_VM_ASSERT(_mod.error == nullptr, wasm_interpreter_exception, _mod.error);
125
127 if(_mod.memories.size()) {
128 SYS_VM_ASSERT(_mod.memories[0].limits.initial <= _max_pages, wasm_bad_alloc, "Cannot allocate initial linear memory.");
129 _wasm_alloc->reset(_mod.memories[0].limits.initial);
130 } else
132
133 for (uint32_t i = 0; i < _mod.data.size(); i++) {
134 const auto& data_seg = _mod.data[i];
135 uint32_t offset = data_seg.offset.value.i32; // force to unsigned
136 auto available_memory = _mod.memories[0].limits.initial * static_cast<uint64_t>(page_size);
137 auto required_memory = static_cast<uint64_t>(offset) + data_seg.data.size();
138 SYS_VM_ASSERT(required_memory <= available_memory, wasm_memory_exception, "data out of range");
139 auto addr = _linear_memory + offset;
140 memcpy((char*)(addr), data_seg.data.raw(), data_seg.data.size());
141 }
142
143 // reset the mutable globals
144 for (uint32_t i = 0; i < _mod.globals.size(); i++) {
145 if (_mod.globals[i].type.mutability)
146 _mod.globals[i].current = _mod.globals[i].init;
147 }
148 }
void reset(uint32_t new_pages)
unsigned __int64 uint64_t
Definition stdint.h:136
guarded_vector< global_variable > globals
Definition types.hpp:174
const char * error
Definition types.hpp:191
guarded_vector< data_segment > data
Definition types.hpp:178
#define SYS_VM_ASSERT(expr, exc_type, msg)
Definition exceptions.hpp:8
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:

◆ reset() [2/2]

template<typename Derived , typename Host >
void sysio::vm::execution_context_base< Derived, Host >::reset ( )
inline

Definition at line 123 of file execution_context.hpp.

123 {
124 SYS_VM_ASSERT(_mod.error == nullptr, wasm_interpreter_exception, _mod.error);
125
127 if(_mod.memories.size()) {
128 SYS_VM_ASSERT(_mod.memories[0].limits.initial <= _max_pages, wasm_bad_alloc, "Cannot allocate initial linear memory.");
129 _wasm_alloc->reset(_mod.memories[0].limits.initial);
130 } else
132
133 for (uint32_t i = 0; i < _mod.data.size(); i++) {
134 const auto& data_seg = _mod.data[i];
135 uint32_t offset = data_seg.offset.value.i32; // force to unsigned
136 auto available_memory = _mod.memories[0].limits.initial * static_cast<uint64_t>(page_size);
137 auto required_memory = static_cast<uint64_t>(offset) + data_seg.data.size();
138 SYS_VM_ASSERT(required_memory <= available_memory, wasm_memory_exception, "data out of range");
139 auto addr = _linear_memory + offset;
140 memcpy((char*)(addr), data_seg.data.raw(), data_seg.data.size());
141 }
142
143 // reset the mutable globals
144 for (uint32_t i = 0; i < _mod.globals.size(); i++) {
145 if (_mod.globals[i].type.mutability)
146 _mod.globals[i].current = _mod.globals[i].init;
147 }
148 }
Here is the call graph for this function:

◆ set_max_pages() [1/2]

template<typename Derived , typename Host >
void sysio::vm::execution_context_base< Derived, Host >::set_max_pages ( std::uint32_t max_pages)
inline

Definition at line 119 of file execution_context.hpp.

119{ _max_pages = std::min(max_pages, static_cast<std::uint32_t>(vm::max_pages)); }

◆ set_max_pages() [2/2]

template<typename Derived , typename Host >
void sysio::vm::execution_context_base< Derived, Host >::set_max_pages ( std::uint32_t max_pages)
inline

Definition at line 119 of file execution_context.hpp.

119{ _max_pages = std::min(max_pages, static_cast<std::uint32_t>(vm::max_pages)); }

◆ set_wasm_allocator() [1/2]

template<typename Derived , typename Host >
void sysio::vm::execution_context_base< Derived, Host >::set_wasm_allocator ( wasm_allocator * alloc)
inline

Definition at line 113 of file execution_context.hpp.

113{ _wasm_alloc = alloc; }

◆ set_wasm_allocator() [2/2]

template<typename Derived , typename Host >
void sysio::vm::execution_context_base< Derived, Host >::set_wasm_allocator ( wasm_allocator * alloc)
inline

Definition at line 113 of file execution_context.hpp.

113{ _wasm_alloc = alloc; }

◆ type_check_args() [1/2]

template<typename Derived , typename Host >
template<typename... Args>
static void sysio::vm::execution_context_base< Derived, Host >::type_check_args ( const func_type & ft,
Args && ... )
inlinestaticprotected

Definition at line 166 of file execution_context.hpp.

166 {
167 SYS_VM_ASSERT(sizeof...(Args) == ft.param_types.size(), wasm_interpreter_exception, "wrong number of arguments");
168 uint32_t i = 0;
169 SYS_VM_ASSERT((... && (to_wasm_type_v<detail::type_converter_t<Host>, Args> == ft.param_types.at(i++))), wasm_interpreter_exception, "unexpected argument type");
170 }
typename type_converter< HF >::type type_converter_t
constexpr auto to_wasm_type_v

◆ type_check_args() [2/2]

template<typename Derived , typename Host >
template<typename... Args>
static void sysio::vm::execution_context_base< Derived, Host >::type_check_args ( const func_type & ft,
Args && ... )
inlinestaticprotected

Definition at line 166 of file execution_context.hpp.

166 {
167 SYS_VM_ASSERT(sizeof...(Args) == ft.param_types.size(), wasm_interpreter_exception, "wrong number of arguments");
168 uint32_t i = 0;
169 SYS_VM_ASSERT((... && (to_wasm_type_v<detail::type_converter_t<Host>, Args> == ft.param_types.at(i++))), wasm_interpreter_exception, "unexpected argument type");
170 }

Member Data Documentation

◆ _error_code

template<typename Derived , typename Host >
std::error_code sysio::vm::execution_context_base< Derived, Host >::_error_code
protected

Definition at line 190 of file execution_context.hpp.

◆ _linear_memory

template<typename Derived , typename Host >
char * sysio::vm::execution_context_base< Derived, Host >::_linear_memory = nullptr
protected

Definition at line 185 of file execution_context.hpp.

◆ _max_pages

template<typename Derived , typename Host >
uint32_t sysio::vm::execution_context_base< Derived, Host >::_max_pages = max_pages
protected

Definition at line 188 of file execution_context.hpp.

◆ _mod

template<typename Derived , typename Host >
module & sysio::vm::execution_context_base< Derived, Host >::_mod
protected

Definition at line 186 of file execution_context.hpp.

◆ _os

template<typename Derived , typename Host >
operand_stack sysio::vm::execution_context_base< Derived, Host >::_os
protected

Definition at line 191 of file execution_context.hpp.

◆ _rhf

template<typename Derived , typename Host >
detail::host_invoker_t< Host > sysio::vm::execution_context_base< Derived, Host >::_rhf
protected

Definition at line 189 of file execution_context.hpp.

◆ _wasm_alloc

template<typename Derived , typename Host >
wasm_allocator * sysio::vm::execution_context_base< Derived, Host >::_wasm_alloc
protected

Definition at line 187 of file execution_context.hpp.


The documentation for this class was generated from the following files: