Skip to main content

gsc_executor_common/
wasm_runtime.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Definitions for a wasm runtime.
5
6use crate::error::Error;
7
8pub use sp_allocator::AllocationStats;
9
10/// Default heap allocation strategy.
11pub const DEFAULT_HEAP_ALLOC_STRATEGY: HeapAllocStrategy = HeapAllocStrategy::Static {
12    extra_pages: DEFAULT_HEAP_ALLOC_PAGES,
13};
14
15/// Default heap allocation pages.
16pub const DEFAULT_HEAP_ALLOC_PAGES: u32 = 2048;
17
18/// A trait that defines an abstract WASM runtime module.
19///
20/// This can be implemented by an execution engine.
21pub trait WasmModule: Sync + Send {
22    /// Create a new instance.
23    fn new_instance(&self) -> Result<Box<dyn WasmInstance>, Error>;
24}
25
26/// A trait that defines an abstract wasm module instance.
27///
28/// This can be implemented by an execution engine.
29pub trait WasmInstance: Send {
30    /// Call a method on this WASM instance.
31    ///
32    /// Before execution, instance is reset.
33    ///
34    /// Returns the encoded result on success.
35    fn call(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
36        self.call_with_allocation_stats(method, data).0
37    }
38
39    /// Call a method on this WASM instance.
40    ///
41    /// Before execution, instance is reset.
42    ///
43    /// Returns the encoded result on success.
44    fn call_with_allocation_stats(
45        &mut self,
46        method: &str,
47        data: &[u8],
48    ) -> (Result<Vec<u8>, Error>, Option<AllocationStats>);
49
50    /// Call an exported method on this WASM instance.
51    ///
52    /// Before execution, instance is reset.
53    ///
54    /// Returns the encoded result on success.
55    fn call_export(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
56        self.call(method, data)
57    }
58
59    /// Get the value from a global with the given `name`.
60    ///
61    /// This method is only suitable for getting immutable globals.
62    fn get_global_const(&mut self, name: &str) -> Result<Option<sp_wasm_interface::Value>, Error>;
63}
64
65/// Defines the heap pages allocation strategy the wasm runtime should use.
66///
67/// A heap page is defined as 64KiB of memory.
68#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
69pub enum HeapAllocStrategy {
70    /// Allocate a static number of heap pages.
71    ///
72    /// The total number of allocated heap pages is the initial number of heap pages requested by
73    /// the wasm file plus the `extra_pages`.
74    Static {
75        /// The number of pages that will be added on top of the initial heap pages requested by
76        /// the wasm file.
77        extra_pages: u32,
78    },
79    /// Allocate the initial heap pages as requested by the wasm file and then allow it to grow
80    /// dynamically.
81    Dynamic {
82        /// The absolute maximum size of the linear memory (in pages).
83        ///
84        /// When `Some(_)` the linear memory will be allowed to grow up to this limit.
85        /// When `None` the linear memory will be allowed to grow up to the maximum limit supported
86        /// by WASM (4GB).
87        maximum_pages: Option<u32>,
88    },
89}