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.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 #[cfg(target_pointer_width = "64")] 38 fn linear_memory_limits() -> Result<()> { 39 // this test will allocate 4GB of virtual memory space, and may not work in 40 // situations like CI QEMU emulation where it triggers SIGKILL. 41 if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() { 42 return Ok(()); 43 } 44 test(&Engine::default())?; 45 let mut pool = crate::small_pool_config(); 46 pool.max_memory_size(1 << 32); 47 test(&Engine::new(Config::new().allocation_strategy( 48 InstanceAllocationStrategy::Pooling(pool), 49 ))?)?; 50 return Ok(()); 51 52 fn test(engine: &Engine) -> Result<()> { 53 let wat = r#" 54 (module 55 (memory 65534) 56 57 (func (export "grow") (result i32) 58 i32.const 1 59 memory.grow) 60 (func (export "size") (result i32) 61 memory.size) 62 ) 63 "#; 64 let module = Module::new(engine, wat)?; 65 66 let mut store = Store::new(engine, ()); 67 let instance = Instance::new(&mut store, &module, &[])?; 68 let size = instance.get_typed_func::<(), i32>(&mut store, "size")?; 69 let grow = instance.get_typed_func::<(), i32>(&mut store, "grow")?; 70 71 assert_eq!(size.call(&mut store, ())?, 65534); 72 assert_eq!(grow.call(&mut store, ())?, 65534); 73 assert_eq!(size.call(&mut store, ())?, 65535); 74 assert_eq!(grow.call(&mut store, ())?, 65535); 75 assert_eq!(size.call(&mut store, ())?, 65536); 76 assert_eq!(grow.call(&mut store, ())?, -1); 77 assert_eq!(size.call(&mut store, ())?, 65536); 78 Ok(()) 79 } 80 } 81