near_vm_vm/
tunables.rs

1use crate::MemoryError;
2use crate::{LinearMemory, Table};
3use crate::{MemoryStyle, TableStyle};
4use crate::{VMMemoryDefinition, VMTableDefinition};
5use near_vm_types::{MemoryType, TableType};
6use std::ptr::NonNull;
7use std::sync::Arc;
8
9/// An engine delegates the creation of memories, tables, and globals
10/// to a foreign implementor of this trait.
11pub trait Tunables: Sync {
12    /// Construct a `MemoryStyle` for the provided `MemoryType`
13    fn memory_style(&self, memory: &MemoryType) -> MemoryStyle;
14
15    /// Construct a `TableStyle` for the provided `TableType`
16    fn table_style(&self, table: &TableType) -> TableStyle;
17
18    /// Create a memory owned by the host given a [`MemoryType`] and a [`MemoryStyle`].
19    fn create_host_memory(
20        &self,
21        ty: &MemoryType,
22        style: &MemoryStyle,
23    ) -> Result<Arc<LinearMemory>, MemoryError>;
24
25    /// Create a memory owned by the VM given a [`MemoryType`] and a [`MemoryStyle`].
26    ///
27    /// # Safety
28    /// - `vm_definition_location` must point to a valid location in VM memory.
29    unsafe fn create_vm_memory(
30        &self,
31        ty: &MemoryType,
32        style: &MemoryStyle,
33        vm_definition_location: NonNull<VMMemoryDefinition>,
34    ) -> Result<Arc<LinearMemory>, MemoryError>;
35
36    /// Create a table owned by the host given a [`TableType`] and a [`TableStyle`].
37    fn create_host_table(
38        &self,
39        ty: &TableType,
40        style: &TableStyle,
41    ) -> Result<Arc<dyn Table>, String>;
42
43    /// Create a table owned by the VM given a [`TableType`] and a [`TableStyle`].
44    ///
45    /// # Safety
46    /// - `vm_definition_location` must point to a valid location in VM memory.
47    unsafe fn create_vm_table(
48        &self,
49        ty: &TableType,
50        style: &TableStyle,
51        vm_definition_location: NonNull<VMTableDefinition>,
52    ) -> Result<Arc<dyn Table>, String>;
53
54    /// Instrumentation configuration: stack limiter config
55    fn stack_limiter_cfg(&self) -> Box<dyn finite_wasm::max_stack::SizeConfig>;
56
57    /// Instrumentation configuration: gas accounting config
58    fn gas_cfg(&self) -> Box<dyn finite_wasm::wasmparser::VisitOperator<Output = u64>>;
59
60    /// Cost for initializing a stack frame
61    fn stack_init_gas_cost(&self, frame_size: u64) -> u64;
62}
63
64#[doc(hidden)]
65pub struct TestTunables;
66
67impl Tunables for TestTunables {
68    fn memory_style(&self, _memory: &MemoryType) -> MemoryStyle {
69        unimplemented!()
70    }
71
72    fn table_style(&self, _table: &TableType) -> TableStyle {
73        unimplemented!()
74    }
75
76    fn create_host_memory(
77        &self,
78        _ty: &MemoryType,
79        _style: &MemoryStyle,
80    ) -> Result<Arc<LinearMemory>, MemoryError> {
81        unimplemented!()
82    }
83
84    unsafe fn create_vm_memory(
85        &self,
86        _ty: &MemoryType,
87        _style: &MemoryStyle,
88        _vm_definition_location: NonNull<VMMemoryDefinition>,
89    ) -> Result<Arc<LinearMemory>, MemoryError> {
90        unimplemented!()
91    }
92
93    fn create_host_table(
94        &self,
95        _ty: &TableType,
96        _style: &TableStyle,
97    ) -> Result<Arc<dyn Table>, String> {
98        unimplemented!()
99    }
100
101    unsafe fn create_vm_table(
102        &self,
103        _ty: &TableType,
104        _style: &TableStyle,
105        _vm_definition_location: NonNull<VMTableDefinition>,
106    ) -> Result<Arc<dyn Table>, String> {
107        unimplemented!()
108    }
109
110    fn stack_limiter_cfg(&self) -> Box<dyn finite_wasm::max_stack::SizeConfig> {
111        unimplemented!()
112    }
113
114    fn gas_cfg(&self) -> Box<dyn finite_wasm::wasmparser::VisitOperator<Output = u64>> {
115        unimplemented!()
116    }
117
118    fn stack_init_gas_cost(&self, _frame_size: u64) -> u64 {
119        unimplemented!()
120    }
121}