You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.4 KiB

use anyhow::Result;
use wasmtime::*;
#[test]
fn wrong_import_numbers() -> Result<()> {
let mut store = Store::<()>::default();
let module = Module::new(store.engine(), r#"(module (import "" "" (func)))"#)?;
assert!(Instance::new(&mut store, &module, &[]).is_err());
let func = Func::wrap(&mut store, || {});
assert!(Instance::new(&mut store, &module, &[func.clone().into(), func.into()]).is_err());
Ok(())
}
#[test]
fn initializes_linear_memory() -> Result<()> {
// Test for https://github.com/bytecodealliance/wasmtime/issues/2784
let wat = r#"
(module
(memory (export "memory") 2)
(data (i32.const 0) "Hello World!")
)"#;
let module = Module::new(&Engine::default(), wat)?;
let mut store = Store::new(module.engine(), ());
let instance = Instance::new(&mut store, &module, &[])?;
let memory = instance.get_memory(&mut store, "memory").unwrap();
let mut bytes = [0; 12];
memory.read(&store, 0, &mut bytes)?;
assert_eq!(bytes, "Hello World!".as_bytes());
Ok(())
}
#[test]
fn initializes_linear_memory_paged() -> Result<()> {
let wat = r#"
(module
(memory (export "memory") 2)
(data (i32.const 0) "Hello World!")
)"#;
let mut config = Config::new();
config.paged_memory_initialization(true);
let module = Module::new(&Engine::new(&config)?, wat)?;
let mut store = Store::new(module.engine(), ());
let instance = Instance::new(&mut store, &module, &[])?;
let memory = instance.get_memory(&mut store, "memory").unwrap();
let mut bytes = [0; 12];
memory.read(&store, 0, &mut bytes)?;
assert_eq!(bytes, "Hello World!".as_bytes());
Ok(())
}
#[test]
fn linear_memory_limits() -> Result<()> {
// this test will allocate 4GB of virtual memory space, and may not work in
// situations like CI QEMU emulation where it triggers SIGKILL.
if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
return Ok(());
}
test(&Engine::default())?;
test(&Engine::new(Config::new().allocation_strategy(
InstanceAllocationStrategy::Pooling {
strategy: PoolingAllocationStrategy::NextAvailable,
module_limits: ModuleLimits {
memory_pages: 65536,
..ModuleLimits::default()
},
instance_limits: InstanceLimits::default(),
},
))?)?;
return Ok(());
fn test(engine: &Engine) -> Result<()> {
let wat = r#"
(module
(memory 65534)
Change VMMemoryDefinition::current_length to `usize` (#3134) * Change VMMemoryDefinition::current_length to `usize` This commit changes the definition of `VMMemoryDefinition::current_length` to `usize` from its previous definition of `u32`. This is a pretty impactful change because it also changes the cranelift semantics of &#34;dynamic&#34; heaps where the bound global value specifier must now match the pointer type for the platform rather than the index type for the heap. The motivation for this change is that the `current_length` field (or bound for the heap) is intended to reflect the current size of the heap. This is bound by `usize` on the host platform rather than `u32` or` u64`. The previous choice of `u32` couldn&#39;t represent a 4GB memory because we couldn&#39;t put a number representing 4GB into the `current_length` field. By using `usize`, which reflects the host&#39;s memory allocation, this should better reflect the size of the heap and allows Wasmtime to support a full 4GB heap for a wasm program (instead of 4GB minus one page). This commit also updates the legalization of the `heap_addr` clif instruction to appropriately cast the address to the platform&#39;s pointer type, handling bounds checks along the way. The practical impact for today&#39;s targets is that a `uextend` is happening sooner than it happened before, but otherwise there is no intended impact of this change. In the future when 64-bit memories are supported there will likely need to be fancier logic which handles offsets a bit differently (especially in the case of a 64-bit memory on a 32-bit host). The clif `filetest` changes should show the differences in codegen, and the Wasmtime changes are largely removing casts here and there. Closes #3022 * Add tests for memory.size at maximum memory size * Add a dfg helper method
3 years ago
(func (export "grow") (result i32)
i32.const 1
memory.grow)
Change VMMemoryDefinition::current_length to `usize` (#3134) * Change VMMemoryDefinition::current_length to `usize` This commit changes the definition of `VMMemoryDefinition::current_length` to `usize` from its previous definition of `u32`. This is a pretty impactful change because it also changes the cranelift semantics of &#34;dynamic&#34; heaps where the bound global value specifier must now match the pointer type for the platform rather than the index type for the heap. The motivation for this change is that the `current_length` field (or bound for the heap) is intended to reflect the current size of the heap. This is bound by `usize` on the host platform rather than `u32` or` u64`. The previous choice of `u32` couldn&#39;t represent a 4GB memory because we couldn&#39;t put a number representing 4GB into the `current_length` field. By using `usize`, which reflects the host&#39;s memory allocation, this should better reflect the size of the heap and allows Wasmtime to support a full 4GB heap for a wasm program (instead of 4GB minus one page). This commit also updates the legalization of the `heap_addr` clif instruction to appropriately cast the address to the platform&#39;s pointer type, handling bounds checks along the way. The practical impact for today&#39;s targets is that a `uextend` is happening sooner than it happened before, but otherwise there is no intended impact of this change. In the future when 64-bit memories are supported there will likely need to be fancier logic which handles offsets a bit differently (especially in the case of a 64-bit memory on a 32-bit host). The clif `filetest` changes should show the differences in codegen, and the Wasmtime changes are largely removing casts here and there. Closes #3022 * Add tests for memory.size at maximum memory size * Add a dfg helper method
3 years ago
(func (export "size") (result i32)
memory.size)
)
"#;
let module = Module::new(engine, wat)?;
let mut store = Store::new(engine, ());
let instance = Instance::new(&mut store, &module, &[])?;
Change VMMemoryDefinition::current_length to `usize` (#3134) * Change VMMemoryDefinition::current_length to `usize` This commit changes the definition of `VMMemoryDefinition::current_length` to `usize` from its previous definition of `u32`. This is a pretty impactful change because it also changes the cranelift semantics of &#34;dynamic&#34; heaps where the bound global value specifier must now match the pointer type for the platform rather than the index type for the heap. The motivation for this change is that the `current_length` field (or bound for the heap) is intended to reflect the current size of the heap. This is bound by `usize` on the host platform rather than `u32` or` u64`. The previous choice of `u32` couldn&#39;t represent a 4GB memory because we couldn&#39;t put a number representing 4GB into the `current_length` field. By using `usize`, which reflects the host&#39;s memory allocation, this should better reflect the size of the heap and allows Wasmtime to support a full 4GB heap for a wasm program (instead of 4GB minus one page). This commit also updates the legalization of the `heap_addr` clif instruction to appropriately cast the address to the platform&#39;s pointer type, handling bounds checks along the way. The practical impact for today&#39;s targets is that a `uextend` is happening sooner than it happened before, but otherwise there is no intended impact of this change. In the future when 64-bit memories are supported there will likely need to be fancier logic which handles offsets a bit differently (especially in the case of a 64-bit memory on a 32-bit host). The clif `filetest` changes should show the differences in codegen, and the Wasmtime changes are largely removing casts here and there. Closes #3022 * Add tests for memory.size at maximum memory size * Add a dfg helper method
3 years ago
let size = instance.get_typed_func::<(), i32, _>(&mut store, "size")?;
let grow = instance.get_typed_func::<(), i32, _>(&mut store, "grow")?;
Change VMMemoryDefinition::current_length to `usize` (#3134) * Change VMMemoryDefinition::current_length to `usize` This commit changes the definition of `VMMemoryDefinition::current_length` to `usize` from its previous definition of `u32`. This is a pretty impactful change because it also changes the cranelift semantics of &#34;dynamic&#34; heaps where the bound global value specifier must now match the pointer type for the platform rather than the index type for the heap. The motivation for this change is that the `current_length` field (or bound for the heap) is intended to reflect the current size of the heap. This is bound by `usize` on the host platform rather than `u32` or` u64`. The previous choice of `u32` couldn&#39;t represent a 4GB memory because we couldn&#39;t put a number representing 4GB into the `current_length` field. By using `usize`, which reflects the host&#39;s memory allocation, this should better reflect the size of the heap and allows Wasmtime to support a full 4GB heap for a wasm program (instead of 4GB minus one page). This commit also updates the legalization of the `heap_addr` clif instruction to appropriately cast the address to the platform&#39;s pointer type, handling bounds checks along the way. The practical impact for today&#39;s targets is that a `uextend` is happening sooner than it happened before, but otherwise there is no intended impact of this change. In the future when 64-bit memories are supported there will likely need to be fancier logic which handles offsets a bit differently (especially in the case of a 64-bit memory on a 32-bit host). The clif `filetest` changes should show the differences in codegen, and the Wasmtime changes are largely removing casts here and there. Closes #3022 * Add tests for memory.size at maximum memory size * Add a dfg helper method
3 years ago
assert_eq!(size.call(&mut store, ())?, 65534);
assert_eq!(grow.call(&mut store, ())?, 65534);
assert_eq!(size.call(&mut store, ())?, 65535);
assert_eq!(grow.call(&mut store, ())?, 65535);
assert_eq!(size.call(&mut store, ())?, 65536);
assert_eq!(grow.call(&mut store, ())?, -1);
assert_eq!(size.call(&mut store, ())?, 65536);
Ok(())
}
}