From 7fa8258494346b9087ac367eba64fafb98442704 Mon Sep 17 00:00:00 2001 From: Tim Zakian <2895723+tzakian@users.noreply.github.com> Date: Mon, 25 Sep 2023 12:13:09 -0700 Subject: [PATCH] [move-diassembler][small] Display strings in disassembler (#13956) ## 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 = b"hello world"; public fun use_X(): vector { X } } ``` ```rust $ move disassemble --name M // Move bytecode v6 module 1.M { public use_X(): vector { B0: 0: LdConst[0](Vector(U8): 0b68656c..) 1: Ret } Constants [ 0 => vector: "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` that is valid utf8. --- Cargo.lock | 1 + external-crates/move/Cargo.lock | 1 + .../move/tools/move-disassembler/Cargo.toml | 1 + .../tools/move-disassembler/src/disassembler.rs | 13 ++++++++++++- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 2ff9fbbd74fc6..d8ad64cc449e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5451,6 +5451,7 @@ name = "move-disassembler" version = "0.1.0" dependencies = [ "anyhow", + "bcs", "clap", "colored", "hex", diff --git a/external-crates/move/Cargo.lock b/external-crates/move/Cargo.lock index 98b4a1e7e5aed..d6297754490b3 100644 --- a/external-crates/move/Cargo.lock +++ b/external-crates/move/Cargo.lock @@ -2709,6 +2709,7 @@ name = "move-disassembler" version = "0.1.0" dependencies = [ "anyhow", + "bcs", "clap 4.4.1", "colored", "hex", diff --git a/external-crates/move/tools/move-disassembler/Cargo.toml b/external-crates/move/tools/move-disassembler/Cargo.toml index e02fcb4ddacbc..3c87cdb48336c 100644 --- a/external-crates/move/tools/move-disassembler/Cargo.toml +++ b/external-crates/move/tools/move-disassembler/Cargo.toml @@ -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" diff --git a/external-crates/move/tools/move-disassembler/src/disassembler.rs b/external-crates/move/tools/move-disassembler/src/disassembler.rs index fc3d39917f421..2f96d4fb8806d 100644 --- a/external-crates/move/tools/move-disassembler/src/disassembler.rs +++ b/external-crates/move/tools/move-disassembler/src/disassembler.rs @@ -1260,8 +1260,19 @@ impl<'a> Disassembler<'a> { constant_index: usize, Constant { type_, data }: &Constant, ) -> Result { + let data_str = match type_ { + SignatureToken::Vector(x) if x.as_ref() == &SignatureToken::U8 => { + match bcs::from_bytes::>(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)) }