referrerpolicy=no-referrer-when-downgrade

sc_executor_common/
wasm_runtime.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Definitions for a wasm runtime.
20
21use crate::error::Error;
22
23pub use sc_allocator::AllocationStats;
24
25/// Default heap allocation strategy.
26pub const DEFAULT_HEAP_ALLOC_STRATEGY: HeapAllocStrategy =
27	HeapAllocStrategy::Static { extra_pages: DEFAULT_HEAP_ALLOC_PAGES };
28
29/// Default heap allocation pages.
30pub const DEFAULT_HEAP_ALLOC_PAGES: u32 = 2048;
31
32/// A trait that defines an abstract WASM runtime module.
33///
34/// This can be implemented by an execution engine.
35pub trait WasmModule: Sync + Send {
36	/// Create a new instance.
37	fn new_instance(&self) -> Result<Box<dyn WasmInstance>, Error>;
38}
39
40/// A trait that defines an abstract wasm module instance.
41///
42/// This can be implemented by an execution engine.
43pub trait WasmInstance: Send {
44	/// Call a method on this WASM instance.
45	///
46	/// Before execution, instance is reset.
47	///
48	/// Returns the encoded result on success.
49	fn call(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
50		self.call_with_allocation_stats(method, data).0
51	}
52
53	/// Call a method on this WASM instance.
54	///
55	/// Before execution, instance is reset.
56	///
57	/// Returns the encoded result on success.
58	fn call_with_allocation_stats(
59		&mut self,
60		method: &str,
61		data: &[u8],
62	) -> (Result<Vec<u8>, Error>, Option<AllocationStats>);
63
64	/// Call an exported method on this WASM instance.
65	///
66	/// Before execution, instance is reset.
67	///
68	/// Returns the encoded result on success.
69	fn call_export(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
70		self.call(method.into(), data)
71	}
72}
73
74/// Defines the heap pages allocation strategy the wasm runtime should use.
75///
76/// A heap page is defined as 64KiB of memory.
77#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
78pub enum HeapAllocStrategy {
79	/// Allocate a static number of heap pages.
80	///
81	/// The total number of allocated heap pages is the initial number of heap pages requested by
82	/// the wasm file plus the `extra_pages`.
83	Static {
84		/// The number of pages that will be added on top of the initial heap pages requested by
85		/// the wasm file.
86		extra_pages: u32,
87	},
88	/// Allocate the initial heap pages as requested by the wasm file and then allow it to grow
89	/// dynamically.
90	Dynamic {
91		/// The absolute maximum size of the linear memory (in pages).
92		///
93		/// When `Some(_)` the linear memory will be allowed to grow up to this limit.
94		/// When `None` the linear memory will be allowed to grow up to the maximum limit supported
95		/// by WASM (4GB).
96		maximum_pages: Option<u32>,
97	},
98}