Skip to content

Commit

Permalink
format using rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mystor committed Jan 3, 2020
1 parent 2cb04b5 commit b56b5e6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 20 deletions.
55 changes: 43 additions & 12 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ use alloc::borrow::ToOwned;
use core::convert::TryFrom;
use core::fmt;

mod wasi;
mod convert;
mod wasi;

use self::convert::WasmPrimitive;

macro_rules! option_helper {
(Some $rt:expr) => { Some($rt) };
(Some) => { None };
(Some $rt:expr) => {
Some($rt)
};
(Some) => {
None
};
}

#[derive(Debug)]
Expand All @@ -33,10 +37,17 @@ impl Host {
fn new(instance: &wasmi::ModuleRef) -> Result<Self, wasmi::Error> {
let memory = match instance.export_by_name("memory") {
Some(wasmi::ExternVal::Memory(memory)) => memory,
_ => return Err(wasmi::Error::Instantiation("required memory export".to_owned())),
_ => {
return Err(wasmi::Error::Instantiation(
"required memory export".to_owned(),
))
}
};

Ok(Host { module: instance.clone(), memory })
Ok(Host {
module: instance.clone(),
memory,
})
}
}

Expand Down Expand Up @@ -162,8 +173,15 @@ impl wasmi::ImportResolver for HostResolver {
field_name: &str,
descriptor: &wasmi::GlobalDescriptor,
) -> Result<wasmi::GlobalRef, wasmi::Error> {
tracing::error!(module_name, field_name, ?descriptor, "unresolved global import");
Err(wasmi::Error::Instantiation("unresolved global import".to_owned()))
tracing::error!(
module_name,
field_name,
?descriptor,
"unresolved global import"
);
Err(wasmi::Error::Instantiation(
"unresolved global import".to_owned(),
))
}

fn resolve_memory(
Expand All @@ -172,8 +190,15 @@ impl wasmi::ImportResolver for HostResolver {
field_name: &str,
descriptor: &wasmi::MemoryDescriptor,
) -> Result<wasmi::MemoryRef, wasmi::Error> {
tracing::error!(module_name, field_name, ?descriptor, "unresolved memory import");
Err(wasmi::Error::Instantiation("unresolved memory import".to_owned()))
tracing::error!(
module_name,
field_name,
?descriptor,
"unresolved memory import"
);
Err(wasmi::Error::Instantiation(
"unresolved memory import".to_owned(),
))
}

fn resolve_table(
Expand All @@ -182,8 +207,15 @@ impl wasmi::ImportResolver for HostResolver {
field_name: &str,
descriptor: &wasmi::TableDescriptor,
) -> Result<wasmi::TableRef, wasmi::Error> {
tracing::error!(module_name, field_name, ?descriptor, "unresolved table import");
Err(wasmi::Error::Instantiation("unresolved table import".to_owned()))
tracing::error!(
module_name,
field_name,
?descriptor,
"unresolved table import"
);
Err(wasmi::Error::Instantiation(
"unresolved table import".to_owned(),
))
}
}

Expand All @@ -199,4 +231,3 @@ pub fn run_wasm(binary: &[u8]) -> Result<(), wasmi::Error> {
instance.invoke_export("_start", &[], &mut host)?;
Ok(())
}

31 changes: 23 additions & 8 deletions src/wasm/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ use super::Host;
// FIXME: These should both be `u16`, and probably generated from the wasi
// snapshot_0 `witx` definitions.
const __WASI_ESUCCESS: u16 = 0;
const __WASI_EIO: u16 = 29; // Not sure I counted right. Might be some other error.
const __WASI_EIO: u16 = 29; // Not sure I counted right. Might be some other error.

const __WASI_STDOUT: u32 = 1;

fn get_element_ptr(base: u32, offset: u32, scale: u32) -> Result<u32, wasmi::Trap> {
let byte_offset = offset.checked_mul(scale)
let byte_offset = offset
.checked_mul(scale)
.ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?;
Ok(base.checked_add(byte_offset).ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?)
Ok(base
.checked_add(byte_offset)
.ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?)
}

/// Loads the value stored at `base + (offset * scale)`.
Expand All @@ -21,7 +24,8 @@ fn mem_read<T: wasmi::LittleEndianConvert>(
scale: u32,
) -> Result<T, wasmi::Trap> {
let addr = get_element_ptr(base, offset, scale)?;
let slice = mem.get(addr as usize..)
let slice = mem
.get(addr as usize..)
.ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?;
Ok(T::from_little_endian(slice).map_err(|_| wasmi::TrapKind::MemoryAccessOutOfBounds)?)
}
Expand All @@ -42,25 +46,36 @@ fn mem_write<T: wasmi::LittleEndianConvert>(
//
// Assume that the size in wasm matches our host size for the given type.
let addr_after = get_element_ptr(addr, core::mem::size_of::<T>() as u32, 1)?;
let slice = mem.get_mut(addr as usize..addr_after as usize)
let slice = mem
.get_mut(addr as usize..addr_after as usize)
.ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?;
Ok(T::into_little_endian(value, slice))
}

/// Reference to a subslice of memory.
fn mem_slice(mem: &[u8], addr: u32, len: u32) -> Result<&[u8], wasmi::Trap> {
let end = get_element_ptr(addr, len, 1)?;
Ok(mem.get(addr as usize..end as usize).ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?)
Ok(mem
.get(addr as usize..end as usize)
.ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?)
}

#[allow(dead_code)] // unused
fn mem_slice_mut(mem: &mut [u8], addr: u32, len: u32) -> Result<&mut [u8], wasmi::Trap> {
let end = get_element_ptr(addr, len, 1)?;
Ok(mem.get_mut(addr as usize..end as usize).ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?)
Ok(mem
.get_mut(addr as usize..end as usize)
.ok_or(wasmi::TrapKind::MemoryAccessOutOfBounds)?)
}

#[tracing::instrument(skip(host))]
pub fn fd_write(host: &mut Host, fd: u32, iovs: u32, iovs_len: u32, nwritten: u32) -> Result<u16, wasmi::Trap> {
pub fn fd_write(
host: &mut Host,
fd: u32,
iovs: u32,
iovs_len: u32,
nwritten: u32,
) -> Result<u16, wasmi::Trap> {
if fd != __WASI_STDOUT {
return Ok(__WASI_EIO);
}
Expand Down

0 comments on commit b56b5e6

Please sign in to comment.