Skip to content

Commit 1624f2b

Browse files
committed
wasmi v1
1 parent 706065f commit 1624f2b

File tree

3 files changed

+297
-0
lines changed

3 files changed

+297
-0
lines changed

recipes/wasmi/all/conandata.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
# Do not update. Maintained by programmability team.
22

33
sources:
4+
"1.0.4":
5+
url: https://github.com/wasmi-labs/wasmi/archive/refs/tags/v1.0.4.tar.gz
6+
sha256: 90bfdd6d43930958c0f3eabe82e90800a7df4204b673c22f6966f94c6a5452e0
47
"0.42.1":
58
url: https://github.com/wasmi-labs/wasmi/archive/refs/tags/v0.42.1.tar.gz
69
sha256: 2a5697be33c7afce8f671af4a5a3621d9e93ce55d253d31bd8201458e465fbb8
710
patches:
11+
"1.0.4":
12+
- patch_file: "patches/0002-xrplf-1.0.4.patch"
13+
patch_description: Integrate wasmi lib into smart-escrow.
14+
patch_type: conan
815
"0.42.1":
916
- patch_file: "patches/0001-xrplf-0.42.1.patch"
1017
patch_description: Integrate wasmi lib into smart-escrow.
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
diff --git a/crates/c_api/CMakeLists.txt b/crates/c_api/CMakeLists.txt
2+
index b15c787a..54eaed2d 100644
3+
--- a/crates/c_api/CMakeLists.txt
4+
+++ b/crates/c_api/CMakeLists.txt
5+
@@ -6,6 +6,8 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" OFF)
6+
option(WASMI_ALWAYS_BUILD "If cmake should always invoke cargo to build Wasmi" ON)
7+
set(WASMI_TARGET "" CACHE STRING "Rust target to build for")
8+
9+
+add_compile_definitions(COMPILING_WASM_RUNTIME_API=1)
10+
+
11+
if(NOT WASMI_TARGET)
12+
execute_process(
13+
COMMAND rustc -vV
14+
@@ -43,6 +45,10 @@ endif()
15+
list(TRANSFORM WASMI_SHARED_FILES PREPEND ${WASMI_TARGET_DIR}/)
16+
list(TRANSFORM WASMI_STATIC_FILES PREPEND ${WASMI_TARGET_DIR}/)
17+
18+
+if(NOT BUILD_SHARED_LIBS)
19+
+ set(WASMI_SHARED_FILES)
20+
+endif()
21+
+
22+
# Instructions on how to build and install the Wasmi Rust crate.
23+
find_program(WASMI_CARGO_BINARY cargo REQUIRED)
24+
include(ExternalProject)
25+
@@ -79,7 +85,6 @@ else()
26+
target_link_libraries(wasmi INTERFACE ${WASMI_STATIC_FILES})
27+
28+
if(WASMI_TARGET MATCHES "windows")
29+
- target_compile_options(wasmi INTERFACE -DWASM_API_EXTERN= -DWASI_API_EXTERN=)
30+
target_link_libraries(wasmi INTERFACE ws2_32 advapi32 userenv ntdll shell32 ole32 bcrypt)
31+
elseif(NOT WASMI_TARGET MATCHES "darwin")
32+
target_link_libraries(wasmi INTERFACE pthread dl m)
33+
@@ -112,6 +117,7 @@ install(
34+
DESTINATION ${CMAKE_INSTALL_LIBDIR}
35+
)
36+
37+
+if(BUILD_SHARED_LIBS)
38+
if(WASMI_TARGET MATCHES "darwin")
39+
set(INSTALLED_LIB "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libwasmi.dylib")
40+
install(
41+
@@ -131,6 +137,7 @@ if(WASMI_TARGET MATCHES "darwin")
42+
install(CODE "execute_process(COMMAND ${install_name_tool_cmd})")
43+
endif()
44+
endif()
45+
+endif()
46+
47+
# Documentation Generation via Doxygen:
48+
set(DOXYGEN_CONF_IN ${CMAKE_CURRENT_SOURCE_DIR}/doxygen.conf.in)
49+
@@ -141,19 +148,3 @@ add_custom_target(doc
50+
DEPENDS ${WASMI_GENERATED_CONF_H} ${DOXYGEN_CONF_OUT}
51+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
52+
)
53+
-
54+
-# C-Header Formatting via clang-format:
55+
-find_program(CLANG_FORMAT clang-format REQUIRED)
56+
-file(GLOB_RECURSE HEADER_FILES
57+
- ${CMAKE_CURRENT_SOURCE_DIR}/include/wasmi.h
58+
- ${CMAKE_CURRENT_SOURCE_DIR}/include/wasmi/*.h
59+
- ${CMAKE_CURRENT_SOURCE_DIR}/include/wasmi/*.hh
60+
-)
61+
-add_custom_target(check-format
62+
- COMMAND ${CLANG_FORMAT} -style=llvm -Werror --dry-run ${HEADER_FILES}
63+
- COMMENT "clang-format: Check formatting for Wasmi C-API header files"
64+
-)
65+
-add_custom_target(format
66+
- COMMAND ${CLANG_FORMAT} -style=llvm -i ${HEADER_FILES}
67+
- COMMENT "clang-format: Apply formatting rules for Wasmi C-API header files"
68+
-)
69+
diff --git a/crates/c_api/include/wasm.h b/crates/c_api/include/wasm.h
70+
index 5ee617ff..a76f10e0 100644
71+
--- a/crates/c_api/include/wasm.h
72+
+++ b/crates/c_api/include/wasm.h
73+
@@ -13,11 +13,17 @@
74+
#include <assert.h>
75+
76+
#ifndef WASM_API_EXTERN
77+
-#if defined(_WIN32) && !defined(__MINGW32__) && !defined(LIBWASM_STATIC)
78+
+#if defined(_MSC_BUILD)
79+
+#if defined(COMPILING_WASM_RUNTIME_API)
80+
+#define WASM_API_EXTERN __declspec(dllexport)
81+
+#elif defined(_DLL)
82+
#define WASM_API_EXTERN __declspec(dllimport)
83+
#else
84+
#define WASM_API_EXTERN
85+
#endif
86+
+#else
87+
+#define WASM_API_EXTERN
88+
+#endif
89+
#endif
90+
91+
#ifdef __cplusplus
92+
@@ -145,7 +151,13 @@ WASM_API_EXTERN own wasm_engine_t* wasm_engine_new_with_config(own wasm_config_t
93+
WASM_DECLARE_OWN(store)
94+
95+
WASM_API_EXTERN own wasm_store_t* wasm_store_new(wasm_engine_t*);
96+
+WASM_API_EXTERN own wasm_store_t* wasm_store_new_with_memory_max_pages(wasm_engine_t*, uint32_t max_pages);
97+
+
98+
+// Store fuel functions (forward declarations)
99+
+struct wasmi_error;
100+
101+
+WASM_API_EXTERN struct wasmi_error* wasm_store_get_fuel(const wasm_store_t*, uint64_t* fuel);
102+
+WASM_API_EXTERN struct wasmi_error* wasm_store_set_fuel(wasm_store_t*, uint64_t fuel);
103+
104+
///////////////////////////////////////////////////////////////////////////////
105+
// Type Representations
106+
diff --git a/crates/c_api/include/wasmi.h b/crates/c_api/include/wasmi.h
107+
index 2caffa37..8f0f0edb 100644
108+
--- a/crates/c_api/include/wasmi.h
109+
+++ b/crates/c_api/include/wasmi.h
110+
@@ -10,18 +10,18 @@
111+
/**
112+
* \brief Wasmi version string.
113+
*/
114+
-#define WASMI_VERSION "0.35.0"
115+
+#define WASMI_VERSION "1.0.4"
116+
/**
117+
* \brief Wasmi major version number.
118+
*/
119+
-#define WASMI_VERSION_MAJOR 0
120+
+#define WASMI_VERSION_MAJOR 1
121+
/**
122+
* \brief Wasmi minor version number.
123+
*/
124+
-#define WASMI_VERSION_MINOR 35
125+
+#define WASMI_VERSION_MINOR 0
126+
/**
127+
* \brief Wasmi patch version number.
128+
*/
129+
-#define WASMI_VERSION_PATCH 0
130+
+#define WASMI_VERSION_PATCH 4
131+
132+
#endif // WASMI_H
133+
diff --git a/crates/c_api/src/store.rs b/crates/c_api/src/store.rs
134+
index 56d4898f..9abda8e7 100644
135+
--- a/crates/c_api/src/store.rs
136+
+++ b/crates/c_api/src/store.rs
137+
@@ -1,7 +1,7 @@
138+
use crate::{wasm_engine_t, wasmi_error_t, ForeignData};
139+
use alloc::{boxed::Box, sync::Arc};
140+
use core::{cell::UnsafeCell, ffi};
141+
-use wasmi::{AsContext, AsContextMut, Store, StoreContext, StoreContextMut};
142+
+use wasmi::{AsContext, AsContextMut, Store, StoreContext, StoreContextMut, StoreLimits, StoreLimitsBuilder};
143+
144+
/// This representation of a `Store` is used to implement the `wasm.h` API (and
145+
/// *not* the `wasmi.h` API!)
146+
@@ -16,7 +16,7 @@ use wasmi::{AsContext, AsContextMut, Store, StoreContext, StoreContextMut};
147+
/// least Wasmi's implementation).
148+
#[derive(Clone)]
149+
pub struct WasmStoreRef {
150+
- inner: Arc<UnsafeCell<Store<()>>>,
151+
+ inner: Arc<UnsafeCell<Store<StoreLimits>>>,
152+
}
153+
154+
impl WasmStoreRef {
155+
@@ -27,7 +27,7 @@ impl WasmStoreRef {
156+
/// # Safety
157+
///
158+
/// It is the callers responsibility to provide a valid `self`.
159+
- pub unsafe fn context(&self) -> StoreContext<'_, ()> {
160+
+ pub unsafe fn context(&self) -> StoreContext<'_, StoreLimits> {
161+
(*self.inner.get()).as_context()
162+
}
163+
164+
@@ -38,7 +38,7 @@ impl WasmStoreRef {
165+
/// # Safety
166+
///
167+
/// It is the callers responsibility to provide a valid `self`.
168+
- pub unsafe fn context_mut(&mut self) -> StoreContextMut<'_, ()> {
169+
+ pub unsafe fn context_mut(&mut self) -> StoreContextMut<'_, StoreLimits> {
170+
(*self.inner.get()).as_context_mut()
171+
}
172+
}
173+
@@ -56,17 +56,71 @@ pub struct wasm_store_t {
174+
175+
wasmi_c_api_macros::declare_own!(wasm_store_t);
176+
177+
-/// Creates a new [`Store<()>`](wasmi::Store) for the given `engine`.
178+
+/// Creates a new [`Store<StoreLimits>`](wasmi::Store) for the given `engine`.
179+
+///
180+
+/// The store is created with no resource limits (original behavior).
181+
+/// For memory-limited stores, use [`wasm_store_new_with_memory_max_pages`].
182+
///
183+
/// The returned [`wasm_store_t`] must be freed using [`wasm_store_delete`].
184+
///
185+
-/// Wraps [`<wasmi::Store<()>>::new`](wasmi::Store::new).
186+
+/// Wraps [`<wasmi::Store<StoreLimits>>::new`](wasmi::Store::new).
187+
#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
188+
#[allow(clippy::arc_with_non_send_sync)]
189+
#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
190+
pub extern "C" fn wasm_store_new(engine: &wasm_engine_t) -> Box<wasm_store_t> {
191+
let engine = &engine.inner;
192+
- let store = Store::new(engine, ());
193+
+
194+
+ // Create store with no resource limits (original behavior)
195+
+ let limits = StoreLimitsBuilder::new().build();
196+
+ let store = Store::new(engine, limits);
197+
+
198+
+ Box::new(wasm_store_t {
199+
+ inner: WasmStoreRef {
200+
+ inner: Arc::new(UnsafeCell::new(store)),
201+
+ },
202+
+ })
203+
+}
204+
+
205+
+/// Creates a new [`Store<StoreLimits>`](wasmi::Store) for the given `engine` with memory limits.
206+
+///
207+
+/// This function creates a store with resource limits suitable for blockchain smart contracts.
208+
+/// The memory limit is enforced during WebAssembly execution.
209+
+///
210+
+/// If `max_pages` exceeds 1024 (64MB), this function will panic.
211+
+///
212+
+/// The returned [`wasm_store_t`] must be freed using [`wasm_store_delete`].
213+
+///
214+
+/// Wraps [`<wasmi::Store<StoreLimits>>::new`](wasmi::Store::new).
215+
+#[cfg_attr(not(feature = "prefix-symbols"), no_mangle)]
216+
+#[allow(clippy::arc_with_non_send_sync)]
217+
+#[cfg_attr(feature = "prefix-symbols", wasmi_c_api_macros::prefix_symbol)]
218+
+pub extern "C" fn wasm_store_new_with_memory_max_pages(
219+
+ engine: &wasm_engine_t,
220+
+ max_pages: u32,
221+
+) -> Box<wasm_store_t> {
222+
+ // Validate max_pages limit (64MB = 1024 pages)
223+
+ if max_pages > 1024 {
224+
+ panic!("max_pages ({}) exceeds maximum allowed value of 1024 pages (64MB)", max_pages);
225+
+ }
226+
+
227+
+ // Convert pages to bytes (each page is 64KB)
228+
+ let max_memory_bytes = (max_pages as usize) * (64 * 1024);
229+
+
230+
+ // Create store limits with blockchain-suitable defaults
231+
+ let limits = StoreLimitsBuilder::new()
232+
+ .memory_size(max_memory_bytes) // User-specified memory limit
233+
+ .instances(1) // Single instance for blockchain
234+
+ .tables(1) // Single table for blockchain
235+
+ .memories(1) // Single memory for blockchain
236+
+ .table_elements(64) // Limited table elements for blockchain
237+
+ .trap_on_grow_failure(false) // Return -1 on growth failure instead of trapping
238+
+ .build();
239+
+
240+
+ let mut store = Store::new(&engine.inner, limits);
241+
+
242+
+ // Install the resource limiter
243+
+ store.limiter(|limits| limits);
244+
+
245+
Box::new(wasm_store_t {
246+
inner: WasmStoreRef {
247+
inner: Arc::new(UnsafeCell::new(store)),
248+
@@ -175,3 +229,40 @@ pub extern "C" fn wasmi_context_set_fuel(
249+
) -> Option<Box<wasmi_error_t>> {
250+
crate::handle_result(store.set_fuel(fuel), |()| {})
251+
}
252+
+
253+
+////////////////////////////////////////////////////////////////////////////////////////
254+
+////////////////////////////////////////////////////////////////////////////////////////
255+
+
256+
+/// Returns the current fuel of the wasm store context in `fuel`.
257+
+///
258+
+/// Wraps [`Store::get_fuel`].
259+
+///
260+
+/// # Errors
261+
+///
262+
+/// If [`Store::get_fuel`] errors.
263+
+#[no_mangle]
264+
+pub extern "C" fn wasm_store_get_fuel(
265+
+ store: &wasm_store_t,
266+
+ fuel: &mut u64,
267+
+) -> Option<Box<wasmi_error_t>> {
268+
+ let context = unsafe { store.inner.context() };
269+
+ crate::handle_result(context.get_fuel(), |amt| {
270+
+ *fuel = amt;
271+
+ })
272+
+}
273+
+
274+
+/// Sets the current fuel of the wasm store context to `fuel`.
275+
+///
276+
+/// Wraps [`Store::set_fuel`].
277+
+///
278+
+/// # Errors
279+
+///
280+
+/// If [`Store::set_fuel`] errors.
281+
+#[no_mangle]
282+
+pub extern "C" fn wasm_store_set_fuel(
283+
+ store: &mut wasm_store_t,
284+
+ fuel: u64,
285+
+) -> Option<Box<wasmi_error_t>> {
286+
+ let mut context = unsafe { store.inner.context_mut() };
287+
+ crate::handle_result(context.set_fuel(fuel), |()| {})
288+
+}

recipes/wasmi/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
versions:
2+
"1.0.4":
3+
folder: all
24
"0.42.1":
35
folder: all

0 commit comments

Comments
 (0)