This commit is contained in:
Wolfgang Silbermayr
2022-08-04 20:39:13 +02:00
parent 89bba319a9
commit 9c7b4343ea
27 changed files with 193 additions and 176 deletions

View File

@@ -134,7 +134,7 @@ impl Memory {
}
/// Creates a view into the memory that then allows for
/// read and write
/// read and write
pub fn view(&self, store: &impl AsStoreRef) -> MemoryView {
MemoryView::new(self, store)
}
@@ -240,7 +240,12 @@ impl<'a> MemoryBuffer<'a> {
let view = unsafe { &*(self.base) };
if end > view.length().into() {
#[cfg(feature = "tracing")]
warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), end, view.length());
warn!(
"attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})",
buf.len(),
end,
view.length()
);
return Err(MemoryAccessError::HeapOutOfBounds);
}
view.subarray(offset as _, end as _)
@@ -259,7 +264,12 @@ impl<'a> MemoryBuffer<'a> {
let view = unsafe { &*(self.base) };
if end > view.length().into() {
#[cfg(feature = "tracing")]
warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), end, view.length());
warn!(
"attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})",
buf.len(),
end,
view.length()
);
return Err(MemoryAccessError::HeapOutOfBounds);
}
let buf_ptr = buf.as_mut_ptr() as *mut u8;
@@ -276,7 +286,12 @@ impl<'a> MemoryBuffer<'a> {
let view = unsafe { &mut *(self.base) };
if end > view.length().into() {
#[cfg(feature = "tracing")]
warn!("attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", data.len(), end, view.length());
warn!(
"attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})",
data.len(),
end,
view.length()
);
return Err(MemoryAccessError::HeapOutOfBounds);
}
view.subarray(offset as _, end as _).copy_from(data);

View File

@@ -9,8 +9,8 @@ use tracing::warn;
use wasmer_types::{Bytes, Pages};
use super::Memory;
use super::memory::MemoryBuffer;
use super::Memory;
/// A WebAssembly `memory` view.
///
@@ -25,8 +25,7 @@ pub struct MemoryView<'a> {
marker: PhantomData<&'a Memory>,
}
impl<'a> MemoryView<'a>
{
impl<'a> MemoryView<'a> {
pub(crate) fn new(memory: &Memory, store: &impl AsStoreRef) -> Self {
let buffer = memory
.handle
@@ -34,17 +33,12 @@ impl<'a> MemoryView<'a>
.memory
.buffer();
let size = js_sys::Reflect::get(
&buffer,
&"byteLength".into(),
)
let size = js_sys::Reflect::get(&buffer, &"byteLength".into())
.unwrap()
.as_f64()
.unwrap() as u64;
let view = js_sys::Uint8Array::new(
&buffer,
);
let view = js_sys::Uint8Array::new(&buffer);
Self {
view,
@@ -94,11 +88,7 @@ impl<'a> MemoryView<'a>
///
/// This method is guaranteed to be safe (from the host side) in the face of
/// concurrent writes.
pub fn read(
&self,
offset: u64,
data: &mut [u8],
) -> Result<(), MemoryAccessError> {
pub fn read(&self, offset: u64, data: &mut [u8]) -> Result<(), MemoryAccessError> {
let view = &self.view;
let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?;
let len: u32 = data
@@ -108,7 +98,12 @@ impl<'a> MemoryView<'a>
let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?;
if end > view.length() {
#[cfg(feature = "tracing")]
warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", len, end, view.length());
warn!(
"attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})",
len,
end,
view.length()
);
Err(MemoryAccessError::HeapOutOfBounds)?;
}
view.subarray(offset, end).copy_to(data);
@@ -119,15 +114,16 @@ impl<'a> MemoryView<'a>
///
/// This method is guaranteed to be safe (from the host side) in the face of
/// concurrent writes.
pub fn read_u8(
&self,
offset: u64
) -> Result<u8, MemoryAccessError> {
pub fn read_u8(&self, offset: u64) -> Result<u8, MemoryAccessError> {
let view = &self.view;
let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?;
if offset >= view.length() {
#[cfg(feature = "tracing")]
warn!("attempted to read beyond the bounds of the memory view ({} >= {})", offset, view.length());
warn!(
"attempted to read beyond the bounds of the memory view ({} >= {})",
offset,
view.length()
);
Err(MemoryAccessError::HeapOutOfBounds)?;
}
Ok(view.get_index(offset))
@@ -157,7 +153,12 @@ impl<'a> MemoryView<'a>
let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?;
if end > view.length() {
#[cfg(feature = "tracing")]
warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", len, end, view.length());
warn!(
"attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})",
len,
end,
view.length()
);
Err(MemoryAccessError::HeapOutOfBounds)?;
}
@@ -179,11 +180,7 @@ impl<'a> MemoryView<'a>
///
/// This method is guaranteed to be safe (from the host side) in the face of
/// concurrent reads/writes.
pub fn write(
&self,
offset: u64,
data: &[u8],
) -> Result<(), MemoryAccessError> {
pub fn write(&self, offset: u64, data: &[u8]) -> Result<(), MemoryAccessError> {
let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?;
let len: u32 = data
.len()
@@ -193,7 +190,12 @@ impl<'a> MemoryView<'a>
let end = offset.checked_add(len).ok_or(MemoryAccessError::Overflow)?;
if end > view.length() {
#[cfg(feature = "tracing")]
warn!("attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", len, end, view.length());
warn!(
"attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})",
len,
end,
view.length()
);
Err(MemoryAccessError::HeapOutOfBounds)?;
}
view.subarray(offset, end).copy_from(data);
@@ -204,16 +206,16 @@ impl<'a> MemoryView<'a>
///
/// This method is guaranteed to be safe (from the host side) in the face of
/// concurrent writes.
pub fn write_u8(
&self,
offset: u64,
val: u8
) -> Result<(), MemoryAccessError> {
pub fn write_u8(&self, offset: u64, val: u8) -> Result<(), MemoryAccessError> {
let view = &self.view;
let offset: u32 = offset.try_into().map_err(|_| MemoryAccessError::Overflow)?;
if offset >= view.length() {
#[cfg(feature = "tracing")]
warn!("attempted to write beyond the bounds of the memory view ({} >= {})", offset, view.length());
warn!(
"attempted to write beyond the bounds of the memory view ({} >= {})",
offset,
view.length()
);
Err(MemoryAccessError::HeapOutOfBounds)?;
}
view.set_index(offset, val);

View File

@@ -1,6 +1,6 @@
use crate::js::externals::memory::MemoryBuffer;
use crate::js::RuntimeError;
use crate::js::{MemoryView, Memory32, Memory64, WasmPtr};
use crate::js::{Memory32, Memory64, MemoryView, WasmPtr};
use std::{
convert::TryInto,
fmt,
@@ -158,11 +158,7 @@ impl<'a, T: ValueType> WasmSlice<'a, T> {
///
/// Returns a `MemoryAccessError` if the slice length overflows.
#[inline]
pub fn new(
memory: &'a MemoryView,
offset: u64,
len: u64,
) -> Result<Self, MemoryAccessError> {
pub fn new(memory: &'a MemoryView, offset: u64, len: u64) -> Result<Self, MemoryAccessError> {
let total_len = len
.checked_mul(mem::size_of::<T>() as u64)
.ok_or(MemoryAccessError::Overflow)?;

View File

@@ -48,8 +48,8 @@ pub use crate::js::error::{DeserializeError, InstantiationError, SerializeError}
pub use crate::js::export::Export;
pub use crate::js::exports::{ExportError, Exportable, Exports, ExportsIterator};
pub use crate::js::externals::{
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryView, MemoryError, Table,
WasmTypeList,
Extern, FromToNativeWasmType, Function, Global, HostFunction, Memory, MemoryError, MemoryView,
Table, WasmTypeList,
};
pub use crate::js::function_env::{FunctionEnv, FunctionEnvMut};
pub use crate::js::imports::Imports;

View File

@@ -149,11 +149,7 @@ impl<T: ValueType, M: MemorySize> WasmPtr<T, M> {
/// Writes to the address pointed to by this `WasmPtr` in a memory.
#[inline]
pub fn write(
self,
view: &MemoryView,
val: T,
) -> Result<(), MemoryAccessError> {
pub fn write(self, view: &MemoryView, val: T) -> Result<(), MemoryAccessError> {
self.deref(view).write(val)
}