1 //! Evaluate an exported Wasm function using the wasmi interpreter.
2 
3 use crate::generators::{Config, DiffValue, DiffValueType};
4 use crate::oracles::engine::{DiffEngine, DiffInstance};
5 use anyhow::{Context, Error, Result};
6 use wasmtime::Trap;
7 
8 /// A wrapper for `wasmi` as a [`DiffEngine`].
9 pub struct WasmiEngine {
10     engine: wasmi::Engine,
11 }
12 
13 impl WasmiEngine {
14     pub(crate) fn new(config: &mut Config) -> Self {
15         let config = &mut config.module_config.config;
16         // Force generated Wasm modules to never have features that Wasmi doesn't support.
17         config.relaxed_simd_enabled = false;
18         config.threads_enabled = false;
19         config.exceptions_enabled = false;
20         config.gc_enabled = false;
21 
22         // FIXME: once the active fuzz bug for wasmi's simd differential fuzzing
23         // has been fixed and we've updated then this should be re-enabled.
24         config.simd_enabled = false;
25         // FIXME: requires updating to a wasmi that contains
26         // wasmi-labs/wasmi#1531.
27         config.memory64_enabled = false;
28 
29         let mut wasmi_config = wasmi::Config::default();
30         wasmi_config
31             .consume_fuel(false)
32             .floats(true)
33             .wasm_mutable_global(true)
34             .wasm_sign_extension(config.sign_extension_ops_enabled)
35             .wasm_saturating_float_to_int(config.saturating_float_to_int_enabled)
36             .wasm_multi_value(config.multi_value_enabled)
37             .wasm_bulk_memory(config.bulk_memory_enabled)
38             .wasm_reference_types(config.reference_types_enabled)
39             .wasm_tail_call(config.tail_call_enabled)
40             .wasm_multi_memory(config.max_memories > 1)
41             .wasm_extended_const(config.extended_const_enabled)
42             .wasm_custom_page_sizes(config.custom_page_sizes_enabled)
43             .wasm_memory64(config.memory64_enabled)
44             .wasm_simd(config.simd_enabled)
45             .wasm_wide_arithmetic(config.wide_arithmetic_enabled);
46         Self {
47             engine: wasmi::Engine::new(&wasmi_config),
48         }
49     }
50 
51     fn trap_code(&self, err: &Error) -> Option<wasmi::core::TrapCode> {
52         let err = err.downcast_ref::<wasmi::Error>()?;
53         if let Some(code) = err.as_trap_code() {
54             return Some(code);
55         }
56 
57         match err.kind() {
58             wasmi::errors::ErrorKind::Instantiation(
59                 wasmi::errors::InstantiationError::ElementSegmentDoesNotFit { .. },
60             ) => Some(wasmi::core::TrapCode::TableOutOfBounds),
61             wasmi::errors::ErrorKind::Memory(wasmi::errors::MemoryError::OutOfBoundsAccess) => {
62                 Some(wasmi::core::TrapCode::MemoryOutOfBounds)
63             }
64             _ => {
65                 log::trace!("unknown wasmi error: {:?}", err.kind());
66                 None
67             }
68         }
69     }
70 }
71 
72 impl DiffEngine for WasmiEngine {
73     fn name(&self) -> &'static str {
74         "wasmi"
75     }
76 
77     fn instantiate(&mut self, wasm: &[u8]) -> Result<Box<dyn DiffInstance>> {
78         let module =
79             wasmi::Module::new(&self.engine, wasm).context("unable to validate Wasm module")?;
80         let mut store = wasmi::Store::new(&self.engine, ());
81         let instance = wasmi::Linker::<()>::new(&self.engine)
82             .instantiate(&mut store, &module)
83             .and_then(|i| i.start(&mut store))
84             .context("unable to instantiate module in wasmi")?;
85         Ok(Box::new(WasmiInstance { store, instance }))
86     }
87 
88     fn assert_error_match(&self, lhs: &Error, rhs: &Trap) {
89         match self.trap_code(lhs) {
90             Some(code) => assert_eq!(wasmi_to_wasmtime_trap_code(code), *rhs),
91             None => panic!("unexpected wasmi error {lhs:?}"),
92         }
93     }
94 
95     fn is_non_deterministic_error(&self, err: &Error) -> bool {
96         matches!(
97             self.trap_code(err),
98             Some(wasmi::core::TrapCode::StackOverflow)
99         )
100     }
101 }
102 
103 /// Converts `wasmi` trap code to `wasmtime` trap code.
104 fn wasmi_to_wasmtime_trap_code(trap: wasmi::core::TrapCode) -> Trap {
105     use wasmi::core::TrapCode;
106     match trap {
107         TrapCode::UnreachableCodeReached => Trap::UnreachableCodeReached,
108         TrapCode::MemoryOutOfBounds => Trap::MemoryOutOfBounds,
109         TrapCode::TableOutOfBounds => Trap::TableOutOfBounds,
110         TrapCode::IndirectCallToNull => Trap::IndirectCallToNull,
111         TrapCode::IntegerDivisionByZero => Trap::IntegerDivisionByZero,
112         TrapCode::IntegerOverflow => Trap::IntegerOverflow,
113         TrapCode::BadConversionToInteger => Trap::BadConversionToInteger,
114         TrapCode::StackOverflow => Trap::StackOverflow,
115         TrapCode::BadSignature => Trap::BadSignature,
116         TrapCode::OutOfFuel => unimplemented!("built-in fuel metering is unused"),
117         TrapCode::GrowthOperationLimited => unimplemented!("resource limiter is unused"),
118     }
119 }
120 
121 /// A wrapper for `wasmi` Wasm instances.
122 struct WasmiInstance {
123     store: wasmi::Store<()>,
124     instance: wasmi::Instance,
125 }
126 
127 impl DiffInstance for WasmiInstance {
128     fn name(&self) -> &'static str {
129         "wasmi"
130     }
131 
132     fn evaluate(
133         &mut self,
134         function_name: &str,
135         arguments: &[DiffValue],
136         result_tys: &[DiffValueType],
137     ) -> Result<Option<Vec<DiffValue>>> {
138         let function = self
139             .instance
140             .get_export(&self.store, function_name)
141             .and_then(wasmi::Extern::into_func)
142             .unwrap();
143         let arguments: Vec<_> = arguments.iter().map(|x| x.into()).collect();
144         let mut results = vec![wasmi::Val::I32(0); result_tys.len()];
145         function
146             .call(&mut self.store, &arguments, &mut results)
147             .context("wasmi function trap")?;
148         Ok(Some(results.into_iter().map(Into::into).collect()))
149     }
150 
151     fn get_global(&mut self, name: &str, _ty: DiffValueType) -> Option<DiffValue> {
152         Some(
153             self.instance
154                 .get_export(&self.store, name)
155                 .unwrap()
156                 .into_global()
157                 .unwrap()
158                 .get(&self.store)
159                 .into(),
160         )
161     }
162 
163     fn get_memory(&mut self, name: &str, shared: bool) -> Option<Vec<u8>> {
164         assert!(!shared);
165         Some(
166             self.instance
167                 .get_export(&self.store, name)
168                 .unwrap()
169                 .into_memory()
170                 .unwrap()
171                 .data(&self.store)
172                 .to_vec(),
173         )
174     }
175 }
176 
177 impl From<&DiffValue> for wasmi::Val {
178     fn from(v: &DiffValue) -> Self {
179         use wasmi::Val as WasmiValue;
180         match *v {
181             DiffValue::I32(n) => WasmiValue::I32(n),
182             DiffValue::I64(n) => WasmiValue::I64(n),
183             DiffValue::F32(n) => WasmiValue::F32(wasmi::core::F32::from_bits(n)),
184             DiffValue::F64(n) => WasmiValue::F64(wasmi::core::F64::from_bits(n)),
185             DiffValue::V128(n) => WasmiValue::V128(wasmi::core::V128::from(n)),
186             DiffValue::FuncRef { null } => {
187                 assert!(null);
188                 WasmiValue::FuncRef(wasmi::FuncRef::null())
189             }
190             DiffValue::ExternRef { null } => {
191                 assert!(null);
192                 WasmiValue::ExternRef(wasmi::ExternRef::null())
193             }
194             DiffValue::AnyRef { .. } => unimplemented!(),
195         }
196     }
197 }
198 
199 impl From<wasmi::Val> for DiffValue {
200     fn from(value: wasmi::Val) -> Self {
201         use wasmi::Val as WasmiValue;
202         match value {
203             WasmiValue::I32(n) => DiffValue::I32(n),
204             WasmiValue::I64(n) => DiffValue::I64(n),
205             WasmiValue::F32(n) => DiffValue::F32(n.to_bits()),
206             WasmiValue::F64(n) => DiffValue::F64(n.to_bits()),
207             WasmiValue::V128(n) => DiffValue::V128(n.as_u128()),
208             WasmiValue::FuncRef(f) => DiffValue::FuncRef { null: f.is_null() },
209             WasmiValue::ExternRef(e) => DiffValue::ExternRef { null: e.is_null() },
210         }
211     }
212 }
213 
214 #[cfg(test)]
215 mod tests {
216     use super::*;
217 
218     #[test]
219     fn smoke() {
220         crate::oracles::engine::smoke_test_engine(|_, config| Ok(WasmiEngine::new(config)))
221     }
222 }
223