xref: /wasmtime-44.0.1/examples/memory.rs (revision 94740588)
1 //! An example of how to interact with wasm memory.
2 //!
3 //! Here a small wasm module is used to show how memory is initialized, how to
4 //! read and write memory through the `Memory` object, and how wasm functions
5 //! can trap when dealing with out-of-bounds addresses.
6 
7 // You can execute this example with `cargo run --example memory`
8 
9 use wasmtime::*;
10 
main() -> Result<()>11 fn main() -> Result<()> {
12     // Create our `store_fn` context and then compile a module and create an
13     // instance from the compiled module all in one go.
14     let mut store: Store<()> = Store::default();
15     let module = Module::from_file(store.engine(), "examples/memory.wat")?;
16     let instance = Instance::new(&mut store, &module, &[])?;
17 
18     // load_fn up our exports from the instance
19     let memory = instance
20         .get_memory(&mut store, "memory")
21         .ok_or(wasmtime::format_err!("failed to find `memory` export"))?;
22     let size = instance.get_typed_func::<(), i32>(&mut store, "size")?;
23     let load_fn = instance.get_typed_func::<i32, i32>(&mut store, "load")?;
24     let store_fn = instance.get_typed_func::<(i32, i32), ()>(&mut store, "store")?;
25 
26     println!("Checking memory...");
27     assert_eq!(memory.size(&store), 2);
28     assert_eq!(memory.data_size(&store), 0x20000);
29     assert_eq!(memory.data_mut(&mut store)[0], 0);
30     assert_eq!(memory.data_mut(&mut store)[0x1000], 1);
31     assert_eq!(memory.data_mut(&mut store)[0x1003], 4);
32 
33     assert_eq!(size.call(&mut store, ())?, 2);
34     assert_eq!(load_fn.call(&mut store, 0)?, 0);
35     assert_eq!(load_fn.call(&mut store, 0x1000)?, 1);
36     assert_eq!(load_fn.call(&mut store, 0x1003)?, 4);
37     assert_eq!(load_fn.call(&mut store, 0x1ffff)?, 0);
38     assert!(load_fn.call(&mut store, 0x20000).is_err()); // out of bounds trap
39 
40     println!("Mutating memory...");
41     memory.data_mut(&mut store)[0x1003] = 5;
42 
43     store_fn.call(&mut store, (0x1002, 6))?;
44     assert!(store_fn.call(&mut store, (0x20000, 0)).is_err()); // out of bounds trap
45 
46     assert_eq!(memory.data(&store)[0x1002], 6);
47     assert_eq!(memory.data(&store)[0x1003], 5);
48     assert_eq!(load_fn.call(&mut store, 0x1002)?, 6);
49     assert_eq!(load_fn.call(&mut store, 0x1003)?, 5);
50 
51     // Grow memory.
52     println!("Growing memory...");
53     memory.grow(&mut store, 1)?;
54     assert_eq!(memory.size(&store), 3);
55     assert_eq!(memory.data_size(&store), 0x30000);
56 
57     assert_eq!(load_fn.call(&mut store, 0x20000)?, 0);
58     store_fn.call(&mut store, (0x20000, 0))?;
59     assert!(load_fn.call(&mut store, 0x30000).is_err());
60     assert!(store_fn.call(&mut store, (0x30000, 0)).is_err());
61 
62     assert!(memory.grow(&mut store, 1).is_err());
63     assert!(memory.grow(&mut store, 0).is_ok());
64 
65     println!("Creating stand-alone memory...");
66     let memorytype = MemoryType::new(5, Some(5));
67     let memory2 = Memory::new(&mut store, memorytype)?;
68     assert_eq!(memory2.size(&store), 5);
69     assert!(memory2.grow(&mut store, 1).is_err());
70     assert!(memory2.grow(&mut store, 0).is_ok());
71 
72     Ok(())
73 }
74