1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright (c) Zefchain Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! A dummy runtime implementation useful for tests.
//!
//! No WebAssembly bytecode can be executed, but it allows calling the canonical ABI functions
//! related to memory allocation.

use std::{
    any::Any,
    borrow::Cow,
    collections::HashMap,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc, Mutex, MutexGuard,
    },
};

use frunk::{hlist, hlist_pat, HList};

use super::{
    GuestPointer, Instance, InstanceWithFunction, InstanceWithMemory, Runtime, RuntimeError,
    RuntimeMemory,
};
use crate::{memory_layout::FlatLayout, ExportFunction, WitLoad, WitStore};

/// A fake Wasm runtime.
pub struct MockRuntime;

impl Runtime for MockRuntime {
    type Export = String;
    type Memory = Arc<Mutex<Vec<u8>>>;
}

/// A closure for handling calls to mocked functions.
pub type FunctionHandler<UserData> =
    Arc<dyn Fn(MockInstance<UserData>, Box<dyn Any>) -> Result<Box<dyn Any>, RuntimeError>>;

/// A fake Wasm instance.
///
/// Only contains exports for the memory and the canonical ABI allocation functions.
pub struct MockInstance<UserData> {
    memory: Arc<Mutex<Vec<u8>>>,
    exported_functions: HashMap<String, FunctionHandler<UserData>>,
    imported_functions: HashMap<String, FunctionHandler<UserData>>,
    user_data: Arc<Mutex<UserData>>,
}

impl<UserData> Default for MockInstance<UserData>
where
    UserData: Default,
{
    fn default() -> Self {
        MockInstance::new(UserData::default())
    }
}

impl<UserData> Clone for MockInstance<UserData> {
    fn clone(&self) -> Self {
        MockInstance {
            memory: self.memory.clone(),
            exported_functions: self.exported_functions.clone(),
            imported_functions: self.imported_functions.clone(),
            user_data: self.user_data.clone(),
        }
    }
}

impl<UserData> MockInstance<UserData> {
    /// Creates a new [`MockInstance`] using the provided `user_data`.
    pub fn new(user_data: UserData) -> Self {
        let memory = Arc::new(Mutex::new(Vec::new()));

        MockInstance {
            memory: memory.clone(),
            exported_functions: HashMap::new(),
            imported_functions: HashMap::new(),
            user_data: Arc::new(Mutex::new(user_data)),
        }
        .with_exported_function("cabi_free", |_, _: HList![i32]| Ok(hlist![]))
        .with_exported_function(
            "cabi_realloc",
            move |_,
                  hlist_pat![_old_address, _old_size, alignment, new_size]: HList![
                i32, i32, i32, i32
            ]| {
                let allocation_size = usize::try_from(new_size)
                    .expect("Failed to allocate a negative amount of memory");

                let mut memory = memory
                    .lock()
                    .expect("Panic while holding a lock to a `MockInstance`'s memory");

                let address = GuestPointer(memory.len().try_into()?).aligned_at(alignment as u32);

                memory.resize(address.0 as usize + allocation_size, 0);

                assert!(
                    memory.len() <= i32::MAX as usize,
                    "No more memory for allocations"
                );

                Ok(hlist![address.0 as i32])
            },
        )
    }
    /// Adds a mock exported function to this [`MockInstance`].
    ///
    /// The `handler` will be called whenever the exported function is called.
    pub fn with_exported_function<Parameters, Results, Handler>(
        mut self,
        name: impl Into<String>,
        handler: Handler,
    ) -> Self
    where
        Parameters: 'static,
        Results: 'static,
        Handler: Fn(MockInstance<UserData>, Parameters) -> Result<Results, RuntimeError> + 'static,
    {
        self.add_exported_function(name, handler);
        self
    }

    /// Adds a mock exported function to this [`MockInstance`].
    ///
    /// The `handler` will be called whenever the exported function is called.
    pub fn add_exported_function<Parameters, Results, Handler>(
        &mut self,
        name: impl Into<String>,
        handler: Handler,
    ) -> &mut Self
    where
        Parameters: 'static,
        Results: 'static,
        Handler: Fn(MockInstance<UserData>, Parameters) -> Result<Results, RuntimeError> + 'static,
    {
        self.exported_functions.insert(
            name.into(),
            Arc::new(move |caller, boxed_parameters| {
                let parameters = boxed_parameters
                    .downcast()
                    .expect("Incorrect parameters used to call handler for exported function");

                handler(caller, *parameters).map(|results| Box::new(results) as Box<dyn Any>)
            }),
        );
        self
    }

    /// Calls a function that the mock instance imported from the host.
    pub fn call_imported_function<Parameters, Results>(
        &self,
        function: &str,
        parameters: Parameters,
    ) -> Result<Results, RuntimeError>
    where
        Parameters: WitStore + 'static,
        Results: WitLoad + 'static,
    {
        let handler = self
            .imported_functions
            .get(function)
            .unwrap_or_else(|| panic!("Missing function imported from host: {function:?}"));

        let flat_parameters = parameters.lower(&mut self.clone().memory()?)?;
        let boxed_flat_results = handler(self.clone(), Box::new(flat_parameters))?;
        let flat_results = *boxed_flat_results
            .downcast()
            .expect("Expected an incorrect results type from imported host function");

        Results::lift_from(flat_results, &self.clone().memory()?)
    }

    /// Returns a copy of the current memory contents.
    pub fn memory_contents(&self) -> Vec<u8> {
        self.memory.lock().unwrap().clone()
    }
}

impl<UserData> Instance for MockInstance<UserData> {
    type Runtime = MockRuntime;
    type UserData = UserData;
    type UserDataReference<'a> = MutexGuard<'a, UserData>
    where
        Self::UserData: 'a,
        Self: 'a;
    type UserDataMutReference<'a> = MutexGuard<'a, UserData>
    where
        Self::UserData: 'a,
        Self: 'a;

    fn load_export(&mut self, name: &str) -> Option<String> {
        if name == "memory" || self.exported_functions.contains_key(name) {
            Some(name.to_owned())
        } else {
            None
        }
    }

    fn user_data(&self) -> Self::UserDataReference<'_> {
        self.user_data
            .try_lock()
            .expect("Unexpected reentrant access to user data in `MockInstance`")
    }

    fn user_data_mut(&mut self) -> Self::UserDataMutReference<'_> {
        self.user_data
            .try_lock()
            .expect("Unexpected reentrant access to user data in `MockInstance`")
    }
}

impl<Parameters, Results, UserData> InstanceWithFunction<Parameters, Results>
    for MockInstance<UserData>
where
    Parameters: FlatLayout + 'static,
    Results: FlatLayout + 'static,
{
    type Function = String;

    fn function_from_export(
        &mut self,
        name: <Self::Runtime as Runtime>::Export,
    ) -> Result<Option<Self::Function>, RuntimeError> {
        Ok(Some(name))
    }

    fn call(
        &mut self,
        function: &Self::Function,
        parameters: Parameters,
    ) -> Result<Results, RuntimeError> {
        let handler = self
            .exported_functions
            .get(function)
            .ok_or_else(|| RuntimeError::FunctionNotFound(function.clone()))?;

        let results = handler(self.clone(), Box::new(parameters))?;

        Ok(*results.downcast().unwrap_or_else(|_| {
            panic!("Incorrect results type expected from handler of expected function: {function}")
        }))
    }
}

impl<UserData> RuntimeMemory<MockInstance<UserData>> for Arc<Mutex<Vec<u8>>> {
    fn read<'instance>(
        &self,
        instance: &'instance MockInstance<UserData>,
        location: GuestPointer,
        length: u32,
    ) -> Result<Cow<'instance, [u8]>, RuntimeError> {
        let memory = instance
            .memory
            .lock()
            .expect("Panic while holding a lock to a `MockInstance`'s memory");

        let start = location.0 as usize;
        let end = start + length as usize;

        Ok(Cow::Owned(memory[start..end].to_owned()))
    }

    fn write(
        &mut self,
        instance: &mut MockInstance<UserData>,
        location: GuestPointer,
        bytes: &[u8],
    ) -> Result<(), RuntimeError> {
        let mut memory = instance
            .memory
            .lock()
            .expect("Panic while holding a lock to a `MockInstance`'s memory");

        let start = location.0 as usize;
        let end = start + bytes.len();

        memory[start..end].copy_from_slice(bytes);

        Ok(())
    }
}

impl<UserData> InstanceWithMemory for MockInstance<UserData> {
    fn memory_from_export(
        &self,
        export: String,
    ) -> Result<Option<Arc<Mutex<Vec<u8>>>>, RuntimeError> {
        if export == "memory" {
            Ok(Some(self.memory.clone()))
        } else {
            Err(RuntimeError::NotMemory)
        }
    }
}

impl<Handler, Parameters, Results, UserData> ExportFunction<Handler, Parameters, Results>
    for MockInstance<UserData>
where
    Handler: Fn(MockInstance<UserData>, Parameters) -> Result<Results, RuntimeError> + 'static,
    Parameters: 'static,
    Results: 'static,
{
    fn export(
        &mut self,
        module_name: &str,
        function_name: &str,
        handler: Handler,
    ) -> Result<(), RuntimeError> {
        let name = format!("{module_name}#{function_name}");

        self.imported_functions.insert(
            name.clone(),
            Arc::new(move |instance, boxed_parameters| {
                let parameters = boxed_parameters.downcast().unwrap_or_else(|_| {
                    panic!(
                        "Incorrect parameters used to call handler for exported function {name:?}"
                    )
                });

                let results = handler(instance, *parameters)?;

                Ok(Box::new(results))
            }),
        );

        Ok(())
    }
}

/// A helper trait to serve as an equivalent to `crate::wasmer::WasmerResults` and
/// `crate::wasmtime::WasmtimeResults` for the [`MockInstance`].
///
/// This is in order to help with writing tests generic over the Wasm guest instance type.
pub trait MockResults {
    /// The mock native type of the results for the [`MockInstance`].
    type Results;
}

impl<T> MockResults for T {
    type Results = T;
}

/// A helper type to verify how many times an exported function is called.
pub struct MockExportedFunction<Parameters, Results, UserData> {
    name: String,
    call_counter: Arc<AtomicUsize>,
    expected_calls: usize,
    handler: Arc<dyn Fn(MockInstance<UserData>, Parameters) -> Result<Results, RuntimeError>>,
}

impl<Parameters, Results, UserData> MockExportedFunction<Parameters, Results, UserData>
where
    Parameters: 'static,
    Results: 'static,
    UserData: 'static,
{
    /// Creates a new [`MockExportedFunction`] for the exported function with the provided `name`.
    ///
    /// Every call to the exported function is called is forwarded to the `handler` and an internal
    /// counter is incremented. When the [`MockExportedFunction`] instance is dropped (which should
    /// be done at the end of the test), it asserts that the function was called `expected_calls`
    /// times.
    pub fn new(
        name: impl Into<String>,
        handler: impl Fn(MockInstance<UserData>, Parameters) -> Result<Results, RuntimeError> + 'static,
        expected_calls: usize,
    ) -> Self {
        MockExportedFunction {
            name: name.into(),
            call_counter: Arc::default(),
            expected_calls,
            handler: Arc::new(handler),
        }
    }

    /// Registers this [`MockExportedFunction`] with the mock `instance`.
    pub fn register(&self, instance: &mut MockInstance<UserData>) {
        let call_counter = self.call_counter.clone();
        let handler = self.handler.clone();

        instance.add_exported_function(self.name.clone(), move |caller, parameters: Parameters| {
            call_counter.fetch_add(1, Ordering::AcqRel);
            handler(caller, parameters)
        });
    }
}

impl<Parameters, Results, UserData> Drop for MockExportedFunction<Parameters, Results, UserData> {
    fn drop(&mut self) {
        assert_eq!(
            self.call_counter.load(Ordering::Acquire),
            self.expected_calls,
            "Unexpected number of calls to `{}`",
            self.name
        );
    }
}