Skip to content

Commit

Permalink
[move-diassembler][small] Display strings in disassembler (#13956)
Browse files Browse the repository at this point in the history
## Description 

Tries to determine if a constant in the constant pool can be interpreted
as a utf8 string. If it can it displays it as such, and adds a comment
saying it's interpreted the data that way.

## Test Plan 

Tested locally: 
```rust
module 0x1::M {
    const X: vector<u8> = b"hello world";
    public fun use_X(): vector<u8> { X }
}
```

```rust
$ move disassemble --name M
// Move bytecode v6
module 1.M {


public use_X(): vector<u8> {
B0:
        0: LdConst[0](Vector(U8): 0b68656c..)
        1: Ret
}

Constants [
        0 => vector<u8>: "hello world" // interpreted as UTF8 string
]
}
```

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [x] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
Updated display of constants in disassembled Move bytecode to try and
show the deserialized string if the constant is a `vector<u8>` that is
valid utf8.
  • Loading branch information
tzakian committed Sep 25, 2023
1 parent 05eed92 commit 7fa8258
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 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 external-crates/move/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 external-crates/move/tools/move-disassembler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ move-binary-format = { path = "../../move-binary-format" }
move-coverage = { path = "../move-coverage" }
move-compiler = { path = "../../move-compiler" }

bcs.workspace = true
clap.workspace = true
hex = "0.4.3"

Expand Down
13 changes: 12 additions & 1 deletion external-crates/move/tools/move-disassembler/src/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,8 +1260,19 @@ impl<'a> Disassembler<'a> {
constant_index: usize,
Constant { type_, data }: &Constant,
) -> Result<String> {
let data_str = match type_ {
SignatureToken::Vector(x) if x.as_ref() == &SignatureToken::U8 => {
match bcs::from_bytes::<Vec<u8>>(data)
.ok()
.and_then(|data| String::from_utf8(data).ok())
{
Some(str) => "\"".to_owned() + &str + "\" // interpreted as UTF8 string",
None => hex::encode(data),
}
}
_ => hex::encode(data),
};
let type_str = self.disassemble_sig_tok(type_.clone(), &[])?;
let data_str = hex::encode(data);
Ok(format!("\t{constant_index} => {}: {}", type_str, data_str))
}

Expand Down

1 comment on commit 7fa8258

@vercel
Copy link

@vercel vercel bot commented on 7fa8258 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.