mirror of
https://github.com/pezkuwichain/wasm-instrument.git
synced 2026-05-08 07:37:57 +00:00
Add rustfmt.toml from substrate repo (#161)
* Add rustfmt.toml from substrate repo * Apply rustfmt to code base * Fix formatting * Move rustfmt job to the top
This commit is contained in:
committed by
GitHub
parent
77ad07e347
commit
a0b548b37d
+58
-39
@@ -1,13 +1,12 @@
|
||||
//! Experimental build tool for cargo
|
||||
|
||||
use pwasm_utils::{build, BuildError, SourceTarget, TargetRuntime, logger};
|
||||
use pwasm_utils::{build, logger, BuildError, SourceTarget, TargetRuntime};
|
||||
|
||||
mod source;
|
||||
|
||||
use std::{fs, io};
|
||||
use std::path::PathBuf;
|
||||
use std::{fs, io, path::PathBuf};
|
||||
|
||||
use clap::{App, Arg, crate_version};
|
||||
use clap::{crate_version, App, Arg};
|
||||
use parity_wasm::elements;
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -25,9 +24,17 @@ impl std::fmt::Display for Error {
|
||||
match self {
|
||||
Io(io) => write!(f, "Generic i/o error: {}", io),
|
||||
FailedToCopy(msg) => write!(f, "{}. Have you tried to run \"cargo build\"?", msg),
|
||||
Decoding(err, file) => write!(f, "Decoding error ({}). Must be a valid wasm file {}. Pointed wrong file?", err, file),
|
||||
Encoding(err) => write!(f, "Encoding error ({}). Almost impossible to happen, no free disk space?", err),
|
||||
Build(err) => write!(f, "Build error: {}", err)
|
||||
Decoding(err, file) => write!(
|
||||
f,
|
||||
"Decoding error ({}). Must be a valid wasm file {}. Pointed wrong file?",
|
||||
err, file
|
||||
),
|
||||
Encoding(err) => write!(
|
||||
f,
|
||||
"Encoding error ({}). Almost impossible to happen, no free disk space?",
|
||||
err
|
||||
),
|
||||
Build(err) => write!(f, "Build error: {}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,21 +48,23 @@ pub fn wasm_path(input: &source::SourceInput) -> String {
|
||||
pub fn process_output(input: &source::SourceInput) -> Result<(), Error> {
|
||||
let mut cargo_path = PathBuf::from(input.target_dir());
|
||||
let wasm_name = input.bin_name().to_string().replace("-", "_");
|
||||
cargo_path.push(
|
||||
match input.target() {
|
||||
SourceTarget::Emscripten => source::EMSCRIPTEN_TRIPLET,
|
||||
SourceTarget::Unknown => source::UNKNOWN_TRIPLET,
|
||||
}
|
||||
);
|
||||
cargo_path.push(match input.target() {
|
||||
SourceTarget::Emscripten => source::EMSCRIPTEN_TRIPLET,
|
||||
SourceTarget::Unknown => source::UNKNOWN_TRIPLET,
|
||||
});
|
||||
cargo_path.push("release");
|
||||
cargo_path.push(format!("{}.wasm", wasm_name));
|
||||
|
||||
let mut target_path = PathBuf::from(input.target_dir());
|
||||
target_path.push(format!("{}.wasm", input.final_name()));
|
||||
fs::copy(cargo_path.as_path(), target_path.as_path())
|
||||
.map_err(|io| Error::FailedToCopy(
|
||||
format!("Failed to copy '{}' to '{}': {}", cargo_path.display(), target_path.display(), io)
|
||||
))?;
|
||||
fs::copy(cargo_path.as_path(), target_path.as_path()).map_err(|io| {
|
||||
Error::FailedToCopy(format!(
|
||||
"Failed to copy '{}' to '{}': {}",
|
||||
cargo_path.display(),
|
||||
target_path.display(),
|
||||
io
|
||||
))
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -121,13 +130,18 @@ fn do_main() -> Result<(), Error> {
|
||||
|
||||
let mut source_input = source::SourceInput::new(target_dir, wasm_binary);
|
||||
|
||||
let source_target_val = matches.value_of("source_target").unwrap_or_else(|| source::EMSCRIPTEN_TRIPLET);
|
||||
let source_target_val =
|
||||
matches.value_of("source_target").unwrap_or_else(|| source::EMSCRIPTEN_TRIPLET);
|
||||
if source_target_val == source::UNKNOWN_TRIPLET {
|
||||
source_input = source_input.unknown()
|
||||
} else if source_target_val == source::EMSCRIPTEN_TRIPLET {
|
||||
source_input = source_input.emscripten()
|
||||
} else {
|
||||
eprintln!("--target can be: '{}' or '{}'", source::EMSCRIPTEN_TRIPLET, source::UNKNOWN_TRIPLET);
|
||||
eprintln!(
|
||||
"--target can be: '{}' or '{}'",
|
||||
source::EMSCRIPTEN_TRIPLET,
|
||||
source::UNKNOWN_TRIPLET
|
||||
);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
|
||||
@@ -139,29 +153,34 @@ fn do_main() -> Result<(), Error> {
|
||||
|
||||
let path = wasm_path(&source_input);
|
||||
|
||||
let module = parity_wasm::deserialize_file(&path)
|
||||
.map_err(|e| Error::Decoding(e, path.to_string()))?;
|
||||
let module =
|
||||
parity_wasm::deserialize_file(&path).map_err(|e| Error::Decoding(e, path.to_string()))?;
|
||||
|
||||
let runtime_type_version = if let (Some(runtime_type), Some(runtime_version))
|
||||
= (matches.value_of("runtime_type"), matches.value_of("runtime_version")) {
|
||||
let runtime_type_version = if let (Some(runtime_type), Some(runtime_version)) =
|
||||
(matches.value_of("runtime_type"), matches.value_of("runtime_version"))
|
||||
{
|
||||
let mut ty: [u8; 4] = Default::default();
|
||||
let runtime_bytes = runtime_type.as_bytes();
|
||||
if runtime_bytes.len() != 4 {
|
||||
panic!("--runtime-type should be equal to 4 bytes");
|
||||
}
|
||||
ty.copy_from_slice(runtime_bytes);
|
||||
let version: u32 = runtime_version.parse()
|
||||
.expect("--runtime-version should be a positive integer");
|
||||
let version: u32 =
|
||||
runtime_version.parse().expect("--runtime-version should be a positive integer");
|
||||
Some((ty, version))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let public_api_entries: Vec<_> = matches.value_of("public_api")
|
||||
let public_api_entries: Vec<_> = matches
|
||||
.value_of("public_api")
|
||||
.map(|val| val.split(',').collect())
|
||||
.unwrap_or_default();
|
||||
|
||||
let target_runtime = match matches.value_of("target-runtime").expect("target-runtime has a default value; qed") {
|
||||
let target_runtime = match matches
|
||||
.value_of("target-runtime")
|
||||
.expect("target-runtime has a default value; qed")
|
||||
{
|
||||
"pwasm" => TargetRuntime::pwasm(),
|
||||
"substrate" => TargetRuntime::substrate(),
|
||||
_ => unreachable!("all possible values are enumerated in clap config; qed"),
|
||||
@@ -173,21 +192,22 @@ fn do_main() -> Result<(), Error> {
|
||||
runtime_type_version,
|
||||
&public_api_entries,
|
||||
matches.is_present("enforce_stack_adjustment"),
|
||||
matches.value_of("shrink_stack").unwrap_or_else(|| "49152").parse()
|
||||
matches
|
||||
.value_of("shrink_stack")
|
||||
.unwrap_or_else(|| "49152")
|
||||
.parse()
|
||||
.expect("New stack size is not valid u32"),
|
||||
matches.is_present("skip_optimization"),
|
||||
&target_runtime,
|
||||
).map_err(Error::Build)?;
|
||||
)
|
||||
.map_err(Error::Build)?;
|
||||
|
||||
if let Some(save_raw_path) = matches.value_of("save_raw") {
|
||||
parity_wasm::serialize_to_file(save_raw_path, module.clone()).map_err(Error::Encoding)?;
|
||||
}
|
||||
|
||||
if let Some(ctor_module) = ctor_module {
|
||||
parity_wasm::serialize_to_file(
|
||||
&path,
|
||||
ctor_module,
|
||||
).map_err(Error::Encoding)?;
|
||||
parity_wasm::serialize_to_file(&path, ctor_module).map_err(Error::Encoding)?;
|
||||
} else {
|
||||
parity_wasm::serialize_to_file(&path, module).map_err(Error::Encoding)?;
|
||||
}
|
||||
@@ -204,11 +224,10 @@ fn main() {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use tempdir::TempDir;
|
||||
use std::fs;
|
||||
use tempdir::TempDir;
|
||||
|
||||
use super::process_output;
|
||||
use super::source::SourceInput;
|
||||
use super::{process_output, source::SourceInput};
|
||||
|
||||
#[test]
|
||||
fn processes_cargo_output() {
|
||||
@@ -230,8 +249,8 @@ mod tests {
|
||||
|
||||
process_output(&input).expect("process output failed");
|
||||
|
||||
assert!(
|
||||
fs::metadata(tmp_dir.path().join("example-wasm.wasm")).expect("metadata failed").is_file()
|
||||
)
|
||||
assert!(fs::metadata(tmp_dir.path().join("example-wasm.wasm"))
|
||||
.expect("metadata failed")
|
||||
.is_file())
|
||||
}
|
||||
}
|
||||
|
||||
+1
-6
@@ -16,12 +16,7 @@ pub struct SourceInput<'a> {
|
||||
|
||||
impl<'a> SourceInput<'a> {
|
||||
pub fn new<'b>(target_dir: &'b str, bin_name: &'b str) -> SourceInput<'b> {
|
||||
SourceInput {
|
||||
target_dir,
|
||||
bin_name,
|
||||
final_name: bin_name,
|
||||
target: SourceTarget::Emscripten,
|
||||
}
|
||||
SourceInput { target_dir, bin_name, final_name: bin_name, target: SourceTarget::Emscripten }
|
||||
}
|
||||
|
||||
pub fn unknown(mut self) -> Self {
|
||||
|
||||
+15
-15
@@ -1,6 +1,6 @@
|
||||
use pwasm_utils::logger;
|
||||
use clap::{App, Arg};
|
||||
use parity_wasm::elements;
|
||||
use pwasm_utils::logger;
|
||||
|
||||
fn fail(msg: &str) -> ! {
|
||||
eprintln!("{}", msg);
|
||||
@@ -32,22 +32,20 @@ const ALLOWED_IMPORTS: &[&str] = &[
|
||||
"suicide",
|
||||
"panic",
|
||||
"elog",
|
||||
"abort"
|
||||
"abort",
|
||||
];
|
||||
|
||||
fn main() {
|
||||
logger::init();
|
||||
|
||||
let matches = App::new("wasm-check")
|
||||
.arg(Arg::with_name("input")
|
||||
.index(1)
|
||||
.required(true)
|
||||
.help("Input WASM file"))
|
||||
.get_matches();
|
||||
.arg(Arg::with_name("input").index(1).required(true).help("Input WASM file"))
|
||||
.get_matches();
|
||||
|
||||
let input = matches.value_of("input").expect("is required; qed");
|
||||
|
||||
let module = parity_wasm::deserialize_file(&input).expect("Input module deserialization failed");
|
||||
let module =
|
||||
parity_wasm::deserialize_file(&input).expect("Input module deserialization failed");
|
||||
|
||||
for section in module.sections() {
|
||||
match section {
|
||||
@@ -60,7 +58,10 @@ fn main() {
|
||||
match entry.external() {
|
||||
elements::External::Function(_) => {
|
||||
if !ALLOWED_IMPORTS.contains(&entry.field()) {
|
||||
fail(&format!("'{}' is not supported by the runtime", entry.field()));
|
||||
fail(&format!(
|
||||
"'{}' is not supported by the runtime",
|
||||
entry.field()
|
||||
));
|
||||
}
|
||||
},
|
||||
elements::External::Memory(m) => {
|
||||
@@ -81,18 +82,17 @@ fn main() {
|
||||
));
|
||||
}
|
||||
},
|
||||
elements::External::Global(_) => {
|
||||
fail("Parity runtime does not provide any globals")
|
||||
},
|
||||
_ => { continue; }
|
||||
elements::External::Global(_) =>
|
||||
fail("Parity runtime does not provide any globals"),
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
if !has_imported_memory_properly_named {
|
||||
fail("No imported memory from env::memory in the contract");
|
||||
}
|
||||
}
|
||||
_ => { continue; }
|
||||
},
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ fn main() {
|
||||
let args = std::env::args().collect::<Vec<_>>();
|
||||
if args.len() != 3 {
|
||||
println!("Usage: {} input_file.wasm output_file.wasm", args[0]);
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
let module = pwasm_utils::externalize(
|
||||
|
||||
+5
-5
@@ -7,15 +7,15 @@ fn main() {
|
||||
let args = env::args().collect::<Vec<_>>();
|
||||
if args.len() != 3 {
|
||||
println!("Usage: {} input_file.wasm output_file.wasm", args[0]);
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
// Loading module
|
||||
let module = parity_wasm::deserialize_file(&args[1]).expect("Module deserialization to succeed");
|
||||
let module =
|
||||
parity_wasm::deserialize_file(&args[1]).expect("Module deserialization to succeed");
|
||||
|
||||
let result = utils::inject_gas_counter(
|
||||
module, &utils::rules::Set::default(), "env"
|
||||
).expect("Failed to inject gas. Some forbidden opcodes?");
|
||||
let result = utils::inject_gas_counter(module, &utils::rules::Set::default(), "env")
|
||||
.expect("Failed to inject gas. Some forbidden opcodes?");
|
||||
|
||||
parity_wasm::serialize_to_file(&args[2], result).expect("Module serialization to succeed")
|
||||
}
|
||||
|
||||
+10
-12
@@ -1,5 +1,5 @@
|
||||
use pwasm_utils::{self as utils, logger};
|
||||
use clap::{App, Arg};
|
||||
use pwasm_utils::{self as utils, logger};
|
||||
|
||||
fn main() {
|
||||
logger::init();
|
||||
@@ -7,27 +7,25 @@ fn main() {
|
||||
let target_runtime = utils::TargetRuntime::pwasm();
|
||||
|
||||
let matches = App::new("wasm-pack")
|
||||
.arg(Arg::with_name("input")
|
||||
.index(1)
|
||||
.required(true)
|
||||
.help("Input WASM file"))
|
||||
.arg(Arg::with_name("output")
|
||||
.index(2)
|
||||
.required(true)
|
||||
.help("Output WASM file"))
|
||||
.arg(Arg::with_name("input").index(1).required(true).help("Input WASM file"))
|
||||
.arg(Arg::with_name("output").index(2).required(true).help("Output WASM file"))
|
||||
.get_matches();
|
||||
|
||||
let input = matches.value_of("input").expect("is required; qed");
|
||||
let output = matches.value_of("output").expect("is required; qed");
|
||||
|
||||
let module = parity_wasm::deserialize_file(&input).expect("Input module deserialization failed");
|
||||
let module =
|
||||
parity_wasm::deserialize_file(&input).expect("Input module deserialization failed");
|
||||
let ctor_module = module.clone();
|
||||
let raw_module = parity_wasm::serialize(module).expect("Serialization failed");
|
||||
|
||||
// Invoke packer
|
||||
let mut result_module = utils::pack_instance(raw_module, ctor_module, &utils::TargetRuntime::pwasm()).expect("Packing failed");
|
||||
let mut result_module =
|
||||
utils::pack_instance(raw_module, ctor_module, &utils::TargetRuntime::pwasm())
|
||||
.expect("Packing failed");
|
||||
// Optimize constructor, since it does not need everything
|
||||
utils::optimize(&mut result_module, vec![target_runtime.symbols().call]).expect("Optimization failed");
|
||||
utils::optimize(&mut result_module, vec![target_runtime.symbols().call])
|
||||
.expect("Optimization failed");
|
||||
|
||||
parity_wasm::serialize_to_file(&output, result_module).expect("Serialization failed");
|
||||
}
|
||||
|
||||
+18
-19
@@ -1,5 +1,5 @@
|
||||
use pwasm_utils::{self as utils, logger};
|
||||
use clap::{App, Arg};
|
||||
use pwasm_utils::{self as utils, logger};
|
||||
|
||||
fn main() {
|
||||
logger::init();
|
||||
@@ -7,27 +7,26 @@ fn main() {
|
||||
let target_runtime = utils::TargetRuntime::pwasm();
|
||||
|
||||
let matches = App::new("wasm-prune")
|
||||
.arg(Arg::with_name("input")
|
||||
.index(1)
|
||||
.required(true)
|
||||
.help("Input WASM file"))
|
||||
.arg(Arg::with_name("output")
|
||||
.index(2)
|
||||
.required(true)
|
||||
.help("Output WASM file"))
|
||||
.arg(Arg::with_name("exports")
|
||||
.long("exports")
|
||||
.short("e")
|
||||
.takes_value(true)
|
||||
.value_name("functions")
|
||||
.help(&format!("Comma-separated list of exported functions to keep. Default: '{}'", target_runtime.symbols().call)))
|
||||
.arg(Arg::with_name("input").index(1).required(true).help("Input WASM file"))
|
||||
.arg(Arg::with_name("output").index(2).required(true).help("Output WASM file"))
|
||||
.arg(
|
||||
Arg::with_name("exports")
|
||||
.long("exports")
|
||||
.short("e")
|
||||
.takes_value(true)
|
||||
.value_name("functions")
|
||||
.help(&format!(
|
||||
"Comma-separated list of exported functions to keep. Default: '{}'",
|
||||
target_runtime.symbols().call
|
||||
)),
|
||||
)
|
||||
.get_matches();
|
||||
|
||||
let exports = matches
|
||||
.value_of("exports")
|
||||
.unwrap_or(target_runtime.symbols().call)
|
||||
.split(',')
|
||||
.collect();
|
||||
.value_of("exports")
|
||||
.unwrap_or(target_runtime.symbols().call)
|
||||
.split(',')
|
||||
.collect();
|
||||
|
||||
let input = matches.value_of("input").expect("is required; qed");
|
||||
let output = matches.value_of("output").expect("is required; qed");
|
||||
|
||||
@@ -7,18 +7,18 @@ fn main() {
|
||||
let args = env::args().collect::<Vec<_>>();
|
||||
if args.len() != 3 {
|
||||
println!("Usage: {} input_file.wasm output_file.wasm", args[0]);
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
let input_file = &args[1];
|
||||
let output_file = &args[2];
|
||||
|
||||
// Loading module
|
||||
let module = parity_wasm::deserialize_file(&input_file).expect("Module deserialization to succeed");
|
||||
let module =
|
||||
parity_wasm::deserialize_file(&input_file).expect("Module deserialization to succeed");
|
||||
|
||||
let result = stack_height::inject_limiter(
|
||||
module, 1024
|
||||
).expect("Failed to inject stack height counter");
|
||||
let result =
|
||||
stack_height::inject_limiter(module, 1024).expect("Failed to inject stack height counter");
|
||||
|
||||
parity_wasm::serialize_to_file(&output_file, result).expect("Module serialization to succeed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user