16 #include <binaryen/wasm-interpreter.h> 18 #ifdef WASM_INTERPRETER_DEBUG 28 int Indenter::indentLevel = 0;
30 std::vector<std::string> indents = []() {
31 std::vector<std::string> indents;
32 for (
size_t i = 0; i < 512; i++) {
33 indents.push_back(std::string(i,
'-'));
38 Indenter::Indenter(
const char *entry) : entryName(entry) {
42 Indenter::~Indenter() {
44 std::cout <<
"exit " << entryName <<
'\n';
48 void Indenter::print() {
49 std::cout << indentLevel <<
':' << indents[indentLevel];
54 #include <binaryen/wasm.h> 65 return "Execution was ended in external function";
67 return "An error occurred during an export call execution";
69 return "Failed to obtain a global value";
71 return "Unknown ModuleInstance error";
78 std::shared_ptr<wasm::Module> parent,
79 std::shared_ptr<RuntimeExternalInterface> rei,
81 : env_{std::move(
env)},
86 std::make_unique<wasm::ModuleInstance>(*
parent_,
rei_.get())},
97 PtrSize args{memory.get().storeBuffer(encoded_args)};
99 const auto args_list =
100 wasm::LiteralList{wasm::Literal{args.
ptr}, wasm::Literal{args.size}};
102 const auto res =
static_cast<uint64_t
>(
108 }
catch (wasm::ExitException &e) {
110 }
catch (wasm::TrapException &e) {
116 std::string_view name)
const {
120 case wasm::Type::i32:
122 case wasm::Type::i64:
124 case wasm::Type::f32:
126 case wasm::Type::f64:
130 "Runtime function returned result of unsupported type: {}",
131 wasm::printType(val.type));
134 }
catch (wasm::TrapException &e) {
145 return outcome::success();
150 for (
auto &segment :
parent_->memory.segments) {
151 wasm::Address offset =
152 (uint32_t)wasm::ConstantExpressionRunner<wasm::TrivialGlobalManager>(
154 .visit(segment.offset)
156 if (offset + segment.data.size()
157 >
parent_->memory.initial * wasm::Memory::kPageSize) {
158 throw std::runtime_error(
"invalid offset when initializing memory");
161 gsl::span<const uint8_t>(
162 reinterpret_cast<const uint8_t *>(segment.data.data()),
163 segment.data.size()));
std::function< void(SegmentOffset, SegmentData)> DataSegmentProcessor
void forDataSegment(DataSegmentProcessor const &callback) const override
common::Hash256 code_hash_
WasmPointer ptr
address of buffer
std::shared_ptr< wasm::Module > parent_
OUTCOME_CPP_DEFINE_CATEGORY(kagome::runtime::binaryen, ModuleInstanceImpl::Error, e)
std::unique_ptr< wasm::ModuleInstance > module_instance_
outcome::result< PtrSize > callExportFunction(std::string_view name, common::BufferView args) const override
outcome::result< std::optional< WasmValue > > getGlobal(std::string_view name) const override
static const wasm::Name env
std::shared_ptr< MemoryProvider > memory_provider
std::shared_ptr< RuntimeExternalInterface > rei_
InstanceEnvironment const & getEnvironment() const override
boost::variant< int32_t, int64_t, float, double > WasmValue
std::shared_ptr< host_api::HostApi > host_api
Logger createLogger(const std::string &tag)
ModuleInstanceImpl(InstanceEnvironment &&env, std::shared_ptr< wasm::Module > parent, std::shared_ptr< RuntimeExternalInterface > rei, const common::Hash256 &code_hash)
outcome::result< void > resetEnvironment() override