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