mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-07-01 07:21:39 +00:00
fix grumbles + added constructor logic
This commit is contained in:
@ -12,6 +12,9 @@ use std::path::PathBuf;
|
|||||||
use clap::{App, Arg};
|
use clap::{App, Arg};
|
||||||
use parity_wasm::elements;
|
use parity_wasm::elements;
|
||||||
|
|
||||||
|
use wasm_utils::CREATE_SYMBOL;
|
||||||
|
use wasm_utils::CALL_SYMBOL;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Io(io::Error),
|
Io(io::Error),
|
||||||
@ -62,7 +65,7 @@ pub fn process_output(target_dir: &str, bin_name: &str) -> Result<(), Error> {
|
|||||||
|
|
||||||
fn has_ctor(module: &elements::Module) -> bool {
|
fn has_ctor(module: &elements::Module) -> bool {
|
||||||
if let Some(ref section) = module.export_section() {
|
if let Some(ref section) = module.export_section() {
|
||||||
section.entries().iter().find(|e| "_create" == e.field()).is_some()
|
section.entries().iter().any(|e| CALL_SYMBOL == e.field())
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
@ -125,23 +128,23 @@ fn main() {
|
|||||||
let mut ctor_module = module.clone();
|
let mut ctor_module = module.clone();
|
||||||
|
|
||||||
if !matches.is_present("skip_optimization") {
|
if !matches.is_present("skip_optimization") {
|
||||||
wasm_utils::optimize(&mut module, vec!["_call", "setTempRet0"]).expect("Optimizer to finish without errors");
|
wasm_utils::optimize(&mut module, vec![CALL_SYMBOL, "setTempRet0"]).expect("Optimizer to finish without errors");
|
||||||
}
|
}
|
||||||
|
|
||||||
let raw_module = parity_wasm::serialize(module).expect("Failed to serialize module");
|
let raw_module = parity_wasm::serialize(module).expect("Failed to serialize module");
|
||||||
|
|
||||||
let mut file = fs::File::create(&path).expect("Failed to create file");;
|
// If module has an exported function with name=CREATE_SYMBOL
|
||||||
file.write_all(&raw_module).expect("Failed to write module to file");
|
// build will pack the module (raw_module) into this funciton and export as CALL_SYMBOL.
|
||||||
|
// Otherwise it will just save an optimised raw_module
|
||||||
// will pack into constructor
|
if !has_ctor(&ctor_module) {
|
||||||
if has_ctor(&ctor_module) {
|
|
||||||
if !matches.is_present("skip_optimization") {
|
if !matches.is_present("skip_optimization") {
|
||||||
wasm_utils::optimize(&mut ctor_module, vec!["_create", "setTempRet0"]).expect("Optimizer to finish without errors");
|
wasm_utils::optimize(&mut ctor_module, vec![CREATE_SYMBOL, "setTempRet0"]).expect("Optimizer to finish without errors");
|
||||||
}
|
}
|
||||||
wasm_utils::pack_instance(raw_module, &mut ctor_module);
|
wasm_utils::pack_instance(raw_module, &mut ctor_module);
|
||||||
|
parity_wasm::serialize_to_file(path, ctor_module).expect("Failed to serialize to file");
|
||||||
let ctor_path = wasm_path(target_dir, &format!("{}_ctor", wasm_binary));
|
} else {
|
||||||
parity_wasm::serialize_to_file(ctor_path, ctor_module);
|
let mut file = fs::File::create(&path).expect("Failed to create file");
|
||||||
|
file.write_all(&raw_module).expect("Failed to write module to file");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,9 @@ extern crate byteorder;
|
|||||||
#[macro_use] extern crate log;
|
#[macro_use] extern crate log;
|
||||||
#[macro_use] extern crate lazy_static;
|
#[macro_use] extern crate lazy_static;
|
||||||
|
|
||||||
|
pub static CREATE_SYMBOL: &'static str = "_create";
|
||||||
|
pub static CALL_SYMBOL: &'static str = "_call";
|
||||||
|
|
||||||
pub mod rules;
|
pub mod rules;
|
||||||
|
|
||||||
mod optimizer;
|
mod optimizer;
|
||||||
@ -22,3 +25,4 @@ pub use ext::externalize;
|
|||||||
pub use pack::pack_instance;
|
pub use pack::pack_instance;
|
||||||
pub use nondeterminism_check::is_deterministic;
|
pub use nondeterminism_check::is_deterministic;
|
||||||
pub use runtime_type::inject_runtime_type;
|
pub use runtime_type::inject_runtime_type;
|
||||||
|
|
||||||
|
36
src/pack.rs
36
src/pack.rs
@ -1,20 +1,23 @@
|
|||||||
use parity_wasm::{serialize,elements, builder, deserialize_buffer};
|
use parity_wasm::{serialize,elements, builder, deserialize_buffer};
|
||||||
use self::elements::{ External, Section, ResizableLimits, Opcode, DataSegment, InitExpr, Internal };
|
use self::elements::{ External, Section, ResizableLimits, Opcode, DataSegment, InitExpr, Internal };
|
||||||
|
|
||||||
/// TODO: desc
|
use super::CREATE_SYMBOL;
|
||||||
pub fn pack_instance(raw_module: Vec<u8>, ctor_module: &mut elements::Module) {
|
use super::CALL_SYMBOL;
|
||||||
let raw_len = raw_module.len();
|
|
||||||
let mem_required = (raw_len / (64 * 1024) + 1) as u32;
|
|
||||||
|
|
||||||
// Func
|
/// If module has an exported "_create" function we want to pack it into "constructor".
|
||||||
|
/// `raw_module` is the actual contract code
|
||||||
|
/// `ctor_module` is the constructor which should return `raw_module`
|
||||||
|
pub fn pack_instance(raw_module: Vec<u8>, ctor_module: &mut 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 create_func_id = {
|
||||||
let export_section = ctor_module.export_section().expect("No export section found");
|
let found_entry = ctor_module.export_section().expect("No export section found").entries().iter()
|
||||||
let found_entry = export_section.entries().iter()
|
.find(|entry| CREATE_SYMBOL == entry.field()).expect("No export with name _create found");
|
||||||
.find(|entry| "_create" == entry.field()).expect("No export with name _create found");
|
|
||||||
|
|
||||||
let function_index: usize = match found_entry.internal() {
|
let function_index: usize = match found_entry.internal() {
|
||||||
&Internal::Function(index) => index as usize,
|
&Internal::Function(index) => index as usize,
|
||||||
_ => panic!("_create export is not a function"),
|
_ => panic!("export is not a function"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let import_section_len: usize = match ctor_module.import_section() {
|
let import_section_len: usize = match ctor_module.import_section() {
|
||||||
@ -30,6 +33,7 @@ pub fn pack_instance(raw_module: Vec<u8>, ctor_module: &mut elements::Module) {
|
|||||||
function_index - import_section_len
|
function_index - import_section_len
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Code data address is an address where we put the contract's code (raw_module)
|
||||||
let mut code_data_address = 0i32;
|
let mut code_data_address = 0i32;
|
||||||
for section in ctor_module.sections_mut() {
|
for section in ctor_module.sections_mut() {
|
||||||
match section {
|
match section {
|
||||||
@ -38,14 +42,18 @@ pub fn pack_instance(raw_module: Vec<u8>, ctor_module: &mut elements::Module) {
|
|||||||
if let Opcode::I32Const(offst) = entry.offset().code()[0] {
|
if let Opcode::I32Const(offst) = entry.offset().code()[0] {
|
||||||
let len = entry.value().len() as i32;
|
let len = entry.value().len() as i32;
|
||||||
let offst = offst as i32;
|
let offst = offst as i32;
|
||||||
(entry.index(), offst + len + len % 32)
|
(entry.index(), offst + (len + 32) - len % 32)
|
||||||
} else {
|
} else {
|
||||||
(0, 0)
|
(0, 0)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
(0, 0)
|
(0, 0)
|
||||||
};
|
};
|
||||||
let code_data = DataSegment::new(index, InitExpr::new(vec![Opcode::I32Const(offset),Opcode::End]), raw_module.clone());
|
let code_data = DataSegment::new(
|
||||||
|
index,
|
||||||
|
InitExpr::new(vec![Opcode::I32Const(offset),Opcode::End]),
|
||||||
|
raw_module.clone()
|
||||||
|
);
|
||||||
data_section.entries_mut().push(code_data);
|
data_section.entries_mut().push(code_data);
|
||||||
code_data_address = offset;
|
code_data_address = offset;
|
||||||
},
|
},
|
||||||
@ -57,9 +65,9 @@ pub fn pack_instance(raw_module: Vec<u8>, ctor_module: &mut elements::Module) {
|
|||||||
match section {
|
match section {
|
||||||
&mut Section::Export(ref mut export_section) => {
|
&mut Section::Export(ref mut export_section) => {
|
||||||
for entry in export_section.entries_mut().iter_mut() {
|
for entry in export_section.entries_mut().iter_mut() {
|
||||||
if "_create" == entry.field() {
|
if CREATE_SYMBOL == entry.field() {
|
||||||
// change _create export name into default _call
|
// change _create export name into default _call
|
||||||
*entry.field_mut() = "_call".to_owned();
|
*entry.field_mut() = CALL_SYMBOL.to_owned();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -72,7 +80,7 @@ pub fn pack_instance(raw_module: Vec<u8>, ctor_module: &mut elements::Module) {
|
|||||||
Opcode::I32Const(code_data_address),
|
Opcode::I32Const(code_data_address),
|
||||||
Opcode::I32Store(0, 8),
|
Opcode::I32Store(0, 8),
|
||||||
Opcode::GetLocal(0),
|
Opcode::GetLocal(0),
|
||||||
Opcode::I32Const(raw_len as i32),
|
Opcode::I32Const(raw_module.len() as i32),
|
||||||
Opcode::I32Store(0, 12),
|
Opcode::I32Store(0, 12),
|
||||||
Opcode::End].iter().cloned());
|
Opcode::End].iter().cloned());
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user