mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-27 05:32:07 +00:00
Merge branch 'master' into tests_finish
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use std::borrow::Cow;
|
||||
use parking_lot::RwLock;
|
||||
use elements::{FunctionType, Internal, ValueType};
|
||||
use interpreter::Error;
|
||||
@ -19,20 +20,67 @@ pub trait UserFunctionExecutor {
|
||||
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.
|
||||
#[derive(Clone)]
|
||||
pub struct UserFunction {
|
||||
/// User function name.
|
||||
pub name: String,
|
||||
/// User function parameters (for signature matching).
|
||||
pub params: Vec<ValueType>,
|
||||
/// User function return type (for signature matching).
|
||||
/// Descriptor with variable-length definitions
|
||||
pub desc: UserFunctionDescriptor,
|
||||
/// Return type of the signature
|
||||
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
|
||||
pub struct UserFunctions<'a> {
|
||||
/// Functions list.
|
||||
pub functions: Vec<UserFunction>,
|
||||
pub functions: Cow<'static, [UserFunction]>,
|
||||
/// Functions executor.
|
||||
pub executor: &'a mut UserFunctionExecutor,
|
||||
}
|
||||
@ -46,7 +94,7 @@ pub struct NativeModuleInstance<'a> {
|
||||
/// By-name functions index.
|
||||
by_name: HashMap<String, u32>,
|
||||
/// User functions list.
|
||||
functions: Vec<UserFunction>,
|
||||
functions: Cow<'static, [UserFunction]>,
|
||||
}
|
||||
|
||||
impl<'a> NativeModuleInstance<'a> {
|
||||
@ -55,7 +103,7 @@ impl<'a> NativeModuleInstance<'a> {
|
||||
Ok(NativeModuleInstance {
|
||||
env: env,
|
||||
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,
|
||||
})
|
||||
}
|
||||
@ -108,7 +156,7 @@ impl<'a> ModuleInstanceInterface for NativeModuleInstance<'a> {
|
||||
self.functions
|
||||
.get((index - NATIVE_INDEX_FUNC_MIN) as usize)
|
||||
.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> {
|
||||
@ -135,7 +183,7 @@ impl<'a> ModuleInstanceInterface for NativeModuleInstance<'a> {
|
||||
self.functions
|
||||
.get((index - NATIVE_INDEX_FUNC_MIN) as usize)
|
||||
.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,
|
||||
}
|
||||
|
||||
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 {
|
||||
/// Create new linear memory instance.
|
||||
pub fn new(memory_type: &MemoryType) -> Result<Arc<Self>, Error> {
|
||||
@ -48,36 +64,18 @@ impl MemoryInstance {
|
||||
|
||||
/// Get data at given offset.
|
||||
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();
|
||||
if buffer.len() < end {
|
||||
return Err(Error::Memory(format!("trying to read region [{}..{}] in memory [0..{}]", begin, end, buffer.len())));
|
||||
}
|
||||
let region = self.checked_region(&buffer, offset as usize, size)?;
|
||||
|
||||
Ok(buffer[begin..end].to_vec())
|
||||
Ok(region.slice().to_vec())
|
||||
}
|
||||
|
||||
/// Set data at given offset.
|
||||
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();
|
||||
if buffer.len() < end {
|
||||
return Err(Error::Memory(format!("trying to update region [{}..{}] in memory [0..{}]", begin, end, buffer.len())));
|
||||
}
|
||||
let range = self.checked_region(&buffer, offset as usize, value.len())?.range();
|
||||
|
||||
let mut mut_buffer = buffer.as_mut_slice();
|
||||
mut_buffer[begin..end].copy_from_slice(value);
|
||||
buffer[range].copy_from_slice(value);
|
||||
|
||||
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::value::RuntimeValue;
|
||||
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;
|
@ -41,7 +41,7 @@ pub enum ExportEntryType {
|
||||
|
||||
/// Module instance API.
|
||||
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>;
|
||||
/// Execute function with the given index.
|
||||
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 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)));
|
||||
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);
|
||||
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 block_body = Interpreter::into_block(instruction, top_frame.frame_type)?;
|
||||
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::End if !function_context.frame_stack().is_empty() => {
|
||||
@ -206,6 +204,7 @@ impl Interpreter {
|
||||
loop {
|
||||
let instruction = &body[context.position];
|
||||
|
||||
debug!(target: "interpreter", "running {:?}", instruction);
|
||||
match Interpreter::run_instruction(context, instruction)? {
|
||||
InstructionOutcome::RunInstruction => (),
|
||||
InstructionOutcome::RunNextInstruction => context.position += 1,
|
||||
|
@ -1,14 +1,16 @@
|
||||
///! Basic tests for instructions/constructions, missing in wabt tests
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, Weak};
|
||||
use builder::module;
|
||||
use elements::{ExportEntry, Internal, ImportEntry, External, GlobalEntry, GlobalType,
|
||||
InitExpr, ValueType, Opcodes, Opcode};
|
||||
InitExpr, ValueType, BlockType, Opcodes, Opcode, FunctionType};
|
||||
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::module::{ModuleInstanceInterface, CallerContext, ItemIndex, ExecutionParams};
|
||||
use interpreter::program::ProgramInstance;
|
||||
use interpreter::validator::{FunctionValidationContext, Validator};
|
||||
use interpreter::value::RuntimeValue;
|
||||
|
||||
#[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()));
|
||||
}
|
||||
|
||||
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]
|
||||
fn single_program_different_modules() {
|
||||
// user function executor
|
||||
@ -168,15 +189,7 @@ fn single_program_different_modules() {
|
||||
{
|
||||
let functions: UserFunctions = UserFunctions {
|
||||
executor: &mut executor,
|
||||
functions: vec![UserFunction {
|
||||
name: "add".into(),
|
||||
params: vec![ValueType::I32],
|
||||
result: Some(ValueType::I32),
|
||||
}, UserFunction {
|
||||
name: "sub".into(),
|
||||
params: vec![ValueType::I32],
|
||||
result: Some(ValueType::I32),
|
||||
}],
|
||||
functions: ::std::borrow::Cow::from(SIGNATURES),
|
||||
};
|
||||
let native_env_instance = Arc::new(env_native_module(env_instance, functions).unwrap());
|
||||
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.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> {
|
||||
// println!("=== VALIDATING {:?}: {:?}", opcode, context.value_stack);
|
||||
debug!(target: "validator", "validating {:?}", opcode);
|
||||
match opcode {
|
||||
&Opcode::Unreachable => Ok(InstructionOutcome::Unreachable),
|
||||
&Opcode::Nop => Ok(InstructionOutcome::ValidateNextInstruction),
|
||||
@ -581,7 +581,14 @@ impl Validator {
|
||||
}
|
||||
|
||||
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 {
|
||||
module: module,
|
||||
imports: imports,
|
||||
|
Reference in New Issue
Block a user