Skip to content

Commit

Permalink
feat: add the EraVM disassembler (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgar2017 committed Aug 30, 2024
1 parent aedcef4 commit 1d861cb
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 82 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# The `zksolc` changelog

## [Unreleased]

### Added

- The EraVM disassembler

## [1.5.3] - 2024-08-27

### Added
Expand Down
115 changes: 36 additions & 79 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion LLVM.lock
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
url = "https://github.com/matter-labs/era-compiler-llvm"
branch = "v1.5.2"
branch = "main"
46 changes: 46 additions & 0 deletions era-compiler-solidity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub use self::solc::version::Version as SolcVersion;
pub use self::solc::Compiler as SolcCompiler;

use std::collections::BTreeSet;
use std::io::Write;
use std::path::PathBuf;

/// The default error compatible with `solc` standard JSON output.
Expand Down Expand Up @@ -844,3 +845,48 @@ pub fn combined_json_evm(
}
std::process::exit(era_compiler_common::EXIT_CODE_SUCCESS);
}

///
/// Runs the disassembler for EraVM bytecode file and prints the output to stdout.
///
pub fn disassemble_eravm(paths: Vec<String>) -> anyhow::Result<()> {
let target_machine = era_compiler_llvm_context::TargetMachine::new(
era_compiler_common::Target::EraVM,
&era_compiler_llvm_context::OptimizerSettings::cycles(),
&[],
)?;

let disassemblies: Vec<(String, String)> = paths
.into_iter()
.map(|path| {
let pathbuf = PathBuf::from(path.as_str());

let bytecode = match pathbuf.extension().and_then(|extension| extension.to_str()) {
Some("hex") => {
let string = std::fs::read_to_string(pathbuf)?;
let hexadecimal_string =
string.trim().strip_prefix("0x").unwrap_or(string.as_str());
hex::decode(hexadecimal_string)?
}
Some("zbin") => std::fs::read(pathbuf)?,
Some(extension) => anyhow::bail!(
"Invalid file extension: {extension}. Supported extensions: *.hex, *.zbin"
),
None => {
anyhow::bail!("Missing file extension. Supported extensions: *.hex, *.zbin")
}
};

let disassembly =
era_compiler_llvm_context::eravm_disassemble(&target_machine, bytecode.as_slice())?;
Ok((path, disassembly))
})
.collect::<anyhow::Result<Vec<(String, String)>>>()?;

for (path, disassembly) in disassemblies.into_iter() {
writeln!(std::io::stderr(), "File `{path}` disassembly:\n\n")?;
writeln!(std::io::stdout(), "{disassembly}")?;
writeln!(std::io::stderr(), "\n\n")?;
}
std::process::exit(era_compiler_common::EXIT_CODE_SUCCESS);
}
Loading

0 comments on commit 1d861cb

Please sign in to comment.