refactor preparation

This commit is contained in:
vms 2020-04-22 16:33:19 +03:00
parent 8ad42ba83f
commit e1e873b2fc
5 changed files with 8 additions and 26 deletions

View File

@ -14,7 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
/*
#![deny( #![deny(
nonstandard_style, nonstandard_style,
unused_imports, unused_imports,
@ -23,14 +22,12 @@
unused_unsafe, unused_unsafe,
unreachable_patterns unreachable_patterns
)] )]
*/
/// Command-line tool intended to test Frank VM. /// Command-line tool intended to test Frank VM.
mod vm; mod vm;
use crate::vm::config::Config; use crate::vm::config::Config;
use crate::vm::frank::Frank; use crate::vm::frank::Frank;
use crate::vm::prepare::prepare_module;
use crate::vm::service::FluenceService; use crate::vm::service::FluenceService;
use clap::{App, AppSettings, Arg, SubCommand}; use clap::{App, AppSettings, Arg, SubCommand};
@ -79,7 +76,6 @@ fn main() -> Result<(), ExitFailure> {
let config = Config::default(); let config = Config::default();
let in_module_path = arg.value_of(IN_MODULE_PATH).unwrap(); let in_module_path = arg.value_of(IN_MODULE_PATH).unwrap();
let wasm_code = fs::read(in_module_path)?; let wasm_code = fs::read(in_module_path)?;
let wasm_code = prepare_module(&wasm_code, &config)?;
let invoke_arg = arg.value_of(INVOKE_ARG).unwrap(); let invoke_arg = arg.value_of(INVOKE_ARG).unwrap();
let mut frank = Frank::new(&wasm_code, config)?; let mut frank = Frank::new(&wasm_code, config)?;

View File

@ -46,9 +46,9 @@ impl Default for WASIConfig {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Config { pub struct Config {
/// Count of Wasm memory pages that will be preallocated on the VM startup. /// Maximum number of Wasm memory pages that loaded module can use.
/// Each Wasm pages is 65536 bytes long. /// Each Wasm pages is 65536 bytes long.
pub mem_pages_count: i32, pub mem_pages_count: u32,
/// If true, registers the logger Wasm module with name 'logger'. /// If true, registers the logger Wasm module with name 'logger'.
/// This functionality is just for debugging, and this module will be disabled in future. /// This functionality is just for debugging, and this module will be disabled in future.

View File

@ -15,21 +15,13 @@
*/ */
use wasmer_runtime::error::{ use wasmer_runtime::error::{
CallError, CallError, CompileError, CreationError, Error as WasmerError, ResolveError, RuntimeError,
CompileError,
CreationError,
Error as WasmerError,
ResolveError,
RuntimeError,
}; };
use std::error::Error; use std::error::Error;
#[derive(Debug)] #[derive(Debug)]
pub enum FrankError { pub enum FrankError {
/// Errors related to the preparation (instrumentation and so on) and compilation by Wasmer steps.
InstantiationError(String),
/// Errors for I/O errors raising while opening a file. /// Errors for I/O errors raising while opening a file.
IOError(String), IOError(String),
@ -54,7 +46,6 @@ impl Error for FrankError {}
impl std::fmt::Display for FrankError { impl std::fmt::Display for FrankError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
match self { match self {
FrankError::InstantiationError(msg) => write!(f, "InstantiationError: {}", msg),
FrankError::IOError(msg) => write!(f, "IOError: {}", msg), FrankError::IOError(msg) => write!(f, "IOError: {}", msg),
FrankError::WasmerResolveError(msg) => write!(f, "WasmerResolveError: {}", msg), FrankError::WasmerResolveError(msg) => write!(f, "WasmerResolveError: {}", msg),
FrankError::WasmerInvokeError(msg) => write!(f, "WasmerInvokeError: {}", msg), FrankError::WasmerInvokeError(msg) => write!(f, "WasmerInvokeError: {}", msg),

View File

@ -15,10 +15,7 @@
*/ */
use crate::vm::{ use crate::vm::{
config::Config, config::Config, errors::FrankError, frank_result::FrankResult, prepare::prepare_module,
errors::FrankError,
frank_result::FrankResult,
prepare::prepare_module,
service::FluenceService, service::FluenceService,
}; };
@ -88,7 +85,7 @@ impl Frank {
/// Creates a new virtual machine executor. /// Creates a new virtual machine executor.
pub fn new(wasm_bytes: &[u8], config: Config) -> Result<Self, FrankError> { pub fn new(wasm_bytes: &[u8], config: Config) -> Result<Self, FrankError> {
let prepared_wasm_bytes = prepare_module(wasm_bytes, &config)?; let prepared_wasm_bytes = prepare_module(wasm_bytes, config.mem_pages_count)?;
let logger_imports = imports! { let logger_imports = imports! {
"logger" => { "logger" => {

View File

@ -18,13 +18,11 @@
// https://github.com/paritytech/substrate/blob/master/srml/contracts/src/wasm/prepare.rs // https://github.com/paritytech/substrate/blob/master/srml/contracts/src/wasm/prepare.rs
// https://github.com/nearprotocol/nearcore/blob/master/runtime/near-vm-runner/src/prepare.rs // https://github.com/nearprotocol/nearcore/blob/master/runtime/near-vm-runner/src/prepare.rs
use crate::vm::config::Config;
use crate::vm::errors::FrankError; use crate::vm::errors::FrankError;
use parity_wasm::{ use parity_wasm::{
builder, elements,
elements::{MemorySection, MemoryType}, elements::{MemorySection, MemoryType},
builder,
elements,
}; };
struct ModulePreparator { struct ModulePreparator {
@ -75,8 +73,8 @@ impl<'a> ModulePreparator {
/// Prepares a Wasm module: /// Prepares a Wasm module:
/// - set memory page count /// - set memory page count
pub fn prepare_module(module: &[u8], config: &Config) -> Result<Vec<u8>, FrankError> { pub fn prepare_module(module: &[u8], mem_pages_count: u32) -> Result<Vec<u8>, FrankError> {
ModulePreparator::init(module)? ModulePreparator::init(module)?
.set_mem_pages_count(config.mem_pages_count as _) .set_mem_pages_count(mem_pages_count)
.into_wasm() .into_wasm()
} }