diff --git a/libraries/chain/include/eosio/chain/webassembly/eos-vm.hpp b/libraries/chain/include/eosio/chain/webassembly/eos-vm.hpp index 1c1a3e50cb..5a1a058878 100644 --- a/libraries/chain/include/eosio/chain/webassembly/eos-vm.hpp +++ b/libraries/chain/include/eosio/chain/webassembly/eos-vm.hpp @@ -41,6 +41,7 @@ struct profile_config { template class eos_vm_runtime : public eosio::chain::wasm_runtime_interface { + using context_t = typename Backend::template context; public: eos_vm_runtime(); std::unique_ptr instantiate_module(const char* code_bytes, size_t code_size, @@ -49,6 +50,7 @@ class eos_vm_runtime : public eosio::chain::wasm_runtime_interface { private: // todo: managing this will get more complicated with sync calls; eos_vm_backend_t* _bkend = nullptr; // non owning pointer to allow for immediate exit + context_t _exec_ctx; template friend class eos_vm_instantiated_module; diff --git a/libraries/chain/webassembly/runtimes/eos-vm.cpp b/libraries/chain/webassembly/runtimes/eos-vm.cpp index 760c8fac14..e23f91b90e 100644 --- a/libraries/chain/webassembly/runtimes/eos-vm.cpp +++ b/libraries/chain/webassembly/runtimes/eos-vm.cpp @@ -129,6 +129,12 @@ class eos_vm_instantiated_module : public wasm_instantiated_module_interface { _instantiated_module(std::move(mod)) {} void apply(apply_context& context) override { + // Reset execution context (reused per thread) + _runtime->_exec_ctx.set_module(&(_instantiated_module->get_module())); + _instantiated_module->set_context(&_runtime->_exec_ctx); + _instantiated_module->reset_max_call_depth(); + _instantiated_module->reset_max_pages(); + _instantiated_module->set_wasm_allocator(&context.control.get_wasm_allocator()); _runtime->_bkend = _instantiated_module.get(); apply_options opts; @@ -242,10 +248,10 @@ std::unique_ptr eos_vm_runtime::instan std::unique_ptr bkend = nullptr; #ifdef EOSIO_EOS_VM_JIT_RUNTIME_ENABLED if constexpr (std::is_same_v) - bkend = std::make_unique(code, code_size, nullptr, options, true); // true -- JIT uses single parsing + bkend = std::make_unique(code, code_size, nullptr, options, true, false); // true, false <--> single parsing, backend does not own execution context (execution context is reused per thread) else #endif - bkend = std::make_unique(code, code_size, nullptr, options, false); // false -- Interpreter uses 2-passes parsing + bkend = std::make_unique(code, code_size, nullptr, options, false, false); // false, false <--> 2-passes parsing, backend does not own execution context (execution context is reused per thread) eos_vm_host_functions_t::resolve(bkend->get_module()); return std::make_unique>(this, std::move(bkend)); } catch(eosio::vm::exception& e) { @@ -267,7 +273,7 @@ std::unique_ptr eos_vm_profile_runtime::inst wasm_code_ptr code((uint8_t*)code_bytes, code_size); apply_options options = { .max_pages = 65536, .max_call_depth = 0 }; - std::unique_ptr bkend = std::make_unique(code, code_size, nullptr, options, true); // true -- JIT uses single parsing + std::unique_ptr bkend = std::make_unique(code, code_size, nullptr, options, true, false); // true, false <--> single parsing, backend does not own execution context (execution context is reused per thread) eos_vm_host_functions_t::resolve(bkend->get_module()); return std::make_unique(std::move(bkend), code_bytes, code_size); } catch(eosio::vm::exception& e) { diff --git a/libraries/eos-vm b/libraries/eos-vm index 169835e427..74dd3f0c5e 160000 --- a/libraries/eos-vm +++ b/libraries/eos-vm @@ -1 +1 @@ -Subproject commit 169835e42729d2345bf654c093937a102aa0c446 +Subproject commit 74dd3f0c5e621c4e22b543b0f1aa2539a516c46f