Skip to content

Commit

Permalink
Removed uint8view() from the memory API and redundant FunctionEnv in …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
john-sharratt committed Aug 2, 2022
1 parent 6074045 commit 8fcf9fd
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 44 deletions.
1 change: 0 additions & 1 deletion examples/engine_headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// We create a headless Universal engine.
let engine = EngineBuilder::headless();
let mut store = Store::new(engine);
let _env = FunctionEnv::new(&mut store, ());

println!("Deserializing module...");
// Here we go.
Expand Down
1 change: 0 additions & 1 deletion examples/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new(Cranelift::default());
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
1 change: 0 additions & 1 deletion examples/exports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new(Cranelift::default());
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
1 change: 0 additions & 1 deletion examples/exports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new(Cranelift::default());
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
1 change: 0 additions & 1 deletion examples/exports_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new(Cranelift::default());
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
1 change: 0 additions & 1 deletion examples/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ fn main() -> anyhow::Result<()> {

// Now, let's define the store, and compile the module.
let mut store = Store::new(engine);
let _env = FunctionEnv::new(&mut store, ());
let module = Module::new(&store, wasm_bytes)?;

// Finally, let's instantiate the module, and execute something
Expand Down
1 change: 0 additions & 1 deletion examples/imports_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new(Cranelift::default());
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
1 change: 0 additions & 1 deletion examples/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new(Cranelift::default());
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
1 change: 0 additions & 1 deletion examples/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fn main() -> anyhow::Result<()> {
// the default provided by Wasmer.
// You can use `Store::default()` for that.
let mut store = Store::new(Cranelift::default());
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
1 change: 0 additions & 1 deletion examples/metering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ fn main() -> anyhow::Result<()> {
// We use our previously create compiler configuration
// with the Universal engine.
let mut store = Store::new(EngineBuilder::new(compiler_config));
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
// Let's compile the Wasm module.
Expand Down
1 change: 0 additions & 1 deletion examples/tunables_limit_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// Create a store, that holds the engine and our custom tunables
let mut store = Store::new_with_tunables(compiler, tunables);
let _env = FunctionEnv::new(&mut store, ());

println!("Compiling module...");
let module = Module::new(&store, wasm_bytes)?;
Expand Down
2 changes: 1 addition & 1 deletion lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ indexmap = { version = "1.6", features = ["serde-1"] }
cfg-if = "1.0"
thiserror = "1.0"
more-asserts = "0.2"
tracing = "0.1"
# - Optional shared dependencies.
wat = { version = "1.0", optional = true }
tracing = { version = "0.1", optional = true }

# Dependencies and Development Dependencies for `sys`.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
5 changes: 4 additions & 1 deletion lib/api/src/js/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::slice;
use thiserror::Error;
#[cfg(feature = "tracing")]
use tracing::warn;

use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -177,7 +178,6 @@ impl Memory {
IntoPages: Into<Pages>,
{
let pages = delta.into();
warn!("memory grow {}", pages.0);
let js_memory = &self.handle.get_mut(store.objects_mut()).memory;
let our_js_memory: &JSMemory = JsCast::unchecked_from_js_ref(js_memory);
let new_pages = our_js_memory.grow(pages.0).map_err(|err| {
Expand Down Expand Up @@ -239,6 +239,7 @@ impl<'a> MemoryBuffer<'a> {
.ok_or(MemoryAccessError::Overflow)?;
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());
return Err(MemoryAccessError::HeapOutOfBounds);
}
Expand All @@ -257,6 +258,7 @@ impl<'a> MemoryBuffer<'a> {
.ok_or(MemoryAccessError::Overflow)?;
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());
return Err(MemoryAccessError::HeapOutOfBounds);
}
Expand All @@ -273,6 +275,7 @@ impl<'a> MemoryBuffer<'a> {
.ok_or(MemoryAccessError::Overflow)?;
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());
return Err(MemoryAccessError::HeapOutOfBounds);
}
Expand Down
48 changes: 42 additions & 6 deletions lib/api/src/js/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::convert::TryInto;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::slice;
#[cfg(feature = "tracing")]
use tracing::warn;

use wasmer_types::{Bytes, Pages};
Expand Down Expand Up @@ -79,12 +80,6 @@ impl<'a> MemoryView<'a>
Bytes(self.size as usize).try_into().unwrap()
}

/// Used by tests
#[doc(hidden)]
pub fn uint8view(&self) -> js_sys::Uint8Array {
self.view.clone()
}

pub(crate) fn buffer(&self) -> MemoryBuffer<'a> {
MemoryBuffer {
base: &self.view as *const _ as *mut _,
Expand Down Expand Up @@ -112,13 +107,32 @@ impl<'a> MemoryView<'a>
.map_err(|_| MemoryAccessError::Overflow)?;
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());
Err(MemoryAccessError::HeapOutOfBounds)?;
}
view.subarray(offset, end).copy_to(data);
Ok(())
}

/// Safely reads a single byte from memory at the given offset
///
/// 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> {
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());
Err(MemoryAccessError::HeapOutOfBounds)?;
}
Ok(view.get_index(offset))
}

/// Safely reads bytes from the memory at the given offset.
///
/// This method is similar to `read` but allows reading into an
Expand All @@ -142,6 +156,7 @@ impl<'a> MemoryView<'a>
.map_err(|_| MemoryAccessError::Overflow)?;
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());
Err(MemoryAccessError::HeapOutOfBounds)?;
}
Expand Down Expand Up @@ -177,10 +192,31 @@ impl<'a> MemoryView<'a>
let view = &self.view;
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());
Err(MemoryAccessError::HeapOutOfBounds)?;
}
view.subarray(offset, end).copy_from(data);
Ok(())
}

/// Safely reads a single byte from memory at the given offset
///
/// 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> {
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());
Err(MemoryAccessError::HeapOutOfBounds)?;
}
view.set_index(offset, val);
Ok(())
}
}
4 changes: 4 additions & 0 deletions lib/api/src/sys/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::marker::PhantomData;
use std::mem;
use std::mem::MaybeUninit;
use std::slice;
#[cfg(feature = "tracing")]
use tracing::warn;
use wasmer_types::Pages;
use wasmer_vm::{InternalStoreHandle, MemoryError, StoreHandle, StoreObjects, VMExtern, VMMemory};
Expand Down Expand Up @@ -177,6 +178,7 @@ impl<'a> MemoryBuffer<'a> {
.checked_add(buf.len() as u64)
.ok_or(MemoryAccessError::Overflow)?;
if end > self.len.try_into().unwrap() {
#[cfg(feature = "tracing")]
warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), end, self.len);
return Err(MemoryAccessError::HeapOutOfBounds);
}
Expand All @@ -195,6 +197,7 @@ impl<'a> MemoryBuffer<'a> {
.checked_add(buf.len() as u64)
.ok_or(MemoryAccessError::Overflow)?;
if end > self.len.try_into().unwrap() {
#[cfg(feature = "tracing")]
warn!("attempted to read ({} bytes) beyond the bounds of the memory view ({} > {})", buf.len(), end, self.len);
return Err(MemoryAccessError::HeapOutOfBounds);
}
Expand All @@ -211,6 +214,7 @@ impl<'a> MemoryBuffer<'a> {
.checked_add(data.len() as u64)
.ok_or(MemoryAccessError::Overflow)?;
if end > self.len.try_into().unwrap() {
#[cfg(feature = "tracing")]
warn!("attempted to write ({} bytes) beyond the bounds of the memory view ({} > {})", data.len(), end, self.len);
return Err(MemoryAccessError::HeapOutOfBounds);
}
Expand Down
27 changes: 27 additions & 0 deletions lib/api/src/sys/externals/memory_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,19 @@ impl<'a> MemoryView<'a>
self.buffer.read(offset, buf)
}

/// Safely reads a single byte from memory at the given offset
///
/// 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> {
let mut buf = [0u8; 1];
self.read(offset, &mut buf)?;
Ok(buf[0])
}

/// Safely reads bytes from the memory at the given offset.
///
/// This method is similar to `read` but allows reading into an
Expand Down Expand Up @@ -147,4 +160,18 @@ impl<'a> MemoryView<'a>
) -> Result<(), MemoryAccessError> {
self.buffer.write(offset, data)
}

/// Safely reads a single byte from memory at the given offset
///
/// 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> {
let buf = [ val ];
self.write(offset, &buf)?;
Ok(())
}
}
6 changes: 0 additions & 6 deletions lib/api/tests/js_externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod js {
#[wasm_bindgen_test]
fn global_new() {
let mut store = Store::default();
let _env = FunctionEnv::new(&mut store, ());
let global = Global::new(&mut store, Value::I32(10));
assert_eq!(
global.ty(&store),
Expand All @@ -29,7 +28,6 @@ mod js {
#[wasm_bindgen_test]
fn global_get() {
let mut store = Store::default();
let _env = FunctionEnv::new(&mut store, ());
let global_i32 = Global::new(&mut store, Value::I32(10));
assert_eq!(global_i32.get(&store), Value::I32(10));
// 64-bit values are not yet fully supported in some versions of Node
Expand All @@ -46,7 +44,6 @@ mod js {
#[wasm_bindgen_test]
fn global_set() {
let mut store = Store::default();
let _env = FunctionEnv::new(&mut store, ());
let global_i32 = Global::new(&mut store, Value::I32(10));
// Set on a constant should error
assert!(global_i32.set(&mut store, Value::I32(20)).is_err());
Expand Down Expand Up @@ -144,7 +141,6 @@ mod js {
#[wasm_bindgen_test]
fn memory_new() {
let mut store = Store::default();
let _env = FunctionEnv::new(&mut store, ());
let memory_type = MemoryType {
shared: false,
minimum: Pages(0),
Expand All @@ -158,7 +154,6 @@ mod js {
#[wasm_bindgen_test]
fn memory_grow() {
let mut store = Store::default();
let _env = FunctionEnv::new(&mut store, ());

let desc = MemoryType::new(Pages(10), Some(Pages(16)), false);
let memory = Memory::new(&mut store, desc).unwrap();
Expand Down Expand Up @@ -444,7 +439,6 @@ mod js {
#[wasm_bindgen_test]
fn function_outlives_instance() {
let mut store = Store::default();
let _env = FunctionEnv::new(&mut store, ());
let wat = r#"(module
(type $sum_t (func (param i32 i32) (result i32)))
(func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
Expand Down
Loading

0 comments on commit 8fcf9fd

Please sign in to comment.