fix memory access in WasmPtr

This commit is contained in:
Mark McCaskey
2019-04-18 17:48:14 -07:00
parent 7e58c4258c
commit 19e830d25a
4 changed files with 71 additions and 28 deletions

View File

@ -36,10 +36,11 @@ impl<T: Copy + ValueType> WasmPtr<T, Item> {
return Err(__WASI_EFAULT);
}
unsafe {
let cell_ptr = memory
.view::<T>()
.get_unchecked((self.offset() as usize) / mem::size_of::<T>())
as *const _;
let aligner = |ptr: usize, align: usize| ptr & !(align - 1);
let cell_ptr = aligner(
memory.view::<u8>().as_ptr().add(self.offset as usize) as usize,
mem::align_of::<T>(),
) as *const Cell<T>;
Ok(&*cell_ptr)
}
}