mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-04-29 19:28:03 +00:00
Merge pull request #48 from paritytech/parity-wasm-update
Fix dependencies and deprecate (delete) test
This commit is contained in:
+1
-2
@@ -4,7 +4,7 @@ version = "0.1.0"
|
||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
parity-wasm = "0.18"
|
||||
parity-wasm = "0.23"
|
||||
log = "0.3"
|
||||
env_logger = "0.4"
|
||||
lazy_static = "0.2"
|
||||
@@ -14,7 +14,6 @@ byteorder = "1"
|
||||
|
||||
[dev-dependencies]
|
||||
tempdir = "0.3"
|
||||
pwasm-emscripten = { git = "https://github.com/paritytech/parity-wasm", path = "pwasm-emscripten" }
|
||||
|
||||
[lib]
|
||||
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||
glob = "0.2"
|
||||
wasm-utils = { path = "../" }
|
||||
clap = "2.24"
|
||||
parity-wasm = "0.18"
|
||||
parity-wasm = "0.23"
|
||||
|
||||
[dev-dependencies]
|
||||
tempdir = "0.3"
|
||||
|
||||
+1
-1
@@ -4,5 +4,5 @@ version = "0.1.0"
|
||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
parity-wasm = "0.18"
|
||||
parity-wasm = "0.23"
|
||||
wasm-utils = { path = "../" }
|
||||
+1
-1
@@ -4,5 +4,5 @@ version = "0.1.0"
|
||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
parity-wasm = "0.18"
|
||||
parity-wasm = "0.23"
|
||||
wasm-utils = { path = "../" }
|
||||
+1
-1
@@ -4,6 +4,6 @@ version = "0.1.0"
|
||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
parity-wasm = "0.18"
|
||||
parity-wasm = "0.23"
|
||||
wasm-utils = { path = "../" }
|
||||
clap = "2.24"
|
||||
|
||||
+20
-20
@@ -23,7 +23,7 @@ pub fn optimize(
|
||||
for (index, entry) in module.export_section().ok_or(Error::NoExportSection)?.entries().iter().enumerate() {
|
||||
if used_exports.iter().find(|e| **e == entry.field()).is_some() {
|
||||
stay.insert(Symbol::Export(index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// All symbols used in data/element segments are also should be preserved
|
||||
@@ -103,7 +103,7 @@ pub fn optimize(
|
||||
} else {
|
||||
remove = true;
|
||||
eliminated_globals.push(top_globals);
|
||||
trace!("Eliminated import({}) global({}, {})", old_index, top_globals, imports.entries()[index].field());
|
||||
trace!("Eliminated import({}) global({}, {})", old_index, top_globals, imports.entries()[index].field());
|
||||
}
|
||||
top_globals += 1;
|
||||
},
|
||||
@@ -190,16 +190,16 @@ pub fn optimize(
|
||||
&mut elements::Section::Function(ref mut function_section) if eliminated_types.len() > 0 => {
|
||||
for ref mut func_signature in function_section.entries_mut() {
|
||||
let totalle = eliminated_types.iter().take_while(|i| (**i as u32) < func_signature.type_ref()).count();
|
||||
*func_signature.type_ref_mut() -= totalle as u32;
|
||||
}
|
||||
*func_signature.type_ref_mut() -= totalle as u32;
|
||||
}
|
||||
},
|
||||
&mut elements::Section::Import(ref mut import_section) if eliminated_types.len() > 0 => {
|
||||
for ref mut import_entry in import_section.entries_mut() {
|
||||
if let &mut elements::External::Function(ref mut type_ref) = import_entry.external_mut() {
|
||||
if let &mut elements::External::Function(ref mut type_ref) = import_entry.external_mut() {
|
||||
let totalle = eliminated_types.iter().take_while(|i| (**i as u32) < *type_ref).count();
|
||||
*type_ref -= totalle as u32;
|
||||
}
|
||||
}
|
||||
*type_ref -= totalle as u32;
|
||||
}
|
||||
}
|
||||
},
|
||||
&mut elements::Section::Code(ref mut code_section) if eliminated_globals.len() > 0 || eliminated_funcs.len() > 0 => {
|
||||
for ref mut func_body in code_section.bodies_mut() {
|
||||
@@ -226,7 +226,7 @@ pub fn optimize(
|
||||
*global_index -= totalle as u32;
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
&mut elements::Section::Global(ref mut global_section) => {
|
||||
@@ -244,7 +244,7 @@ pub fn optimize(
|
||||
update_global_index(segment.offset_mut().code_mut(), &eliminated_globals);
|
||||
// update all indirect call addresses initial values
|
||||
for func_index in segment.members_mut() {
|
||||
let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < *func_index).count();
|
||||
let totalle = eliminated_funcs.iter().take_while(|i| (**i as u32) < *func_index).count();
|
||||
*func_index -= totalle as u32;
|
||||
}
|
||||
}
|
||||
@@ -384,7 +384,7 @@ mod tests {
|
||||
/// Optimizer presumes that export section exists and contains
|
||||
/// all symbols passed as a second parameter. Since empty module
|
||||
/// obviously contains no export section, optimizer should return
|
||||
/// error on it.
|
||||
/// error on it.
|
||||
#[test]
|
||||
fn empty() {
|
||||
let mut module = builder::module().build();
|
||||
@@ -431,7 +431,7 @@ mod tests {
|
||||
1,
|
||||
module.function_section().expect("functions section to be generated").entries().len(),
|
||||
"There should 2 (two) functions in the optimized module"
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
/// @spec 2
|
||||
@@ -466,14 +466,14 @@ mod tests {
|
||||
1,
|
||||
module.global_section().expect("global section to be generated").entries().len(),
|
||||
"There should 1 (one) global entry in the optimized module, since _call function uses it"
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
/// @spec 2
|
||||
/// Imagine there is one exported function in unoptimized module, `_call`, that we specify as the one
|
||||
/// to stay during the optimization. The code of this function uses one global during the execution,
|
||||
/// but we have a bunch of other unused globals in the code. Last globals should not survive the optimization,
|
||||
/// while the former should.
|
||||
/// while the former should.
|
||||
#[test]
|
||||
fn globals_2() {
|
||||
let mut module = builder::module()
|
||||
@@ -485,7 +485,7 @@ mod tests {
|
||||
.build()
|
||||
.global()
|
||||
.value_type().f32()
|
||||
.build()
|
||||
.build()
|
||||
.function()
|
||||
.signature().param().i32().build()
|
||||
.body()
|
||||
@@ -508,7 +508,7 @@ mod tests {
|
||||
1,
|
||||
module.global_section().expect("global section to be generated").entries().len(),
|
||||
"There should 1 (one) global entry in the optimized module, since _call function uses only one"
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
/// @spec 3
|
||||
@@ -571,13 +571,13 @@ mod tests {
|
||||
.build()
|
||||
.function()
|
||||
.signature().param().i32().param().i32().build()
|
||||
.build()
|
||||
.build()
|
||||
.function()
|
||||
.signature().param().i32().build()
|
||||
.body()
|
||||
.with_opcodes(elements::Opcodes::new(
|
||||
vec![
|
||||
elements::Opcode::CallIndirect(1, false),
|
||||
elements::Opcode::CallIndirect(1, 0),
|
||||
elements::Opcode::End
|
||||
]
|
||||
))
|
||||
@@ -598,10 +598,10 @@ mod tests {
|
||||
|
||||
let indirect_opcode = &module.code_section().expect("code section to be generated").bodies()[0].code().elements()[0];
|
||||
match *indirect_opcode {
|
||||
elements::Opcode::CallIndirect(0, false) => {},
|
||||
elements::Opcode::CallIndirect(0, 0) => {},
|
||||
_ => {
|
||||
panic!(
|
||||
"Expected call_indirect to use index 0 after optimization, since previois 0th was eliminated, but got {:?}",
|
||||
"Expected call_indirect to use index 0 after optimization, since previois 0th was eliminated, but got {:?}",
|
||||
indirect_opcode
|
||||
);
|
||||
}
|
||||
|
||||
-92
@@ -126,95 +126,3 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module) ->
|
||||
|
||||
Ok(new_module)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate pwasm_emscripten;
|
||||
extern crate parity_wasm;
|
||||
|
||||
use parity_wasm::builder;
|
||||
use parity_wasm::interpreter;
|
||||
use parity_wasm::interpreter::RuntimeValue;
|
||||
use parity_wasm::ModuleInstanceInterface;
|
||||
use super::*;
|
||||
use super::super::optimize;
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use self::pwasm_emscripten::program_with_emscripten_env;
|
||||
|
||||
#[test]
|
||||
fn call_returns_code() {
|
||||
let mut module = builder::module()
|
||||
.import()
|
||||
.module("env")
|
||||
.field("memory")
|
||||
.external().memory(1 as u32, Some(1 as u32))
|
||||
.build()
|
||||
.data()
|
||||
.offset(elements::Opcode::I32Const(16)).value(vec![0u8])
|
||||
.build()
|
||||
.function()
|
||||
.signature()
|
||||
.params().i32().i32().build()
|
||||
.build()
|
||||
.body().build()
|
||||
.build()
|
||||
.function()
|
||||
.signature().build()
|
||||
.body()
|
||||
.with_opcodes(elements::Opcodes::new(
|
||||
vec![
|
||||
elements::Opcode::End
|
||||
]
|
||||
))
|
||||
.build()
|
||||
.build()
|
||||
.function()
|
||||
.signature().param().i32().build()
|
||||
.body()
|
||||
.with_opcodes(elements::Opcodes::new(
|
||||
vec![
|
||||
elements::Opcode::End
|
||||
]
|
||||
))
|
||||
.build()
|
||||
.build()
|
||||
.export()
|
||||
.field(CALL_SYMBOL)
|
||||
.internal().func(1)
|
||||
.build()
|
||||
.export()
|
||||
.field(CREATE_SYMBOL)
|
||||
.internal().func(2)
|
||||
.build()
|
||||
.build();
|
||||
|
||||
let mut ctor_module = module.clone();
|
||||
optimize(&mut module, vec![CALL_SYMBOL]).expect("Optimizer to finish without errors");
|
||||
optimize(&mut ctor_module, vec![CREATE_SYMBOL]).expect("Optimizer to finish without errors");
|
||||
|
||||
let raw_module = parity_wasm::serialize(module).unwrap();
|
||||
let ctor_module = pack_instance(raw_module.clone(), ctor_module).expect("Packing failed");
|
||||
|
||||
let program = program_with_emscripten_env(Default::default()).expect("Wasm program to be created");
|
||||
let env_instance = program.module("env").expect("Wasm program to contain env module");
|
||||
let env_memory = env_instance.memory(interpreter::ItemIndex::Internal(0)).expect("Linear memory to exist in wasm runtime");
|
||||
|
||||
let constructor_module = program.add_module("contract", ctor_module, None).expect("Failed to initialize module");
|
||||
|
||||
let _ = constructor_module.execute_export(CALL_SYMBOL, vec![RuntimeValue::I32(1024)].into());
|
||||
|
||||
let pointer = LittleEndian::read_u32(&env_memory.get(1024 + 8, 4).unwrap());
|
||||
let len = LittleEndian::read_u32(&env_memory.get(1024 + 12, 4).unwrap());
|
||||
|
||||
let contract_code = env_memory.get(pointer, len as usize).expect("Failed to get code");
|
||||
|
||||
assert_eq!(raw_module, contract_code);
|
||||
|
||||
let contract_module: elements::Module = parity_wasm::deserialize_buffer(contract_code).expect("Constructed contract module is not valid");
|
||||
|
||||
let program = program_with_emscripten_env(Default::default()).expect("Wasm program to be created");
|
||||
let contract_module_instance = program.add_module("contract", contract_module, None).expect("Failed to initialize constructed contract module");
|
||||
|
||||
contract_module_instance.execute_export(CALL_SYMBOL, Default::default()).expect("Constructed contract failed to execute");
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -220,7 +220,7 @@ impl InstructionType {
|
||||
F64Max => InstructionType::Numeric,
|
||||
F64Copysign => InstructionType::Numeric,
|
||||
|
||||
I32WarpI64 => InstructionType::Conversion,
|
||||
I32WrapI64 => InstructionType::Conversion,
|
||||
I32TruncSF32 => InstructionType::Conversion,
|
||||
I32TruncUF32 => InstructionType::Conversion,
|
||||
I32TruncSF64 => InstructionType::Conversion,
|
||||
|
||||
Reference in New Issue
Block a user