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