1 use anyhow::Result; 2 use wasmtime::*; 3 4 #[test] 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.clone().into(), func.into()]).is_err()); 12 Ok(()) 13 } 14 15 #[test] 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 fn initializes_linear_memory_paged() -> Result<()> { 37 let wat = r#" 38 (module 39 (memory (export "memory") 2) 40 (data (i32.const 0) "Hello World!") 41 )"#; 42 43 let mut config = Config::new(); 44 config.paged_memory_initialization(true); 45 46 let module = Module::new(&Engine::new(&config)?, wat)?; 47 48 let mut store = Store::new(module.engine(), ()); 49 let instance = Instance::new(&mut store, &module, &[])?; 50 let memory = instance.get_memory(&mut store, "memory").unwrap(); 51 52 let mut bytes = [0; 12]; 53 memory.read(&store, 0, &mut bytes)?; 54 assert_eq!(bytes, "Hello World!".as_bytes()); 55 Ok(()) 56 } 57 58 #[test] 59 fn linear_memory_limits() -> Result<()> { 60 // this test will allocate 4GB of virtual memory space, and may not work in 61 // situations like CI QEMU emulation where it triggers SIGKILL. 62 if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() { 63 return Ok(()); 64 } 65 test(&Engine::default())?; 66 test(&Engine::new(Config::new().allocation_strategy( 67 InstanceAllocationStrategy::Pooling { 68 strategy: PoolingAllocationStrategy::NextAvailable, 69 module_limits: ModuleLimits { 70 memory_pages: 65536, 71 ..ModuleLimits::default() 72 }, 73 instance_limits: InstanceLimits::default(), 74 }, 75 ))?)?; 76 return Ok(()); 77 78 fn test(engine: &Engine) -> Result<()> { 79 let wat = r#" 80 (module 81 (memory 65534) 82 83 (func (export "grow") (result i32) 84 i32.const 1 85 memory.grow) 86 (func (export "size") (result i32) 87 memory.size) 88 ) 89 "#; 90 let module = Module::new(engine, wat)?; 91 92 let mut store = Store::new(engine, ()); 93 let instance = Instance::new(&mut store, &module, &[])?; 94 let size = instance.get_typed_func::<(), i32, _>(&mut store, "size")?; 95 let grow = instance.get_typed_func::<(), i32, _>(&mut store, "grow")?; 96 97 assert_eq!(size.call(&mut store, ())?, 65534); 98 assert_eq!(grow.call(&mut store, ())?, 65534); 99 assert_eq!(size.call(&mut store, ())?, 65535); 100 assert_eq!(grow.call(&mut store, ())?, 65535); 101 assert_eq!(size.call(&mut store, ())?, 65536); 102 assert_eq!(grow.call(&mut store, ())?, -1); 103 assert_eq!(size.call(&mut store, ())?, 65536); 104 Ok(()) 105 } 106 } 107