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 with the given heap allocation strategy.
37 ///
38 /// The `heap_alloc_strategy` determines the memory limits applied to this instance.
39 fn new_instance(
40 &self,
41 heap_alloc_strategy: HeapAllocStrategy,
42 ) -> Result<Box<dyn WasmInstance>, Error>;
43}
44
45/// A trait that defines an abstract wasm module instance.
46///
47/// This can be implemented by an execution engine.
48pub trait WasmInstance: Send {
49 /// Call a method on this WASM instance.
50 ///
51 /// Before execution, instance is reset.
52 ///
53 /// Returns the encoded result on success.
54 fn call(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
55 self.call_with_allocation_stats(method, data).0
56 }
57
58 /// Call a method on this WASM instance.
59 ///
60 /// Before execution, instance is reset.
61 ///
62 /// Returns the encoded result on success.
63 fn call_with_allocation_stats(
64 &mut self,
65 method: &str,
66 data: &[u8],
67 ) -> (Result<Vec<u8>, Error>, Option<AllocationStats>);
68
69 /// Call an exported method on this WASM instance.
70 ///
71 /// Before execution, instance is reset.
72 ///
73 /// Returns the encoded result on success.
74 fn call_export(&mut self, method: &str, data: &[u8]) -> Result<Vec<u8>, Error> {
75 self.call(method.into(), data)
76 }
77
78 /// Update the heap allocation strategy for subsequent calls.
79 ///
80 /// This is used when an instance is reused from a pool but the caller needs
81 /// a different memory limit than what the instance was originally created with.
82 fn set_heap_alloc_strategy(&mut self, _heap_alloc_strategy: HeapAllocStrategy) {}
83}
84
85/// Defines the heap pages allocation strategy the wasm runtime should use.
86///
87/// A heap page is defined as 64KiB of memory.
88#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
89pub enum HeapAllocStrategy {
90 /// Allocate a static number of heap pages.
91 ///
92 /// The total number of allocated heap pages is the initial number of heap pages requested by
93 /// the wasm file plus the `extra_pages`.
94 Static {
95 /// The number of pages that will be added on top of the initial heap pages requested by
96 /// the wasm file.
97 extra_pages: u32,
98 },
99 /// Allocate the initial heap pages as requested by the wasm file and then allow it to grow
100 /// dynamically.
101 Dynamic {
102 /// The absolute maximum size of the linear memory (in pages).
103 ///
104 /// When `Some(_)` the linear memory will be allowed to grow up to this limit.
105 /// When `None` the linear memory will be allowed to grow up to the maximum limit supported
106 /// by WASM (4GB).
107 maximum_pages: Option<u32>,
108 },
109}
110
111impl HeapAllocStrategy {
112 /// Double this heap allocation strategy.
113 pub const fn double(self) -> Self {
114 match self {
115 Self::Static { extra_pages } => {
116 Self::Static { extra_pages: extra_pages.saturating_mul(2) }
117 },
118 Self::Dynamic { maximum_pages } => Self::Dynamic {
119 maximum_pages: match maximum_pages {
120 Some(p) => Some(p.saturating_mul(2)),
121 None => None,
122 },
123 },
124 }
125 }
126}