mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-06-17 14:41:03 +00:00
Merge branch 'preserve-start' into unknown-lift
This commit is contained in:
+1
-2
@@ -4,7 +4,7 @@ version = "0.1.0"
|
|||||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
parity-wasm = "0.18"
|
parity-wasm = "0.23"
|
||||||
log = "0.3"
|
log = "0.3"
|
||||||
env_logger = "0.4"
|
env_logger = "0.4"
|
||||||
lazy_static = "0.2"
|
lazy_static = "0.2"
|
||||||
@@ -14,7 +14,6 @@ byteorder = "1"
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempdir = "0.3"
|
tempdir = "0.3"
|
||||||
pwasm-emscripten = { git = "https://github.com/paritytech/parity-wasm", path = "pwasm-emscripten" }
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
|
|
||||||
|
|||||||
Generated
+7
-7
@@ -123,17 +123,17 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parity-wasm"
|
name = "parity-wasm"
|
||||||
version = "0.18.0"
|
version = "0.23.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"byteorder 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.4.8"
|
version = "0.5.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
@@ -275,7 +275,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"clap 2.27.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clap 2.27.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-wasm 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-wasm 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"wasm-utils 0.1.0",
|
"wasm-utils 0.1.0",
|
||||||
]
|
]
|
||||||
@@ -290,7 +290,7 @@ dependencies = [
|
|||||||
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"lazy_static 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parity-wasm 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parity-wasm 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -321,8 +321,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
|
"checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b"
|
||||||
"checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
|
"checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
|
||||||
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
|
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
|
||||||
"checksum parity-wasm 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f02e35fda913b8873799b817dcab145d1f935a900722ab7274027949d9947a56"
|
"checksum parity-wasm 0.23.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b1ba4b1d4236b76694f6ab8d8d00cdbe1e37c6dd1b5c803d26721f27e097d4d9"
|
||||||
"checksum parking_lot 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "149d8f5b97f3c1133e3cfcd8886449959e856b557ff281e292b733d7c69e005e"
|
"checksum parking_lot 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3e7f7c9857874e54afeb950eebeae662b1e51a2493666d2ea4c0a5d91dcf0412"
|
||||||
"checksum parking_lot_core 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "4f610cb9664da38e417ea3225f23051f589851999535290e077939838ab7a595"
|
"checksum parking_lot_core 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "4f610cb9664da38e417ea3225f23051f589851999535290e077939838ab7a595"
|
||||||
"checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd"
|
"checksum rand 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6475140dfd8655aeb72e1fd4b7a1cc1c202be65d71669476e392fe62532b9edd"
|
||||||
"checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509"
|
"checksum redox_syscall 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "8dde11f18c108289bef24469638a04dce49da56084f2d50618b226e47eb04509"
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@ authors = ["NikVolf <nikvolf@gmail.com>"]
|
|||||||
glob = "0.2"
|
glob = "0.2"
|
||||||
wasm-utils = { path = "../" }
|
wasm-utils = { path = "../" }
|
||||||
clap = "2.24"
|
clap = "2.24"
|
||||||
parity-wasm = "0.18"
|
parity-wasm = "0.23"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tempdir = "0.3"
|
tempdir = "0.3"
|
||||||
|
|||||||
+1
-1
@@ -4,5 +4,5 @@ version = "0.1.0"
|
|||||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
parity-wasm = "0.18"
|
parity-wasm = "0.23"
|
||||||
wasm-utils = { path = "../" }
|
wasm-utils = { path = "../" }
|
||||||
+1
-1
@@ -4,5 +4,5 @@ version = "0.1.0"
|
|||||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
parity-wasm = "0.18"
|
parity-wasm = "0.23"
|
||||||
wasm-utils = { path = "../" }
|
wasm-utils = { path = "../" }
|
||||||
+1
-1
@@ -4,6 +4,6 @@ version = "0.1.0"
|
|||||||
authors = ["NikVolf <nikvolf@gmail.com>"]
|
authors = ["NikVolf <nikvolf@gmail.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
parity-wasm = "0.18"
|
parity-wasm = "0.23"
|
||||||
wasm-utils = { path = "../" }
|
wasm-utils = { path = "../" }
|
||||||
clap = "2.24"
|
clap = "2.24"
|
||||||
|
|||||||
+5
-2
@@ -26,6 +26,9 @@ pub fn optimize(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If there is start function in module, it should stary
|
||||||
|
module.start_section().map(|ss| stay.insert(Symbol::Function(ss as usize)));
|
||||||
|
|
||||||
// All symbols used in data/element segments are also should be preserved
|
// All symbols used in data/element segments are also should be preserved
|
||||||
let mut init_symbols = Vec::new();
|
let mut init_symbols = Vec::new();
|
||||||
if let Some(data_section) = module.data_section() {
|
if let Some(data_section) = module.data_section() {
|
||||||
@@ -577,7 +580,7 @@ mod tests {
|
|||||||
.body()
|
.body()
|
||||||
.with_opcodes(elements::Opcodes::new(
|
.with_opcodes(elements::Opcodes::new(
|
||||||
vec![
|
vec![
|
||||||
elements::Opcode::CallIndirect(1, false),
|
elements::Opcode::CallIndirect(1, 0),
|
||||||
elements::Opcode::End
|
elements::Opcode::End
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
@@ -598,7 +601,7 @@ mod tests {
|
|||||||
|
|
||||||
let indirect_opcode = &module.code_section().expect("code section to be generated").bodies()[0].code().elements()[0];
|
let indirect_opcode = &module.code_section().expect("code section to be generated").bodies()[0].code().elements()[0];
|
||||||
match *indirect_opcode {
|
match *indirect_opcode {
|
||||||
elements::Opcode::CallIndirect(0, false) => {},
|
elements::Opcode::CallIndirect(0, 0) => {},
|
||||||
_ => {
|
_ => {
|
||||||
panic!(
|
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 {:?}",
|
||||||
|
|||||||
-92
@@ -126,95 +126,3 @@ pub fn pack_instance(raw_module: Vec<u8>, mut ctor_module: elements::Module) ->
|
|||||||
|
|
||||||
Ok(new_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,
|
F64Max => InstructionType::Numeric,
|
||||||
F64Copysign => InstructionType::Numeric,
|
F64Copysign => InstructionType::Numeric,
|
||||||
|
|
||||||
I32WarpI64 => InstructionType::Conversion,
|
I32WrapI64 => InstructionType::Conversion,
|
||||||
I32TruncSF32 => InstructionType::Conversion,
|
I32TruncSF32 => InstructionType::Conversion,
|
||||||
I32TruncUF32 => InstructionType::Conversion,
|
I32TruncUF32 => InstructionType::Conversion,
|
||||||
I32TruncSF64 => InstructionType::Conversion,
|
I32TruncSF64 => InstructionType::Conversion,
|
||||||
|
|||||||
Reference in New Issue
Block a user