Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meta - replace wasm-objdump with internal impl #1226

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions framework/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ common-path = "1.0.0"
lazy_static = "1.4.0"
convert_case = "0.6.0"
hex = "0.4"
wasmparser = "0.113.1"

[dependencies.multiversx-sc]
version = "=0.43.4"
Expand Down
7 changes: 4 additions & 3 deletions framework/meta/src/cmd/contract/output_contract/wasm_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
ei::EIVersion,
mxsc_file_json::{save_mxsc_file_json, MxscFileJson},
print_util::*,
tools::post_build,
tools::{self, post_build},
};

impl OutputContract {
Expand Down Expand Up @@ -134,8 +134,9 @@ impl OutputContract {
self.imports_json_output_name(build_args)
);
print_extract_imports(&output_imports_json_path);
let result = post_build::run_wasm_objdump(output_wasm_path.as_str());
let import_names = post_build::parse_imports(result.as_str());
let import_names = tools::extract_wasm_imports(&output_wasm_path)
.expect("error occured while extracting imports from .wasm ");
println!("{import_names:?}");
write_imports_output(output_imports_json_path.as_str(), import_names.as_slice());
validate_ei(&import_names, &self.settings.check_ei);
}
Expand Down
2 changes: 2 additions & 0 deletions framework/meta/src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod git_describe;
pub mod post_build;
mod wasm_imports;

pub use git_describe::git_describe;
pub use wasm_imports::extract_wasm_imports;
37 changes: 0 additions & 37 deletions framework/meta/src/tools/post_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::cli_args::BuildArgs;

const WASM_OPT_NAME: &str = "wasm-opt";
const WASM2WAT_NAME: &str = "wasm2wat";
const WASM_OBJDUMP_NAME: &str = "wasm-objdump";
const TWIGGY_NAME: &str = "twiggy";

pub(crate) fn check_tools_installed(build_args: &mut BuildArgs) {
Expand All @@ -16,10 +15,6 @@ pub(crate) fn check_tools_installed(build_args: &mut BuildArgs) {
println!("Warning: {WASM2WAT_NAME} not installed");
build_args.wat = false;
}
if build_args.extract_imports && !is_wasm_objdump_installed() {
println!("Warning: {WASM_OBJDUMP_NAME} not installed");
build_args.extract_imports = false;
}
if build_args.has_twiggy_call() && !is_twiggy_installed() {
println!("Warning: {TWIGGY_NAME} not installed");
build_args.twiggy_top = false;
Expand All @@ -43,13 +38,6 @@ fn is_wasm2wat_installed() -> bool {
.is_ok()
}

fn is_wasm_objdump_installed() -> bool {
Command::new(WASM_OBJDUMP_NAME)
.args(["--version"])
.output()
.is_ok()
}

fn is_twiggy_installed() -> bool {
Command::new(TWIGGY_NAME)
.args(["--version"])
Expand Down Expand Up @@ -79,31 +67,6 @@ pub(crate) fn run_wasm2wat(output_wasm_path: &str, output_wat_path: &str) {
assert!(exit_status.success(), "wasm2wat process failed");
}

pub(crate) fn run_wasm_objdump(output_wasm_path: &str) -> String {
let output = Command::new(WASM_OBJDUMP_NAME)
.args([output_wasm_path, "--details", "--section", "Import"])
.output()
.expect("failed to execute wasm-objdump");

assert!(
output.status.success(),
"wasm-objdump exited with error: {}",
core::str::from_utf8(&output.stderr).unwrap()
);
String::from_utf8(output.stdout).unwrap()
}

pub(crate) fn parse_imports(result: &str) -> Vec<String> {
let mut import_names = Vec::new();
for line in result.lines() {
let split = line.split("<- env.").collect::<Vec<_>>();
if split.len() == 2 {
import_names.push(split[1].to_string());
}
}
import_names
}

fn run_with_stdout_file<I, S>(stdout_file_name: &str, args: I)
where
I: IntoIterator<Item = S>,
Expand Down
20 changes: 20 additions & 0 deletions framework/meta/src/tools/wasm_imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use anyhow::Result;
use std::fs;
use wasmparser::{Parser, Payload};

/// Parses the WebAssembly code and extracts all the import names.
pub fn extract_wasm_imports(output_wasm_path: &str) -> Result<Vec<String>> {
let wasm_data = fs::read(output_wasm_path)?;
let mut import_names = Vec::new();

let parser = Parser::new(0);
for payload in parser.parse_all(&wasm_data) {
if let Payload::ImportSection(import_section) = payload? {
for import in import_section {
import_names.push(import?.name.to_string());
}
}
}

Ok(import_names)
}