From 614cdbf13170ec5496991529c01b74decedd1b58 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Thu, 8 Feb 2018 00:49:52 +0300 Subject: [PATCH 1/3] refactor pack --- src/lib.rs | 1 + src/pack.rs | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5fcf8b5..015afaa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ extern crate byteorder; pub static CREATE_SYMBOL: &'static str = "deploy"; pub static CALL_SYMBOL: &'static str = "call"; +pub static RET_SYMBOL: &'static str = "ret"; pub mod rules; diff --git a/src/pack.rs b/src/pack.rs index 24a77a8..9905815 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -1,6 +1,6 @@ -use parity_wasm::elements::{self, Section, Opcode, DataSegment, InitExpr, Internal}; +use parity_wasm::elements::{self, Section, Opcode, DataSegment, InitExpr, Internal, External}; use parity_wasm::builder; -use super::{CREATE_SYMBOL, CALL_SYMBOL}; +use super::{CREATE_SYMBOL, CALL_SYMBOL, RET_SYMBOL}; /// Pack error. /// @@ -15,6 +15,8 @@ pub enum Error { InvalidCreateSignature, NoCreateSymbol, InvalidCreateMember, + NoRetImported, + NoImportSection, } /// If module has an exported "_create" function we want to pack it into "constructor". @@ -47,7 +49,8 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> let &elements::Type::Function(ref func) = ctor_module.type_section().ok_or(Error::NoTypeSection)? .types().get(type_id as usize).ok_or(Error::MalformedModule)?; - if func.params() != &[elements::ValueType::I32] { + // Deploy should have no arguments and also should return nothing + if !func.params().is_empty() { return Err(Error::InvalidCreateSignature); } if func.return_type().is_some() { @@ -57,6 +60,19 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> function_internal_index }; + let ret_function_id = { + let mut id = 0; + let mut found = false; + for entry in ctor_module.import_section().ok_or(Error::NoImportSection)?.entries().iter() { + if let External::Function(_) = *entry.external() { + if entry.field() == RET_SYMBOL { found = true; break; } + else { id += 1; } + } + } + if !found { return Err(Error::NoRetImported); } + else { id } + }; + // If new function is put in ctor module, it will have this callable index let last_function_index = ctor_module.function_section().map(|x| x.entries().len()).unwrap_or(0) + ctor_import_functions; @@ -93,17 +109,13 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> let mut new_module = builder::from_module(ctor_module) .function() - .signature().param().i32().build() + .signature().build() .body().with_opcodes(elements::Opcodes::new( vec![ - Opcode::GetLocal(0), Opcode::Call((create_func_id + ctor_import_functions) as u32), - Opcode::GetLocal(0), Opcode::I32Const(code_data_address), - Opcode::I32Store(0, 8), - Opcode::GetLocal(0), Opcode::I32Const(raw_module.len() as i32), - Opcode::I32Store(0, 12), + Opcode::Call(ret_function_id as u32), Opcode::End, ])).build() .build() From 7dd716b6bca042ce882108c0e94213c3ff026768 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Thu, 8 Feb 2018 01:17:39 +0300 Subject: [PATCH 2/3] finish with ret introduction --- src/pack.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/src/pack.rs b/src/pack.rs index 9905815..654dfba 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -1,6 +1,10 @@ -use parity_wasm::elements::{self, Section, Opcode, DataSegment, InitExpr, Internal, External}; +use parity_wasm::elements::{ + self, Section, Opcode, DataSegment, InitExpr, Internal, External, + ImportCountType, +}; use parity_wasm::builder; use super::{CREATE_SYMBOL, CALL_SYMBOL, RET_SYMBOL}; +use super::gas::update_call_index; /// Pack error. /// @@ -29,7 +33,7 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> // We need to find an internal ID of function witch is exported as "_create" // in order to find it in the Code section of the module - let create_func_id = { + let mut create_func_id = { let found_entry = ctor_module.export_section().ok_or(Error::NoExportSection)?.entries().iter() .find(|entry| CREATE_SYMBOL == entry.field()).ok_or(Error::NoCreateSymbol)?; @@ -69,13 +73,63 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> else { id += 1; } } } - if !found { return Err(Error::NoRetImported); } + if !found { + let mut mbuilder = builder::from_module(ctor_module); + let import_sig = mbuilder.push_signature( + builder::signature() + .param().i32().param().i32() + .build_sig() + ); + + mbuilder.push_import( + builder::import() + .module("env") + .field("ret") + .external().func(import_sig) + .build() + ); + + ctor_module = mbuilder.build(); + + let ret_func = ctor_module.import_count(ImportCountType::Function) as u32 - 1; + + for section in ctor_module.sections_mut() { + match section { + &mut elements::Section::Code(ref mut code_section) => { + for ref mut func_body in code_section.bodies_mut() { + update_call_index(func_body.code_mut(), ret_func); + } + }, + &mut elements::Section::Export(ref mut export_section) => { + for ref mut export in export_section.entries_mut() { + match export.internal_mut() { + &mut elements::Internal::Function(ref mut func_index) => { + if *func_index >= ret_func { *func_index += 1} + }, + _ => {} + } + } + }, + &mut elements::Section::Element(ref mut elements_section) => { + for ref mut segment in elements_section.entries_mut() { + // update all indirect call addresses initial values + for func_index in segment.members_mut() { + if *func_index >= ret_func { *func_index += 1} + } + } + }, + _ => { } + } + } + + create_func_id += 1; + ret_func + } else { id } }; // If new function is put in ctor module, it will have this callable index - let last_function_index = ctor_module.function_section().map(|x| x.entries().len()).unwrap_or(0) - + ctor_import_functions; + let last_function_index = ctor_module.functions_space(); // Code data address is an address where we put the contract's code (raw_module) let mut code_data_address = 0i32; From 68e03ef150e489daf7fee58c1a28a8cf6f631f20 Mon Sep 17 00:00:00 2001 From: NikVolf Date: Thu, 8 Feb 2018 14:45:57 +0300 Subject: [PATCH 3/3] match derefed --- src/pack.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pack.rs b/src/pack.rs index 654dfba..9fa66f8 100644 --- a/src/pack.rs +++ b/src/pack.rs @@ -94,13 +94,13 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> let ret_func = ctor_module.import_count(ImportCountType::Function) as u32 - 1; for section in ctor_module.sections_mut() { - match section { - &mut elements::Section::Code(ref mut code_section) => { + match *section { + elements::Section::Code(ref mut code_section) => { for ref mut func_body in code_section.bodies_mut() { update_call_index(func_body.code_mut(), ret_func); } }, - &mut elements::Section::Export(ref mut export_section) => { + elements::Section::Export(ref mut export_section) => { for ref mut export in export_section.entries_mut() { match export.internal_mut() { &mut elements::Internal::Function(ref mut func_index) => { @@ -110,7 +110,7 @@ pub fn pack_instance(raw_module: Vec, mut ctor_module: elements::Module) -> } } }, - &mut elements::Section::Element(ref mut elements_section) => { + elements::Section::Element(ref mut elements_section) => { for ref mut segment in elements_section.entries_mut() { // update all indirect call addresses initial values for func_index in segment.members_mut() {