preflight checks for wasm (#5517)

This commit is contained in:
Olek
2025-06-30 09:34:38 -04:00
committed by GitHub
parent 1cd16fab87
commit 463acf51b5
9 changed files with 385 additions and 173 deletions

View File

@@ -29,19 +29,9 @@
namespace ripple {
Expected<EscrowResult, TER>
runEscrowWasm(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
HostFunctions* hfs,
int64_t gasLimit,
beast::Journal j)
static std::vector<WasmImportFunc>
createImports(HostFunctions* hfs)
{
// create VM and set cost limit
auto& vm = WasmEngine::instance();
vm.initMaxPages(MAX_PAGES);
std::vector<WasmImportFunc> imports;
if (hfs)
@@ -107,11 +97,27 @@ runEscrowWasm(
WASM_IMPORT_FUNC2(imports, traceNum, "trace_num", hfs);
}
auto ret = vm.run(
return imports;
}
Expected<EscrowResult, TER>
runEscrowWasm(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
HostFunctions* hfs,
int64_t gasLimit,
beast::Journal j)
{
// create VM and set cost limit
auto& vm = WasmEngine::instance();
vm.initMaxPages(MAX_PAGES);
auto const ret = vm.run(
wasmCode,
funcName,
params,
imports,
createImports(hfs),
hfs,
gasLimit,
hfs ? hfs->getJournal() : j);
@@ -130,6 +136,28 @@ runEscrowWasm(
return EscrowResult{ret->result > 0, ret->cost};
}
NotTEC
preflightEscrowWasm(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
HostFunctions* hfs,
beast::Journal j)
{
// create VM and set cost limit
auto& vm = WasmEngine::instance();
vm.initMaxPages(MAX_PAGES);
auto const ret = vm.check(
wasmCode,
funcName,
params,
createImports(hfs),
hfs ? hfs->getJournal() : j);
return ret;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WasmEngine::WasmEngine() : impl(std::make_unique<WamrEngine>())
@@ -156,6 +184,17 @@ WasmEngine::run(
return impl->run(wasmCode, funcName, params, imports, hfs, gasLimit, j);
}
NotTEC
WasmEngine::check(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
beast::Journal j)
{
return impl->check(wasmCode, funcName, params, imports, j);
}
std::int32_t
WasmEngine::initMaxPages(std::int32_t def)
{