From e99d9d649394611d5405a960a59e583de6b34a27 Mon Sep 17 00:00:00 2001 From: Mikhael Skvortsov Date: Thu, 20 Oct 2022 15:19:38 +0300 Subject: [PATCH 1/3] Add find-dup.py tool --- find-dup.py | 50 ++++++++++++++++++++++++++++++++++++++++++ src/disasm/commands.rs | 2 +- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100755 find-dup.py diff --git a/find-dup.py b/find-dup.py new file mode 100755 index 00000000..f38d213b --- /dev/null +++ b/find-dup.py @@ -0,0 +1,50 @@ +# Find duplicate fragments of linear sequences of code + +# Sample invocation: +# 1. extract code boc into the input.boc with tvm_linker decode +# 2. tvm_linker disasm text --raw input.boc >input.code +# 3. python3 find-dup.py input.code + +import sys + +if len(sys.argv) != 2: + print("Usage: find-dup.py input.code") + exit(1) + +size_low = 5 +size_high = 101 +size_width = len(str(size_high)) + +file = open(sys.argv[1], "r") +lines = file.readlines() +lines = [line.strip() for line in lines] +width = len(str(len(lines))) + +def is_valid(pattern): + for line in pattern: + s = line.split(";")[0].strip() + if s.endswith("{") or s.startswith("}"): + return False + return True + +for n in range(size_low, size_high): + matches = {} + + for i in range(len(lines) - n + 1): + pattern = lines[i:i + n] + if not is_valid(pattern): + continue + for j in range(i + 1, len(lines) - n + 1): + fragment = lines[j:j + n] + if fragment == pattern: + if i not in matches: + matches.update({i: []}) + l = matches[i] + l.append(j) + + for i, js in matches.items(): + if len(js) > 1: + s = "{n:0{w1}}: {i:0{w2}}".format(n = n, w1 = size_width, i = i, w2 = width) + for j in js: + s += " {j:0{w}}".format(j = j, w = width) + print(s) diff --git a/src/disasm/commands.rs b/src/disasm/commands.rs index 5b433c3b..8b144796 100644 --- a/src/disasm/commands.rs +++ b/src/disasm/commands.rs @@ -223,7 +223,7 @@ fn disasm_text_command(m: &ArgMatches) -> Status { let mut roots = deserialize_cells_tree(&mut csor).map_err(|e| format_err!("{}", e))?; if m.is_present("RAW") { - println!("{}", disasm_ex(&mut SliceData::from(roots.get(0).unwrap()), true)); + print!("{}", disasm_ex(&mut SliceData::from(roots.get(0).unwrap()), true)); return Ok(()) } From 12465eee54bd8965f7cac751759169b981b17d4b Mon Sep 17 00:00:00 2001 From: Mikhael Skvortsov Date: Tue, 24 Jan 2023 23:33:12 +0300 Subject: [PATCH 2/3] Bump version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6b792ab7..a89df4da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1325,7 +1325,7 @@ dependencies = [ [[package]] name = "tvm_linker" -version = "0.19.0" +version = "0.19.1" dependencies = [ "assert_cmd", "base64 0.13.1", diff --git a/Cargo.toml b/Cargo.toml index 0c400412..29510d9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ license-file = 'LICENSE.md' name = 'tvm_linker' readme = 'README.md' repository = 'https://github.com/tonlabs/TVM-linker' -version = '0.19.0' +version = '0.19.1' [[bin]] name = 'tvm_linker' From 43130fff2944016ad7b5243f493779671ef11495 Mon Sep 17 00:00:00 2001 From: tonjen Date: Tue, 24 Jan 2023 20:41:56 +0000 Subject: [PATCH 3/3] Preparing to merge with the master --- find-dup.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 find-dup.py diff --git a/find-dup.py b/find-dup.py old mode 100755 new mode 100644