Add tool to generate zoneup opcodes if they are offset

Also only accept confident matches if the score is above 0
This commit is contained in:
Flawed
2023-10-02 20:38:11 -07:00
parent 4bafe6d9e5
commit 4bc6cd8ea8
3 changed files with 57 additions and 4 deletions
+43
View File
@@ -0,0 +1,43 @@
import click
import json
@click.command()
@click.argument("diff_file", type=click.File("r"))
@click.argument("offset", type=int)
def generate_diff_offset(diff_file, offset):
"""
Given a diff file, produces another diff file where the matches
are offset by OFFSET.
Example:
python generate_diff_offset.py -- diff.json -1 > offset.diff.json
"""
diff_json = json.load(diff_file)
olds = []
news = []
for pair in diff_json:
if "old" not in pair or "new" not in pair:
continue
olds.append(pair["old"])
news.append(pair["new"])
if offset < 0:
news = [[] for _ in range(-offset)] + news
if offset > 0:
olds = [[] for _ in range(offset)] + olds
opcodes_object = []
for old, new in zip(olds, news):
opcodes_object.append(
{
"old": old,
"new": new,
}
)
print(json.dumps(opcodes_object, indent=2))
if __name__ == "__main__":
generate_diff_offset()
+12 -3
View File
@@ -35,13 +35,22 @@ def sanity_check(vtable_diff, minor_patch_diff):
for old_opcode, new_opcodes in diff2.items(): for old_opcode, new_opcodes in diff2.items():
if old_opcode not in diff1: if old_opcode not in diff1:
if len(new_opcodes) < 50: if len(new_opcodes) < 50:
print(f"Missing old opcode in vtable diff: {old_opcode}") print(f"Missing old opcode in vtable diff: {hex(old_opcode)}")
all_good = False all_good = False
continue continue
other_new_opcodes = diff1[old_opcode] other_new_opcodes = diff1[old_opcode]
if list(other_new_opcodes)[0] not in new_opcodes: vtable_new_opcode = None
print(f"vtable diff mismatch for case {old_opcode} => {other_new_opcodes}") if len(other_new_opcodes) > 0:
vtable_new_opcode = list(other_new_opcodes)[0]
elif len(new_opcodes) == 0:
continue
if vtable_new_opcode not in new_opcodes:
new_text = (
hex(vtable_new_opcode) if vtable_new_opcode is not None else "None"
)
print(f"vtable diff mismatch for case {hex(old_opcode)} => {new_text}")
all_good = False all_good = False
if all_good: if all_good:
+2 -1
View File
@@ -135,6 +135,7 @@ class Similarity:
rev_top_matches = rev_scores[new_op] rev_top_matches = rev_scores[new_op]
if ( if (
rev_top_matches[0][0] == old_op rev_top_matches[0][0] == old_op
and rev_top_matches[0][1] > 0
and rev_top_matches[0][1] - rev_top_matches[1][1] >= threshold and rev_top_matches[0][1] - rev_top_matches[1][1] >= threshold
): ):
matches.append((old_op, new_op)) matches.append((old_op, new_op))
@@ -203,7 +204,7 @@ def vtable_alignment(old_exe, new_exe, similarity_json_file):
filter(lambda x: x[0] in old_seq_set and x[1] in new_seq_set, matches) filter(lambda x: x[0] in old_seq_set and x[1] in new_seq_set, matches)
) )
eprint(f"Found {len(matches)} confident matches") eprint(f'Found {len(matches)} "confident" matches')
eprint("Running initial alignment...") eprint("Running initial alignment...")
alignment, score = needleman_wunsch(old_seq, new_seq, similarity, -1) alignment, score = needleman_wunsch(old_seq, new_seq, similarity, -1)