Make data() unsafe

This commit is contained in:
Yury Delendik
2019-09-18 09:27:53 -05:00
committed by Dan Gohman
parent 6a41417b52
commit 9c747db429
2 changed files with 13 additions and 11 deletions

View File

@@ -103,9 +103,9 @@ fn main() -> Result<(), Error> {
println!("Checking memory..."); println!("Checking memory...");
check!(memory.borrow().size(), 2u32); check!(memory.borrow().size(), 2u32);
check!(memory.borrow().data_size(), 0x20000usize); check!(memory.borrow().data_size(), 0x20000usize);
check!(memory.borrow().data()[0], 0); check!(unsafe { memory.borrow().data()[0] }, 0);
check!(memory.borrow().data()[0x1000], 1); check!(unsafe { memory.borrow().data()[0x1000] }, 1);
check!(memory.borrow().data()[0x1003], 4); check!(unsafe { memory.borrow().data()[0x1003] }, 4);
check!(call!(size_func,), 2); check!(call!(size_func,), 2);
check!(call!(load_func, 0), 0); check!(call!(load_func, 0), 0);
@@ -116,13 +116,15 @@ fn main() -> Result<(), Error> {
// Mutate memory. // Mutate memory.
println!("Mutating memory..."); println!("Mutating memory...");
unsafe {
memory.borrow_mut().data()[0x1003] = 5; memory.borrow_mut().data()[0x1003] = 5;
}
check_ok!(store_func, 0x1002, 6); check_ok!(store_func, 0x1002, 6);
check_trap!(store_func, 0x20000, 0); check_trap!(store_func, 0x20000, 0);
check!(memory.borrow().data()[0x1002], 6); check!(unsafe { memory.borrow().data()[0x1002] }, 6);
check!(memory.borrow().data()[0x1003], 5); check!(unsafe { memory.borrow().data()[0x1003] }, 5);
check!(call!(load_func, 0x1002), 6); check!(call!(load_func, 0x1002), 6);
check!(call!(load_func, 0x1003), 5); check!(call!(load_func, 0x1003), 5);

View File

@@ -434,12 +434,12 @@ impl Memory {
} }
} }
pub fn data(&self) -> &mut [u8] { // Marked unsafe due to posibility that wasmtime can resize internal memory
unsafe { // from other threads.
pub unsafe fn data(&self) -> &mut [u8] {
let definition = &*self.wasmtime_memory_definition(); let definition = &*self.wasmtime_memory_definition();
slice::from_raw_parts_mut(definition.base, definition.current_length) slice::from_raw_parts_mut(definition.base, definition.current_length)
} }
}
pub fn data_ptr(&self) -> *mut u8 { pub fn data_ptr(&self) -> *mut u8 {
unsafe { (*self.wasmtime_memory_definition()).base } unsafe { (*self.wasmtime_memory_definition()).base }