Merge branch 'ripple/se/fees' into ripple/smart-escrow

This commit is contained in:
Mayukha Vadari
2025-12-02 07:48:22 -05:00
committed by GitHub
23 changed files with 767 additions and 11341 deletions

View File

@@ -2,10 +2,10 @@
sources:
"0.42.1":
sha256: 2a5697be33c7afce8f671af4a5a3621d9e93ce55d253d31bd8201458e465fbb8
url: https://github.com/wasmi-labs/wasmi/archive/refs/tags/v0.42.1.tar.gz
sha256: 2a5697be33c7afce8f671af4a5a3621d9e93ce55d253d31bd8201458e465fbb8
patches:
"0.42.1":
- patch_file: "patches/0001-xrplf-0.42.1.patch"
patch_description: Integrate wasmi lib into smart-escrow.
patch_type: conan
patch_description: Integration patch. Make wasmi lib to be able to build with existing smart-escrow branch.

View File

@@ -1,5 +1,5 @@
from conan import ConanFile, tools
from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, export_conandata_patches, get
import os
@@ -30,9 +30,6 @@ class WasmiConan(ConanFile):
tc.variables["BUILD_SHARED_LIBS"] = self.options.shared
tc.generate()
deps = CMakeDeps(self)
deps.generate()
def build(self):
cmake = CMake(self)
cmake.configure(build_script_folder=os.path.join(self.source_folder, "crates", "c_api"))
@@ -44,5 +41,3 @@ class WasmiConan(ConanFile):
def package_info(self):
self.cpp_info.libs = ["wasmi"]
self.cpp_info.names["cmake_find_package"] = "wasmi"
self.cpp_info.names["cmake_find_package_multi"] = "wasmi"

View File

@@ -1,8 +1,17 @@
diff --git a/crates/c_api/CMakeLists.txt b/crates/c_api/CMakeLists.txt
index b15c787a..4e6de690 100644
index b15c787..54eaed2 100644
--- a/crates/c_api/CMakeLists.txt
+++ b/crates/c_api/CMakeLists.txt
@@ -43,6 +43,10 @@ endif()
@@ -6,6 +6,8 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
option(WASMI_ALWAYS_BUILD "If cmake should always invoke cargo to build Wasmi" ON)
set(WASMI_TARGET "" CACHE STRING "Rust target to build for")
+add_compile_definitions(COMPILING_WASM_RUNTIME_API=1)
+
if(NOT WASMI_TARGET)
execute_process(
COMMAND rustc -vV
@@ -43,6 +45,10 @@ endif()
list(TRANSFORM WASMI_SHARED_FILES PREPEND ${WASMI_TARGET_DIR}/)
list(TRANSFORM WASMI_STATIC_FILES PREPEND ${WASMI_TARGET_DIR}/)
@@ -13,7 +22,15 @@ index b15c787a..4e6de690 100644
# Instructions on how to build and install the Wasmi Rust crate.
find_program(WASMI_CARGO_BINARY cargo REQUIRED)
include(ExternalProject)
@@ -112,6 +116,7 @@ install(
@@ -79,7 +85,6 @@ else()
target_link_libraries(wasmi INTERFACE ${WASMI_STATIC_FILES})
if(WASMI_TARGET MATCHES "windows")
- target_compile_options(wasmi INTERFACE -DWASM_API_EXTERN= -DWASI_API_EXTERN=)
target_link_libraries(wasmi INTERFACE ws2_32 advapi32 userenv ntdll shell32 ole32 bcrypt)
elseif(NOT WASMI_TARGET MATCHES "darwin")
target_link_libraries(wasmi INTERFACE pthread dl m)
@@ -112,6 +117,7 @@ install(
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
@@ -21,7 +38,7 @@ index b15c787a..4e6de690 100644
if(WASMI_TARGET MATCHES "darwin")
set(INSTALLED_LIB "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libwasmi.dylib")
install(
@@ -131,6 +136,7 @@ if(WASMI_TARGET MATCHES "darwin")
@@ -131,6 +137,7 @@ if(WASMI_TARGET MATCHES "darwin")
install(CODE "execute_process(COMMAND ${install_name_tool_cmd})")
endif()
endif()
@@ -29,7 +46,7 @@ index b15c787a..4e6de690 100644
# Documentation Generation via Doxygen:
set(DOXYGEN_CONF_IN ${CMAKE_CURRENT_SOURCE_DIR}/doxygen.conf.in)
@@ -141,19 +147,3 @@ add_custom_target(doc
@@ -141,19 +148,3 @@ add_custom_target(doc
DEPENDS ${WASMI_GENERATED_CONF_H} ${DOXYGEN_CONF_OUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
@@ -50,25 +67,44 @@ index b15c787a..4e6de690 100644
- COMMENT "clang-format: Apply formatting rules for Wasmi C-API header files"
-)
diff --git a/crates/c_api/include/wasm.h b/crates/c_api/include/wasm.h
index 5ee617ff..0199192d 100644
index 5ee617f..a76f10e 100644
--- a/crates/c_api/include/wasm.h
+++ b/crates/c_api/include/wasm.h
@@ -146,6 +146,13 @@ WASM_DECLARE_OWN(store)
@@ -13,11 +13,17 @@
#include <assert.h>
#ifndef WASM_API_EXTERN
-#if defined(_WIN32) && !defined(__MINGW32__) && !defined(LIBWASM_STATIC)
+#if defined(_MSC_BUILD)
+#if defined(COMPILING_WASM_RUNTIME_API)
+#define WASM_API_EXTERN __declspec(dllexport)
+#elif defined(_DLL)
#define WASM_API_EXTERN __declspec(dllimport)
#else
#define WASM_API_EXTERN
#endif
+#else
+#define WASM_API_EXTERN
+#endif
#endif
#ifdef __cplusplus
@@ -145,7 +151,13 @@ WASM_API_EXTERN own wasm_engine_t* wasm_engine_new_with_config(own wasm_config_t
WASM_DECLARE_OWN(store)
WASM_API_EXTERN own wasm_store_t* wasm_store_new(wasm_engine_t*);
+WASM_API_EXTERN own wasm_store_t* wasm_store_new_with_memory_max_pages(wasm_engine_t*, uint32_t max_pages);
+
+// Store fuel functions (forward declarations)
+struct wasmi_error;
+
+WASM_API_EXTERN struct wasmi_error* wasm_store_get_fuel(const wasm_store_t*, uint64_t* fuel);
+WASM_API_EXTERN struct wasmi_error* wasm_store_set_fuel(wasm_store_t*, uint64_t fuel);
+//WASM_API_EXTERN void *wasm_store_get_data(const wasm_store_t*);
+//WASM_API_EXTERN void wasm_store_set_data(wasm_store_t*, void *data);
///////////////////////////////////////////////////////////////////////////////
// Type Representations
diff --git a/crates/c_api/include/wasmi.h b/crates/c_api/include/wasmi.h
index 2caffa37..0c0584ec 100644
index 2caffa3..0c0584e 100644
--- a/crates/c_api/include/wasmi.h
+++ b/crates/c_api/include/wasmi.h
@@ -10,7 +10,7 @@
@@ -94,18 +130,127 @@ index 2caffa37..0c0584ec 100644
#endif // WASMI_H
diff --git a/crates/c_api/src/store.rs b/crates/c_api/src/store.rs
index 56d4898f..543dbff8 100644
index 56d4898..9abda8e 100644
--- a/crates/c_api/src/store.rs
+++ b/crates/c_api/src/store.rs
@@ -175,3 +175,44 @@ pub extern "C" fn wasmi_context_set_fuel(
@@ -1,7 +1,7 @@
use crate::{wasm_engine_t, wasmi_error_t, ForeignData};
use alloc::{boxed::Box, sync::Arc};
use core::{cell::UnsafeCell, ffi};
-use wasmi::{AsContext, AsContextMut, Store, StoreContext, StoreContextMut};
+use wasmi::{AsContext, AsContextMut, Store, StoreContext, StoreContextMut, StoreLimits, StoreLimitsBuilder};
/// This representation of a `Store` is used to implement the `wasm.h` API (and
/// *not* the `wasmi.h` API!)
@@ -16,7 +16,7 @@ use wasmi::{AsContext, AsContextMut, Store, StoreContext, StoreContextMut};
/// least Wasmi's implementation).
#[derive(Clone)]
pub struct WasmStoreRef {
- inner: Arc<UnsafeCell<Store<()>>>,
+ inner: Arc<UnsafeCell<Store<StoreLimits>>>,
}
impl WasmStoreRef {
@@ -27,7 +27,7 @@ impl WasmStoreRef {
/// # Safety
///
/// It is the callers responsibility to provide a valid `self`.
- pub unsafe fn context(&self) -> StoreContext<'_, ()> {
+ pub unsafe fn context(&self) -> StoreContext<'_, StoreLimits> {
(*self.inner.get()).as_context()
}
@@ -38,7 +38,7 @@ impl WasmStoreRef {
/// # Safety
///
/// It is the callers responsibility to provide a valid `self`.
- pub unsafe fn context_mut(&mut self) -> StoreContextMut<'_, ()> {
+ pub unsafe fn context_mut(&mut self) -> StoreContextMut<'_, StoreLimits> {
(*self.inner.get()).as_context_mut()
}
}
@@ -56,17 +56,71 @@ pub struct wasm_store_t {
wasmi_c_api_macros::declare_own!(wasm_store_t);
-/// Creates a new [`Store<()>`](wasmi::Store) for the given `engine`.
+/// Creates a new [`Store<StoreLimits>`](wasmi::Store) for the given `engine`.
+///
+/// The store is created with no resource limits (original behavior).
+/// For memory-limited stores, use [`wasm_store_new_with_memory_max_pages`].
///
/// The returned [`wasm_store_t`] must be freed using [`wasm_store_delete`].
///
-/// Wraps [`<wasmi::Store<()>>::new`](wasmi::Store::new).
+/// Wraps [`<wasmi::Store<StoreLimits>>::new`](wasmi::Store::new).
#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
#[allow(clippy::arc_with_non_send_sync)]
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
pub extern "C" fn wasm_store_new(engine: &wasm_engine_t) -> Box<wasm_store_t> {
let engine = &engine.inner;
- let store = Store::new(engine, ());
+
+ // Create store with no resource limits (original behavior)
+ let limits = StoreLimitsBuilder::new().build();
+ let store = Store::new(engine, limits);
+
+ Box::new(wasm_store_t {
+ inner: WasmStoreRef {
+ inner: Arc::new(UnsafeCell::new(store)),
+ },
+ })
+}
+
+/// Creates a new [`Store<StoreLimits>`](wasmi::Store) for the given `engine` with memory limits.
+///
+/// This function creates a store with resource limits suitable for blockchain smart contracts.
+/// The memory limit is enforced during WebAssembly execution.
+///
+/// If `max_pages` exceeds 1024 (64MB), this function will panic.
+///
+/// The returned [`wasm_store_t`] must be freed using [`wasm_store_delete`].
+///
+/// Wraps [`<wasmi::Store<StoreLimits>>::new`](wasmi::Store::new).
+#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
+#[allow(clippy::arc_with_non_send_sync)]
+#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
+pub extern "C" fn wasm_store_new_with_memory_max_pages(
+ engine: &wasm_engine_t,
+ max_pages: u32,
+) -> Box<wasm_store_t> {
+ // Validate max_pages limit (64MB = 1024 pages)
+ if max_pages > 1024 {
+ panic!("max_pages ({}) exceeds maximum allowed value of 1024 pages (64MB)", max_pages);
+ }
+
+ // Convert pages to bytes (each page is 64KB)
+ let max_memory_bytes = (max_pages as usize) * (64 * 1024);
+
+ // Create store limits with blockchain-suitable defaults
+ let limits = StoreLimitsBuilder::new()
+ .memory_size(max_memory_bytes) // User-specified memory limit
+ .instances(1) // Single instance for blockchain
+ .tables(1) // Single table for blockchain
+ .memories(1) // Single memory for blockchain
+ .table_elements(64) // Limited table elements for blockchain
+ .trap_on_grow_failure(false) // Return -1 on growth failure instead of trapping
+ .build();
+
+ let mut store = Store::new(&engine.inner, limits);
+
+ // Install the resource limiter
+ store.limiter(|limits| limits);
+
Box::new(wasm_store_t {
inner: WasmStoreRef {
inner: Arc::new(UnsafeCell::new(store)),
@@ -175,3 +229,40 @@ pub extern "C" fn wasmi_context_set_fuel(
) -> Option<Box<wasmi_error_t>> {
crate::handle_result(store.set_fuel(fuel), |()| {})
}
+
+////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////
+////////////////////////////////////////////////////////////////////////////////////////
+
+/// Returns the current fuel of the wasm store context in `fuel`.
+///
@@ -137,8 +282,6 @@ index 56d4898f..543dbff8 100644
+ store: &mut wasm_store_t,
+ fuel: u64,
+) -> Option<Box<wasmi_error_t>> {
+
+ let mut context = unsafe { store.inner.context_mut() };
+ crate::handle_result(context.set_fuel(fuel), |()| {})
+}
+

View File

@@ -15,11 +15,12 @@ namespace test {
struct TestLedgerDataProvider : public HostFunctions
{
jtx::Env* env_;
jtx::Env& env_;
void const* rt_ = nullptr;
public:
TestLedgerDataProvider(jtx::Env* env) : env_(env)
TestLedgerDataProvider(jtx::Env& env)
: HostFunctions(env.journal), env_(env)
{
}
@@ -38,7 +39,7 @@ public:
Expected<std::int32_t, HostFunctionError>
getLedgerSqn() override
{
return env_->current()->seq();
return env_.current()->seq();
}
};
@@ -52,7 +53,7 @@ struct TestHostFunctions : public HostFunctions
public:
TestHostFunctions(test::jtx::Env& env, int cd = 0)
: env_(env), clock_drift_(cd)
: HostFunctions(env.journal), env_(env), clock_drift_(cd)
{
accountID_ = env_.master.id();
std::string t = "10000";
@@ -71,12 +72,6 @@ public:
return rt_;
}
beast::Journal
getJournal() override
{
return env_.journal;
}
Expected<std::int32_t, HostFunctionError>
getLedgerSqn() override
{
@@ -395,7 +390,7 @@ public:
#endif
if (!asHex)
{
j << "WAMR TRACE: " << msg << " "
j << "WASM TRACE: " << msg << " "
<< std::string_view(
reinterpret_cast<char const*>(data.data()), data.size());
}
@@ -405,7 +400,7 @@ public:
hex.reserve(data.size() * 2);
boost::algorithm::hex(
data.begin(), data.end(), std::back_inserter(hex));
j << "WAMR DEV TRACE: " << msg << " " << hex;
j << "WASM DEV TRACE: " << msg << " " << hex;
}
#ifdef DEBUG_OUTPUT
@@ -423,7 +418,7 @@ public:
#else
auto j = getJournal().trace();
#endif
j << "WAMR TRACE NUM: " << msg << " " << data;
j << "WASM TRACE NUM: " << msg << " " << data;
#ifdef DEBUG_OUTPUT
j << std::endl;
@@ -435,7 +430,7 @@ public:
traceAccount(std::string_view const& msg, AccountID const& account) override
{
#ifdef DEBUG_OUTPUT
auto j = getJournal().error();
auto& j = std::cerr;
#else
auto j = getJournal().trace();
#endif
@@ -444,7 +439,7 @@ public:
auto const accountStr = toBase58(account);
j << "WAMR TRACE ACCOUNT: " << msg << " " << accountStr;
j << "WASM TRACE ACCOUNT: " << msg << " " << accountStr;
return msg.size() + accountStr.size();
}
@@ -457,7 +452,7 @@ public:
auto j = getJournal().trace();
#endif
auto const s = floatToString(data);
j << "WAMR TRACE FLOAT: " << msg << " " << s;
j << "WASM TRACE FLOAT: " << msg << " " << s;
#ifdef DEBUG_OUTPUT
j << std::endl;
@@ -469,12 +464,12 @@ public:
traceAmount(std::string_view const& msg, STAmount const& amount) override
{
#ifdef DEBUG_OUTPUT
auto j = getJournal().error();
auto& j = std::cerr;
#else
auto j = getJournal().trace();
#endif
auto const amountStr = amount.getFullText();
j << "WAMR TRACE AMOUNT: " << msg << " " << amountStr;
j << "WASM TRACE AMOUNT: " << msg << " " << amountStr;
return msg.size() + amountStr.size();
}
@@ -548,15 +543,13 @@ public:
struct TestHostFunctionsSink : public TestHostFunctions
{
test::StreamSink sink_;
beast::Journal jlog_;
void const* rt_ = nullptr;
public:
explicit TestHostFunctionsSink(test::jtx::Env& env, int cd = 0)
: TestHostFunctions(env, cd)
, sink_(beast::severities::kDebug)
, jlog_(sink_)
: TestHostFunctions(env, cd), sink_(beast::severities::kDebug)
{
j_ = beast::Journal(sink_);
}
test::StreamSink&
@@ -564,12 +557,6 @@ public:
{
return sink_;
}
beast::Journal
getJournal() override
{
return jlog_;
}
};
struct PerfHostFunctions : public TestHostFunctions
@@ -1284,14 +1271,14 @@ struct PerfHostFunctions : public TestHostFunctions
{
if (!account || !nftId)
{
getJournal().trace() << "WAMR getNFT: Invalid account or NFT ID";
getJournal().trace() << "WASM getNFT: Invalid account or NFT ID";
return Unexpected(HostFunctionError::INVALID_PARAMS);
}
auto obj = nft::findToken(*env_.current(), account, nftId);
if (!obj)
{
getJournal().trace() << "WAMR getNFT: NFT not found";
getJournal().trace() << "WASM getNFT: NFT not found";
return Unexpected(HostFunctionError::LEDGER_OBJ_NOT_FOUND);
}

View File

@@ -67,7 +67,7 @@ struct Wasm_test : public beast::unit_test::suite
// clang-format on
auto& vm = WasmEngine::instance();
std::vector<WasmImportFunc> imports;
ImportVec imports;
WasmImpFunc<Add_proto>(
imports, "func-add", reinterpret_cast<void*>(&Add));
@@ -90,7 +90,7 @@ struct Wasm_test : public beast::unit_test::suite
using namespace test::jtx;
Env env{*this};
HostFunctions hfs;
HostFunctions hfs(env.journal);
{
auto wasmHex = "00000000";
@@ -98,7 +98,7 @@ struct Wasm_test : public beast::unit_test::suite
std::vector<uint8_t> wasm(wasmStr.begin(), wasmStr.end());
std::string funcName("mock_escrow");
auto re = runEscrowWasm(wasm, funcName, {}, &hfs, 15, env.journal);
auto re = runEscrowWasm(wasm, hfs, funcName, {}, 15);
BEAST_EXPECT(!re);
}
@@ -108,8 +108,7 @@ struct Wasm_test : public beast::unit_test::suite
std::vector<uint8_t> wasm(wasmStr.begin(), wasmStr.end());
std::string funcName("mock_escrow");
auto const re =
preflightEscrowWasm(wasm, funcName, {}, &hfs, env.journal);
auto const re = preflightEscrowWasm(wasm, hfs, funcName);
BEAST_EXPECT(!isTesSuccess(re));
}
@@ -132,8 +131,8 @@ struct Wasm_test : public beast::unit_test::suite
auto wasmStr = boost::algorithm::unhex(std::string(badWasmHex));
std::vector<uint8_t> wasm(wasmStr.begin(), wasmStr.end());
auto const re = preflightEscrowWasm(
wasm, ESCROW_FUNCTION_NAME, {}, &hfs, env.journal);
auto const re =
preflightEscrowWasm(wasm, hfs, ESCROW_FUNCTION_NAME);
BEAST_EXPECT(!isTesSuccess(re));
}
}
@@ -149,9 +148,9 @@ struct Wasm_test : public beast::unit_test::suite
using namespace test::jtx;
Env env{*this};
TestLedgerDataProvider hf(&env);
TestLedgerDataProvider hf(env);
std::vector<WasmImportFunc> imports;
ImportVec imports;
WASM_IMPORT_FUNC2(imports, getLedgerSqn, "get_ledger_sqn", &hf, 33);
auto& engine = WasmEngine::instance();
@@ -164,11 +163,10 @@ struct Wasm_test : public beast::unit_test::suite
1'000'000,
env.journal);
// code takes 11 gas + 1 getLedgerSqn call
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 0, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 5, std::to_string(re->cost));
BEAST_EXPECTS(re->cost == 38, std::to_string(re->cost));
}
env.close();
@@ -178,11 +176,10 @@ struct Wasm_test : public beast::unit_test::suite
re = engine.run(
{}, ESCROW_FUNCTION_NAME, {}, imports, &hf, 1'000'000, env.journal);
// code takes 22 gas + 2 getLedgerSqn calls
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 5, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 10, std::to_string(re->cost));
BEAST_EXPECTS(re->cost == 76, std::to_string(re->cost));
}
}
@@ -248,41 +245,6 @@ struct Wasm_test : public beast::unit_test::suite
}
}
void
testWasmSP1Verifier()
{
testcase("Wasm sp1 zkproof verifier");
auto const ws = boost::algorithm::unhex(sp1WasmHex);
Bytes const wasm(ws.begin(), ws.end());
auto& engine = WasmEngine::instance();
auto const re = engine.run(wasm, "sp1_groth16_verifier");
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 1, std::to_string(re->result));
BEAST_EXPECTS(
re->cost == 4'191'711'969ll, std::to_string(re->cost));
}
}
void
testWasmBG16Verifier()
{
testcase("Wasm BG16 zkproof verifier");
auto const ws = boost::algorithm::unhex(zkProofWasmHex);
Bytes const wasm(ws.begin(), ws.end());
auto& engine = WasmEngine::instance();
auto const re = engine.run(wasm, "bellman_groth16_test");
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 1, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 270'282'552, std::to_string(re->cost));
}
}
void
testHFCost()
{
@@ -299,9 +261,9 @@ struct Wasm_test : public beast::unit_test::suite
auto& engine = WasmEngine::instance();
TestHostFunctions hfs(env, 0);
std::vector<WasmImportFunc> imp = createWasmImport(&hfs);
ImportVec imp = createWasmImport(hfs);
for (auto& i : imp)
i.gas = 0;
i.second.gas = 0;
auto re = engine.run(
wasm,
@@ -335,7 +297,7 @@ struct Wasm_test : public beast::unit_test::suite
auto& engine = WasmEngine::instance();
TestHostFunctions hfs(env, 0);
std::vector<WasmImportFunc> const imp = createWasmImport(&hfs);
ImportVec const imp = createWasmImport(hfs);
auto re = engine.run(
wasm,
@@ -349,7 +311,31 @@ struct Wasm_test : public beast::unit_test::suite
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 1, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 842, std::to_string(re->cost));
BEAST_EXPECTS(re->cost == 40'102, std::to_string(re->cost));
}
env.close();
}
// not enough gas
{
std::string const wasmHex = allHostFunctionsWasmHex;
std::string const wasmStr = boost::algorithm::unhex(wasmHex);
std::vector<uint8_t> const wasm(wasmStr.begin(), wasmStr.end());
auto& engine = WasmEngine::instance();
TestHostFunctions hfs(env, 0);
ImportVec const imp = createWasmImport(hfs);
auto re = engine.run(
wasm, ESCROW_FUNCTION_NAME, {}, imp, &hfs, 200, env.journal);
if (BEAST_EXPECT(!re))
{
BEAST_EXPECTS(
re.error() == tecFAILED_PROCESSING,
std::to_string(TERtoInt(re.error())));
}
env.close();
@@ -370,11 +356,22 @@ struct Wasm_test : public beast::unit_test::suite
{
TestHostFunctions nfs(env, 0);
auto re =
runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs, 100'000);
runEscrowWasm(wasm, nfs, ESCROW_FUNCTION_NAME, {}, 100'000);
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 1, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 842, std::to_string(re->cost));
BEAST_EXPECTS(re->cost == 40'102, std::to_string(re->cost));
}
}
{
// max<int64_t>() gas
TestHostFunctions nfs(env, 0);
auto re = runEscrowWasm(wasm, nfs, ESCROW_FUNCTION_NAME, {}, -1);
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 1, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 40'102, std::to_string(re->cost));
}
}
@@ -392,11 +389,11 @@ struct Wasm_test : public beast::unit_test::suite
};
BadTestHostFunctions nfs(env);
auto re =
runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs, 100'000);
runEscrowWasm(wasm, nfs, ESCROW_FUNCTION_NAME, {}, 100'000);
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == -201, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 262, std::to_string(re->cost));
BEAST_EXPECTS(re->cost == 5'012, std::to_string(re->cost));
}
}
@@ -414,11 +411,11 @@ struct Wasm_test : public beast::unit_test::suite
};
BadTestHostFunctions nfs(env);
auto re =
runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs, 100'000);
runEscrowWasm(wasm, nfs, ESCROW_FUNCTION_NAME, {}, 100'000);
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == -201, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 262, std::to_string(re->cost));
BEAST_EXPECTS(re->cost == 5'012, std::to_string(re->cost));
}
}
@@ -428,8 +425,8 @@ struct Wasm_test : public beast::unit_test::suite
std::vector<uint8_t> wasm(wasmStr.begin(), wasmStr.end());
TestHostFunctionsSink nfs(env);
std::string funcName("recursive");
auto re = runEscrowWasm(wasm, funcName, {}, &nfs, 1'000'000'000);
std::string funcName("finish");
auto re = runEscrowWasm(wasm, nfs, funcName, {}, 1'000'000'000);
BEAST_EXPECT(!re && re.error());
// std::cout << "bad case (deep recursion) result " << re.error()
// << std::endl;
@@ -450,15 +447,16 @@ struct Wasm_test : public beast::unit_test::suite
auto const s = sink.messages().str();
BEAST_EXPECT(
countSubstr(s, "WASMI Error: failure to call func") == 1);
BEAST_EXPECT(countSubstr(s, "exception: <recursive> failure") > 0);
BEAST_EXPECT(countSubstr(s, "exception: <finish> failure") > 0);
}
{
// expected import not provided
auto wasmStr = boost::algorithm::unhex(ledgerSqnWasmHex);
Bytes wasm(wasmStr.begin(), wasmStr.end());
TestLedgerDataProvider ledgerDataProvider(&env);
TestLedgerDataProvider ledgerDataProvider(env);
std::vector<WasmImportFunc> imports;
ImportVec imports;
WASM_IMPORT_FUNC2(
imports, getLedgerSqn, "get_ledger_sqn2", &ledgerDataProvider);
@@ -469,11 +467,58 @@ struct Wasm_test : public beast::unit_test::suite
ESCROW_FUNCTION_NAME,
{},
imports,
nullptr,
&ledgerDataProvider,
1'000'000,
env.journal);
BEAST_EXPECT(!re);
}
{
// bad import format
auto wasmStr = boost::algorithm::unhex(ledgerSqnWasmHex);
Bytes wasm(wasmStr.begin(), wasmStr.end());
TestLedgerDataProvider ledgerDataProvider(env);
ImportVec imports;
WASM_IMPORT_FUNC2(
imports, getLedgerSqn, "get_ledger_sqn", &ledgerDataProvider);
imports[0].first = nullptr;
auto& engine = WasmEngine::instance();
auto re = engine.run(
wasm,
ESCROW_FUNCTION_NAME,
{},
imports,
&ledgerDataProvider,
1'000'000,
env.journal);
BEAST_EXPECT(!re);
}
{
// bad function name
auto wasmStr = boost::algorithm::unhex(ledgerSqnWasmHex);
Bytes wasm(wasmStr.begin(), wasmStr.end());
TestLedgerDataProvider ledgerDataProvider(env);
ImportVec imports;
WASM_IMPORT_FUNC2(
imports, getLedgerSqn, "get_ledger_sqn", &ledgerDataProvider);
auto& engine = WasmEngine::instance();
auto re = engine.run(
wasm,
"func1",
{},
imports,
&ledgerDataProvider,
1'000'000,
env.journal);
// expected import not provided
BEAST_EXPECT(!re);
}
}
@@ -494,11 +539,11 @@ struct Wasm_test : public beast::unit_test::suite
std::vector<uint8_t> const wasm(wasmStr.begin(), wasmStr.end());
TestHostFunctions hf(env, 0);
auto re = runEscrowWasm(wasm, funcName, {}, &hf, 100'000);
auto re = runEscrowWasm(wasm, hf, funcName, {}, 100'000);
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 1, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 326, std::to_string(re->cost));
BEAST_EXPECTS(re->cost == 97'356, std::to_string(re->cost));
}
env.close();
}
@@ -509,11 +554,11 @@ struct Wasm_test : public beast::unit_test::suite
std::vector<uint8_t> const wasm(wasmStr.begin(), wasmStr.end());
TestHostFunctions hf(env, 0);
auto re = runEscrowWasm(wasm, funcName, {}, &hf, 100'000);
auto re = runEscrowWasm(wasm, hf, funcName, {}, 100'000);
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECTS(re->result == 1, std::to_string(re->result));
BEAST_EXPECTS(re->cost == 34, std::to_string(re->cost));
BEAST_EXPECTS(re->cost == 2'054, std::to_string(re->cost));
}
env.close();
}
@@ -597,7 +642,7 @@ struct Wasm_test : public beast::unit_test::suite
PerfHostFunctions nfs(env, k, env.tx());
auto re = runEscrowWasm(wasm, ESCROW_FUNCTION_NAME, {}, &nfs);
auto re = runEscrowWasm(wasm, nfs, ESCROW_FUNCTION_NAME);
if (BEAST_EXPECT(re.has_value()))
{
BEAST_EXPECT(re->result);
@@ -627,9 +672,8 @@ struct Wasm_test : public beast::unit_test::suite
Bytes const wasm(wasmStr.begin(), wasmStr.end());
TestHostFunctions hfs(env, 0);
auto const allowance = 1'814;
auto re = runEscrowWasm(
wasm, ESCROW_FUNCTION_NAME, {}, &hfs, allowance, env.journal);
auto const allowance = 153'534;
auto re = runEscrowWasm(wasm, hfs, ESCROW_FUNCTION_NAME, {}, allowance);
if (BEAST_EXPECT(re.has_value()))
{
@@ -653,8 +697,7 @@ struct Wasm_test : public beast::unit_test::suite
{
// f32 set constant, opcode disabled exception
auto const re =
runEscrowWasm(wasm, funcName, {}, &hfs, 1'000'000, env.journal);
auto const re = runEscrowWasm(wasm, hfs, funcName, {}, 1'000'000);
if (BEAST_EXPECT(!re.has_value()))
{
BEAST_EXPECT(re.error() == tecFAILED_PROCESSING);
@@ -664,8 +707,7 @@ struct Wasm_test : public beast::unit_test::suite
{
// f32 add, can't create module exception
wasm[0x117] = 0x92;
auto const re =
runEscrowWasm(wasm, funcName, {}, &hfs, 1'000'000, env.journal);
auto const re = runEscrowWasm(wasm, hfs, funcName, {}, 1'000'000);
if (BEAST_EXPECT(!re.has_value()))
{
BEAST_EXPECT(re.error() == tecFAILED_PROCESSING);
@@ -687,17 +729,12 @@ struct Wasm_test : public beast::unit_test::suite
testWasmSha();
testWasmB58();
// running too long
// testWasmSP1Verifier();
testWasmBG16Verifier();
testHFCost();
testEscrowWasmDN();
testFloat();
testCodecovWasm();
// testDisabledFloat();
testDisabledFloat();
// perfTest();
}

File diff suppressed because it is too large Load Diff

View File

@@ -16,10 +16,6 @@ extern std::string const b58WasmHex;
extern std::string const sha512PureWasmHex;
extern std::string const zkProofWasmHex;
extern std::string const sp1WasmHex;
extern std::string const hfPerfTest;
extern std::string const allKeyletsWasmHex;

File diff suppressed because it is too large Load Diff

View File

@@ -1,16 +0,0 @@
[package]
edition = "2021"
name = "sp1"
version = "0.0.1"
[lib]
crate-type = ["cdylib"]
[dependencies]
sp1-verifier = "4.1.3"
[profile.release]
opt-level = 3 # "z" for size or "3" for speed
lto = true # Link Time Optimization
codegen-units = 1 # Single unit = better optimization
panic = "abort" # Smaller binary, faster execution

View File

@@ -1,37 +0,0 @@
use sp1_verifier::Groth16Verifier;
#[no_mangle]
pub fn sp1_groth16_verifier() -> bool {
let groth16_vk = *sp1_verifier::GROTH16_VK_BYTES;
let proof: Vec<u8> = vec![
17, 182, 160, 157, 31, 189, 116, 200, 17, 224, 230, 34, 195, 108, 230, 185, 62, 91, 181,
212, 80, 111, 197, 89, 247, 206, 99, 206, 147, 13, 216, 101, 252, 192, 149, 2, 40, 4, 249,
44, 97, 227, 127, 36, 244, 18, 27, 75, 248, 3, 45, 11, 103, 45, 183, 204, 61, 217, 19, 208,
66, 73, 202, 108, 136, 162, 221, 184, 6, 189, 49, 196, 104, 128, 151, 21, 104, 109, 145,
150, 243, 51, 27, 243, 203, 75, 176, 59, 193, 51, 177, 64, 83, 13, 133, 140, 248, 242, 13,
24, 12, 103, 126, 112, 244, 181, 129, 246, 52, 110, 134, 57, 149, 23, 163, 43, 202, 7, 164,
233, 179, 160, 16, 5, 22, 45, 129, 76, 183, 76, 150, 139, 27, 224, 191, 59, 47, 105, 71,
47, 8, 176, 157, 159, 234, 253, 239, 131, 138, 120, 101, 4, 98, 236, 106, 235, 98, 76, 93,
220, 174, 153, 58, 216, 28, 141, 129, 191, 188, 40, 184, 225, 22, 61, 75, 139, 159, 162,
117, 83, 214, 239, 1, 246, 236, 255, 64, 228, 116, 107, 206, 23, 59, 3, 221, 95, 14, 170,
28, 171, 36, 179, 75, 101, 177, 40, 198, 12, 193, 82, 105, 155, 177, 62, 158, 72, 209, 252,
51, 169, 109, 32, 121, 179, 194, 73, 164, 14, 8, 206, 181, 9, 5, 38, 74, 136, 97, 0, 89,
80, 75, 88, 228, 94, 46, 196, 199, 83, 229, 11, 103, 115, 25, 31, 215, 137, 65, 159, 95,
192,
];
let sp1_public_values = vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 8,
];
let sp1_vkey_hash: String =
"0x00aea8e9c83c73d74036923de1b4a66d18547d58eee4eacfee70235ed291954c".to_string();
let _ = Groth16Verifier::verify(&proof, &sp1_public_values, &sp1_vkey_hash, groth16_vk);
true
}

View File

@@ -0,0 +1,29 @@
(module
;; Define a Mutable Global Variable to act as our counter.
;; We initialize it to 1,000,000.
(global $counter (mut i32) (i32.const 1000000))
(func $finish (result i32)
;; 1. Check if counter == 0 (Base Case)
global.get $counter
i32.eqz
if
;; If counter is 0, we are done. Return 1.
i32.const 1
return
end
;; 2. Decrement the Global Counter
global.get $counter
i32.const 1
i32.sub
global.set $counter
;; 3. Recursive Step: Call SELF
;; This puts an i32 (1) on the stack when it returns.
call $finish
)
;; Export the only function we have
(export "finish" (func $finish))
)

View File

@@ -1,106 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "bls12_381"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7bc6d6292be3a19e6379786dac800f551e5865a5bb51ebbe3064ab80433f403"
dependencies = [
"ff",
"group",
"pairing",
"rand_core",
"subtle",
]
[[package]]
name = "ff"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0b50bfb653653f9ca9095b427bed08ab8d75a137839d9ad64eb11810d5b6393"
dependencies = [
"bitvec",
"rand_core",
"subtle",
]
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "group"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0f9ef7462f7c099f518d754361858f86d8a07af53ba9af0fe635bbccb151a63"
dependencies = [
"ff",
"rand_core",
"subtle",
]
[[package]]
name = "pairing"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81fec4625e73cf41ef4bb6846cafa6d44736525f442ba45e407c4a000a13996f"
dependencies = [
"group",
]
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "wyz"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
dependencies = [
"tap",
]
[[package]]
name = "zk_proof"
version = "0.0.1"
dependencies = [
"bls12_381",
"group",
]

View File

@@ -1,19 +0,0 @@
[package]
edition = "2021"
name = "zk_proof"
version = "0.0.1"
[lib]
crate-type = ["cdylib"]
[dependencies]
# bellman = "=0.14.0"
bls12_381 = "=0.8.0"
group = "0.13.0"
[profile.release]
# opt-level = 3 # Optimize for time
opt-level = "z" # Optimize for size
lto = true # Enable Link Time Optimization
codegen-units = 1
panic = "abort" # Remove unnecessary panic machinery

View File

@@ -1,254 +0,0 @@
use bls12_381::multi_miller_loop;
use bls12_381::Scalar;
use bls12_381::{G1Affine, G2Affine, G2Prepared};
use group::prime::PrimeCurveAffine;
use group::Curve;
use std::mem;
use std::os::raw::c_void;
// use std::time::{Instant};
// Groth16 proof struct
pub struct Proof {
pub a: G1Affine,
pub b: G2Affine,
pub c: G1Affine,
}
// Groth16 verification key struct
pub struct VerifyingKey {
pub alpha_g1: G1Affine,
pub beta_g1: G1Affine,
pub beta_g2: G2Affine,
pub gamma_g2: G2Affine,
pub delta_g1: G1Affine,
pub delta_g2: G2Affine,
pub ic: Vec<G1Affine>,
}
#[no_mangle]
pub extern "C" fn allocate(size: usize) -> *mut c_void {
let mut buffer = Vec::with_capacity(size);
let pointer = buffer.as_mut_ptr();
mem::forget(buffer);
pointer as *mut c_void
// }
}
#[no_mangle]
fn deserialize_g1_wasm(buffer: &mut Vec<u8>) -> G1Affine {
let d_g1 = G1Affine::from_compressed(&buffer[0..48].try_into().unwrap())
.expect("Failed to deserialize vk");
d_g1
}
fn deserialize_g2_wasm(buffer: &mut Vec<u8>) -> G2Affine {
let d_g2 = G2Affine::from_compressed(&buffer[0..96].try_into().unwrap())
.expect("Failed to deserialize vk");
d_g2
}
#[no_mangle]
// pub extern fn bellman_groth16_test(pointer: *mut u8, capacity: usize) -> bool {
pub extern "C" fn bellman_groth16_test() -> bool {
// let mut bytes = Vec::new();
// unsafe {
// // println!("Test in vm {:?}", pointer);
// let v = Vec::from_raw_parts(pointer, capacity, capacity); //TODO no need to deallocate??
// bytes.extend_from_slice(&v);
// }
// Hardcode the input bytes for testing in different WASM VMs
// let bytes = [172, 197, 81, 189, 121, 193, 159, 27, 92, 95, 151, 164, 40, 59, 214, 96, 132, 58, 87, 37, 169, 1, 63, 230, 35, 74, 245, 6, 185, 56, 120, 108, 214, 179, 187, 21, 36, 206, 43, 160, 10, 250, 249, 73, 210, 35, 137, 87, 177, 66, 65, 154, 11, 232, 137, 246, 125, 72, 227, 222, 116, 168, 87, 24, 165, 160, 132, 109, 108, 101, 222, 143, 78, 97, 48, 95, 59, 177, 29, 247, 219, 166, 73, 249, 69, 206, 15, 151, 30, 248, 235, 63, 148, 240, 17, 22, 150, 67, 252, 141, 95, 179, 94, 111, 207, 201, 192, 144, 154, 94, 21, 2, 22, 58, 96, 144, 227, 107, 107, 182, 142, 0, 57, 27, 168, 39, 226, 40, 163, 159, 112, 83, 196, 182, 215, 74, 92, 20, 158, 60, 23, 184, 198, 143, 17, 6, 242, 7, 75, 220, 87, 47, 224, 145, 99, 169, 203, 218, 112, 185, 51, 102, 59, 56, 171, 46, 49, 255, 116, 108, 241, 50, 180, 247, 62, 218, 181, 197, 155, 80, 61, 252, 8, 41, 232, 73, 51, 250, 223, 82, 94, 8, 185, 83, 223, 187, 6, 41, 20, 62, 189, 254, 11, 11, 58, 187, 200, 88, 53, 234, 98, 172, 213, 62, 22, 34, 90, 166, 182, 133, 8, 230, 103, 219, 233, 141, 10, 137, 210, 151, 4, 129, 29, 92, 103, 251, 72, 182, 162, 59, 20, 222, 188, 232, 13, 74, 214, 182, 172, 120, 33, 198, 57, 204, 134, 93, 26, 79, 213, 45, 146, 6, 128, 103, 63, 202, 226, 120, 141, 193, 248, 65, 196, 235, 21, 184, 104, 228, 206, 117, 190, 28, 153, 183, 68, 36, 63, 60, 131, 87, 137, 213, 105, 27, 110, 37, 238, 200, 250, 145, 76, 25, 57, 81, 69, 164, 208, 255, 49, 80, 14, 64, 181, 143, 12, 58, 35, 63, 199, 35, 70, 25, 86, 158, 210, 150, 59, 159, 253, 238, 174, 211, 142, 166, 223, 51, 134, 118, 171, 27, 218, 219, 117, 163, 71, 134, 95, 142, 83, 251, 240, 241, 162, 232, 93, 248, 167, 112, 197, 212, 169, 209, 159, 101, 140, 248, 222, 234, 201, 169, 76, 242, 7, 10, 192, 30, 151, 167, 74, 186, 97, 121, 144, 36, 6, 187, 92, 7, 248, 45, 134, 85, 240, 112, 74, 224, 70, 64, 198, 59, 26, 195, 192, 140, 101, 118, 175, 17, 160, 195, 142, 133, 1, 139, 5, 130, 245, 17, 73, 176, 232, 107, 130, 172, 110, 20, 190, 37, 108, 250, 178, 187, 151, 158, 35, 248, 246, 143, 38, 212, 133, 226, 24, 45, 33, 164, 46, 125, 200, 157, 253, 225, 132, 181, 60, 90, 7, 240, 80, 232, 97, 206, 164, 28, 12, 75, 68, 126, 230, 145, 216, 45, 180, 203, 19, 152, 29, 203, 9, 4, 145, 122, 206, 146, 179, 44, 145, 191, 126, 199, 175, 171, 127, 189, 222, 108, 126, 161, 80, 190, 47, 44, 8, 40, 65, 68, 95, 61, 109, 148, 175, 113, 226, 8, 93, 126, 53, 39, 192, 196, 6, 152, 194, 105, 169, 226, 192, 201, 184, 198, 134, 210, 153, 170, 12, 241, 90, 250, 233, 20, 152, 119, 142, 120, 83, 2, 164, 80, 178, 125, 227, 253, 207, 240, 201, 127, 213, 196, 100, 90, 65, 120, 50, 108, 175, 34, 192, 197, 173, 202, 176, 210, 131, 22, 216, 57, 169, 241, 28, 40, 44, 62, 11, 42, 50, 46, 204, 242, 109, 158, 114, 41, 127, 206, 25, 194, 255, 128, 245, 232, 193, 189, 229, 51, 93, 94, 64, 117, 33, 132, 75, 253, 114, 64, 116, 155, 183, 137, 112, 201, 243, 13, 221, 142, 164, 59, 98, 152, 249, 40, 133, 70, 185, 231, 249, 151, 253, 240, 122, 214, 60, 18, 132, 177, 37, 42, 75, 206, 12, 100, 214, 248, 234, 78, 165, 74, 212, 248, 32, 162, 254, 227, 218, 46, 9, 87, 0, 118, 13, 249, 107, 83, 5, 138, 223, 9, 247, 70, 160, 228, 197, 54, 87, 18, 1, 37, 199, 162, 84, 189, 161, 10, 26, 75, 45, 168, 185, 153, 245, 243, 51, 176, 208, 187, 235, 135, 239, 231, 42, 43, 233, 150, 46, 249, 73, 229, 138, 84, 89, 75, 129, 238, 211, 80, 147, 67, 159, 227, 214, 131, 188, 130, 70, 224, 1, 77, 139, 239, 185, 53, 68, 41, 193, 207, 16, 2, 33, 139, 214, 103, 240, 14, 141, 223, 24, 236, 50, 64, 79, 178, 6, 79, 38, 165, 35, 173, 203, 101, 3, 162, 49, 51, 4, 151, 127, 49, 47, 223, 244, 157, 229, 7, 88, 106, 141, 167, 183, 220, 15, 8, 119, 12, 82, 218, 14, 207, 0, 73, 27, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let bytes = [
147, 235, 138, 182, 249, 146, 149, 28, 58, 36, 144, 99, 188, 155, 153, 135, 239, 79, 76,
109, 152, 156, 202, 1, 153, 84, 239, 184, 69, 145, 133, 48, 156, 80, 122, 227, 231, 161,
137, 232, 67, 183, 34, 186, 230, 135, 25, 90, 136, 201, 110, 134, 208, 93, 78, 82, 153,
239, 208, 236, 160, 231, 192, 150, 215, 128, 193, 255, 107, 39, 133, 12, 136, 148, 119, 17,
59, 198, 100, 49, 37, 89, 132, 205, 45, 79, 151, 112, 247, 140, 94, 179, 215, 165, 52, 182,
153, 68, 204, 210, 218, 156, 69, 74, 192, 30, 160, 13, 80, 188, 23, 112, 21, 124, 91, 147,
21, 140, 217, 226, 248, 60, 182, 119, 18, 34, 32, 41, 181, 128, 165, 97, 168, 76, 98, 44,
114, 122, 128, 215, 68, 156, 18, 91, 5, 33, 22, 141, 249, 137, 49, 252, 82, 122, 206, 58,
183, 108, 176, 15, 38, 183, 87, 254, 34, 102, 195, 78, 166, 227, 96, 180, 137, 173, 131,
178, 179, 25, 89, 159, 5, 73, 125, 24, 25, 86, 227, 19, 184, 117, 228, 173, 150, 1, 82,
142, 48, 251, 236, 132, 73, 79, 201, 165, 192, 191, 195, 60, 100, 198, 251, 187, 161, 220,
63, 143, 38, 21, 189, 219, 194, 100, 64, 186, 102, 7, 186, 213, 227, 92, 228, 52, 181, 171,
223, 222, 218, 206, 221, 22, 15, 46, 77, 175, 34, 43, 221, 110, 21, 89, 149, 213, 68, 242,
140, 185, 176, 73, 88, 216, 75, 237, 209, 10, 75, 251, 152, 101, 15, 146, 168, 27, 81, 8,
61, 76, 103, 230, 171, 23, 144, 171, 6, 118, 157, 233, 234, 214, 132, 106, 30, 171, 121,
77, 147, 175, 170, 62, 48, 251, 12, 221, 202, 109, 80, 97, 180, 27, 45, 87, 162, 19, 168,
152, 27, 205, 113, 91, 83, 52, 99, 109, 17, 149, 189, 244, 174, 164, 192, 79, 133, 111,
195, 215, 232, 129, 166, 204, 3, 169, 248, 49, 18, 190, 198, 145, 177, 169, 10, 4, 66, 134,
46, 11, 163, 170, 94, 230, 234, 234, 43, 122, 51, 230, 100, 106, 149, 228, 208, 217, 87,
231, 125, 170, 47, 143, 151, 45, 208, 64, 91, 10, 188, 136, 15, 155, 131, 200, 141, 243,
200, 5, 109, 22, 98, 189, 193, 44, 40, 95, 126, 145, 234, 190, 205, 179, 172, 224, 147,
253, 238, 162, 157, 60, 126, 9, 174, 34, 16, 161, 197, 60, 243, 211, 241, 78, 114, 51, 167,
214, 53, 149, 172, 56, 149, 32, 66, 123, 48, 240, 179, 53, 154, 29, 134, 34, 141, 204, 168,
184, 158, 165, 115, 241, 119, 228, 11, 35, 82, 186, 132, 103, 65, 243, 215, 31, 105, 201,
191, 155, 210, 53, 194, 76, 63, 199, 181, 28, 138, 181, 181, 211, 145, 15, 139, 244, 38,
56, 159, 161, 95, 46, 147, 141, 163, 221, 88, 167, 134, 73, 45, 70, 98, 98, 167, 55, 52,
234, 110, 150, 79, 248, 157, 167, 84, 210, 89, 10, 193, 169, 32, 40, 218, 7, 236, 206, 85,
178, 174, 157, 132, 181, 192, 119, 60, 205, 46, 217, 120, 97, 59, 82, 121, 11, 189, 21,
213, 176, 255, 225, 57, 76, 239, 38, 99, 226, 55, 98, 227, 10, 45, 193, 69, 255, 247, 39,
121, 86, 150, 6, 220, 98, 41, 132, 237, 189, 169, 110, 213, 115, 33, 228, 197, 61, 219,
202, 58, 54, 70, 223, 179, 208, 139, 232, 103, 76, 165, 169, 68, 6, 148, 47, 244, 26, 203,
186, 110, 69, 44, 175, 128, 119, 212, 188, 167, 223, 87, 119, 238, 199, 201, 61, 78, 96,
175, 0, 156, 145, 196, 253, 162, 175, 172, 227, 80, 251, 96, 61, 189, 35, 13, 97, 22, 157,
86, 249, 128, 148, 172, 66, 80, 172, 208, 222, 131, 0, 207, 80, 163, 27, 155, 113, 57, 186,
246, 139, 111, 71, 117, 152, 184, 60, 1, 230, 44, 169, 213, 88, 82, 156, 194, 234, 41, 183,
87, 36, 175, 154, 156, 128, 59, 187, 208, 101, 9, 51, 205, 42, 174, 29, 215, 43, 150, 183,
129, 125, 2, 84, 210, 149, 245, 126, 140, 166, 255, 134, 116, 162, 107, 82, 178, 158, 38,
11, 135, 91, 224, 157, 112, 189, 164, 250, 1, 215, 49, 21, 214, 211, 73, 243, 251, 58, 198,
1, 165, 196, 122, 13, 238, 252, 227, 229, 149, 47, 13, 173, 171, 176, 185, 220, 82, 96,
163, 4, 36, 199, 152, 88, 3, 162, 49, 51, 4, 151, 127, 49, 47, 223, 244, 157, 229, 7, 88,
106, 141, 167, 183, 220, 15, 8, 119, 12, 82, 218, 14, 207, 0, 73, 27, 5, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
// ***** Test deserialization and reconstruction of vk *****
// let start_key_recons = Instant::now();
// println!("Start verification key reconstruction");
// alpha_g1
let mut vec_alpha_g1 = bytes[0..48].to_vec();
let r_alpha_g1 = deserialize_g1_wasm(&mut vec_alpha_g1);
// beta_g1
let mut vec_beta_g1 = bytes[48..96].to_vec();
let r_beta_g1 = deserialize_g1_wasm(&mut vec_beta_g1);
// beta_g2
let mut vec_beta_g2 = bytes[96..192].to_vec();
let r_beta_g2 = deserialize_g2_wasm(&mut vec_beta_g2);
// gamma_g2
let mut vec_gamma_g2 = bytes[192..288].to_vec();
let r_gamma_g2 = deserialize_g2_wasm(&mut vec_gamma_g2);
// delta_g1
let mut vec_delta_g1 = bytes[288..336].to_vec();
let r_delta_g1 = deserialize_g1_wasm(&mut vec_delta_g1);
// delta_g2
let mut vec_delta_g2 = bytes[336..432].to_vec();
let r_delta_g2 = deserialize_g2_wasm(&mut vec_delta_g2);
// ic
let vec_ic = bytes[432..576].to_vec();
// println!("\nic vector: {:?}", vec_ic);
let mut r_ic: Vec<G1Affine> = Vec::new();
let mut vec_ic_de = vec_ic[0..48].to_vec();
r_ic.push(deserialize_g1_wasm(&mut vec_ic_de));
vec_ic_de = vec_ic[48..96].to_vec();
r_ic.push(deserialize_g1_wasm(&mut vec_ic_de));
vec_ic_de = vec_ic[96..144].to_vec();
r_ic.push(deserialize_g1_wasm(&mut vec_ic_de));
// Reconstruct vk
// replace following if using bellman::{groth16, groth16::Proof};
// let deserialized_vk = groth16::VerifyingKey::<Bls12> {
let deserialized_vk = VerifyingKey {
alpha_g1: r_alpha_g1,
beta_g1: r_beta_g1,
beta_g2: r_beta_g2,
gamma_g2: r_gamma_g2,
delta_g1: r_delta_g1,
delta_g2: r_delta_g2,
ic: r_ic,
};
// Uncomment following if using bellman::{groth16, groth16::Proof};
// let pvk = groth16::prepare_verifying_key(&deserialized_vk);
// println!("Key reconstruction time: {:?}", start_key_recons.elapsed());
// ***** Reconstruct proof *****
// let start_proof_recons = Instant::now();
// proof.g1
let r_a = G1Affine::from_compressed(&bytes[576..624].try_into().unwrap())
.expect("Failed to deserialize a");
// proof.g2
let r_b = G2Affine::from_compressed(&bytes[624..720].try_into().unwrap())
.expect("Failed to deserialize b");
// proof.g1
let r_c = G1Affine::from_compressed(&bytes[720..768].try_into().unwrap())
.expect("Failed to deserialize c");
// Replace following if using bellman::{groth16, groth16::Proof};
// let r_proof: Proof<Bls12> = Proof{a: r_a, b: r_b, c: r_c};
let r_proof: Proof = Proof {
a: r_a,
b: r_b,
c: r_c,
};
// println!("Proof reconstruction time: {:?}", start_proof_recons.elapsed());
// ***** Reconstruct input *****
// let start_input_recons = Instant::now();
let last_64_bytes = &bytes[bytes.len() - 64..];
let r_inputs: Vec<Scalar> = last_64_bytes
.chunks(32) // Each Scalar in bls12_381 uses 32 bytes
.map(|chunk| {
Scalar::from_bytes(chunk.try_into().unwrap()).expect("Invalid bytes for Scalar")
})
.collect();
// println!("Input reconstruction time: {:?}", start_input_recons.elapsed());
/***** proof verification *****/
// uncomment following if bellman groth16 is used
// assert!(groth16::verify_proof(&pvk, &r_proof, &r_inputs).is_ok());
// let start_verify = Instant::now();
// Ensure the number of inputs matches the vk.ic length minus 1 (for IC[0])
if (r_inputs.len() + 1) != deserialized_vk.ic.len() {
return false;
}
/***** Compute linear combination: input_acc = IC[0] + sum(input[i] * IC[i+1]) *****/
let mut acc = deserialized_vk.ic[0].to_curve(); // Convert G1Affine to G1Projective
// Computes multi-scalar multiplication,
// which is a weighted sum of elliptic curve points.
// In Groth16, this builds the point:
// acc = IC₀ + input₁ × IC₁ + input₂ × IC₂ + ... + inputₙ × ICₙ
// Where: ICᵢ are fixed elliptic curve points (from the verifying key).
// inputᵢ are the public inputs to the circuit.
// Example: public_inputs = [x₁, x₂], vk.ic = [IC₀, IC₁, IC₂], acc = IC₀ + x₁ * IC₁ + x₂ * IC₂
// This binds the public inputs to the proof
for (input, ic_point) in r_inputs.iter().zip(&deserialized_vk.ic[1..]) {
acc += ic_point.to_curve() * input;
}
let acc_affine = acc.to_affine(); // converts the point acc from projective form back to affine form.
// Preparing G2 elements for pairing by converting them into G2Prepared format.
let proof_b_prepared = G2Prepared::from(r_proof.b);
let gamma_g2_prepared = G2Prepared::from(deserialized_vk.gamma_g2);
let delta_g2_prepared = G2Prepared::from(deserialized_vk.delta_g2);
let beta_g2_prepared = G2Prepared::from(deserialized_vk.beta_g2);
// Compute required product of pairings in their Miller loop form
// Groth16 verifier checks if e(A, B) * e(acc, γ)⁻¹ * e(C, δ)⁻¹ * e(α, β)⁻¹ == 1
// which boils down to
// let start_miller = Instant::now();
let ml_result = multi_miller_loop(&[
(&r_proof.a, &proof_b_prepared), // e(A,B)
(&(-acc_affine), &gamma_g2_prepared), // e(acc, γ)⁻¹
(&(-r_proof.c), &delta_g2_prepared), // e(C, δ)⁻¹
(&(-deserialized_vk.alpha_g1), &beta_g2_prepared), //e(α, β)⁻¹
]);
// println!("Miller time: {:?}", start_miller.elapsed());
// let start_final = Instant::now();
let result = ml_result.final_exponentiation();
// println!("Final time: {:?}", start_final.elapsed());
// println!("Proof verification time: {:?}", start_verify.elapsed());
// true
result == bls12_381::Gt::identity()
}

View File

@@ -34,6 +34,8 @@ enum class HostFunctionError : int32_t {
INDEX_OUT_OF_BOUNDS = -18,
FLOAT_INPUT_MALFORMED = -19,
FLOAT_COMPUTATION_ERROR = -20,
NO_RUNTIME = -21,
OUT_OF_GAS = -22,
};
inline int32_t
@@ -80,6 +82,14 @@ floatLogImpl(Slice const& x, int32_t mode);
struct HostFunctions
{
beast::Journal j_;
HostFunctions(
beast::Journal j = beast::Journal{beast::Journal::getNullSink()})
: j_(j)
{
}
// LCOV_EXCL_START
virtual void
setRT(void const*)
@@ -92,10 +102,22 @@ struct HostFunctions
return nullptr;
}
virtual beast::Journal
std::int64_t
getGas()
{
return -1;
}
void
setGas(std::int64_t)
{
return;
}
beast::Journal
getJournal()
{
return beast::Journal{beast::Journal::getNullSink()};
return j_;
}
virtual Expected<std::int32_t, HostFunctionError>

View File

@@ -42,8 +42,8 @@ class WasmHostFunctionsImpl : public HostFunctions
}
public:
WasmHostFunctionsImpl(ApplyContext& ctx, Keylet const& leKey)
: ctx(ctx), leKey(leKey)
WasmHostFunctionsImpl(ApplyContext& ct, Keylet const& leKey)
: HostFunctions(ct.journal), ctx(ct), leKey(leKey)
{
}
@@ -59,12 +59,6 @@ public:
return rt_;
}
beast::Journal
getJournal() override
{
return ctx.journal;
}
std::optional<Bytes> const&
getData() const
{

View File

@@ -41,12 +41,15 @@ struct WasmImportFunc
std::string name;
std::optional<WasmTypes> result;
std::vector<WasmTypes> params;
void* udata = nullptr;
// void* udata = nullptr;
// wasm_func_callback_with_env_t
void* wrap = nullptr;
uint32_t gas = 0;
};
typedef std::pair<void*, WasmImportFunc> WasmUserData;
typedef std::vector<WasmUserData> ImportVec;
#define WASM_IMPORT_FUNC(v, f, ...) \
WasmImpFunc<f##_proto>( \
v, #f, reinterpret_cast<void*>(&f##_wrap), ##__VA_ARGS__)
@@ -111,7 +114,7 @@ WasmImpFuncHelper(WasmImportFunc& e)
template <typename F>
void
WasmImpFunc(
std::vector<WasmImportFunc>& v,
ImportVec& v,
std::string_view imp_name,
void* f_wrap,
void* data = nullptr,
@@ -119,11 +122,10 @@ WasmImpFunc(
{
WasmImportFunc e;
e.name = imp_name;
e.udata = data;
e.wrap = f_wrap;
e.gas = gas;
WasmImpFuncHelper<F>(e);
v.push_back(std::move(e));
v.push_back(std::make_pair(data, std::move(e)));
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@@ -18,6 +18,8 @@ static std::string_view const W_PROC_EXIT = "proc_exit";
static std::string_view const ESCROW_FUNCTION_NAME = "finish";
uint32_t const MAX_PAGES = 128; // 8MB = 64KB*128
class WasmiEngine;
class WasmEngine
@@ -43,7 +45,7 @@ public:
run(Bytes const& wasmCode,
std::string_view funcName = {},
std::vector<WasmParam> const& params = {},
std::vector<WasmImportFunc> const& imports = {},
ImportVec const& imports = {},
HostFunctions* hfs = nullptr,
int64_t gasLimit = -1,
beast::Journal j = beast::Journal{beast::Journal::getNullSink()});
@@ -53,12 +55,13 @@ public:
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params = {},
std::vector<WasmImportFunc> const& imports = {},
ImportVec const& imports = {},
HostFunctions* hfs = nullptr,
beast::Journal j = beast::Journal{beast::Journal::getNullSink()});
// Host functions helper functionality
void*
newTrap(std::string_view msg = {});
newTrap(std::string const& txt = std::string());
beast::Journal
getJournal() const;
@@ -66,24 +69,22 @@ public:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<WasmImportFunc>
createWasmImport(HostFunctions* hfs);
ImportVec
createWasmImport(HostFunctions& hfs);
Expected<EscrowResult, TER>
runEscrowWasm(
Bytes const& wasmCode,
HostFunctions& hfs,
std::string_view funcName = ESCROW_FUNCTION_NAME,
std::vector<WasmParam> const& params = {},
HostFunctions* hfs = nullptr,
int64_t gasLimit = -1,
beast::Journal j = beast::Journal(beast::Journal::getNullSink()));
int64_t gasLimit = -1);
NotTEC
preflightEscrowWasm(
Bytes const& wasmCode,
HostFunctions& hfs,
std::string_view funcName = ESCROW_FUNCTION_NAME,
std::vector<WasmParam> const& params = {},
HostFunctions* hfs = nullptr,
beast::Journal j = beast::Journal(beast::Journal::getNullSink()));
std::vector<WasmParam> const& params = {});
} // namespace ripple

View File

@@ -48,11 +48,14 @@ struct WasmiResult
using ModulePtr = std::unique_ptr<wasm_module_t, decltype(&wasm_module_delete)>;
using InstancePtr =
std::unique_ptr<wasm_instance_t, decltype(&wasm_instance_delete)>;
using EnginePtr = std::unique_ptr<wasm_engine_t, decltype(&wasm_engine_delete)>;
using StorePtr = std::unique_ptr<wasm_store_t, decltype(&wasm_store_delete)>;
using FuncInfo = std::pair<wasm_func_t const*, wasm_functype_t const*>;
struct InstanceWrapper
{
wasm_store_t* store_ = nullptr;
wasm_extern_vec_t exports_;
InstancePtr instance_;
beast::Journal j_ = beast::Journal(beast::Journal::getNullSink());
@@ -91,11 +94,15 @@ public:
wmem
getMem() const;
std::int64_t
getGas() const;
std::int64_t setGas(std::int64_t) const;
};
struct ModuleWrapper
{
wasm_store_t* store_ = nullptr;
ModulePtr module_;
InstanceWrapper instanceWrap_;
wasm_exporttype_vec_t exportTypes_;
@@ -114,7 +121,7 @@ public:
wasm_store_t* s,
Bytes const& wasmBin,
bool instantiate,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
beast::Journal j);
~ModuleWrapper();
@@ -142,13 +149,13 @@ private:
static void
makeImpReturn(wasm_valtype_vec_t& v, WasmImportFunc const& imp);
wasm_extern_vec_t
buildImports(wasm_store_t* s, std::vector<WasmImportFunc> const& imports);
buildImports(wasm_store_t* s, ImportVec const& imports);
};
class WasmiEngine
{
std::unique_ptr<wasm_engine_t, decltype(&wasm_engine_delete)> engine_;
std::unique_ptr<wasm_store_t, decltype(&wasm_store_delete)> store_;
EnginePtr engine_;
StorePtr store_;
std::unique_ptr<ModuleWrapper> moduleWrap_;
beast::Journal j_ = beast::Journal(beast::Journal::getNullSink());
@@ -158,14 +165,14 @@ public:
WasmiEngine();
~WasmiEngine() = default;
static std::unique_ptr<wasm_engine_t, decltype(&wasm_engine_delete)>
static EnginePtr
init();
Expected<WasmResult<int32_t>, TER>
run(Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
HostFunctions* hfs,
int64_t gas,
beast::Journal j);
@@ -175,7 +182,8 @@ public:
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
HostFunctions* hfs,
beast::Journal j);
std::int64_t
@@ -183,7 +191,7 @@ public:
// Host functions helper functionality
wasm_trap_t*
newTrap(std::string_view msg);
newTrap(std::string const& msg);
beast::Journal
getJournal() const;
@@ -203,7 +211,7 @@ private:
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
HostFunctions* hfs,
int64_t gas);
@@ -212,14 +220,14 @@ private:
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports);
ImportVec const& imports);
int
addModule(
Bytes const& wasmCode,
bool instantiate,
int64_t gas,
std::vector<WasmImportFunc> const& imports);
ImportVec const& imports);
void
clearModules();

View File

@@ -1269,10 +1269,12 @@ floatPowerImpl(Slice const& x, int32_t n, int32_t mode)
return res.toBytes();
}
// LCOV_EXCL_START
catch (...)
{
}
return Unexpected(HostFunctionError::FLOAT_COMPUTATION_ERROR);
// LCOV_EXCL_STOP
}
Expected<Bytes, HostFunctionError>

View File

@@ -300,13 +300,52 @@ returnResult(
}
}
static inline HostFunctions*
getHF(void* env)
{
auto const* udata = reinterpret_cast<WasmUserData*>(env);
HostFunctions* hf = reinterpret_cast<HostFunctions*>(udata->first);
return hf;
}
static inline Expected<std::int64_t, wasm_trap_t*>
checkGas(void* env)
{
auto const* udata = reinterpret_cast<WasmUserData*>(env);
HostFunctions* hf = reinterpret_cast<HostFunctions*>(udata->first);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
if (!runtime)
{
wasm_trap_t* trap = reinterpret_cast<wasm_trap_t*>(
WasmEngine::instance().newTrap("hf no runtime")); // LCOV_EXCL_LINE
return Unexpected(trap); // LCOV_EXCL_LINE
}
int64_t const gas = runtime->getGas();
WasmImportFunc const& impFunc = udata->second;
int64_t const x = gas >= impFunc.gas ? gas - impFunc.gas : 0;
runtime->setGas(x);
if (gas < impFunc.gas)
{
wasm_trap_t* trap = reinterpret_cast<wasm_trap_t*>(
WasmEngine::instance().newTrap("hf out of gas"));
return Unexpected(trap);
}
return x;
}
//----------------------------------------------------------------------------------------------------------------------
wasm_trap_t*
getLedgerSqn_wrap(
void* env,
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -319,7 +358,9 @@ getParentLedgerTime_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -333,7 +374,9 @@ getParentLedgerHash_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -347,7 +390,9 @@ getBaseFee_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -360,7 +405,9 @@ isAmendmentEnabled_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -396,7 +443,9 @@ cacheLedgerObj_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -422,7 +471,9 @@ getTxField_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -441,7 +492,9 @@ getCurrentLedgerObjField_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -461,7 +514,9 @@ getLedgerObjField_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -487,7 +542,9 @@ getTxNestedField_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -507,7 +564,9 @@ getCurrentLedgerObjNestedField_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -530,7 +589,9 @@ getLedgerObjNestedField_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -560,7 +621,9 @@ getTxArrayLen_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -580,7 +643,9 @@ getCurrentLedgerObjArrayLen_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -604,7 +669,9 @@ getLedgerObjArrayLen_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -634,7 +701,9 @@ getTxNestedArrayLen_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -654,7 +723,9 @@ getCurrentLedgerObjNestedArrayLen_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -677,7 +748,9 @@ getLedgerObjNestedArrayLen_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -706,7 +779,9 @@ updateData_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -726,7 +801,9 @@ checkSignature_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -762,7 +839,9 @@ computeSha512HalfHash_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -781,7 +860,9 @@ accountKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -798,7 +879,9 @@ accountKeylet_wrap(
wasm_trap_t*
ammKeylet_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -828,7 +911,9 @@ checkKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -854,7 +939,9 @@ credentialKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -890,7 +977,9 @@ delegateKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -920,7 +1009,9 @@ depositPreauthKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -947,7 +1038,9 @@ depositPreauthKeylet_wrap(
wasm_trap_t*
didKeylet_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -967,7 +1060,9 @@ escrowKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -993,7 +1088,9 @@ lineKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1029,7 +1126,9 @@ mptIssuanceKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1059,7 +1158,9 @@ mptokenKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1095,7 +1196,9 @@ nftOfferKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1125,7 +1228,9 @@ offerKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1155,7 +1260,9 @@ oracleKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1180,7 +1287,9 @@ paychanKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1216,7 +1325,9 @@ permissionedDomainKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1246,7 +1357,9 @@ signersKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1266,7 +1379,9 @@ ticketKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1296,7 +1411,9 @@ vaultKeylet_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1323,7 +1440,9 @@ vaultKeylet_wrap(
wasm_trap_t*
getNFT_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1349,7 +1468,9 @@ getNFTIssuer_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1369,7 +1490,9 @@ getNFTTaxon_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1389,7 +1512,9 @@ getNFTFlags_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1409,7 +1534,9 @@ getNFTTransferFee_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1429,7 +1556,9 @@ getNFTSerial_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1446,7 +1575,9 @@ getNFTSerial_wrap(
wasm_trap_t*
trace_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
@@ -1480,7 +1611,9 @@ trace_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
wasm_trap_t*
traceNum_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int index = 0;
if (params->data[1].of.i32 > maxWasmParamLength)
@@ -1510,7 +1643,9 @@ traceAccount_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
if (params->data[1].of.i32 > maxWasmParamLength)
@@ -1535,7 +1670,9 @@ traceFloat_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
if (params->data[1].of.i32 > maxWasmParamLength)
@@ -1560,7 +1697,9 @@ traceAmount_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
if (params->data[1].of.i32 > maxWasmParamLength)
@@ -1600,7 +1739,9 @@ floatFromInt_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1624,7 +1765,9 @@ floatFromUint_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1645,7 +1788,9 @@ floatFromUint_wrap(
wasm_trap_t*
floatSet_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1673,7 +1818,9 @@ floatCompare_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1691,7 +1838,9 @@ floatCompare_wrap(
wasm_trap_t*
floatAdd_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1719,7 +1868,9 @@ floatSubtract_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1747,7 +1898,9 @@ floatMultiply_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1775,7 +1928,9 @@ floatDivide_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1800,7 +1955,9 @@ floatDivide_wrap(
wasm_trap_t*
floatRoot_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1828,7 +1985,9 @@ floatPower_wrap(
wasm_val_vec_t const* params,
wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;
@@ -1853,7 +2012,9 @@ floatPower_wrap(
wasm_trap_t*
floatLog_wrap(void* env, wasm_val_vec_t const* params, wasm_val_vec_t* results)
{
auto* hf = reinterpret_cast<HostFunctions*>(env);
if (auto g = checkGas(env); !g)
return g.error(); // LCOV_EXCL_LINE
auto* hf = getHF(env);
auto const* runtime = reinterpret_cast<InstanceWrapper const*>(hf->getRT());
int i = 0;

View File

@@ -16,7 +16,7 @@
namespace ripple {
static void
setCommonHostFunctions(HostFunctions* hfs, std::vector<WasmImportFunc>& i)
setCommonHostFunctions(HostFunctions* hfs, ImportVec& i)
{
// clang-format off
WASM_IMPORT_FUNC2(i, getLedgerSqn, "get_ledger_sqn", hfs, 60);
@@ -89,16 +89,13 @@ setCommonHostFunctions(HostFunctions* hfs, std::vector<WasmImportFunc>& i)
// clang-format on
}
std::vector<WasmImportFunc>
createWasmImport(HostFunctions* hfs)
ImportVec
createWasmImport(HostFunctions& hfs)
{
std::vector<WasmImportFunc> i;
ImportVec i;
if (hfs)
{
setCommonHostFunctions(hfs, i);
WASM_IMPORT_FUNC2(i, updateData, "update_data", hfs, 1000);
}
setCommonHostFunctions(&hfs, i);
WASM_IMPORT_FUNC2(i, updateData, "update_data", &hfs, 1000);
return i;
}
@@ -106,11 +103,10 @@ createWasmImport(HostFunctions* hfs)
Expected<EscrowResult, TER>
runEscrowWasm(
Bytes const& wasmCode,
HostFunctions& hfs,
std::string_view funcName,
std::vector<WasmParam> const& params,
HostFunctions* hfs,
int64_t gasLimit,
beast::Journal j)
int64_t gasLimit)
{
// create VM and set cost limit
auto& vm = WasmEngine::instance();
@@ -121,9 +117,9 @@ runEscrowWasm(
funcName,
params,
createWasmImport(hfs),
hfs,
&hfs,
gasLimit,
hfs ? hfs->getJournal() : j);
hfs.getJournal());
// std::cout << "runEscrowWasm, mod size: " << wasmCode.size()
// << ", gasLimit: " << gasLimit << ", funcName: " << funcName;
@@ -146,10 +142,9 @@ runEscrowWasm(
NotTEC
preflightEscrowWasm(
Bytes const& wasmCode,
HostFunctions& hfs,
std::string_view funcName,
std::vector<WasmParam> const& params,
HostFunctions* hfs,
beast::Journal j)
std::vector<WasmParam> const& params)
{
// create VM and set cost limit
auto& vm = WasmEngine::instance();
@@ -160,7 +155,8 @@ preflightEscrowWasm(
funcName,
params,
createWasmImport(hfs),
hfs ? hfs->getJournal() : j);
&hfs,
hfs.getJournal());
return ret;
}
@@ -183,7 +179,7 @@ WasmEngine::run(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
HostFunctions* hfs,
int64_t gasLimit,
beast::Journal j)
@@ -196,22 +192,25 @@ WasmEngine::check(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
HostFunctions* hfs,
beast::Journal j)
{
return impl->check(wasmCode, funcName, params, imports, j);
return impl->check(wasmCode, funcName, params, imports, hfs, j);
}
void*
WasmEngine::newTrap(std::string_view msg)
WasmEngine::newTrap(std::string const& msg)
{
return impl->newTrap(msg);
}
// LCOV_EXCL_START
beast::Journal
WasmEngine::getJournal() const
{
return impl->getJournal();
}
// LCOV_EXCL_STOP
} // namespace ripple

View File

@@ -75,18 +75,21 @@ InstanceWrapper::InstanceWrapper()
{
}
// LCOV_EXCL_START
InstanceWrapper::InstanceWrapper(InstanceWrapper&& o)
: exports_{0, nullptr}, instance_(nullptr, &wasm_instance_delete)
{
*this = std::move(o);
}
// LCOV_EXCL_STOP
InstanceWrapper::InstanceWrapper(
wasm_store_t* s,
wasm_module_t* m,
wasm_extern_vec_t const& imports,
beast::Journal j)
: exports_ WASM_EMPTY_VEC
: store_(s)
, exports_ WASM_EMPTY_VEC
, instance_(init(s, m, &exports_, imports, j))
, j_(j)
{
@@ -102,10 +105,12 @@ InstanceWrapper&
InstanceWrapper::operator=(InstanceWrapper&& o)
{
if (this == &o)
return *this;
return *this; // LCOV_EXCL_LINE
store_ = o.store_;
o.store_ = nullptr;
if (exports_.size)
wasm_extern_vec_delete(&exports_);
wasm_extern_vec_delete(&exports_); // LCOV_EXCL_LINE
exports_ = o.exports_;
o.exports_ = {0, nullptr};
@@ -130,12 +135,12 @@ InstanceWrapper::getFunc(
wasm_functype_t* ft = nullptr;
if (!instance_)
throw std::runtime_error("no instance");
throw std::runtime_error("no instance"); // LCOV_EXCL_LINE
if (!export_types.size)
throw std::runtime_error("no export");
throw std::runtime_error("no export"); // LCOV_EXCL_LINE
if (export_types.size != exports_.size)
throw std::runtime_error("invalid export");
throw std::runtime_error("invalid export"); // LCOV_EXCL_LINE
for (unsigned i = 0; i < export_types.size; ++i)
{
@@ -149,7 +154,8 @@ InstanceWrapper::getFunc(
{
auto* exn(exports_.data[i]);
if (wasm_extern_kind(exn) != WASM_EXTERN_FUNC)
throw std::runtime_error("invalid export");
throw std::runtime_error(
"invalid export"); // LCOV_EXCL_LINE
ft = wasm_externtype_as_functype(
const_cast<wasm_externtype_t*>(exn_type));
@@ -170,7 +176,7 @@ wmem
InstanceWrapper::getMem() const
{
if (!instance_)
throw std::runtime_error("no instance");
throw std::runtime_error("no instance"); // LCOV_EXCL_LINE
wasm_memory_t* mem = nullptr;
for (unsigned i = 0; i < exports_.size; ++i)
@@ -184,13 +190,43 @@ InstanceWrapper::getMem() const
}
if (!mem)
throw std::runtime_error("no memory exported");
throw std::runtime_error("no memory exported"); // LCOV_EXCL_LINE
return {
reinterpret_cast<std::uint8_t*>(wasm_memory_data(mem)),
wasm_memory_data_size(mem)};
}
std::int64_t
InstanceWrapper::getGas() const
{
if (!store_)
return -1; // LCOV_EXCL_LINE
std::uint64_t gas = 0;
wasm_store_get_fuel(store_, &gas);
return static_cast<std::int64_t>(gas);
}
std::int64_t
InstanceWrapper::setGas(std::int64_t gas) const
{
if (!store_)
return -1; // LCOV_EXCL_LINE
if (gas < 0)
gas = std::numeric_limits<decltype(gas)>::max();
wasmi_error_t* err =
wasm_store_set_fuel(store_, static_cast<std::uint64_t>(gas));
if (err)
{
print_wasm_error(
"Can't set instance gas", nullptr, j_); // LCOV_EXCL_LINE
throw std::runtime_error("Can't set instance gas"); // LCOV_EXCL_LINE
}
return gas;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
ModulePtr
@@ -204,6 +240,7 @@ ModuleWrapper::init(wasm_store_t* s, Bytes const& wasmBin, beast::Journal j)
return m;
}
// LCOV_EXCL_START
ModuleWrapper::ModuleWrapper()
: module_(nullptr, &wasm_module_delete), exportTypes_{0, nullptr}
{
@@ -214,14 +251,15 @@ ModuleWrapper::ModuleWrapper(ModuleWrapper&& o)
{
*this = std::move(o);
}
// LCOV_EXCL_STOP
ModuleWrapper::ModuleWrapper(
wasm_store_t* s,
Bytes const& wasmBin,
bool instantiate,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
beast::Journal j)
: store_(s), module_(init(s, wasmBin, j)), exportTypes_{0, nullptr}, j_(j)
: module_(init(s, wasmBin, j)), exportTypes_{0, nullptr}, j_(j)
{
wasm_module_exports(module_.get(), &exportTypes_);
if (instantiate)
@@ -238,14 +276,13 @@ ModuleWrapper::~ModuleWrapper()
wasm_exporttype_vec_delete(&exportTypes_);
}
// LCOV_EXCL_START
ModuleWrapper&
ModuleWrapper::operator=(ModuleWrapper&& o)
{
if (this == &o)
return *this;
store_ = o.store_;
o.store_ = nullptr;
module_ = std::move(o.module_);
instanceWrap_ = std::move(o.instanceWrap_);
if (exportTypes_.size)
@@ -262,6 +299,8 @@ ModuleWrapper::operator bool() const
return instanceWrap_;
}
// LCOV_EXCL_STOP
void
ModuleWrapper::makeImpParams(wasm_valtype_vec_t& v, WasmImportFunc const& imp)
{
@@ -284,8 +323,10 @@ ModuleWrapper::makeImpParams(wasm_valtype_vec_t& v, WasmImportFunc const& imp)
case WT_I64:
v.data[i] = wasm_valtype_new_i64();
break;
// LCOV_EXCL_START
default:
throw std::runtime_error("invalid import type");
// LCOV_EXCL_STOP
}
}
}
@@ -301,21 +342,21 @@ ModuleWrapper::makeImpReturn(wasm_valtype_vec_t& v, WasmImportFunc const& imp)
case WT_I32:
v.data[0] = wasm_valtype_new_i32();
break;
// LCOV_EXCL_START
case WT_I64:
v.data[0] = wasm_valtype_new_i64();
break;
default:
throw std::runtime_error("invalid return type");
// LCOV_EXCL_STOP
}
}
else
v = WASM_EMPTY_VEC;
v = WASM_EMPTY_VEC; // LCOV_EXCL_LINE
}
wasm_extern_vec_t
ModuleWrapper::buildImports(
wasm_store_t* s,
std::vector<WasmImportFunc> const& imports)
ModuleWrapper::buildImports(wasm_store_t* s, ImportVec const& imports)
{
wasm_importtype_vec_t importTypes = WASM_EMPTY_VEC;
wasm_module_imports(module_.get(), &importTypes);
@@ -343,15 +384,17 @@ ModuleWrapper::buildImports(
wasm_externtype_kind(wasm_importtype_type(importtype));
if ((itype) != WASM_EXTERN_FUNC)
throw std::runtime_error(
"Invalid import type " + std::to_string(itype));
"Invalid import type " +
std::to_string(itype)); // LCOV_EXCL_LINE
// for multi-module support
// if ((W_ENV != modName) && (W_HOST_LIB != modName))
// continue;
bool impSet = false;
for (auto const& imp : imports)
for (auto const& obj : imports)
{
auto const& imp = obj.second;
if (imp.name != fieldName)
continue;
@@ -368,7 +411,7 @@ ModuleWrapper::buildImports(
s,
ftype.get(),
reinterpret_cast<wasm_func_callback_with_env_t>(imp.wrap),
imp.udata,
(void*)&obj,
nullptr);
if (!func)
{
@@ -378,14 +421,6 @@ ModuleWrapper::buildImports(
// LCOV_EXCL_STOP
}
// if (imp.gas && !wasm_func_set_gas(func, imp.gas))
// {
// // LCOV_EXCL_START
// throw std::runtime_error(
// "can't set gas for import function " + imp.name);
// // LCOV_EXCL_STOP
// }
wimports.data[i] = wasm_func_as_extern(func);
++impCnt;
impSet = true;
@@ -396,9 +431,7 @@ ModuleWrapper::buildImports(
if (!impSet)
{
print_wasm_error(
std::string("Import not found: ") + fieldName.data(),
nullptr,
j_);
"Import not found: " + std::string(fieldName), nullptr, j_);
}
}
@@ -452,11 +485,7 @@ ModuleWrapper::addInstance(wasm_store_t* s, wasm_extern_vec_t const& imports)
std::int64_t
ModuleWrapper::getGas()
{
if (!store_)
return 0;
std::uint64_t gas = 0;
wasm_store_get_fuel(store_, &gas);
return static_cast<std::int64_t>(gas);
return instanceWrap_ ? instanceWrap_.getGas() : -1;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -475,8 +504,18 @@ WasmiEngine::init()
wasm_config_t* config = wasm_config_new();
if (!config)
return std::unique_ptr<wasm_engine_t, decltype(&wasm_engine_delete)>{
nullptr, &wasm_engine_delete};
nullptr, &wasm_engine_delete}; // LCOV_EXCL_LINE
wasmi_config_consume_fuel_set(config, true);
wasmi_config_ignore_custom_sections_set(config, true);
wasmi_config_wasm_mutable_globals_set(config, false);
wasmi_config_wasm_multi_value_set(config, false);
wasmi_config_wasm_sign_extension_set(config, false);
wasmi_config_wasm_saturating_float_to_int_set(config, false);
wasmi_config_wasm_bulk_memory_set(config, false);
wasmi_config_wasm_reference_types_set(config, false);
wasmi_config_wasm_tail_call_set(config, false);
wasmi_config_wasm_extended_const_set(config, false);
wasmi_config_floats_set(config, false);
return std::unique_ptr<wasm_engine_t, decltype(&wasm_engine_delete)>(
wasm_engine_new_with_config(config), &wasm_engine_delete);
@@ -492,11 +531,13 @@ WasmiEngine::addModule(
Bytes const& wasmCode,
bool instantiate,
int64_t gas,
std::vector<WasmImportFunc> const& imports)
ImportVec const& imports)
{
moduleWrap_.reset();
store_.reset(); // to free the memory before creating new store
store_ = {wasm_store_new(engine_.get()), &wasm_store_delete};
store_ = {
wasm_store_new_with_memory_max_pages(engine_.get(), MAX_PAGES),
&wasm_store_delete};
if (gas < 0)
gas = std::numeric_limits<decltype(gas)>::max();
@@ -504,15 +545,18 @@ WasmiEngine::addModule(
wasm_store_set_fuel(store_.get(), static_cast<std::uint64_t>(gas));
if (err)
{
// LCOV_EXCL_START
print_wasm_error("Error setting gas", nullptr, j_);
throw std::runtime_error("can't set gas");
// LCOV_EXCL_STOP
}
moduleWrap_ = std::make_unique<ModuleWrapper>(
store_.get(), wasmCode, instantiate, imports, j_);
if (!moduleWrap_)
throw std::runtime_error("can't create module wrapper");
throw std::runtime_error(
"can't create module wrapper"); // LCOV_EXCL_LINE
return moduleWrap_ ? 0 : -1;
}
@@ -541,9 +585,11 @@ WasmiEngine::convertParams(std::vector<WasmParam> const& params)
case WT_I32:
v.push_back(WASM_I32_VAL(p.of.i32));
break;
// LCOV_EXCL_START
case WT_I64:
v.push_back(WASM_I64_VAL(p.of.i64));
break;
// LCOV_EXCL_STOP
case WT_U8V: {
auto const sz = p.of.u8v.sz;
auto const ptr = allocate(sz);
@@ -554,10 +600,12 @@ WasmiEngine::convertParams(std::vector<WasmParam> const& params)
v.push_back(WASM_I32_VAL(sz));
}
break;
// LCOV_EXCL_START
default:
throw std::runtime_error(
"unknown parameter type: " + std::to_string(p.type));
break;
// LCOV_EXCL_STOP
}
}
@@ -583,6 +631,7 @@ WasmiEngine::compareParamTypes(
return -1;
}
// LCOV_EXCL_START
void
WasmiEngine::add_param(std::vector<wasm_val_t>& in, int32_t p)
{
@@ -592,6 +641,8 @@ WasmiEngine::add_param(std::vector<wasm_val_t>& in, int32_t p)
el = WASM_I32_VAL(p); // WASM_I32;
}
// LCOV_EXCL_STOP
void
WasmiEngine::add_param(std::vector<wasm_val_t>& in, int64_t p)
{
@@ -719,12 +770,22 @@ WasmiEngine::call(
return call<NR>(f, in, p.data(), p.size(), std::forward<Types>(args)...);
}
static inline void
checkImports(ImportVec const& imports, HostFunctions* hfs)
{
for (auto const& obj : imports)
{
if (hfs != obj.first)
Throw<std::runtime_error>("Imports hf unsync");
}
}
Expected<WasmResult<int32_t>, TER>
WasmiEngine::run(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
HostFunctions* hfs,
int64_t gas,
beast::Journal j)
@@ -732,16 +793,19 @@ WasmiEngine::run(
j_ = j;
try
{
checkImports(imports, hfs);
return runHlp(wasmCode, funcName, params, imports, hfs, gas);
}
catch (std::exception const& e)
{
print_wasm_error(std::string("exception: ") + e.what(), nullptr, j_);
}
// LCOV_EXCL_START
catch (...)
{
print_wasm_error(std::string("exception: unknown"), nullptr, j_);
}
// LCOV_EXCL_STOP
return Unexpected<TER>(tecFAILED_PROCESSING);
}
@@ -750,7 +814,7 @@ WasmiEngine::runHlp(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
HostFunctions* hfs,
int64_t gas)
{
@@ -764,7 +828,7 @@ WasmiEngine::runHlp(
}
if (!moduleWrap_ || !moduleWrap_->instanceWrap_)
throw std::runtime_error("no instance");
throw std::runtime_error("no instance"); // LCOV_EXCL_LINE
if (hfs)
hfs->setRT(&getRT());
@@ -787,7 +851,8 @@ WasmiEngine::runHlp(
throw std::runtime_error("<" + std::string(funcName) + "> failure");
else if (!res.r.size)
throw std::runtime_error(
"<" + std::string(funcName) + "> return nothing");
"<" + std::string(funcName) +
"> return nothing"); // LCOV_EXCL_LINE
assert(res.r.data[0].kind == WASM_I32);
if (gas == -1)
@@ -810,23 +875,27 @@ WasmiEngine::check(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports,
ImportVec const& imports,
HostFunctions* hfs,
beast::Journal j)
{
j_ = j;
try
{
checkImports(imports, hfs);
return checkHlp(wasmCode, funcName, params, imports);
}
catch (std::exception const& e)
{
print_wasm_error(std::string("exception: ") + e.what(), nullptr, j_);
}
// LCOV_EXCL_START
catch (...)
{
print_wasm_error(std::string("exception: unknown"), nullptr, j_);
}
// LCOV_EXCL_STOP
return temBAD_WASM;
}
@@ -836,7 +905,7 @@ WasmiEngine::checkHlp(
Bytes const& wasmCode,
std::string_view funcName,
std::vector<WasmParam> const& params,
std::vector<WasmImportFunc> const& imports)
ImportVec const& imports)
{
// currently only 1 module support, possible parallel UT run
std::lock_guard<decltype(m_)> lg(m_);
@@ -847,7 +916,7 @@ WasmiEngine::checkHlp(
int const m = addModule(wasmCode, true, -1, imports);
if ((m < 0) || !moduleWrap_ || !moduleWrap_->instanceWrap_)
throw std::runtime_error("no instance");
throw std::runtime_error("no instance"); // LCOV_EXCL_LINE
// Looking for a func and compare parameter types
auto const f = getFunc(!funcName.empty() ? funcName : "_start");
@@ -861,11 +930,13 @@ WasmiEngine::checkHlp(
return tesSUCCESS;
}
// LCOV_EXCL_START
std::int64_t
WasmiEngine::getGas()
{
return moduleWrap_ ? moduleWrap_->getGas() : 0;
return moduleWrap_ ? moduleWrap_->getGas() : -1;
}
// LCOV_EXCL_STOP
wmem
WasmiEngine::getMem() const
@@ -894,20 +965,23 @@ WasmiEngine::allocate(int32_t sz)
}
wasm_trap_t*
WasmiEngine::newTrap(std::string_view txt)
WasmiEngine::newTrap(std::string const& txt)
{
wasm_message_t msg = WASM_EMPTY_VEC;
static char empty[1] = {0};
wasm_message_t msg = {1, empty};
if (!txt.empty())
wasm_name_new(&msg, txt.size(), txt.data());
wasm_name_new(&msg, txt.size() + 1, txt.c_str()); // include 0
return wasm_trap_new(store_.get(), &msg);
}
// LCOV_EXCL_START
beast::Journal
WasmiEngine::getJournal() const
{
return j_;
}
// LCOV_EXCL_STOP
} // namespace ripple