xref: /wasmtime-44.0.1/tests/all/instance.rs (revision 6ac02e10)
14c82da44SAlex Crichton use wasmtime::*;
24c82da44SAlex Crichton 
34c82da44SAlex Crichton #[test]
4*6ac02e10SAlex Crichton #[cfg_attr(miri, ignore)]
wrong_import_numbers() -> Result<()>54c82da44SAlex Crichton fn wrong_import_numbers() -> Result<()> {
67a1b7cdfSAlex Crichton     let mut store = Store::<()>::default();
715c68f2cSYury Delendik     let module = Module::new(store.engine(), r#"(module (import "" "" (func)))"#)?;
84c82da44SAlex Crichton 
97a1b7cdfSAlex Crichton     assert!(Instance::new(&mut store, &module, &[]).is_err());
107a1b7cdfSAlex Crichton     let func = Func::wrap(&mut store, || {});
11a0442ea0SHamir Mahal     assert!(Instance::new(&mut store, &module, &[func.into(), func.into()]).is_err());
124c82da44SAlex Crichton     Ok(())
134c82da44SAlex Crichton }
1437bb7af4SPeter Huene 
1537bb7af4SPeter Huene #[test]
16c0bb341dSAlex Crichton #[cfg_attr(miri, ignore)]
initializes_linear_memory() -> Result<()>1737bb7af4SPeter Huene fn initializes_linear_memory() -> Result<()> {
1837bb7af4SPeter Huene     // Test for https://github.com/bytecodealliance/wasmtime/issues/2784
1937bb7af4SPeter Huene     let wat = r#"
2037bb7af4SPeter Huene         (module
2137bb7af4SPeter Huene             (memory (export "memory") 2)
2237bb7af4SPeter Huene             (data (i32.const 0) "Hello World!")
2337bb7af4SPeter Huene         )"#;
2437bb7af4SPeter Huene     let module = Module::new(&Engine::default(), wat)?;
2537bb7af4SPeter Huene 
267a1b7cdfSAlex Crichton     let mut store = Store::new(module.engine(), ());
277a1b7cdfSAlex Crichton     let instance = Instance::new(&mut store, &module, &[])?;
287a1b7cdfSAlex Crichton     let memory = instance.get_memory(&mut store, "memory").unwrap();
2937bb7af4SPeter Huene 
3037bb7af4SPeter Huene     let mut bytes = [0; 12];
317a1b7cdfSAlex Crichton     memory.read(&store, 0, &mut bytes)?;
3237bb7af4SPeter Huene     assert_eq!(bytes, "Hello World!".as_bytes());
3337bb7af4SPeter Huene     Ok(())
3437bb7af4SPeter Huene }
357a1b7cdfSAlex Crichton 
367a1b7cdfSAlex Crichton #[test]
37c0bb341dSAlex Crichton #[cfg_attr(miri, ignore)]
381f812627SAlex Crichton #[cfg(target_pointer_width = "64")]
linear_memory_limits() -> Result<()>397a1b7cdfSAlex Crichton fn linear_memory_limits() -> Result<()> {
407a1b7cdfSAlex Crichton     // this test will allocate 4GB of virtual memory space, and may not work in
417a1b7cdfSAlex Crichton     // situations like CI QEMU emulation where it triggers SIGKILL.
427a1b7cdfSAlex Crichton     if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
437a1b7cdfSAlex Crichton         return Ok(());
447a1b7cdfSAlex Crichton     }
457a1b7cdfSAlex Crichton     test(&Engine::default())?;
46a34427a3SNick Fitzgerald     let mut pool = crate::small_pool_config();
47906ea017SAlex Crichton     pool.max_memory_size(1 << 32);
487a1b7cdfSAlex Crichton     test(&Engine::new(Config::new().allocation_strategy(
49b14551d7SAlex Crichton         InstanceAllocationStrategy::Pooling(pool),
507a1b7cdfSAlex Crichton     ))?)?;
517a1b7cdfSAlex Crichton     return Ok(());
527a1b7cdfSAlex Crichton 
537a1b7cdfSAlex Crichton     fn test(engine: &Engine) -> Result<()> {
547a1b7cdfSAlex Crichton         let wat = r#"
557a1b7cdfSAlex Crichton         (module
5683007b79SUlrich Weigand             (memory 65534)
577a1b7cdfSAlex Crichton 
5863a3bbbfSAlex Crichton             (func (export "grow")  (result i32)
597a1b7cdfSAlex Crichton                 i32.const 1
607a1b7cdfSAlex Crichton                 memory.grow)
6163a3bbbfSAlex Crichton             (func (export "size")  (result i32)
6263a3bbbfSAlex Crichton                 memory.size)
637a1b7cdfSAlex Crichton         )
647a1b7cdfSAlex Crichton     "#;
657a1b7cdfSAlex Crichton         let module = Module::new(engine, wat)?;
667a1b7cdfSAlex Crichton 
677a1b7cdfSAlex Crichton         let mut store = Store::new(engine, ());
687a1b7cdfSAlex Crichton         let instance = Instance::new(&mut store, &module, &[])?;
69b0939f66SAlex Crichton         let size = instance.get_typed_func::<(), i32>(&mut store, "size")?;
70b0939f66SAlex Crichton         let grow = instance.get_typed_func::<(), i32>(&mut store, "grow")?;
717a1b7cdfSAlex Crichton 
7263a3bbbfSAlex Crichton         assert_eq!(size.call(&mut store, ())?, 65534);
7363a3bbbfSAlex Crichton         assert_eq!(grow.call(&mut store, ())?, 65534);
7463a3bbbfSAlex Crichton         assert_eq!(size.call(&mut store, ())?, 65535);
7563a3bbbfSAlex Crichton         assert_eq!(grow.call(&mut store, ())?, 65535);
7663a3bbbfSAlex Crichton         assert_eq!(size.call(&mut store, ())?, 65536);
7763a3bbbfSAlex Crichton         assert_eq!(grow.call(&mut store, ())?, -1);
7863a3bbbfSAlex Crichton         assert_eq!(size.call(&mut store, ())?, 65536);
797a1b7cdfSAlex Crichton         Ok(())
807a1b7cdfSAlex Crichton     }
817a1b7cdfSAlex Crichton }
82