Compare commits

..

3 Commits

Author SHA1 Message Date
Sergey Pepyakin e8151ed272 Optimize pushing the stack 2019-07-16 18:13:46 +03:00
Sergey Pepyakin 358adb334a Cache bytebuf 2019-07-15 23:28:53 +03:00
Sergey Pepyakin 97661cf3fe CoW 2019-07-15 22:17:25 +03:00
22 changed files with 217 additions and 146 deletions

View File

@ -40,17 +40,7 @@ after_success: |
ghp-import -n target/doc && ghp-import -n target/doc &&
git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages git push -fq https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages
cache: cache: cargo
# Don't use `cache: cargo` since it adds the `target` directory and that can be huge.
# Saving and loading this directory dwarfes actual compilation and test times. But what is more
# important, is that travis timeouts the build since the job doesn't produce any output for more
# than 10 minutes.
#
# So we just cache ~/.cargo directory
directories:
- /home/travis/.cargo
before_cache: before_cache:
# Travis can't cache files that are not readable by "others" # Travis can't cache files that are not readable by "others"
- chmod -R a+r $HOME/.cargo - chmod -R a+r $HOME/.cargo
# According to the Travis CI docs for building Rust project this is done by,
- rm -rf /home/travis/.cargo/registry

View File

@ -1,6 +1,6 @@
[package] [package]
name = "wasmi" name = "wasmi"
version = "0.5.1" version = "0.5.0"
authors = ["Nikolay Volf <nikvolf@gmail.com>", "Svyatoslav Nikolsky <svyatonik@yandex.ru>", "Sergey Pepyakin <s.pepyakin@gmail.com>"] authors = ["Nikolay Volf <nikvolf@gmail.com>", "Svyatoslav Nikolsky <svyatonik@yandex.ru>", "Sergey Pepyakin <s.pepyakin@gmail.com>"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
readme = "README.md" readme = "README.md"
@ -11,17 +11,18 @@ keywords = ["wasm", "webassembly", "bytecode", "interpreter"]
exclude = [ "/res/*", "/tests/*", "/fuzz/*", "/benches/*" ] exclude = [ "/res/*", "/tests/*", "/fuzz/*", "/benches/*" ]
[dependencies] [dependencies]
wasmi-validation = { version = "0.2", path = "validation", default-features = false } wasmi-validation = { version = "0.1", path = "validation", default-features = false }
parity-wasm = { version = "0.40.1", default-features = false } parity-wasm = { version = "0.31", default-features = false }
memory_units = "0.3.0" memory_units = "0.3.0"
libm = { version = "0.1.2", optional = true } libm = { version = "0.1.2", optional = true }
num-rational = { version = "0.2.2", default-features = false } num-rational = "0.2.2"
num-traits = { version = "0.2.8", default-features = false } num-traits = "0.2.8"
libc = "0.2.58"
[dev-dependencies] [dev-dependencies]
assert_matches = "1.1" assert_matches = "1.1"
rand = "0.4.2" rand = "0.4.2"
wabt = "0.9" wabt = "0.6"
[features] [features]
default = ["std"] default = ["std"]
@ -30,7 +31,6 @@ std = [
"parity-wasm/std", "parity-wasm/std",
"wasmi-validation/std", "wasmi-validation/std",
"num-rational/std", "num-rational/std",
"num-rational/bigint-std",
"num-traits/std" "num-traits/std"
] ]
# Enable for no_std support # Enable for no_std support

View File

@ -26,8 +26,8 @@ This crate supports `no_std` environments.
Enable the `core` feature and disable default features: Enable the `core` feature and disable default features:
```toml ```toml
[dependencies] [dependencies]
wasmi = { parity-wasm = {
version = "*", version = "0.31",
default-features = false, default-features = false,
features = "core" features = "core"
} }

View File

@ -6,7 +6,7 @@ authors = ["Sergey Pepyakin <s.pepyakin@gmail.com>"]
[dependencies] [dependencies]
wasmi = { path = ".." } wasmi = { path = ".." }
assert_matches = "1.2" assert_matches = "1.2"
wabt = "0.9" wabt = "0.6"
[profile.bench] [profile.bench]
debug = true debug = true

View File

@ -33,7 +33,7 @@ pub extern "C" fn prepare_tiny_keccak() -> *const TinyKeccakTestData {
} }
#[no_mangle] #[no_mangle]
pub extern "C" fn bench_tiny_keccak(test_data: *mut TinyKeccakTestData) { pub extern "C" fn bench_tiny_keccak(test_data: *const TinyKeccakTestData) {
unsafe { unsafe {
let mut keccak = Keccak::new_keccak256(); let mut keccak = Keccak::new_keccak256();
keccak.update((*test_data).data); keccak.update((*test_data).data);

View File

@ -10,7 +10,7 @@ cargo-fuzz = true
[dependencies] [dependencies]
wasmi = { path = ".." } wasmi = { path = ".." }
wabt = "0.9" wabt = "0.6.0"
wasmparser = "0.14.1" wasmparser = "0.14.1"
tempdir = "0.3.6" tempdir = "0.3.6"

View File

@ -7,4 +7,4 @@ authors = ["Sergey Pepyakin <s.pepyakin@gmail.com>"]
honggfuzz = "=0.5.9" # Strict equal since hfuzz requires dep and cmd versions to match. honggfuzz = "=0.5.9" # Strict equal since hfuzz requires dep and cmd versions to match.
wasmi = { path = ".." } wasmi = { path = ".." }
tempdir = "0.3.6" tempdir = "0.3.6"
wabt = "0.9" wabt = "0.6.0"

View File

@ -1,5 +1,4 @@
use alloc::{ use alloc::{
borrow::Cow,
rc::{Rc, Weak}, rc::{Rc, Weak},
vec::Vec, vec::Vec,
}; };
@ -196,13 +195,12 @@ impl FuncInstance {
/// [`resume_execution`]: struct.FuncInvocation.html#method.resume_execution /// [`resume_execution`]: struct.FuncInvocation.html#method.resume_execution
pub fn invoke_resumable<'args>( pub fn invoke_resumable<'args>(
func: &FuncRef, func: &FuncRef,
args: impl Into<Cow<'args, [RuntimeValue]>>, args: &'args [RuntimeValue],
) -> Result<FuncInvocation<'args>, Trap> { ) -> Result<FuncInvocation<'args>, Trap> {
let args = args.into();
check_function_args(func.signature(), &args)?; check_function_args(func.signature(), &args)?;
match *func.as_internal() { match *func.as_internal() {
FuncInstanceInternal::Internal { .. } => { FuncInstanceInternal::Internal { .. } => {
let interpreter = Interpreter::new(func, &*args, None)?; let interpreter = Interpreter::new(func, args, None)?;
Ok(FuncInvocation { Ok(FuncInvocation {
kind: FuncInvocationKind::Internal(interpreter), kind: FuncInvocationKind::Internal(interpreter),
}) })
@ -259,7 +257,7 @@ pub struct FuncInvocation<'args> {
enum FuncInvocationKind<'args> { enum FuncInvocationKind<'args> {
Internal(Interpreter), Internal(Interpreter),
Host { Host {
args: Cow<'args, [RuntimeValue]>, args: &'args [RuntimeValue],
host_func_index: usize, host_func_index: usize,
finished: bool, finished: bool,
}, },
@ -306,7 +304,7 @@ impl<'args> FuncInvocation<'args> {
return Err(ResumableError::AlreadyStarted); return Err(ResumableError::AlreadyStarted);
} }
*finished = true; *finished = true;
Ok(externals.invoke_index(*host_func_index, args.as_ref().into())?) Ok(externals.invoke_index(*host_func_index, args.clone().into())?)
} }
} }
} }

View File

@ -404,7 +404,7 @@ pub use self::func::{FuncInstance, FuncInvocation, FuncRef, ResumableError};
pub use self::global::{GlobalInstance, GlobalRef}; pub use self::global::{GlobalInstance, GlobalRef};
pub use self::host::{Externals, HostError, NopExternals, RuntimeArgs}; pub use self::host::{Externals, HostError, NopExternals, RuntimeArgs};
pub use self::imports::{ImportResolver, ImportsBuilder, ModuleImportResolver}; pub use self::imports::{ImportResolver, ImportsBuilder, ModuleImportResolver};
pub use self::memory::{MemoryInstance, MemoryRef, LINEAR_MEMORY_PAGE_SIZE}; pub use self::memory::{MemoryBackend, ByteBuf, MemoryInstance, MemoryRef, LINEAR_MEMORY_PAGE_SIZE};
pub use self::module::{ExternVal, ModuleInstance, ModuleRef, NotStartedModuleRef}; pub use self::module::{ExternVal, ModuleInstance, ModuleRef, NotStartedModuleRef};
pub use self::runner::{StackRecycler, DEFAULT_CALL_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT}; pub use self::runner::{StackRecycler, DEFAULT_CALL_STACK_LIMIT, DEFAULT_VALUE_STACK_LIMIT};
pub use self::table::{TableInstance, TableRef}; pub use self::table::{TableInstance, TableRef};

View File

@ -7,6 +7,7 @@
use std::ptr::{self, NonNull}; use std::ptr::{self, NonNull};
use std::slice; use std::slice;
use super::{MemoryBackend, ByteBuf};
struct Mmap { struct Mmap {
/// The pointer that points to the start of the mapping. /// The pointer that points to the start of the mapping.
@ -111,11 +112,15 @@ impl Drop for Mmap {
} }
} }
pub struct ByteBuf { pub struct MmapByteBuf {
mmap: Option<Mmap>, mmap: Option<Mmap>,
} }
impl ByteBuf { impl MmapByteBuf {
pub fn empty() -> Self {
MmapByteBuf { mmap: None }
}
pub fn new(len: usize) -> Result<Self, &'static str> { pub fn new(len: usize) -> Result<Self, &'static str> {
let mmap = if len == 0 { let mmap = if len == 0 {
None None
@ -124,8 +129,14 @@ impl ByteBuf {
}; };
Ok(Self { mmap }) Ok(Self { mmap })
} }
}
pub fn realloc(&mut self, new_len: usize) -> Result<(), &'static str> { impl MemoryBackend for MmapByteBuf {
fn alloc(&mut self, initial: usize, _maximum: Option<usize>) -> Result<ByteBuf, &'static str> {
self.realloc(initial)
}
fn realloc(&mut self, new_len: usize) -> Result<ByteBuf, &'static str> {
let new_mmap = if new_len == 0 { let new_mmap = if new_len == 0 {
None None
} else { } else {
@ -139,27 +150,16 @@ impl ByteBuf {
Some(new_mmap) Some(new_mmap)
}; };
let bytebuf = ByteBuf {
ptr: new_mmap.as_ref().map(|m| m.ptr.as_ptr()).unwrap_or(NonNull::dangling().as_ptr()),
len: new_mmap.as_ref().map(|m| m.len).unwrap_or(0),
};
self.mmap = new_mmap; self.mmap = new_mmap;
Ok(()) Ok(bytebuf)
} }
pub fn len(&self) -> usize { fn erase(&mut self) -> Result<(), &'static str> {
self.mmap.as_ref().map(|m| m.len).unwrap_or(0) let len = self.mmap.as_ref().map(|m| m.len).unwrap_or(0);
}
pub fn as_slice(&self) -> &[u8] {
self.mmap.as_ref().map(|m| m.as_slice()).unwrap_or(&[])
}
pub fn as_slice_mut(&mut self) -> &mut [u8] {
self.mmap
.as_mut()
.map(|m| m.as_slice_mut())
.unwrap_or(&mut [])
}
pub fn erase(&mut self) -> Result<(), &'static str> {
let len = self.len();
if len > 0 { if len > 0 {
// The order is important. // The order is important.
// //
@ -176,14 +176,14 @@ impl ByteBuf {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::ByteBuf; use super::{MmapByteBuf, MemoryBackend};
const PAGE_SIZE: usize = 4096; const PAGE_SIZE: usize = 4096;
// This is not required since wasm memories can only grow but nice to have. // This is not required since wasm memories can only grow but nice to have.
#[test] #[test]
fn byte_buf_shrink() { fn byte_buf_shrink() {
let mut byte_buf = ByteBuf::new(PAGE_SIZE * 3).unwrap(); let mut byte_buf = MmapByteBuf::new(PAGE_SIZE * 3).unwrap();
byte_buf.realloc(PAGE_SIZE * 2).unwrap(); byte_buf.realloc(PAGE_SIZE * 2).unwrap();
} }
} }

View File

@ -3,6 +3,7 @@ use core::{
cell::{Cell, RefCell}, cell::{Cell, RefCell},
cmp, fmt, cmp, fmt,
ops::Range, ops::Range,
slice,
u32, u32,
}; };
use memory_units::{Bytes, Pages, RoundUpTo}; use memory_units::{Bytes, Pages, RoundUpTo};
@ -12,13 +13,13 @@ use Error;
#[cfg(all(unix, not(feature = "vec_memory")))] #[cfg(all(unix, not(feature = "vec_memory")))]
#[path="mmap_bytebuf.rs"] #[path="mmap_bytebuf.rs"]
mod bytebuf; mod mmap_bytebuf;
#[cfg(any(not(unix), feature = "vec_memory"))] #[cfg(all(unix, not(feature = "vec_memory")))]
#[path = "vec_bytebuf.rs"] use self::mmap_bytebuf::MmapByteBuf;
mod bytebuf;
use self::bytebuf::ByteBuf; // #[cfg(any(not(unix), feature = "vec_memory"))]
// mod bytebuf;
/// Size of a page of [linear memory][`MemoryInstance`] - 64KiB. /// Size of a page of [linear memory][`MemoryInstance`] - 64KiB.
/// ///
@ -43,6 +44,35 @@ impl ::core::ops::Deref for MemoryRef {
} }
} }
pub struct ByteBuf {
pub ptr: *mut u8,
pub len: usize,
}
impl ByteBuf {
pub fn as_slice(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self.ptr, self.len)
}
}
pub fn as_slice_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self.ptr, self.len)
}
}
pub fn len(&self) -> usize {
self.len
}
}
pub trait MemoryBackend {
fn alloc(&mut self, initial: usize, maximum: Option<usize>) -> Result<ByteBuf, &'static str>;
fn realloc(&mut self, new_len: usize) -> Result<ByteBuf, &'static str>;
fn erase(&mut self) -> Result<(), &'static str>;
}
/// Runtime representation of a linear memory (or `memory` for short). /// Runtime representation of a linear memory (or `memory` for short).
/// ///
/// A memory is a contiguous, mutable array of raw bytes. Wasm code can load and store values /// A memory is a contiguous, mutable array of raw bytes. Wasm code can load and store values
@ -60,7 +90,8 @@ pub struct MemoryInstance {
/// Memory limits. /// Memory limits.
limits: ResizableLimits, limits: ResizableLimits,
/// Linear memory buffer with lazy allocation. /// Linear memory buffer with lazy allocation.
buffer: RefCell<ByteBuf>, backend: RefCell<Box<dyn MemoryBackend>>,
bytebuf: RefCell<ByteBuf>,
initial: Pages, initial: Pages,
current_size: Cell<usize>, current_size: Cell<usize>,
maximum: Option<Pages>, maximum: Option<Pages>,
@ -70,7 +101,7 @@ impl fmt::Debug for MemoryInstance {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("MemoryInstance") f.debug_struct("MemoryInstance")
.field("limits", &self.limits) .field("limits", &self.limits)
.field("buffer.len", &self.buffer.borrow().len()) .field("buffer.len", &self.bytebuf.borrow().len())
.field("maximum", &self.maximum) .field("maximum", &self.maximum)
.field("initial", &self.initial) .field("initial", &self.initial)
.finish() .finish()
@ -142,17 +173,35 @@ impl MemoryInstance {
let limits = ResizableLimits::new(initial.0 as u32, maximum.map(|p| p.0 as u32)); let limits = ResizableLimits::new(initial.0 as u32, maximum.map(|p| p.0 as u32));
let initial_size: Bytes = initial.into(); let initial_size: Bytes = initial.into();
let maximum_size: Option<Bytes> = maximum.map(|m| m.into());
let mut backend = MmapByteBuf::new(initial_size.0).map_err(|err| Error::Memory(err.to_string()))?;
let bytebuf = backend.alloc(
initial_size.0,
maximum_size.map(|m| m.0),
).map_err(|err| Error::Memory(err.to_string()))?;
Ok(MemoryInstance { Ok(MemoryInstance {
limits: limits, limits: limits,
buffer: RefCell::new( backend: RefCell::new(Box::new(backend)),
ByteBuf::new(initial_size.0).map_err(|err| Error::Memory(err.to_string()))?, bytebuf: RefCell::new(bytebuf),
),
initial: initial, initial: initial,
current_size: Cell::new(initial_size.0), current_size: Cell::new(initial_size.0),
maximum: maximum, maximum: maximum,
}) })
} }
pub fn set_backend(&self, mut backend: Box<dyn MemoryBackend>) {
let initial_size: Bytes = self.initial.into();
let maximum_size: Option<Bytes> = self.maximum.map(|m| m.into());
let bytebuf = backend.alloc(
initial_size.0,
maximum_size.map(|m| m.0),
).unwrap();
*self.backend.borrow_mut() = backend;
*self.bytebuf.borrow_mut() = bytebuf;
}
/// Return linear memory limits. /// Return linear memory limits.
pub(crate) fn limits(&self) -> &ResizableLimits { pub(crate) fn limits(&self) -> &ResizableLimits {
&self.limits &self.limits
@ -191,12 +240,12 @@ impl MemoryInstance {
/// ); /// );
/// ``` /// ```
pub fn current_size(&self) -> Pages { pub fn current_size(&self) -> Pages {
Bytes(self.buffer.borrow().len()).round_up_to() Bytes(self.bytebuf.borrow().len()).round_up_to()
} }
/// Get value from memory at given offset. /// Get value from memory at given offset.
pub fn get_value<T: LittleEndianConvert>(&self, offset: u32) -> Result<T, Error> { pub fn get_value<T: LittleEndianConvert>(&self, offset: u32) -> Result<T, Error> {
let mut buffer = self.buffer.borrow_mut(); let mut buffer = self.bytebuf.borrow_mut();
let region = let region =
self.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::<T>())?; self.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::<T>())?;
Ok( Ok(
@ -212,7 +261,7 @@ impl MemoryInstance {
/// ///
/// [`get_into`]: #method.get_into /// [`get_into`]: #method.get_into
pub fn get(&self, offset: u32, size: usize) -> Result<Vec<u8>, Error> { pub fn get(&self, offset: u32, size: usize) -> Result<Vec<u8>, Error> {
let mut buffer = self.buffer.borrow_mut(); let mut buffer = self.bytebuf.borrow_mut();
let region = self.checked_region(&mut buffer, offset as usize, size)?; let region = self.checked_region(&mut buffer, offset as usize, size)?;
Ok(buffer.as_slice_mut()[region.range()].to_vec()) Ok(buffer.as_slice_mut()[region.range()].to_vec())
@ -224,7 +273,7 @@ impl MemoryInstance {
/// ///
/// Returns `Err` if the specified region is out of bounds. /// Returns `Err` if the specified region is out of bounds.
pub fn get_into(&self, offset: u32, target: &mut [u8]) -> Result<(), Error> { pub fn get_into(&self, offset: u32, target: &mut [u8]) -> Result<(), Error> {
let mut buffer = self.buffer.borrow_mut(); let mut buffer = self.bytebuf.borrow_mut();
let region = self.checked_region(&mut buffer, offset as usize, target.len())?; let region = self.checked_region(&mut buffer, offset as usize, target.len())?;
target.copy_from_slice(&buffer.as_slice_mut()[region.range()]); target.copy_from_slice(&buffer.as_slice_mut()[region.range()]);
@ -234,7 +283,7 @@ impl MemoryInstance {
/// Copy data in the memory at given offset. /// Copy data in the memory at given offset.
pub fn set(&self, offset: u32, value: &[u8]) -> Result<(), Error> { pub fn set(&self, offset: u32, value: &[u8]) -> Result<(), Error> {
let mut buffer = self.buffer.borrow_mut(); let mut buffer = self.bytebuf.borrow_mut();
let range = self let range = self
.checked_region(&mut buffer, offset as usize, value.len())? .checked_region(&mut buffer, offset as usize, value.len())?
.range(); .range();
@ -246,7 +295,7 @@ impl MemoryInstance {
/// Copy value in the memory at given offset. /// Copy value in the memory at given offset.
pub fn set_value<T: LittleEndianConvert>(&self, offset: u32, value: T) -> Result<(), Error> { pub fn set_value<T: LittleEndianConvert>(&self, offset: u32, value: T) -> Result<(), Error> {
let mut buffer = self.buffer.borrow_mut(); let mut buffer = self.bytebuf.borrow_mut();
let range = self let range = self
.checked_region(&mut buffer, offset as usize, ::core::mem::size_of::<T>())? .checked_region(&mut buffer, offset as usize, ::core::mem::size_of::<T>())?
.range(); .range();
@ -284,10 +333,11 @@ impl MemoryInstance {
} }
let new_buffer_length: Bytes = new_size.into(); let new_buffer_length: Bytes = new_size.into();
self.buffer let bytebuf = self.backend
.borrow_mut() .borrow_mut()
.realloc(new_buffer_length.0) .realloc(new_buffer_length.0)
.map_err(|err| Error::Memory(err.to_string()))?; .map_err(|err| Error::Memory(err.to_string()))?;
*self.bytebuf.borrow_mut() = bytebuf;
self.current_size.set(new_buffer_length.0); self.current_size.set(new_buffer_length.0);
@ -382,7 +432,7 @@ impl MemoryInstance {
/// ///
/// Returns `Err` if either of specified regions is out of bounds. /// Returns `Err` if either of specified regions is out of bounds.
pub fn copy(&self, src_offset: usize, dst_offset: usize, len: usize) -> Result<(), Error> { pub fn copy(&self, src_offset: usize, dst_offset: usize, len: usize) -> Result<(), Error> {
let mut buffer = self.buffer.borrow_mut(); let mut buffer = self.bytebuf.borrow_mut();
let (read_region, write_region) = let (read_region, write_region) =
self.checked_region_pair(&mut buffer, src_offset, len, dst_offset, len)?; self.checked_region_pair(&mut buffer, src_offset, len, dst_offset, len)?;
@ -415,7 +465,7 @@ impl MemoryInstance {
dst_offset: usize, dst_offset: usize,
len: usize, len: usize,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut buffer = self.buffer.borrow_mut(); let mut buffer = self.bytebuf.borrow_mut();
let (read_region, write_region) = let (read_region, write_region) =
self.checked_region_pair(&mut buffer, src_offset, len, dst_offset, len)?; self.checked_region_pair(&mut buffer, src_offset, len, dst_offset, len)?;
@ -455,8 +505,8 @@ impl MemoryInstance {
// Because memory references point to different memory instances, it is safe to `borrow_mut` // Because memory references point to different memory instances, it is safe to `borrow_mut`
// both buffers at once (modulo `with_direct_access_mut`). // both buffers at once (modulo `with_direct_access_mut`).
let mut src_buffer = src.buffer.borrow_mut(); let mut src_buffer = src.bytebuf.borrow_mut();
let mut dst_buffer = dst.buffer.borrow_mut(); let mut dst_buffer = dst.bytebuf.borrow_mut();
let src_range = src let src_range = src
.checked_region(&mut src_buffer, src_offset, len)? .checked_region(&mut src_buffer, src_offset, len)?
@ -478,7 +528,7 @@ impl MemoryInstance {
/// ///
/// Returns `Err` if the specified region is out of bounds. /// Returns `Err` if the specified region is out of bounds.
pub fn clear(&self, offset: usize, new_val: u8, len: usize) -> Result<(), Error> { pub fn clear(&self, offset: usize, new_val: u8, len: usize) -> Result<(), Error> {
let mut buffer = self.buffer.borrow_mut(); let mut buffer = self.bytebuf.borrow_mut();
let range = self.checked_region(&mut buffer, offset, len)?.range(); let range = self.checked_region(&mut buffer, offset, len)?.range();
@ -501,7 +551,7 @@ impl MemoryInstance {
/// ///
/// Might be useful for some optimization shenanigans. /// Might be useful for some optimization shenanigans.
pub fn erase(&self) -> Result<(), Error> { pub fn erase(&self) -> Result<(), Error> {
self.buffer self.backend
.borrow_mut() .borrow_mut()
.erase() .erase()
.map_err(|err| Error::Memory(err.to_string())) .map_err(|err| Error::Memory(err.to_string()))
@ -517,7 +567,7 @@ impl MemoryInstance {
/// [`set`]: #method.get /// [`set`]: #method.get
/// [`clear`]: #method.set /// [`clear`]: #method.set
pub fn with_direct_access<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R { pub fn with_direct_access<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
let buf = self.buffer.borrow(); let buf = self.bytebuf.borrow();
f(buf.as_slice()) f(buf.as_slice())
} }
@ -531,7 +581,7 @@ impl MemoryInstance {
/// [`get`]: #method.get /// [`get`]: #method.get
/// [`set`]: #method.set /// [`set`]: #method.set
pub fn with_direct_access_mut<R, F: FnOnce(&mut [u8]) -> R>(&self, f: F) -> R { pub fn with_direct_access_mut<R, F: FnOnce(&mut [u8]) -> R>(&self, f: F) -> R {
let mut buf = self.buffer.borrow_mut(); let mut buf = self.bytebuf.borrow_mut();
f(buf.as_slice_mut()) f(buf.as_slice_mut())
} }
} }

71
src/memory/raw_bytebuf.rs Normal file
View File

@ -0,0 +1,71 @@
//! An implementation of `ByteBuf` based on a plain `Vec`.
use alloc::vec::Vec;
use std::{
slice,
mem,
};
use super::MemoryBackend;
pub struct RawByteBuf {
ptr: *mut u8,
len: usize,
cap: usize,
}
impl RawByteBuf {
pub fn from_raw_parts(ptr: *mut u8, len: usize, cap: usize) -> Self {
Self {
ptr,
len,
cap,
}
}
pub fn new(len: usize) -> Result<Self, &'static str> {
let mut v = vec![0u8; len];
let cap = len;
let ptr = v.as_mut_ptr();
mem::forget(v);
Ok(Self {
ptr,
len,
cap,
})
}
}
impl MemoryBackend for RawByteBuf {
pub fn realloc(&mut self, new_len: usize) -> Result<(), &'static str> {
if new_len > self.cap {
return Err("exceeds cap");
}
self.len = new_len;
Ok(())
}
pub fn len(&self) -> usize {
self.len
}
pub fn as_slice(&self) -> &[u8] {
unsafe {
slice::from_raw_parts(self.ptr, self.len)
}
}
pub fn as_slice_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self.ptr, self.len)
}
}
pub fn erase(&mut self) -> Result<(), &'static str> {
for v in self.as_slice_mut() {
*v = 0;
}
Ok(())
}
}

View File

@ -421,11 +421,7 @@ impl ModuleInstance {
.map(|es| es.entries()) .map(|es| es.entries())
.unwrap_or(&[]) .unwrap_or(&[])
{ {
let offset = element_segment let offset_val = match eval_init_expr(element_segment.offset(), &module_ref) {
.offset()
.as_ref()
.expect("passive segments are rejected due to validation");
let offset_val = match eval_init_expr(offset, &module_ref) {
RuntimeValue::I32(v) => v as u32, RuntimeValue::I32(v) => v as u32,
_ => panic!("Due to validation elem segment offset should evaluate to i32"), _ => panic!("Due to validation elem segment offset should evaluate to i32"),
}; };
@ -454,11 +450,7 @@ impl ModuleInstance {
} }
for data_segment in module.data_section().map(|ds| ds.entries()).unwrap_or(&[]) { for data_segment in module.data_section().map(|ds| ds.entries()).unwrap_or(&[]) {
let offset = data_segment let offset_val = match eval_init_expr(data_segment.offset(), &module_ref) {
.offset()
.as_ref()
.expect("passive segments are rejected due to validation");
let offset_val = match eval_init_expr(offset, &module_ref) {
RuntimeValue::I32(v) => v as u32, RuntimeValue::I32(v) => v as u32,
_ => panic!("Due to validation data segment offset should evaluate to i32"), _ => panic!("Due to validation data segment offset should evaluate to i32"),
}; };

View File

@ -1,8 +1,5 @@
#![allow(missing_docs)] #![allow(missing_docs)]
#[cfg(not(feature = "std"))]
use libm::{F32Ext, F64Ext};
use core::cmp::{Ordering, PartialEq, PartialOrd}; use core::cmp::{Ordering, PartialEq, PartialOrd};
use core::ops::{Add, Div, Mul, Neg, Rem, Sub}; use core::ops::{Add, Div, Mul, Neg, Rem, Sub};

View File

@ -251,14 +251,13 @@ impl Compiler {
); );
self.sink.emit_br_nez(target); self.sink.emit_br_nez(target);
} }
BrTable(ref br_table_data) => { BrTable(ref table, default) => {
// At this point, the condition value is at the top of the stack. // At this point, the condition value is at the top of the stack.
// But at the point of actual jump the condition will already be // But at the point of actual jump the condition will already be
// popped off. // popped off.
let value_stack_height = context.value_stack.len().saturating_sub(1); let value_stack_height = context.value_stack.len().saturating_sub(1);
let targets = br_table_data let targets = table
.table
.iter() .iter()
.map(|depth| { .map(|depth| {
require_target( require_target(
@ -270,7 +269,7 @@ impl Compiler {
}) })
.collect::<Result<Vec<_>, _>>(); .collect::<Result<Vec<_>, _>>();
let default_target = require_target( let default_target = require_target(
br_table_data.default, default,
value_stack_height, value_stack_height,
&context.frame_stack, &context.frame_stack,
&self.label_stack, &self.label_stack,

View File

@ -1289,8 +1289,11 @@ impl FunctionContext {
debug_assert!(!self.is_initialized); debug_assert!(!self.is_initialized);
let num_locals = locals.iter().map(|l| l.count() as usize).sum(); let num_locals = locals.iter().map(|l| l.count() as usize).sum();
for _ in 0..num_locals {
value_stack.extend(num_locals)?; value_stack
.push(Default::default())
.map_err(|_| TrapKind::StackOverflow)?;
}
self.is_initialized = true; self.is_initialized = true;
Ok(()) Ok(())
@ -1436,18 +1439,6 @@ impl ValueStack {
Ok(()) Ok(())
} }
fn extend(&mut self, len: usize) -> Result<(), TrapKind> {
let cells = self
.buf
.get_mut(self.sp..self.sp + len)
.ok_or_else(|| TrapKind::StackOverflow)?;
for cell in cells {
*cell = Default::default();
}
self.sp += len;
Ok(())
}
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
self.sp self.sp

View File

@ -285,7 +285,7 @@ fn resume_call_host_func() {
let export = instance.export_by_name("test").unwrap(); let export = instance.export_by_name("test").unwrap();
let func_instance = export.as_func().unwrap(); let func_instance = export.as_func().unwrap();
let mut invocation = FuncInstance::invoke_resumable(&func_instance, &[][..]).unwrap(); let mut invocation = FuncInstance::invoke_resumable(&func_instance, &[]).unwrap();
let result = invocation.start_execution(&mut env); let result = invocation.start_execution(&mut env);
match result { match result {
Err(ResumableError::Trap(_)) => {} Err(ResumableError::Trap(_)) => {}
@ -330,7 +330,7 @@ fn resume_call_host_func_type_mismatch() {
let export = instance.export_by_name("test").unwrap(); let export = instance.export_by_name("test").unwrap();
let func_instance = export.as_func().unwrap(); let func_instance = export.as_func().unwrap();
let mut invocation = FuncInstance::invoke_resumable(&func_instance, &[][..]).unwrap(); let mut invocation = FuncInstance::invoke_resumable(&func_instance, &[]).unwrap();
let result = invocation.start_execution(&mut env); let result = invocation.start_execution(&mut env);
match result { match result {
Err(ResumableError::Trap(_)) => {} Err(ResumableError::Trap(_)) => {}

View File

@ -368,7 +368,6 @@ impl WrapInto<F32> for F64 {
macro_rules! impl_try_truncate_into { macro_rules! impl_try_truncate_into {
(@primitive $from: ident, $into: ident, $to_primitive:path) => { (@primitive $from: ident, $into: ident, $to_primitive:path) => {
impl TryTruncateInto<$into, TrapKind> for $from { impl TryTruncateInto<$into, TrapKind> for $from {
#[cfg(feature = "std")]
fn try_truncate_into(self) -> Result<$into, TrapKind> { fn try_truncate_into(self) -> Result<$into, TrapKind> {
// Casting from a float to an integer will round the float towards zero // Casting from a float to an integer will round the float towards zero
num_rational::BigRational::from_float(self) num_rational::BigRational::from_float(self)
@ -376,23 +375,6 @@ macro_rules! impl_try_truncate_into {
.and_then(|val| $to_primitive(&val)) .and_then(|val| $to_primitive(&val))
.ok_or(TrapKind::InvalidConversionToInt) .ok_or(TrapKind::InvalidConversionToInt)
} }
#[cfg(not(feature = "std"))]
fn try_truncate_into(self) -> Result<$into, TrapKind> {
// Casting from a float to an integer will round the float towards zero
// NOTE: currently this will cause Undefined Behavior if the rounded value cannot be represented by the
// target integer type. This includes Inf and NaN. This is a bug and will be fixed.
if self.is_nan() || self.is_infinite() {
return Err(TrapKind::InvalidConversionToInt);
}
// range check
let result = self as $into;
if result as $from != self.trunc() {
return Err(TrapKind::InvalidConversionToInt);
}
Ok(self as $into)
}
} }
}; };
(@wrapped $from:ident, $intermediate:ident, $into:ident) => { (@wrapped $from:ident, $intermediate:ident, $into:ident) => {
@ -837,6 +819,15 @@ impl_integer!(u32);
impl_integer!(i64); impl_integer!(i64);
impl_integer!(u64); impl_integer!(u64);
// Use std float functions in std environment.
// And libm's implementation in no_std
#[cfg(feature = "std")]
macro_rules! call_math {
($op:ident, $e:expr, $fXX:ident, $FXXExt:ident) => {
$fXX::$op($e)
};
}
#[cfg(not(feature = "std"))]
macro_rules! call_math { macro_rules! call_math {
($op:ident, $e:expr, $fXX:ident, $FXXExt:ident) => { ($op:ident, $e:expr, $fXX:ident, $FXXExt:ident) => {
::libm::$FXXExt::$op($e) ::libm::$FXXExt::$op($e)

View File

@ -18,7 +18,6 @@ fn spec_to_runtime_value(val: Value<u32, u64>) -> RuntimeValue {
Value::I64(v) => RuntimeValue::I64(v), Value::I64(v) => RuntimeValue::I64(v),
Value::F32(v) => RuntimeValue::F32(v.into()), Value::F32(v) => RuntimeValue::F32(v.into()),
Value::F64(v) => RuntimeValue::F64(v.into()), Value::F64(v) => RuntimeValue::F64(v.into()),
Value::V128(_) => panic!("v128 is not supported"),
} }
} }

View File

@ -1,6 +1,6 @@
[package] [package]
name = "wasmi-validation" name = "wasmi-validation"
version = "0.2.0" version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"] authors = ["Parity Technologies <admin@parity.io>"]
edition = "2018" edition = "2018"
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
@ -8,7 +8,7 @@ repository = "https://github.com/paritytech/wasmi"
description = "Wasm code validator" description = "Wasm code validator"
[dependencies] [dependencies]
parity-wasm = { version = "0.40.1", default-features = false } parity-wasm = { version = "0.31", default-features = false }
[dev-dependencies] [dev-dependencies]
assert_matches = "1.1" assert_matches = "1.1"

View File

@ -266,8 +266,8 @@ impl<'a> FunctionValidationContext<'a> {
BrIf(depth) => { BrIf(depth) => {
self.validate_br_if(depth)?; self.validate_br_if(depth)?;
} }
BrTable(ref br_table_data) => { BrTable(ref table, default) => {
self.validate_br_table(&*br_table_data.table, br_table_data.default)?; self.validate_br_table(table, default)?;
make_top_frame_polymorphic(&mut self.value_stack, &mut self.frame_stack); make_top_frame_polymorphic(&mut self.value_stack, &mut self.frame_stack);
} }
Return => { Return => {

View File

@ -319,11 +319,7 @@ pub fn validate_module<V: Validator>(module: &Module) -> Result<V::Output, Error
if let Some(data_section) = module.data_section() { if let Some(data_section) = module.data_section() {
for data_segment in data_section.entries() { for data_segment in data_section.entries() {
context.require_memory(data_segment.index())?; context.require_memory(data_segment.index())?;
let offset = data_segment let init_ty = expr_const_type(data_segment.offset(), context.globals())?;
.offset()
.as_ref()
.ok_or_else(|| Error("passive memory segments are not supported".into()))?;
let init_ty = expr_const_type(&offset, context.globals())?;
if init_ty != ValueType::I32 { if init_ty != ValueType::I32 {
return Err(Error("segment offset should return I32".into())); return Err(Error("segment offset should return I32".into()));
} }
@ -334,11 +330,8 @@ pub fn validate_module<V: Validator>(module: &Module) -> Result<V::Output, Error
if let Some(element_section) = module.elements_section() { if let Some(element_section) = module.elements_section() {
for element_segment in element_section.entries() { for element_segment in element_section.entries() {
context.require_table(element_segment.index())?; context.require_table(element_segment.index())?;
let offset = element_segment
.offset() let init_ty = expr_const_type(element_segment.offset(), context.globals())?;
.as_ref()
.ok_or_else(|| Error("passive element segments are not supported".into()))?;
let init_ty = expr_const_type(&offset, context.globals())?;
if init_ty != ValueType::I32 { if init_ty != ValueType::I32 {
return Err(Error("segment offset should return I32".into())); return Err(Error("segment offset should return I32".into()));
} }