Skip to content

Commit

Permalink
[sui cli] allow disassembling module without source
Browse files Browse the repository at this point in the history
`sui move disassemble` needs a source package, which is a bit silly since a dissasembler is often used to look at bytecode without knowledge of the source.

This PR changes the command to dissasemble w/source context if pointed at a `.mv` file inside a package, and disassemble directly with no source context otherwise. Example usage:

```
sui move disassemble frenemies/build/frenemies/bytecode_modules/frenemies.mv
cp frenemies.mv tmp/
sui move disassemble frenemies.mv
```
  • Loading branch information
sblackshear committed Mar 2, 2023
1 parent ac4c7e6 commit b59c6ed
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/sui-move/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ colored = "2.0.0"

telemetry-subscribers.workspace = true

move-binary-format.workspace = true
move-core-types.workspace = true
move-disassembler.workspace = true
move-ir-types.workspace = true
move-prover.workspace = true
move-prover-boogie-backend.workspace = true
move-stackless-bytecode.workspace = true
Expand Down
50 changes: 45 additions & 5 deletions crates/sui-move/src/disassemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,59 @@
// SPDX-License-Identifier: Apache-2.0

use clap::Parser;
use move_cli::base::disassemble;
use move_binary_format::{binary_views::BinaryIndexedView, CompiledModule};
use move_cli::base;
use move_disassembler::disassembler::Disassembler;
use move_ir_types::location::Spanned;
use move_package::BuildConfig;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;
use std::path::PathBuf;

#[derive(Parser)]
pub struct Disassemble {
#[clap(flatten)]
pub disassemble: disassemble::Disassemble,
/// Path to a .mv file to disassemble
#[clap(name = "module_path")]
module_path: PathBuf,
}

impl Disassemble {
pub fn execute(self, path: Option<PathBuf>, build_config: BuildConfig) -> anyhow::Result<()> {
self.disassemble.execute(path, build_config)?;
pub fn execute(
self,
package_path: Option<PathBuf>,
build_config: BuildConfig,
) -> anyhow::Result<()> {
if base::reroot_path(Some(self.module_path.clone())).is_ok() {
// disassembling bytecode inside the source package that produced it--use the source info
let module_name = self
.module_path
.file_stem()
.expect("Bad module path")
.to_str()
.expect("Cannot convert module name to string")
.to_owned();
move_cli::base::disassemble::Disassemble {
interactive: false,
package_name: None,
module_or_script_name: module_name,
debug: false,
}
.execute(package_path, build_config)?
} else {
// disassembling a bytecode file with no source info
assert!(
Path::new(&self.module_path).exists(),
"Bath path to .mv file"
);
let mut bytes = Vec::new();
let mut file = BufReader::new(File::open(self.module_path)?);
file.read_to_end(&mut bytes)?;
let module = CompiledModule::deserialize(&bytes)?;
let view = BinaryIndexedView::Module(&module);
let d = Disassembler::from_view(view, Spanned::unsafe_no_loc(()).loc)?;
println!("{}", d.disassemble()?);
}
Ok(())
}
}

0 comments on commit b59c6ed

Please sign in to comment.