Skip to content

Commit

Permalink
Merge pull request #2801 from peterhuene/fix-initialize-instance-range
Browse files Browse the repository at this point in the history
Fix incorrect range in `ininitialize_instance`.
  • Loading branch information
peterhuene authored Apr 3, 2021
2 parents b7b47e3 + 37bb7af commit 4d036a4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crates/runtime/src/instance/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ fn initialize_instance(
for (page_index, page) in pages.iter().enumerate() {
if let Some(data) = page {
debug_assert_eq!(data.len(), WASM_PAGE_SIZE as usize);
slice[page_index * WASM_PAGE_SIZE as usize..].copy_from_slice(data);
let start = page_index * WASM_PAGE_SIZE as usize;
let end = start + WASM_PAGE_SIZE as usize;
slice[start..end].copy_from_slice(data);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/all/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,23 @@ fn wrong_import_numbers() -> Result<()> {
assert!(Instance::new(&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 store = Store::new(module.engine());
let instance = Instance::new(&store, &module, &[])?;
let memory = instance.get_memory("memory").unwrap();

let mut bytes = [0; 12];
memory.read(0, &mut bytes)?;
assert_eq!(bytes, "Hello World!".as_bytes());
Ok(())
}

0 comments on commit 4d036a4

Please sign in to comment.