1 //! An example of how to interact with multiple memories.
2 //!
3 //! Here a small wasm module with multiple memories is used to show how memory
4 //! is initialized, how to read and write memory through the `Memory` object,
5 //! and how wasm functions can trap when dealing with out-of-bounds addresses.
6 
7 // You can execute this example with `cargo run --example example`
8 
9 use anyhow::Result;
10 use wasmtime::*;
11 
12 fn main() -> Result<()> {
13     // Enable the multi-memory feature.
14     let mut config = Config::new();
15     config.wasm_multi_memory(true);
16 
17     let engine = Engine::new(&config)?;
18 
19     // Create our `store_fn` context and then compile a module and create an
20     // instance from the compiled module all in one go.
21     let mut store = Store::new(&engine, ());
22     let module = Module::from_file(store.engine(), "examples/multimemory.wat")?;
23     let instance = Instance::new(&mut store, &module, &[])?;
24 
25     let memory0 = instance
26         .get_memory(&mut store, "memory0")
27         .ok_or(anyhow::format_err!("failed to find `memory0` export"))?;
28     let size0 = instance.get_typed_func::<(), i32, _>(&mut store, "size0")?;
29     let load0 = instance.get_typed_func::<i32, i32, _>(&mut store, "load0")?;
30     let store0 = instance.get_typed_func::<(i32, i32), (), _>(&mut store, "store0")?;
31 
32     let memory1 = instance
33         .get_memory(&mut store, "memory1")
34         .ok_or(anyhow::format_err!("failed to find `memory1` export"))?;
35     let size1 = instance.get_typed_func::<(), i32, _>(&mut store, "size1")?;
36     let load1 = instance.get_typed_func::<i32, i32, _>(&mut store, "load1")?;
37     let store1 = instance.get_typed_func::<(i32, i32), (), _>(&mut store, "store1")?;
38 
39     println!("Checking memory...");
40     assert_eq!(memory0.size(&store), 2);
41     assert_eq!(memory0.data_size(&store), 0x20000);
42     assert_eq!(memory0.data_mut(&mut store)[0], 0);
43     assert_eq!(memory0.data_mut(&mut store)[0x1000], 1);
44     assert_eq!(memory0.data_mut(&mut store)[0x1001], 2);
45     assert_eq!(memory0.data_mut(&mut store)[0x1002], 3);
46     assert_eq!(memory0.data_mut(&mut store)[0x1003], 4);
47 
48     assert_eq!(size0.call(&mut store, ())?, 2);
49     assert_eq!(load0.call(&mut store, 0)?, 0);
50     assert_eq!(load0.call(&mut store, 0x1000)?, 1);
51     assert_eq!(load0.call(&mut store, 0x1001)?, 2);
52     assert_eq!(load0.call(&mut store, 0x1002)?, 3);
53     assert_eq!(load0.call(&mut store, 0x1003)?, 4);
54     assert_eq!(load0.call(&mut store, 0x1ffff)?, 0);
55     assert!(load0.call(&mut store, 0x20000).is_err()); // out of bounds trap
56 
57     assert_eq!(memory1.size(&store), 2);
58     assert_eq!(memory1.data_size(&store), 0x20000);
59     assert_eq!(memory1.data_mut(&mut store)[0], 0);
60     assert_eq!(memory1.data_mut(&mut store)[0x1000], 4);
61     assert_eq!(memory1.data_mut(&mut store)[0x1001], 3);
62     assert_eq!(memory1.data_mut(&mut store)[0x1002], 2);
63     assert_eq!(memory1.data_mut(&mut store)[0x1003], 1);
64 
65     assert_eq!(size1.call(&mut store, ())?, 2);
66     assert_eq!(load1.call(&mut store, 0)?, 0);
67     assert_eq!(load1.call(&mut store, 0x1000)?, 4);
68     assert_eq!(load1.call(&mut store, 0x1001)?, 3);
69     assert_eq!(load1.call(&mut store, 0x1002)?, 2);
70     assert_eq!(load1.call(&mut store, 0x1003)?, 1);
71     assert_eq!(load1.call(&mut store, 0x1ffff)?, 0);
72     assert!(load0.call(&mut store, 0x20000).is_err()); // out of bounds trap
73 
74     println!("Mutating memory...");
75     memory0.data_mut(&mut store)[0x1003] = 5;
76 
77     store0.call(&mut store, (0x1002, 6))?;
78     assert!(store0.call(&mut store, (0x20000, 0)).is_err()); // out of bounds trap
79 
80     assert_eq!(memory0.data(&store)[0x1002], 6);
81     assert_eq!(memory0.data(&store)[0x1003], 5);
82     assert_eq!(load0.call(&mut store, 0x1002)?, 6);
83     assert_eq!(load0.call(&mut store, 0x1003)?, 5);
84 
85     memory1.data_mut(&mut store)[0x1003] = 7;
86 
87     store1.call(&mut store, (0x1002, 8))?;
88     assert!(store1.call(&mut store, (0x20000, 0)).is_err()); // out of bounds trap
89 
90     assert_eq!(memory1.data(&store)[0x1002], 8);
91     assert_eq!(memory1.data(&store)[0x1003], 7);
92     assert_eq!(load1.call(&mut store, 0x1002)?, 8);
93     assert_eq!(load1.call(&mut store, 0x1003)?, 7);
94 
95     println!("Growing memory...");
96     memory0.grow(&mut store, 1)?;
97     assert_eq!(memory0.size(&store), 3);
98     assert_eq!(memory0.data_size(&store), 0x30000);
99 
100     assert_eq!(load0.call(&mut store, 0x20000)?, 0);
101     store0.call(&mut store, (0x20000, 0))?;
102     assert!(load0.call(&mut store, 0x30000).is_err());
103     assert!(store0.call(&mut store, (0x30000, 0)).is_err());
104 
105     assert!(memory0.grow(&mut store, 1).is_err());
106     assert!(memory0.grow(&mut store, 0).is_ok());
107 
108     memory1.grow(&mut store, 2)?;
109     assert_eq!(memory1.size(&store), 4);
110     assert_eq!(memory1.data_size(&store), 0x40000);
111 
112     assert_eq!(load1.call(&mut store, 0x30000)?, 0);
113     store1.call(&mut store, (0x30000, 0))?;
114     assert!(load1.call(&mut store, 0x40000).is_err());
115     assert!(store1.call(&mut store, (0x40000, 0)).is_err());
116 
117     assert!(memory1.grow(&mut store, 1).is_err());
118     assert!(memory1.grow(&mut store, 0).is_ok());
119 
120     Ok(())
121 }
122