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