xref: /wasmtime-44.0.1/tests/all/instance.rs (revision 6ac02e10)
1 use wasmtime::*;
2 
3 #[test]
4 #[cfg_attr(miri, ignore)]
wrong_import_numbers() -> Result<()>5 fn wrong_import_numbers() -> Result<()> {
6     let mut store = Store::<()>::default();
7     let module = Module::new(store.engine(), r#"(module (import "" "" (func)))"#)?;
8 
9     assert!(Instance::new(&mut store, &module, &[]).is_err());
10     let func = Func::wrap(&mut store, || {});
11     assert!(Instance::new(&mut store, &module, &[func.into(), func.into()]).is_err());
12     Ok(())
13 }
14 
15 #[test]
16 #[cfg_attr(miri, ignore)]
initializes_linear_memory() -> Result<()>17 fn initializes_linear_memory() -> Result<()> {
18     // Test for https://github.com/bytecodealliance/wasmtime/issues/2784
19     let wat = r#"
20         (module
21             (memory (export "memory") 2)
22             (data (i32.const 0) "Hello World!")
23         )"#;
24     let module = Module::new(&Engine::default(), wat)?;
25 
26     let mut store = Store::new(module.engine(), ());
27     let instance = Instance::new(&mut store, &module, &[])?;
28     let memory = instance.get_memory(&mut store, "memory").unwrap();
29 
30     let mut bytes = [0; 12];
31     memory.read(&store, 0, &mut bytes)?;
32     assert_eq!(bytes, "Hello World!".as_bytes());
33     Ok(())
34 }
35 
36 #[test]
37 #[cfg_attr(miri, ignore)]
38 #[cfg(target_pointer_width = "64")]
linear_memory_limits() -> Result<()>39 fn linear_memory_limits() -> Result<()> {
40     // this test will allocate 4GB of virtual memory space, and may not work in
41     // situations like CI QEMU emulation where it triggers SIGKILL.
42     if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
43         return Ok(());
44     }
45     test(&Engine::default())?;
46     let mut pool = crate::small_pool_config();
47     pool.max_memory_size(1 << 32);
48     test(&Engine::new(Config::new().allocation_strategy(
49         InstanceAllocationStrategy::Pooling(pool),
50     ))?)?;
51     return Ok(());
52 
53     fn test(engine: &Engine) -> Result<()> {
54         let wat = r#"
55         (module
56             (memory 65534)
57 
58             (func (export "grow")  (result i32)
59                 i32.const 1
60                 memory.grow)
61             (func (export "size")  (result i32)
62                 memory.size)
63         )
64     "#;
65         let module = Module::new(engine, wat)?;
66 
67         let mut store = Store::new(engine, ());
68         let instance = Instance::new(&mut store, &module, &[])?;
69         let size = instance.get_typed_func::<(), i32>(&mut store, "size")?;
70         let grow = instance.get_typed_func::<(), i32>(&mut store, "grow")?;
71 
72         assert_eq!(size.call(&mut store, ())?, 65534);
73         assert_eq!(grow.call(&mut store, ())?, 65534);
74         assert_eq!(size.call(&mut store, ())?, 65535);
75         assert_eq!(grow.call(&mut store, ())?, 65535);
76         assert_eq!(size.call(&mut store, ())?, 65536);
77         assert_eq!(grow.call(&mut store, ())?, -1);
78         assert_eq!(size.call(&mut store, ())?, 65536);
79         Ok(())
80     }
81 }
82