From 5d7fdec0ee8d397e0628cb7aba0aff1e2b8a2380 Mon Sep 17 00:00:00 2001 From: vms Date: Thu, 4 Jun 2020 20:36:07 +0300 Subject: [PATCH] add delete subcommand to wit_embedder --- .../src/wit_parser/deleter.rs | 53 +++++++++++ .../src/wit_parser/embedder.rs | 18 ++-- .../fce_wit_interfaces/src/wit_parser/mod.rs | 4 +- fce/src/vm/instance/wit_instance.rs | 2 +- tools/wit_embedder/src/args.rs | 56 +++++++++++ tools/wit_embedder/src/main.rs | 93 +++++++------------ 6 files changed, 155 insertions(+), 71 deletions(-) create mode 100644 crates/fce_wit_interfaces/src/wit_parser/deleter.rs create mode 100644 tools/wit_embedder/src/args.rs diff --git a/crates/fce_wit_interfaces/src/wit_parser/deleter.rs b/crates/fce_wit_interfaces/src/wit_parser/deleter.rs new file mode 100644 index 00000000..7b928581 --- /dev/null +++ b/crates/fce_wit_interfaces/src/wit_parser/deleter.rs @@ -0,0 +1,53 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use super::errors::WITParserError; +use super::custom::WIT_SECTION_NAME; + +use walrus::ModuleConfig; + +use std::path::PathBuf; + +pub fn delete_wit_section( + in_wasm_path: PathBuf, + out_wasm_path: PathBuf, +) -> Result<(), WITParserError> { + let mut module = ModuleConfig::new() + .parse_file(&in_wasm_path) + .map_err(WITParserError::CorruptedWasmFile)?; + + let wit_section_ids = module + .customs + .iter() + .filter_map(|(id, section)| { + if section.name() == WIT_SECTION_NAME { + Some(id) + } else { + None + } + }) + .collect::>(); + + for id in wit_section_ids { + module.customs.delete(id); + } + + module + .emit_wasm_file(&out_wasm_path) + .map_err(WITParserError::WasmEmitError)?; + + Ok(()) +} diff --git a/crates/fce_wit_interfaces/src/wit_parser/embedder.rs b/crates/fce_wit_interfaces/src/wit_parser/embedder.rs index 556fe99d..bf1e29e4 100644 --- a/crates/fce_wit_interfaces/src/wit_parser/embedder.rs +++ b/crates/fce_wit_interfaces/src/wit_parser/embedder.rs @@ -25,18 +25,16 @@ use wasmer_wit::{ use std::path::PathBuf; -pub struct EmbedderConfig { - pub in_wasm_path: PathBuf, - pub out_wasm_path: PathBuf, - pub wit: String, -} - -pub fn embed_text_wit(options: &EmbedderConfig) -> Result<(), WITParserError> { +pub fn embed_text_wit( + in_wasm_path: PathBuf, + out_wasm_path: PathBuf, + wit: &str, +) -> Result<(), WITParserError> { let mut module = ModuleConfig::new() - .parse_file(&options.in_wasm_path) + .parse_file(&in_wasm_path) .map_err(WITParserError::CorruptedWasmFile)?; - let buffer = Buffer::new(&options.wit)?; + let buffer = Buffer::new(wit)?; let ast = parse(&buffer)?; let mut bytes = vec![]; @@ -45,7 +43,7 @@ pub fn embed_text_wit(options: &EmbedderConfig) -> Result<(), WITParserError> { let custom = WITCustom(bytes); module.customs.add(custom); module - .emit_wasm_file(&options.out_wasm_path) + .emit_wasm_file(&out_wasm_path) .map_err(WITParserError::WasmEmitError)?; Ok(()) diff --git a/crates/fce_wit_interfaces/src/wit_parser/mod.rs b/crates/fce_wit_interfaces/src/wit_parser/mod.rs index 4cdc32b7..5b409670 100644 --- a/crates/fce_wit_interfaces/src/wit_parser/mod.rs +++ b/crates/fce_wit_interfaces/src/wit_parser/mod.rs @@ -18,11 +18,11 @@ mod custom; mod errors; mod extractor; mod embedder; +mod deleter; pub use errors::WITParserError; -pub use embedder::EmbedderConfig; pub use embedder::embed_text_wit; - +pub use deleter::delete_wit_section; pub use extractor::extract_fce_wit; pub use extractor::extract_text_wit; diff --git a/fce/src/vm/instance/wit_instance.rs b/fce/src/vm/instance/wit_instance.rs index 035129b2..63c46a01 100644 --- a/fce/src/vm/instance/wit_instance.rs +++ b/fce/src/vm/instance/wit_instance.rs @@ -18,8 +18,8 @@ use super::wit_prelude::*; use super::fce_module::FCEModule; use fce_wit_interfaces::FCEWITInterfaces; -use wasmer_wit::interpreter::wasm; use fce_wit_interfaces::WITAstType; +use wasmer_wit::interpreter::wasm; use wasmer_wit::interpreter::wasm::structures::{LocalImportIndex, TypedIndex}; use wasmer_core::Instance as WasmerInstance; diff --git a/tools/wit_embedder/src/args.rs b/tools/wit_embedder/src/args.rs new file mode 100644 index 00000000..7f557b21 --- /dev/null +++ b/tools/wit_embedder/src/args.rs @@ -0,0 +1,56 @@ +use clap::{App, Arg, SubCommand}; + +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const AUTHORS: &str = env!("CARGO_PKG_AUTHORS"); +pub const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION"); + +pub const IN_WASM_PATH: &str = "in-wasm-path"; +pub const WIT_PATH: &str = "wit-path"; +pub const OUT_WASM_PATH: &str = "out-wasm-path"; + +pub fn embed_wit<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("embed") + .about("embed WIT to provided Wasm file") + .args(&[ + Arg::with_name(IN_WASM_PATH) + .required(true) + .takes_value(true) + .short("i") + .help("path to the wasm file"), + Arg::with_name(WIT_PATH) + .required(true) + .takes_value(true) + .short("w") + .help("path to file with WIT"), + Arg::with_name(OUT_WASM_PATH) + .takes_value(true) + .short("o") + .help("path to result file with embedded WIT"), + ]) +} + +pub fn show_wit<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("show") + .about("show WIT in provided Wasm file") + .args(&[Arg::with_name(IN_WASM_PATH) + .required(true) + .takes_value(true) + .short("i") + .help("path to the wasm file")]) +} + +pub fn delete_wit<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("delete") + .about("delete all WIT sections in provided Wasm file") + .args(&[ + Arg::with_name(IN_WASM_PATH) + .required(true) + .takes_value(true) + .short("i") + .help("path to the wasm file"), + Arg::with_name(OUT_WASM_PATH) + .takes_value(true) + .short("o") + .help("path to result file with deleted WIT sections"), + ]) +} diff --git a/tools/wit_embedder/src/main.rs b/tools/wit_embedder/src/main.rs index c727e297..dde15a86 100644 --- a/tools/wit_embedder/src/main.rs +++ b/tools/wit_embedder/src/main.rs @@ -13,54 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#![warn(rust_2018_idioms)] +#![deny( + dead_code, + nonstandard_style, + unused_imports, + unused_mut, + unused_variables, + unused_unsafe, + unreachable_patterns +)] + +mod args; + +use args::*; -use fce_wit_interfaces::EmbedderConfig; use fce_wit_interfaces::embed_text_wit; use fce_wit_interfaces::extract_text_wit; +use fce_wit_interfaces::delete_wit_section; -use clap::{App, AppSettings, Arg, SubCommand}; -use failure::err_msg; +use clap::{App, AppSettings}; use std::path::PathBuf; -const VERSION: &str = env!("CARGO_PKG_VERSION"); -const AUTHORS: &str = env!("CARGO_PKG_AUTHORS"); -const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION"); - -const IN_WASM_PATH: &str = "in-wasm-path"; -const WIT_PATH: &str = "wit-path"; -const OUT_WASM_PATH: &str = "out-wasm-path"; - -fn embed_wit<'a, 'b>() -> App<'a, 'b> { - SubCommand::with_name("embed") - .about("embed WIT to provided Wasm file") - .args(&[ - Arg::with_name(IN_WASM_PATH) - .required(true) - .takes_value(true) - .short("i") - .help("path to the wasm file"), - Arg::with_name(WIT_PATH) - .required(true) - .takes_value(true) - .short("w") - .help("path to file with WIT"), - Arg::with_name(OUT_WASM_PATH) - .takes_value(true) - .short("o") - .help("path to result file with embedded WIT"), - ]) -} - -fn show_wit<'a, 'b>() -> App<'a, 'b> { - SubCommand::with_name("show") - .about("show WIT in provided Wasm file") - .args(&[Arg::with_name(IN_WASM_PATH) - .required(true) - .takes_value(true) - .short("i") - .help("path to the wasm file")]) -} - pub fn main() -> Result<(), exitfailure::ExitFailure> { let app = App::new("CLI tool for embedding WIT to provided Wasm file") .version(VERSION) @@ -68,7 +42,8 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> { .about(DESCRIPTION) .setting(AppSettings::ArgRequiredElseHelp) .subcommand(embed_wit()) - .subcommand(show_wit()); + .subcommand(show_wit()) + .subcommand(delete_wit()); match app.get_matches().subcommand() { ("embed", Some(arg)) => { @@ -81,20 +56,11 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> { let wit = String::from_utf8(std::fs::read(wit_path)?).unwrap(); - let options = EmbedderConfig { - in_wasm_path: PathBuf::from(in_wasm_path), - wit, - out_wasm_path: PathBuf::from(out_wasm_path), - }; - - match embed_text_wit(&options) { - Ok(_) => { - println!("WIT successfully embedded"); - } - Err(e) => { - println!("{}", e); - } - }; + embed_text_wit( + PathBuf::from(in_wasm_path), + PathBuf::from(out_wasm_path), + &wit, + )?; Ok(()) } @@ -107,6 +73,17 @@ pub fn main() -> Result<(), exitfailure::ExitFailure> { Ok(()) } - c => Err(err_msg(format!("Unexpected command: {}", c.0)).into()), + ("delete", Some(arg)) => { + let in_wasm_path = arg.value_of(IN_WASM_PATH).unwrap(); + let out_wasm_path = match arg.value_of(OUT_WASM_PATH) { + Some(path) => path, + None => in_wasm_path, + }; + + delete_wit_section(PathBuf::from(in_wasm_path), PathBuf::from(out_wasm_path))?; + + Ok(()) + } + c => Err(failure::err_msg(format!("Unexpected command: {}", c.0)).into()), } }