From 0db564d2616f4cdee531028706ae6dfd530c135f Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Tue, 4 Nov 2025 15:57:07 -0500 Subject: [PATCH] WASMI data --- BUILD.md | 1 + CMakeLists.txt | 1 + cmake/XrplCore.cmake | 6 ++ conanfile.py | 4 + external/wasmi/conandata.yml | 5 ++ external/wasmi/conanfile.py | 90 +++++++++++++++++++ external/wasmi/patches/static_only.patch | 15 ++++ external/wasmi/patches/wasmi.patch | 109 +++++++++++++++++++++++ 8 files changed, 231 insertions(+) create mode 100644 external/wasmi/conandata.yml create mode 100644 external/wasmi/conanfile.py create mode 100644 external/wasmi/patches/static_only.patch create mode 100644 external/wasmi/patches/wasmi.patch diff --git a/BUILD.md b/BUILD.md index f13204ea4c..3a30caea57 100644 --- a/BUILD.md +++ b/BUILD.md @@ -144,6 +144,7 @@ git fetch origin master git checkout master conan export --version 1.1.10 recipes/snappy/all conan export --version 4.0.3 recipes/soci/all +conan export --version 0.42.1 external/wasmi rm -rf .git ``` diff --git a/CMakeLists.txt b/CMakeLists.txt index cedc41eae0..a7c450ce0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,6 +119,7 @@ endif() find_package(nudb REQUIRED) find_package(date REQUIRED) +find_package(wasmi REQUIRED) find_package(xxHash REQUIRED) target_link_libraries(xrpl_libs INTERFACE diff --git a/cmake/XrplCore.cmake b/cmake/XrplCore.cmake index 164fc8523f..cf9cdca272 100644 --- a/cmake/XrplCore.cmake +++ b/cmake/XrplCore.cmake @@ -63,11 +63,17 @@ target_link_libraries(xrpl.imports.main Xrpl::opts Xrpl::syslibs secp256k1::secp256k1 + wasmi::wasmi xrpl.libpb xxHash::xxhash $<$:antithesis-sdk-cpp> ) +if (WIN32) + target_link_libraries(xrpl.imports.main INTERFACE ntdll) +endif() + + include(add_module) include(target_link_modules) diff --git a/conanfile.py b/conanfile.py index 7f8ab24fbd..d748461551 100644 --- a/conanfile.py +++ b/conanfile.py @@ -2,6 +2,7 @@ from conan import ConanFile, __version__ as conan_version from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout import re + class Xrpl(ConanFile): name = 'xrpl' @@ -29,6 +30,7 @@ class Xrpl(ConanFile): 'nudb/2.0.9', 'openssl/3.5.4', 'soci/4.0.3', + 'wasmi/0.42.1', 'zlib/1.3.1', ] @@ -133,6 +135,7 @@ class Xrpl(ConanFile): self.folders.generators = 'build/generators' generators = 'CMakeDeps' + def generate(self): tc = CMakeToolchain(self) tc.variables['tests'] = self.options.tests @@ -190,6 +193,7 @@ class Xrpl(ConanFile): 'protobuf::libprotobuf', 'soci::soci', 'sqlite3::sqlite', + 'wasmi::wasmi', 'xxhash::xxhash', 'zlib::zlib', ] diff --git a/external/wasmi/conandata.yml b/external/wasmi/conandata.yml new file mode 100644 index 0000000000..628590aea4 --- /dev/null +++ b/external/wasmi/conandata.yml @@ -0,0 +1,5 @@ +patches: + 0.42.1: + - patch_description: install only static lib (to prevent conflict with shared) + patch_file: patches/wasmi.patch + patch_type: conan diff --git a/external/wasmi/conanfile.py b/external/wasmi/conanfile.py new file mode 100644 index 0000000000..d6b1c0b259 --- /dev/null +++ b/external/wasmi/conanfile.py @@ -0,0 +1,90 @@ +from conan import ConanFile, tools +from conan.tools.cmake import CMake, CMakeToolchain, CMakeDeps, cmake_layout +from conan.tools.files import ( + apply_conandata_patches, + export_conandata_patches, + # get, +) +from conan.tools.scm import Git + +import os +# import json + +required_conan_version = ">=1.55.0" + +class WasmiConan(ConanFile): + name = "wasmi" + license = "Apache License v2.0" + url = "https://github.com/wasmi-labs/wasmi.git" + description = "WebAssembly (Wasm) interpreter" + package_type = "library" + settings = "os", "compiler", "build_type", "arch" + options = {"shared": [True, False]} + default_options = {"shared": False} + # generators = "CMakeToolchain", "CMakeDeps" + #requires = [("llvm/20.1.1@")] + + def export_sources(self): + export_conandata_patches(self) + pass + + # def build_requirements(self): + # self.tool_requires("llvm/20.1.1") + + + def config_options(self): + #if self.settings.os == "Windows": + # del self.options.fPIC + pass + + + def layout(self): + cmake_layout(self, src_folder="src") + + + def source(self): + git = Git(self) + git.fetch_commit( + url="https://github.com/wasmi-labs/wasmi.git", + commit="f628a7a86c9715f2c306f6ef9aea1cc2bdca5fa7", + ) + #get(self, **self.conan_data["sources"][self.version], strip_root=True) + + + def generate(self): + tc = CMakeToolchain(self) + + tc.variables["CMAKE_CXX_STANDARD"] = 20 + tc.variables["BUILD_SHARED_LIBS"] = 0 + + tc.generate() + + # This generates "foo-config.cmake" and "bar-config.cmake" in self.generators_folder + deps = CMakeDeps(self) + deps.generate() + + + def build(self): + apply_conandata_patches(self) + cmake = CMake(self) + cmake.verbose = True + cmake.configure(build_script_folder=os.path.join(self.source_folder, "crates", "c_api")) + cmake.build() + #self.run(f'echo {self.source_folder}') + + # Explicit way: + # self.run('cmake %s/hello %s' % (self.source_folder, cmake.command_line)) + # self.run("cmake --build . %s" % cmake.build_config) + + + def package(self): + cmake = CMake(self) + cmake.verbose = True + cmake.install() + + + 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" + diff --git a/external/wasmi/patches/static_only.patch b/external/wasmi/patches/static_only.patch new file mode 100644 index 0000000000..aff4b408ea --- /dev/null +++ b/external/wasmi/patches/static_only.patch @@ -0,0 +1,15 @@ +diff --git a/crates/c_api/CMakeLists.txt b/crates/c_api/CMakeLists.txt +index b15c787..97dd294 100644 +--- a/crates/c_api/CMakeLists.txt ++++ b/crates/c_api/CMakeLists.txt +@@ -43,6 +43,10 @@ endif() + list(TRANSFORM WASMI_SHARED_FILES PREPEND ${WASMI_TARGET_DIR}/) + list(TRANSFORM WASMI_STATIC_FILES PREPEND ${WASMI_TARGET_DIR}/) + ++if(NOT BUILD_SHARED_LIBS) ++ set(WASMI_SHARED_FILES ) ++endif() ++ + # Instructions on how to build and install the Wasmi Rust crate. + find_program(WASMI_CARGO_BINARY cargo REQUIRED) + include(ExternalProject) diff --git a/external/wasmi/patches/wasmi.patch b/external/wasmi/patches/wasmi.patch new file mode 100644 index 0000000000..d79d52d6db --- /dev/null +++ b/external/wasmi/patches/wasmi.patch @@ -0,0 +1,109 @@ +diff --git a/crates/c_api/CMakeLists.txt b/crates/c_api/CMakeLists.txt +index b15c787a..0d0a04b7 100644 +--- a/crates/c_api/CMakeLists.txt ++++ b/crates/c_api/CMakeLists.txt +@@ -43,6 +43,11 @@ endif() + list(TRANSFORM WASMI_SHARED_FILES PREPEND ${WASMI_TARGET_DIR}/) + list(TRANSFORM WASMI_STATIC_FILES PREPEND ${WASMI_TARGET_DIR}/) + ++if(NOT BUILD_SHARED_LIBS) ++ set(WASMI_SHARED_FILES ) ++endif() ++ ++ + # Instructions on how to build and install the Wasmi Rust crate. + find_program(WASMI_CARGO_BINARY cargo REQUIRED) + include(ExternalProject) +diff --git a/crates/c_api/include/wasm.h b/crates/c_api/include/wasm.h +index 5ee617ff..0199192d 100644 +--- a/crates/c_api/include/wasm.h ++++ b/crates/c_api/include/wasm.h +@@ -146,6 +146,13 @@ WASM_DECLARE_OWN(store) + + WASM_API_EXTERN own wasm_store_t* wasm_store_new(wasm_engine_t*); + ++// 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 +--- a/crates/c_api/include/wasmi.h ++++ b/crates/c_api/include/wasmi.h +@@ -10,7 +10,7 @@ + /** + * \brief Wasmi version string. + */ +-#define WASMI_VERSION "0.35.0" ++#define WASMI_VERSION "0.42.1" + /** + * \brief Wasmi major version number. + */ +@@ -18,10 +18,10 @@ + /** + * \brief Wasmi minor version number. + */ +-#define WASMI_VERSION_MINOR 35 ++#define WASMI_VERSION_MINOR 42 + /** + * \brief Wasmi patch version number. + */ +-#define WASMI_VERSION_PATCH 0 ++#define WASMI_VERSION_PATCH 1 + + #endif // WASMI_H +diff --git a/crates/c_api/src/store.rs b/crates/c_api/src/store.rs +index 56d4898f..543dbff8 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( + ) -> Option> { + crate::handle_result(store.set_fuel(fuel), |()| {}) + } ++ ++//////////////////////////////////////////////////////////////////////////////////////// ++//////////////////////////////////////////////////////////////////////////////////////// ++//////////////////////////////////////////////////////////////////////////////////////// ++//////////////////////////////////////////////////////////////////////////////////////// ++ ++/// Returns the current fuel of the wasm store context in `fuel`. ++/// ++/// Wraps [`Store::get_fuel`]. ++/// ++/// # Errors ++/// ++/// If [`Store::get_fuel`] errors. ++#[no_mangle] ++pub extern "C" fn wasm_store_get_fuel( ++ store: &wasm_store_t, ++ fuel: &mut u64, ++) -> Option> { ++ let context = unsafe { store.inner.context() }; ++ crate::handle_result(context.get_fuel(), |amt| { ++ *fuel = amt; ++ }) ++} ++ ++/// Sets the current fuel of the wasm store context to `fuel`. ++/// ++/// Wraps [`Store::set_fuel`]. ++/// ++/// # Errors ++/// ++/// If [`Store::set_fuel`] errors. ++#[no_mangle] ++pub extern "C" fn wasm_store_set_fuel( ++ store: &mut wasm_store_t, ++ fuel: u64, ++) -> Option> { ++ ++ let mut context = unsafe { store.inner.context_mut() }; ++ crate::handle_result(context.set_fuel(fuel), |()| {}) ++} ++