mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-21 18:51:52 +00:00
Merge branch 'master' into tests_finish
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "parity-wasm"
|
name = "parity-wasm"
|
||||||
version = "0.8.5"
|
version = "0.10.0"
|
||||||
authors = ["Nikolay Volf <nikvolf@gmail.com>", "Svyatoslav Nikolsky <svyatonik@yandex.ru>"]
|
authors = ["Nikolay Volf <nikvolf@gmail.com>", "Svyatoslav Nikolsky <svyatonik@yandex.ru>"]
|
||||||
license = "MIT/Apache-2.0"
|
license = "MIT/Apache-2.0"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
@ -12,5 +12,6 @@ keywords = ["wasm", "webassembly", "bytecode", "serde", "interpreter"]
|
|||||||
exclude = [ "res/*", "spec/*" ]
|
exclude = [ "res/*", "spec/*" ]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
log = "0.3"
|
||||||
byteorder = "1.0"
|
byteorder = "1.0"
|
||||||
parking_lot = "0.4"
|
parking_lot = "0.4"
|
49
src/builder/data.rs
Normal file
49
src/builder/data.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
use super::invoke::{Identity, Invoke};
|
||||||
|
use elements;
|
||||||
|
|
||||||
|
pub struct DataSegmentBuilder<F=Identity> {
|
||||||
|
callback: F,
|
||||||
|
// todo: add mapper once multiple memory refs possible
|
||||||
|
mem_index: u32,
|
||||||
|
offset: elements::InitExpr,
|
||||||
|
value: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DataSegmentBuilder {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
DataSegmentBuilder::with_callback(Identity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> DataSegmentBuilder<F> {
|
||||||
|
pub fn with_callback(callback: F) -> Self {
|
||||||
|
DataSegmentBuilder {
|
||||||
|
callback: callback,
|
||||||
|
mem_index: 0,
|
||||||
|
offset: elements::InitExpr::empty(),
|
||||||
|
value: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn offset(mut self, opcode: elements::Opcode) -> Self {
|
||||||
|
self.offset = elements::InitExpr::new(vec![opcode, elements::Opcode::End]);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn value(mut self, value: Vec<u8>) -> Self {
|
||||||
|
self.value = value;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> DataSegmentBuilder<F> where F: Invoke<elements::DataSegment> {
|
||||||
|
pub fn build(self) -> F::Result {
|
||||||
|
self.callback.invoke(
|
||||||
|
elements::DataSegment::new(
|
||||||
|
self.mem_index,
|
||||||
|
self.offset,
|
||||||
|
self.value,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -56,7 +56,6 @@ impl<F> GlobalBuilder<F> where F: Invoke<elements::GlobalEntry> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<F> Invoke<elements::ValueType> for GlobalBuilder<F> {
|
impl<F> Invoke<elements::ValueType> for GlobalBuilder<F> {
|
||||||
type Result = Self;
|
type Result = Self;
|
||||||
fn invoke(self, the_type: elements::ValueType) -> Self {
|
fn invoke(self, the_type: elements::ValueType) -> Self {
|
||||||
|
@ -9,6 +9,7 @@ mod memory;
|
|||||||
mod table;
|
mod table;
|
||||||
mod export;
|
mod export;
|
||||||
mod global;
|
mod global;
|
||||||
|
mod data;
|
||||||
|
|
||||||
pub use self::module::{module, from_module, ModuleBuilder};
|
pub use self::module::{module, from_module, ModuleBuilder};
|
||||||
pub use self::code::{signatures, signature, function};
|
pub use self::code::{signatures, signature, function};
|
||||||
|
@ -2,7 +2,7 @@ use super::invoke::{Invoke, Identity};
|
|||||||
use super::code::{self, SignaturesBuilder, FunctionBuilder};
|
use super::code::{self, SignaturesBuilder, FunctionBuilder};
|
||||||
use super::memory::{self, MemoryBuilder};
|
use super::memory::{self, MemoryBuilder};
|
||||||
use super::table::{self, TableBuilder};
|
use super::table::{self, TableBuilder};
|
||||||
use super::{import, export, global};
|
use super::{import, export, global, data};
|
||||||
use elements;
|
use elements;
|
||||||
|
|
||||||
/// Module builder
|
/// Module builder
|
||||||
@ -327,6 +327,17 @@ impl<F> ModuleBuilder<F> where F: Invoke<elements::Module> {
|
|||||||
global::GlobalBuilder::with_callback(self)
|
global::GlobalBuilder::with_callback(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add data segment to the builder
|
||||||
|
pub fn with_data_segment(mut self, segment: elements::DataSegment) -> Self {
|
||||||
|
self.module.data.entries_mut().push(segment);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Data entry builder
|
||||||
|
pub fn data(self) -> data::DataSegmentBuilder<Self> {
|
||||||
|
data::DataSegmentBuilder::with_callback(self)
|
||||||
|
}
|
||||||
|
|
||||||
/// Build module (final step)
|
/// Build module (final step)
|
||||||
pub fn build(self) -> F::Result {
|
pub fn build(self) -> F::Result {
|
||||||
self.callback.invoke(self.module.into())
|
self.callback.invoke(self.module.into())
|
||||||
@ -420,6 +431,16 @@ impl<F> Invoke<elements::GlobalEntry> for ModuleBuilder<F>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<F> Invoke<elements::DataSegment> for ModuleBuilder<F>
|
||||||
|
where F: Invoke<elements::Module>
|
||||||
|
{
|
||||||
|
type Result = Self;
|
||||||
|
|
||||||
|
fn invoke(self, segment: elements::DataSegment) -> Self {
|
||||||
|
self.with_data_segment(segment)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Start new module builder
|
/// Start new module builder
|
||||||
pub fn module() -> ModuleBuilder {
|
pub fn module() -> ModuleBuilder {
|
||||||
ModuleBuilder::new()
|
ModuleBuilder::new()
|
||||||
@ -472,4 +493,16 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);
|
assert_eq!(module.global_section().expect("global section to exist").entries().len(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn data() {
|
||||||
|
let module = module()
|
||||||
|
.data()
|
||||||
|
.offset(::elements::Opcode::I32Const(16))
|
||||||
|
.value(vec![0u8, 15, 10, 5, 25])
|
||||||
|
.build()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
assert_eq!(module.data_section().expect("data section to exist").entries().len(), 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,6 +122,8 @@ pub enum Opcode {
|
|||||||
GetGlobal(u32),
|
GetGlobal(u32),
|
||||||
SetGlobal(u32),
|
SetGlobal(u32),
|
||||||
|
|
||||||
|
// All store/load opcodes operate with 'memory immediates'
|
||||||
|
// which represented here as (flag, offset) tuple
|
||||||
I32Load(u32, u32),
|
I32Load(u32, u32),
|
||||||
I64Load(u32, u32),
|
I64Load(u32, u32),
|
||||||
F32Load(u32, u32),
|
F32Load(u32, u32),
|
||||||
@ -145,6 +147,7 @@ pub enum Opcode {
|
|||||||
I64Store8(u32, u32),
|
I64Store8(u32, u32),
|
||||||
I64Store16(u32, u32),
|
I64Store16(u32, u32),
|
||||||
I64Store32(u32, u32),
|
I64Store32(u32, u32),
|
||||||
|
|
||||||
CurrentMemory(bool),
|
CurrentMemory(bool),
|
||||||
GrowMemory(bool),
|
GrowMemory(bool),
|
||||||
|
|
||||||
@ -649,97 +652,97 @@ impl Serialize for Opcode {
|
|||||||
SetGlobal(index) => op!(writer, 0x24, {
|
SetGlobal(index) => op!(writer, 0x24, {
|
||||||
VarUint32::from(index).serialize(writer)?;
|
VarUint32::from(index).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I32Load(from, to) => op!(writer, 0x28, {
|
I32Load(flags, offset) => op!(writer, 0x28, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Load(from, to) => op!(writer, 0x29, {
|
I64Load(flags, offset) => op!(writer, 0x29, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
F32Load(from, to) => op!(writer, 0x2a, {
|
F32Load(flags, offset) => op!(writer, 0x2a, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
F64Load(from, to) => op!(writer, 0x2b, {
|
F64Load(flags, offset) => op!(writer, 0x2b, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I32Load8S(from, to) => op!(writer, 0x2c, {
|
I32Load8S(flags, offset) => op!(writer, 0x2c, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I32Load8U(from, to) => op!(writer, 0x2d, {
|
I32Load8U(flags, offset) => op!(writer, 0x2d, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I32Load16S(from, to) => op!(writer, 0x2e, {
|
I32Load16S(flags, offset) => op!(writer, 0x2e, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I32Load16U(from, to) => op!(writer, 0x2f, {
|
I32Load16U(flags, offset) => op!(writer, 0x2f, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Load8S(from, to) => op!(writer, 0x30, {
|
I64Load8S(flags, offset) => op!(writer, 0x30, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Load8U(from, to) => op!(writer, 0x31, {
|
I64Load8U(flags, offset) => op!(writer, 0x31, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Load16S(from, to) => op!(writer, 0x32, {
|
I64Load16S(flags, offset) => op!(writer, 0x32, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Load16U(from, to) => op!(writer, 0x33, {
|
I64Load16U(flags, offset) => op!(writer, 0x33, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Load32S(from, to) => op!(writer, 0x34, {
|
I64Load32S(flags, offset) => op!(writer, 0x34, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Load32U(from, to) => op!(writer, 0x35, {
|
I64Load32U(flags, offset) => op!(writer, 0x35, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I32Store(from, to) => op!(writer, 0x36, {
|
I32Store(flags, offset) => op!(writer, 0x36, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Store(from, to) => op!(writer, 0x37, {
|
I64Store(flags, offset) => op!(writer, 0x37, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
F32Store(from, to) => op!(writer, 0x38, {
|
F32Store(flags, offset) => op!(writer, 0x38, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
F64Store(from, to) => op!(writer, 0x39, {
|
F64Store(flags, offset) => op!(writer, 0x39, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I32Store8(from, to) => op!(writer, 0x3a, {
|
I32Store8(flags, offset) => op!(writer, 0x3a, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I32Store16(from, to) => op!(writer, 0x3b, {
|
I32Store16(flags, offset) => op!(writer, 0x3b, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Store8(from, to) => op!(writer, 0x3c, {
|
I64Store8(flags, offset) => op!(writer, 0x3c, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Store16(from, to) => op!(writer, 0x3d, {
|
I64Store16(flags, offset) => op!(writer, 0x3d, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
I64Store32(from, to) => op!(writer, 0x3e, {
|
I64Store32(flags, offset) => op!(writer, 0x3e, {
|
||||||
VarUint32::from(from).serialize(writer)?;
|
VarUint32::from(flags).serialize(writer)?;
|
||||||
VarUint32::from(to).serialize(writer)?;
|
VarUint32::from(offset).serialize(writer)?;
|
||||||
}),
|
}),
|
||||||
CurrentMemory(flag) => op!(writer, 0x3f, {
|
CurrentMemory(flag) => op!(writer, 0x3f, {
|
||||||
VarUint1::from(flag).serialize(writer)?;
|
VarUint1::from(flag).serialize(writer)?;
|
||||||
|
@ -427,7 +427,6 @@ impl Deserialize for VarUint1 {
|
|||||||
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
|
||||||
let mut u8buf = [0u8; 1];
|
let mut u8buf = [0u8; 1];
|
||||||
reader.read_exact(&mut u8buf)?;
|
reader.read_exact(&mut u8buf)?;
|
||||||
// todo check range
|
|
||||||
match u8buf[0] {
|
match u8buf[0] {
|
||||||
0 => Ok(VarUint1(false)),
|
0 => Ok(VarUint1(false)),
|
||||||
1 => Ok(VarUint1(true)),
|
1 => Ok(VarUint1(true)),
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::borrow::Cow;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use elements::{FunctionType, Internal, ValueType};
|
use elements::{FunctionType, Internal, ValueType};
|
||||||
use interpreter::Error;
|
use interpreter::Error;
|
||||||
@ -19,20 +20,67 @@ pub trait UserFunctionExecutor {
|
|||||||
fn execute(&mut self, name: &str, context: CallerContext) -> Result<Option<RuntimeValue>, Error>;
|
fn execute(&mut self, name: &str, context: CallerContext) -> Result<Option<RuntimeValue>, Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// User function descriptor
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum UserFunctionDescriptor {
|
||||||
|
/// Static function definition
|
||||||
|
Static(&'static str, &'static [ValueType]),
|
||||||
|
/// Dynamic heap function definition
|
||||||
|
Heap(String, Vec<ValueType>),
|
||||||
|
}
|
||||||
|
|
||||||
/// User function type.
|
/// User function type.
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct UserFunction {
|
pub struct UserFunction {
|
||||||
/// User function name.
|
/// Descriptor with variable-length definitions
|
||||||
pub name: String,
|
pub desc: UserFunctionDescriptor,
|
||||||
/// User function parameters (for signature matching).
|
/// Return type of the signature
|
||||||
pub params: Vec<ValueType>,
|
|
||||||
/// User function return type (for signature matching).
|
|
||||||
pub result: Option<ValueType>,
|
pub result: Option<ValueType>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UserFunction {
|
||||||
|
/// New function with statically known params
|
||||||
|
pub fn statik(name: &'static str, params: &'static [ValueType], result: Option<ValueType>) -> Self {
|
||||||
|
UserFunction {
|
||||||
|
desc: UserFunctionDescriptor::Static(name, params),
|
||||||
|
result: result,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// New function with statically unknown params
|
||||||
|
pub fn heap(name: String, params: Vec<ValueType>, result: Option<ValueType>) -> Self {
|
||||||
|
UserFunction {
|
||||||
|
desc: UserFunctionDescriptor::Heap(name, params),
|
||||||
|
result: result,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Name of the function
|
||||||
|
pub fn name(&self) -> &str {
|
||||||
|
match self.desc {
|
||||||
|
UserFunctionDescriptor::Static(name, _) => name,
|
||||||
|
UserFunctionDescriptor::Heap(ref name, _) => name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Arguments of the function
|
||||||
|
pub fn params(&self) -> &[ValueType] {
|
||||||
|
match self.desc {
|
||||||
|
UserFunctionDescriptor::Static(_, params) => params,
|
||||||
|
UserFunctionDescriptor::Heap(_, ref params) => params,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return type of the function
|
||||||
|
pub fn result(&self) -> Option<ValueType> {
|
||||||
|
self.result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Set of user-defined functions
|
/// Set of user-defined functions
|
||||||
pub struct UserFunctions<'a> {
|
pub struct UserFunctions<'a> {
|
||||||
/// Functions list.
|
/// Functions list.
|
||||||
pub functions: Vec<UserFunction>,
|
pub functions: Cow<'static, [UserFunction]>,
|
||||||
/// Functions executor.
|
/// Functions executor.
|
||||||
pub executor: &'a mut UserFunctionExecutor,
|
pub executor: &'a mut UserFunctionExecutor,
|
||||||
}
|
}
|
||||||
@ -46,7 +94,7 @@ pub struct NativeModuleInstance<'a> {
|
|||||||
/// By-name functions index.
|
/// By-name functions index.
|
||||||
by_name: HashMap<String, u32>,
|
by_name: HashMap<String, u32>,
|
||||||
/// User functions list.
|
/// User functions list.
|
||||||
functions: Vec<UserFunction>,
|
functions: Cow<'static, [UserFunction]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> NativeModuleInstance<'a> {
|
impl<'a> NativeModuleInstance<'a> {
|
||||||
@ -55,7 +103,7 @@ impl<'a> NativeModuleInstance<'a> {
|
|||||||
Ok(NativeModuleInstance {
|
Ok(NativeModuleInstance {
|
||||||
env: env,
|
env: env,
|
||||||
executor: RwLock::new(functions.executor),
|
executor: RwLock::new(functions.executor),
|
||||||
by_name: functions.functions.iter().enumerate().map(|(i, f)| (f.name.clone(), i as u32)).collect(),
|
by_name: functions.functions.iter().enumerate().map(|(i, f)| (f.name().to_owned(), i as u32)).collect(),
|
||||||
functions: functions.functions,
|
functions: functions.functions,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -108,7 +156,7 @@ impl<'a> ModuleInstanceInterface for NativeModuleInstance<'a> {
|
|||||||
self.functions
|
self.functions
|
||||||
.get((index - NATIVE_INDEX_FUNC_MIN) as usize)
|
.get((index - NATIVE_INDEX_FUNC_MIN) as usize)
|
||||||
.ok_or(Error::Native(format!("missing native env function with index {}", index)))
|
.ok_or(Error::Native(format!("missing native env function with index {}", index)))
|
||||||
.map(|f| FunctionType::new(f.params.clone(), f.result.clone()))
|
.map(|f| FunctionType::new(f.params().to_vec(), f.result().clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn function_type_by_index(&self, type_index: u32) -> Result<FunctionType, Error> {
|
fn function_type_by_index(&self, type_index: u32) -> Result<FunctionType, Error> {
|
||||||
@ -135,7 +183,7 @@ impl<'a> ModuleInstanceInterface for NativeModuleInstance<'a> {
|
|||||||
self.functions
|
self.functions
|
||||||
.get((index - NATIVE_INDEX_FUNC_MIN) as usize)
|
.get((index - NATIVE_INDEX_FUNC_MIN) as usize)
|
||||||
.ok_or(Error::Native(format!("trying to call native function with index {}", index)))
|
.ok_or(Error::Native(format!("trying to call native function with index {}", index)))
|
||||||
.and_then(|f| self.executor.write().execute(&f.name, outer))
|
.and_then(|f| self.executor.write().execute(&f.name(), outer))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,22 @@ pub struct MemoryInstance {
|
|||||||
maximum_size: u32,
|
maximum_size: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct CheckedRegion<'a, B: 'a> where B: ::std::ops::Deref<Target=Vec<u8>> {
|
||||||
|
buffer: &'a B,
|
||||||
|
offset: usize,
|
||||||
|
size: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, B: 'a> CheckedRegion<'a, B> where B: ::std::ops::Deref<Target=Vec<u8>> {
|
||||||
|
fn range(&self) -> ::std::ops::Range<usize> {
|
||||||
|
self.offset..self.offset+self.size
|
||||||
|
}
|
||||||
|
|
||||||
|
fn slice(&self) -> &[u8] {
|
||||||
|
&self.buffer[self.range()]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MemoryInstance {
|
impl MemoryInstance {
|
||||||
/// Create new linear memory instance.
|
/// Create new linear memory instance.
|
||||||
pub fn new(memory_type: &MemoryType) -> Result<Arc<Self>, Error> {
|
pub fn new(memory_type: &MemoryType) -> Result<Arc<Self>, Error> {
|
||||||
@ -48,36 +64,18 @@ impl MemoryInstance {
|
|||||||
|
|
||||||
/// Get data at given offset.
|
/// Get data at given offset.
|
||||||
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 begin = offset as usize;
|
|
||||||
let end = match begin.checked_add(size) {
|
|
||||||
Some(end) => end,
|
|
||||||
None => return Err(Error::Memory(format!("trying to read memory block of size {} from offset {}", size, offset))),
|
|
||||||
};
|
|
||||||
|
|
||||||
let buffer = self.buffer.read();
|
let buffer = self.buffer.read();
|
||||||
if buffer.len() < end {
|
let region = self.checked_region(&buffer, offset as usize, size)?;
|
||||||
return Err(Error::Memory(format!("trying to read region [{}..{}] in memory [0..{}]", begin, end, buffer.len())));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(buffer[begin..end].to_vec())
|
Ok(region.slice().to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set data at given offset.
|
/// Set data at given offset.
|
||||||
pub fn set(&self, offset: u32, value: &[u8]) -> Result<(), Error> {
|
pub fn set(&self, offset: u32, value: &[u8]) -> Result<(), Error> {
|
||||||
let size = value.len();
|
|
||||||
let begin = offset as usize;
|
|
||||||
let end = match begin.checked_add(size) {
|
|
||||||
Some(end) => end,
|
|
||||||
None => return Err(Error::Memory(format!("trying to update memory block of size {} from offset {}", size, offset))),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut buffer = self.buffer.write();
|
let mut buffer = self.buffer.write();
|
||||||
if buffer.len() < end {
|
let range = self.checked_region(&buffer, offset as usize, value.len())?.range();
|
||||||
return Err(Error::Memory(format!("trying to update region [{}..{}] in memory [0..{}]", begin, end, buffer.len())));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut mut_buffer = buffer.as_mut_slice();
|
buffer[range].copy_from_slice(value);
|
||||||
mut_buffer[begin..end].copy_from_slice(value);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -96,4 +94,46 @@ impl MemoryInstance {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn checked_region<'a, B>(&self, buffer: &'a B, offset: usize, size: usize) -> Result<CheckedRegion<'a, B>, Error>
|
||||||
|
where B: ::std::ops::Deref<Target=Vec<u8>>
|
||||||
|
{
|
||||||
|
let end = offset.checked_add(size)
|
||||||
|
.ok_or(Error::Memory(format!("trying to access memory block of size {} from offset {}", size, offset)))?;
|
||||||
|
|
||||||
|
if end > buffer.len() {
|
||||||
|
return Err(Error::Memory(format!("trying to access region [{}..{}] in memory [0..{}]", offset, end, buffer.len())));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(CheckedRegion {
|
||||||
|
buffer: buffer,
|
||||||
|
offset: offset,
|
||||||
|
size: size,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Copy memory region
|
||||||
|
pub fn copy(&self, src_offset: usize, dst_offset: usize, len: usize) -> Result<(), Error> {
|
||||||
|
let buffer = self.buffer.write();
|
||||||
|
|
||||||
|
let read_region = self.checked_region(&buffer, src_offset, len)?;
|
||||||
|
let write_region = self.checked_region(&buffer, dst_offset, len)?;
|
||||||
|
|
||||||
|
unsafe { ::std::ptr::copy(
|
||||||
|
buffer[read_region.range()].as_ptr(),
|
||||||
|
buffer[write_region.range()].as_ptr() as *mut _,
|
||||||
|
len,
|
||||||
|
)}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Zero memory region
|
||||||
|
pub fn zero(&self, offset: usize, len: usize) -> Result<(), Error> {
|
||||||
|
let mut buffer = self.buffer.write();
|
||||||
|
|
||||||
|
let range = self.checked_region(&buffer, offset, len)?.range();
|
||||||
|
for val in &mut buffer[range] { *val = 0 }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -79,5 +79,5 @@ pub use self::table::TableInstance;
|
|||||||
pub use self::program::ProgramInstance;
|
pub use self::program::ProgramInstance;
|
||||||
pub use self::value::RuntimeValue;
|
pub use self::value::RuntimeValue;
|
||||||
pub use self::variable::VariableInstance;
|
pub use self::variable::VariableInstance;
|
||||||
pub use self::env_native::{env_native_module, UserFunctions, UserFunction, UserFunctionExecutor};
|
pub use self::env_native::{env_native_module, UserFunctions, UserFunction, UserFunctionExecutor, UserFunctionDescriptor};
|
||||||
pub use self::env::EnvParams;
|
pub use self::env::EnvParams;
|
@ -41,7 +41,7 @@ pub enum ExportEntryType {
|
|||||||
|
|
||||||
/// Module instance API.
|
/// Module instance API.
|
||||||
pub trait ModuleInstanceInterface {
|
pub trait ModuleInstanceInterface {
|
||||||
/// Run instantiation-time procedures (validation and start function call). Module is not completely validated until this call.
|
/// Run instantiation-time procedures (validation and start function [if any] call). Module is not completely validated until this call.
|
||||||
fn instantiate<'a>(&self, is_user_module: bool, externals: Option<&'a HashMap<String, Arc<ModuleInstanceInterface + 'a>>>) -> Result<(), Error>;
|
fn instantiate<'a>(&self, is_user_module: bool, externals: Option<&'a HashMap<String, Arc<ModuleInstanceInterface + 'a>>>) -> Result<(), Error>;
|
||||||
/// Execute function with the given index.
|
/// Execute function with the given index.
|
||||||
fn execute_index(&self, index: u32, params: ExecutionParams) -> Result<Option<RuntimeValue>, Error>;
|
fn execute_index(&self, index: u32, params: ExecutionParams) -> Result<Option<RuntimeValue>, Error>;
|
||||||
@ -335,9 +335,23 @@ impl ModuleInstanceInterface for ModuleInstance {
|
|||||||
let function_body = code_section.bodies().get(index as usize).ok_or(Error::Validation(format!("Missing body for function {}", index)))?;
|
let function_body = code_section.bodies().get(index as usize).ok_or(Error::Validation(format!("Missing body for function {}", index)))?;
|
||||||
let mut locals = function_type.params().to_vec();
|
let mut locals = function_type.params().to_vec();
|
||||||
locals.extend(function_body.locals().iter().flat_map(|l| repeat(l.value_type()).take(l.count() as usize)));
|
locals.extend(function_body.locals().iter().flat_map(|l| repeat(l.value_type()).take(l.count() as usize)));
|
||||||
let mut context = FunctionValidationContext::new(&self.module, &self.imports, &locals, DEFAULT_VALUE_STACK_LIMIT, DEFAULT_FRAME_STACK_LIMIT, &function_type);
|
let mut context = FunctionValidationContext::new(
|
||||||
|
&self.module,
|
||||||
|
&self.imports,
|
||||||
|
&locals,
|
||||||
|
DEFAULT_VALUE_STACK_LIMIT,
|
||||||
|
DEFAULT_FRAME_STACK_LIMIT,
|
||||||
|
&function_type);
|
||||||
|
|
||||||
let block_type = function_type.return_type().map(BlockType::Value).unwrap_or(BlockType::NoResult);
|
let block_type = function_type.return_type().map(BlockType::Value).unwrap_or(BlockType::NoResult);
|
||||||
Validator::validate_function(&mut context, block_type, function_body.code().elements())?;
|
Validator::validate_function(&mut context, block_type, function_body.code().elements())?;
|
||||||
|
.map_err(|e| {
|
||||||
|
if let Error::Validation(msg) = e {
|
||||||
|
Error::Validation(format!("Function #{} validation error: {}", index, msg))
|
||||||
|
} else {
|
||||||
|
e
|
||||||
|
}
|
||||||
|
})?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,8 +187,6 @@ impl Interpreter {
|
|||||||
let instruction = &body_stack.back().expect("TODO")[top_frame.begin_position];
|
let instruction = &body_stack.back().expect("TODO")[top_frame.begin_position];
|
||||||
let block_body = Interpreter::into_block(instruction, top_frame.frame_type)?;
|
let block_body = Interpreter::into_block(instruction, top_frame.frame_type)?;
|
||||||
body_stack.push_back(block_body);
|
body_stack.push_back(block_body);
|
||||||
//body_stack.insert(block_body.len() - 1, block_body);
|
|
||||||
//function_context.frame_stack_mut().push_penultimate(block_frame)?;
|
|
||||||
},
|
},
|
||||||
InstructionOutcome::ExecuteCall(func_ref) => return Ok(RunResult::NestedCall(function_context.nested(func_ref)?)),
|
InstructionOutcome::ExecuteCall(func_ref) => return Ok(RunResult::NestedCall(function_context.nested(func_ref)?)),
|
||||||
InstructionOutcome::End if !function_context.frame_stack().is_empty() => {
|
InstructionOutcome::End if !function_context.frame_stack().is_empty() => {
|
||||||
@ -206,6 +204,7 @@ impl Interpreter {
|
|||||||
loop {
|
loop {
|
||||||
let instruction = &body[context.position];
|
let instruction = &body[context.position];
|
||||||
|
|
||||||
|
debug!(target: "interpreter", "running {:?}", instruction);
|
||||||
match Interpreter::run_instruction(context, instruction)? {
|
match Interpreter::run_instruction(context, instruction)? {
|
||||||
InstructionOutcome::RunInstruction => (),
|
InstructionOutcome::RunInstruction => (),
|
||||||
InstructionOutcome::RunNextInstruction => context.position += 1,
|
InstructionOutcome::RunNextInstruction => context.position += 1,
|
||||||
|
@ -1,14 +1,16 @@
|
|||||||
///! Basic tests for instructions/constructions, missing in wabt tests
|
///! Basic tests for instructions/constructions, missing in wabt tests
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, Weak};
|
||||||
use builder::module;
|
use builder::module;
|
||||||
use elements::{ExportEntry, Internal, ImportEntry, External, GlobalEntry, GlobalType,
|
use elements::{ExportEntry, Internal, ImportEntry, External, GlobalEntry, GlobalType,
|
||||||
InitExpr, ValueType, Opcodes, Opcode};
|
InitExpr, ValueType, BlockType, Opcodes, Opcode, FunctionType};
|
||||||
use interpreter::Error;
|
use interpreter::Error;
|
||||||
use interpreter::env_native::{env_native_module, UserFunction, UserFunctions, UserFunctionExecutor};
|
use interpreter::env_native::{env_native_module, UserFunction, UserFunctions, UserFunctionExecutor, UserFunctionDescriptor};
|
||||||
|
use interpreter::imports::ModuleImports;
|
||||||
use interpreter::memory::MemoryInstance;
|
use interpreter::memory::MemoryInstance;
|
||||||
use interpreter::module::{ModuleInstanceInterface, CallerContext, ItemIndex, ExecutionParams};
|
use interpreter::module::{ModuleInstanceInterface, CallerContext, ItemIndex, ExecutionParams};
|
||||||
use interpreter::program::ProgramInstance;
|
use interpreter::program::ProgramInstance;
|
||||||
|
use interpreter::validator::{FunctionValidationContext, Validator};
|
||||||
use interpreter::value::RuntimeValue;
|
use interpreter::value::RuntimeValue;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -121,6 +123,25 @@ fn global_get_set() {
|
|||||||
assert_eq!(module.execute_index(2, vec![].into()).unwrap_err(), Error::Variable("trying to update variable of type I32 with value of type Some(I64)".into()));
|
assert_eq!(module.execute_index(2, vec![].into()).unwrap_err(), Error::Variable("trying to update variable of type I32 with value of type Some(I64)".into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SIGNATURE_I32: &'static [ValueType] = &[ValueType::I32];
|
||||||
|
|
||||||
|
const SIGNATURES: &'static [UserFunction] = &[
|
||||||
|
UserFunction {
|
||||||
|
desc: UserFunctionDescriptor::Static(
|
||||||
|
"add",
|
||||||
|
SIGNATURE_I32,
|
||||||
|
),
|
||||||
|
result: Some(ValueType::I32),
|
||||||
|
},
|
||||||
|
UserFunction {
|
||||||
|
desc: UserFunctionDescriptor::Static(
|
||||||
|
"sub",
|
||||||
|
SIGNATURE_I32,
|
||||||
|
),
|
||||||
|
result: Some(ValueType::I32),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn single_program_different_modules() {
|
fn single_program_different_modules() {
|
||||||
// user function executor
|
// user function executor
|
||||||
@ -168,15 +189,7 @@ fn single_program_different_modules() {
|
|||||||
{
|
{
|
||||||
let functions: UserFunctions = UserFunctions {
|
let functions: UserFunctions = UserFunctions {
|
||||||
executor: &mut executor,
|
executor: &mut executor,
|
||||||
functions: vec![UserFunction {
|
functions: ::std::borrow::Cow::from(SIGNATURES),
|
||||||
name: "add".into(),
|
|
||||||
params: vec![ValueType::I32],
|
|
||||||
result: Some(ValueType::I32),
|
|
||||||
}, UserFunction {
|
|
||||||
name: "sub".into(),
|
|
||||||
params: vec![ValueType::I32],
|
|
||||||
result: Some(ValueType::I32),
|
|
||||||
}],
|
|
||||||
};
|
};
|
||||||
let native_env_instance = Arc::new(env_native_module(env_instance, functions).unwrap());
|
let native_env_instance = Arc::new(env_native_module(env_instance, functions).unwrap());
|
||||||
let params = ExecutionParams::with_external("env".into(), native_env_instance);
|
let params = ExecutionParams::with_external("env".into(), native_env_instance);
|
||||||
@ -215,3 +228,26 @@ fn single_program_different_modules() {
|
|||||||
assert_eq!(executor.memory.get(0, 1).unwrap()[0], 42);
|
assert_eq!(executor.memory.get(0, 1).unwrap()[0], 42);
|
||||||
assert_eq!(executor.values, vec![7, 57, 42]);
|
assert_eq!(executor.values, vec![7, 57, 42]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_else_with_return_type_validation() {
|
||||||
|
let module = module().build();
|
||||||
|
let imports = ModuleImports::new(Weak::default(), None);
|
||||||
|
let mut context = FunctionValidationContext::new(&module, &imports, &[], 1024, 1024, &FunctionType::default());
|
||||||
|
|
||||||
|
Validator::validate_block(&mut context, false, BlockType::NoResult, &[
|
||||||
|
Opcode::I32Const(1),
|
||||||
|
Opcode::If(BlockType::NoResult, Opcodes::new(vec![
|
||||||
|
Opcode::I32Const(1),
|
||||||
|
Opcode::If(BlockType::Value(ValueType::I32), Opcodes::new(vec![
|
||||||
|
Opcode::I32Const(1),
|
||||||
|
Opcode::Else,
|
||||||
|
Opcode::I32Const(2),
|
||||||
|
Opcode::End,
|
||||||
|
])),
|
||||||
|
Opcode::Drop,
|
||||||
|
Opcode::End,
|
||||||
|
])),
|
||||||
|
Opcode::End,
|
||||||
|
], Opcode::End).unwrap();
|
||||||
|
}
|
||||||
|
@ -130,7 +130,7 @@ impl Validator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_instruction<'a>(context: &mut FunctionValidationContext, opcode: &'a Opcode) -> Result<InstructionOutcome<'a>, Error> {
|
pub fn validate_instruction<'a>(context: &mut FunctionValidationContext, opcode: &'a Opcode) -> Result<InstructionOutcome<'a>, Error> {
|
||||||
// println!("=== VALIDATING {:?}: {:?}", opcode, context.value_stack);
|
debug!(target: "validator", "validating {:?}", opcode);
|
||||||
match opcode {
|
match opcode {
|
||||||
&Opcode::Unreachable => Ok(InstructionOutcome::Unreachable),
|
&Opcode::Unreachable => Ok(InstructionOutcome::Unreachable),
|
||||||
&Opcode::Nop => Ok(InstructionOutcome::ValidateNextInstruction),
|
&Opcode::Nop => Ok(InstructionOutcome::ValidateNextInstruction),
|
||||||
@ -581,7 +581,14 @@ impl Validator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> FunctionValidationContext<'a> {
|
impl<'a> FunctionValidationContext<'a> {
|
||||||
pub fn new(module: &'a Module, imports: &'a ModuleImports, locals: &'a [ValueType], value_stack_limit: usize, frame_stack_limit: usize, function: &FunctionType) -> Self {
|
pub fn new(
|
||||||
|
module: &'a Module,
|
||||||
|
imports: &'a ModuleImports,
|
||||||
|
locals: &'a [ValueType],
|
||||||
|
value_stack_limit: usize,
|
||||||
|
frame_stack_limit: usize,
|
||||||
|
function: &FunctionType,
|
||||||
|
) -> Self {
|
||||||
FunctionValidationContext {
|
FunctionValidationContext {
|
||||||
module: module,
|
module: module,
|
||||||
imports: imports,
|
imports: imports,
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#![warn(missing_docs)]
|
#![warn(missing_docs)]
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
extern crate byteorder;
|
extern crate byteorder;
|
||||||
extern crate parking_lot;
|
extern crate parking_lot;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user