wasmedge_sdk/
vm.rs

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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Defines WasmEdge Vm struct.
use crate::{
    error::{VmError, WasmEdgeError},
    ImportObject, Instance, Module, Store, WasmEdgeResult, WasmValue,
};
use sys::AsInstance;
use wasmedge_sys as sys;

pub trait SyncInst: AsInstance {}
impl<T> SyncInst for ImportObject<T> {}
impl SyncInst for Instance {}

/// A [Vm] defines a virtual environment for managing WebAssembly programs.
///
/// # Example
///
/// The example below presents how to register a module as named module in a Vm instance and run a target wasm function.
///
/// ```rust
/// use std::collections::HashMap;
/// use wasmedge_sdk::{params, Store, Module, WasmVal, wat2wasm, ValType, NeverType, Vm, vm::SyncInst};
///
/// // create a Vm context
/// let mut vm =
///     Vm::new(Store::new(None, HashMap::<String, &mut dyn SyncInst>::new()).unwrap());
/// // register a wasm module from the given in-memory wasm bytes
/// // load wasm module
/// let result = wat2wasm(
///     br#"(module
///     (export "fib" (func $fib))
///     (func $fib (param $n i32) (result i32)
///      (if
///       (i32.lt_s
///        (local.get $n)
///        (i32.const 2)
///       )
///       (then
///         (return i32.const 1)
///       )
///      )
///      (return
///       (i32.add
///        (call $fib
///         (i32.sub
///          (local.get $n)
///          (i32.const 2)
///         )
///        )
///        (call $fib
///         (i32.sub
///          (local.get $n)
///          (i32.const 1)
///         )
///        )
///       )
///      )
///     )
///    )
/// "#,
/// );
/// assert!(result.is_ok());
/// let wasm_bytes = result.unwrap();
/// // run `fib` function from the wasm bytes
/// let fib_module = Module::from_bytes(None, wasm_bytes).unwrap();
/// vm.register_module(None, fib_module).unwrap();
/// let result = vm.run_func(None, "fib", params!(10i32));
/// assert!(result.is_ok());
/// let returns = result.unwrap();
/// assert_eq!(returns.len(), 1);
/// assert_eq!(returns[0].to_i32(), 89);
/// ```
#[derive(Debug)]
pub struct Vm<'inst, T: ?Sized + SyncInst> {
    store: Store<'inst, T>,
    active_instance: Option<sys::Instance>,
}
impl<'inst, T: ?Sized + SyncInst> Vm<'inst, T> {
    pub fn new(store: Store<'inst, T>) -> Self {
        // create a Vm instance
        Vm {
            store,
            active_instance: None,
        }
    }

    /// Registers a [wasm module](crate::Module) into this vm as a named or active module [instance](crate::Instance).
    ///
    /// # Arguments
    ///
    /// * `mod_name` - The exported name for the registered module. If `None`, then the module is registered as an active instance.
    ///
    /// * `module` - The module to be registered.
    ///
    /// # Error
    ///
    /// If fail to register the given [module](crate::Module), then an error is returned.
    ///
    pub fn register_module(
        &mut self,
        mod_name: Option<&str>,
        module: Module,
    ) -> WasmEdgeResult<&mut Self> {
        match mod_name {
            Some(name) => {
                self.store.register_named_module(name, &module)?;
            }
            None => {
                self.active_instance = Some(self.store.register_active_module(&module)?);
            }
        };

        Ok(self)
    }

    /// Runs an exported wasm function in a (named or active) [module instance](crate::Instance).
    ///
    /// # Arguments
    ///
    /// * `mod_name` - The exported name of the module instance, which holds the target function. If `None`, then the active module is used.
    ///
    /// * `func_name` - The exported name of the target wasm function.
    ///
    /// * `args` - The arguments to be passed to the target wasm function.
    ///
    /// # Error
    ///
    /// If fail to run the wasm function, then an error is returned.
    pub fn run_func(
        &mut self,
        mod_name: Option<&str>,
        func_name: impl AsRef<str>,
        args: impl IntoIterator<Item = WasmValue>,
    ) -> WasmEdgeResult<Vec<WasmValue>> {
        let (mut func, executor) = match mod_name {
            Some(mod_name) => {
                if let Some((inst, executor)) = self.store.get_instance_and_executor(mod_name) {
                    (inst.get_func_mut(func_name.as_ref())?, executor)
                } else if let Some((wasm_mod, executor)) =
                    self.store.get_named_wasm_and_executor(mod_name)
                {
                    (wasm_mod.get_func_mut(func_name.as_ref())?, executor)
                } else {
                    return Err(Box::new(WasmEdgeError::Vm(VmError::NotFoundModule(
                        mod_name.into(),
                    ))));
                }
            }
            None => {
                let active_inst = self
                    .active_instance
                    .as_mut()
                    .ok_or(Box::new(WasmEdgeError::Vm(VmError::NotFoundActiveModule)))?;

                (
                    active_inst.get_func_mut(func_name.as_ref())?,
                    self.store.executor(),
                )
            }
        };
        executor.call_func(&mut func, args)
    }

    /// Runs an exported wasm function in a (named or active) [module instance](crate::Instance) with a timeout setting
    ///
    /// # Arguments
    ///
    /// * `mod_name` - The exported name of the module instance, which holds the target function. If `None`, then the active module is used.
    ///
    /// * `func_name` - The exported name of the target wasm function.
    ///
    /// * `args` - The arguments to be passed to the target wasm function.
    ///
    /// * `timeout` - The maximum execution time of the function to be run.
    ///
    /// # Error
    ///
    /// If fail to run the wasm function, then an error is returned.
    #[cfg(all(target_os = "linux", not(target_env = "musl")))]
    pub fn run_func_with_timeout(
        &mut self,
        mod_name: Option<&str>,
        func_name: impl AsRef<str>,
        args: impl IntoIterator<Item = WasmValue>,
        timeout: std::time::Duration,
    ) -> WasmEdgeResult<Vec<WasmValue>> {
        let (mut func, executor) = match mod_name {
            Some(mod_name) => {
                if let Some((inst, executor)) = self.store.get_instance_and_executor(mod_name) {
                    (inst.get_func_mut(func_name.as_ref())?, executor)
                } else if let Some((wasm_mod, executor)) =
                    self.store.get_named_wasm_and_executor(mod_name)
                {
                    (wasm_mod.get_func_mut(func_name.as_ref())?, executor)
                } else {
                    return Err(Box::new(WasmEdgeError::Vm(VmError::NotFoundModule(
                        mod_name.into(),
                    ))));
                }
            }
            None => {
                let active_inst = self
                    .active_instance
                    .as_mut()
                    .ok_or(Box::new(WasmEdgeError::Vm(VmError::NotFoundActiveModule)))?;

                (
                    active_inst.get_func_mut(func_name.as_ref())?,
                    self.store.executor(),
                )
            }
        };
        executor.call_func_with_timeout(&mut func, args, timeout)
    }

    /// Returns a reference to the internal [store](crate::Store) from this vm.
    pub fn store(&self) -> &Store<'inst, T> {
        &self.store
    }

    /// Returns a mutable reference to the internal [store](crate::Store) from this vm.
    pub fn store_mut(&mut self) -> &mut Store<'inst, T> {
        &mut self.store
    }

    /// Returns a reference to the active [module instance](crate::Instance) from this vm.
    ///
    /// # Error
    ///
    /// If fail to get the reference to the active module instance, then an error is returned.
    pub fn active_module(&self) -> Option<&Instance> {
        self.active_instance.as_ref()
    }

    /// Returns a mutable reference to the active [module instance](crate::Instance) from this vm.
    ///
    /// # Error
    ///
    /// If fail to get the mutable reference to the active module instance, then an error is returned.
    pub fn active_module_mut(&mut self) -> Option<&mut Instance> {
        self.active_instance.as_mut()
    }

    /// Checks if the vm contains a named module instance.
    ///
    /// # Argument
    ///
    /// * `mod_name` - The exported name of the target module instance.
    ///
    pub fn contains_module(&self, mod_name: impl AsRef<str>) -> bool {
        self.store.contains(mod_name)
    }

    /// Returns the count of the named [module instances](crate::Instance) this vm holds.
    pub fn named_instance_count(&self) -> usize {
        self.store.named_instance_count()
    }

    /// Returns the names of all named [module instances](crate::Instance) this vm holds.
    pub fn instance_names(&self) -> Vec<String> {
        self.store.instance_names()
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use wasmedge_types::wat2wasm;

    use super::*;
    use crate::{params, WasmVal};

    #[test]
    #[cfg(target_os = "linux")]
    // To enable this test function, please install `wasi_crypto` plugin first.
    fn test_vmbuilder() -> Result<(), Box<dyn std::error::Error>> {
        use crate::{params, plugin::PluginManager};

        // load plugins from the default plugin path
        PluginManager::load(None)?;

        PluginManager::names().iter().for_each(|name| {
            println!("plugin name: {}", name);
        });

        let wasm_app_file = "examples/wasmedge-sys/data/test_crypto.wasm";

        let mut wasi = crate::wasi::WasiModule::create(None, None, None).unwrap();
        let mut wasi_crypto_asymmetric_common =
            PluginManager::load_wasi_crypto_asymmetric_common().unwrap();
        let mut wasi_crypto_signatures = PluginManager::load_wasi_crypto_signatures().unwrap();
        let mut wasi_crypto_symmetric = PluginManager::load_wasi_crypto_symmetric().unwrap();

        let mut instances = HashMap::new();
        instances.insert(wasi.name().to_string(), wasi.as_mut());
        instances.insert(
            wasi_crypto_asymmetric_common.name().unwrap(),
            &mut wasi_crypto_asymmetric_common,
        );
        instances.insert(
            wasi_crypto_signatures.name().unwrap(),
            &mut wasi_crypto_signatures,
        );
        instances.insert(
            wasi_crypto_symmetric.name().unwrap(),
            &mut wasi_crypto_symmetric,
        );

        let mut vm = Vm::new(Store::new(None, instances).unwrap());

        let module = Module::from_file(None, &wasm_app_file).unwrap();

        vm.register_module(Some("wasm-app"), module).unwrap();
        vm.run_func(Some("wasm-app"), "_start", params!())?;

        Ok(())
    }

    #[test]
    fn test_vm_run_func_from_file() {
        // create a Vm context
        let mut vm =
            Vm::new(Store::new(None, HashMap::<String, &mut dyn SyncInst>::new()).unwrap());

        // register a wasm module from a specified wasm file
        let file = std::env::current_dir()
            .unwrap()
            .join("examples/wasmedge-sys/data/fibonacci.wat");

        // run `fib` function from the wasm file
        let fib_module = Module::from_file(None, file).unwrap();
        vm.register_module(None, fib_module).unwrap();
        let result = vm.run_func(None, "fib", params!(10i32));
        assert!(result.is_ok());
        let returns = result.unwrap();
        assert_eq!(returns.len(), 1);
        assert_eq!(returns[0].to_i32(), 89);
    }

    #[test]
    fn test_vm_run_func_from_bytes() {
        // create a Vm context
        let mut vm =
            Vm::new(Store::new(None, HashMap::<String, &mut dyn SyncInst>::new()).unwrap());

        // register a wasm module from the given in-memory wasm bytes
        // load wasm module
        let result = wat2wasm(
            br#"(module
            (export "fib" (func $fib))
            (func $fib (param $n i32) (result i32)
             (if
              (i32.lt_s
               (local.get $n)
               (i32.const 2)
              )
              (then
                (return (i32.const 1))
              )
             )
             (return
              (i32.add
               (call $fib
                (i32.sub
                 (local.get $n)
                 (i32.const 2)
                )
               )
               (call $fib
                (i32.sub
                 (local.get $n)
                 (i32.const 1)
                )
               )
              )
             )
            )
           )
        "#,
        );
        assert!(result.is_ok());
        let wasm_bytes = result.unwrap();

        // run `fib` function from the wasm bytes
        let fib_module = Module::from_bytes(None, wasm_bytes).unwrap();
        vm.register_module(None, fib_module).unwrap();
        let result = vm.run_func(None, "fib", params!(10i32));
        assert!(result.is_ok());
        let returns = result.unwrap();
        assert_eq!(returns.len(), 1);
        assert_eq!(returns[0].to_i32(), 89);
    }

    #[test]
    fn test_vm_run_func_in_named_module_instance() {
        // create a Vm context
        let mut vm =
            Vm::new(Store::new(None, HashMap::<String, &mut dyn SyncInst>::new()).unwrap());

        // register a wasm module from the given in-memory wasm bytes
        // load wasm module
        let result = wat2wasm(
            br#"(module
            (export "fib" (func $fib))
            (func $fib (param $n i32) (result i32)
             (if
              (i32.lt_s
               (local.get $n)
               (i32.const 2)
              )
              (then
                (return (i32.const 1))
              )
             )
             (return
              (i32.add
               (call $fib
                (i32.sub
                 (local.get $n)
                 (i32.const 2)
                )
               )
               (call $fib
                (i32.sub
                 (local.get $n)
                 (i32.const 1)
                )
               )
              )
             )
            )
           )
        "#,
        );
        assert!(result.is_ok());
        let wasm_bytes = result.unwrap();
        let fib_module = Module::from_bytes(None, wasm_bytes).unwrap();
        vm.register_module(Some("extern"), fib_module).unwrap();
        // run `fib` function in the named module instance
        let result = vm.run_func(Some("extern"), "fib", params!(10));
        assert!(result.is_ok());
        let returns = result.unwrap();
        assert_eq!(returns.len(), 1);
        assert_eq!(returns[0].to_i32(), 89);
    }
}