-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug_similarity_matrix.py
67 lines (54 loc) · 1.91 KB
/
debug_similarity_matrix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import click
from vtable_alignment import Similarity
from utils import HexIntParamType
@click.command()
@click.argument(
"similarity_json_file",
type=click.Path(exists=True, dir_okay=False, resolve_path=True),
)
@click.argument("opcode", type=HexIntParamType())
@click.option(
"--reverse",
is_flag=True,
help="Prints a column of the similarity matrix given a new opcode",
)
@click.option(
"--accept",
default=None,
help="Modifies the similarity matrix file by accepting the match between the opcode argument and the argument to this option",
type=HexIntParamType(),
)
def debug_similarity_matrix(similarity_json_file, opcode, reverse, accept):
"""
Given an old opcode, debug prints a row of the similarity matrix generated
from generate_similarity_matrix.py.
Example:
python debug_similarity_matrix.py similarity.json 0x200
"""
entries = dict()
similarity = Similarity(similarity_json_file)
if accept is not None:
similarity.accept(opcode, accept)
similarity.write_to_file(similarity_json_file)
return
if reverse:
print("Checking column of matrix since --reverse was provided")
for old_opcode in similarity.old_opcodes:
entries[old_opcode] = similarity.lookup(old_opcode, opcode)
else:
for new_opcode in similarity.new_opcodes:
entries[new_opcode] = similarity.lookup(opcode, new_opcode)
max_opcode = 0
max_score = -9999
print(f"Similarities for {hex(opcode)}")
entries = list(entries.items())
entries.sort(key=lambda x: x[1], reverse=True)
for candidate, similarity in entries:
print(f"\t{hex(candidate)} => {similarity}")
if similarity > max_score:
max_opcode = candidate
max_score = similarity
print("Best match")
print(f"{hex(max_opcode)} => {max_score}")
if __name__ == "__main__":
debug_similarity_matrix()