Spaces:
Sleeping
Sleeping
Commit
·
f6ba557
1
Parent(s):
9b9d925
Do the comparison
Browse files
main.py
CHANGED
@@ -96,35 +96,39 @@ def compile(compiler, flags, source):
|
|
96 |
# Filter out .text
|
97 |
json_relocs = [r for r in json_relocs if r["Symbol"]["Name"] != ".text"]
|
98 |
|
99 |
-
def reloc_type2size(s):
|
100 |
-
match s:
|
101 |
-
case "R_X86_64_PC32":
|
102 |
-
return 4
|
103 |
-
case "R_X86_64_PLT32":
|
104 |
-
return 4
|
105 |
-
case _:
|
106 |
-
assert False, f"Unknown reloc {s}"
|
107 |
|
108 |
-
relocs_byte_range = [range(r["Offset"], r["Offset"] + reloc_type2size(r["Type"]["Name"])) for r in json_relocs]
|
109 |
-
# Flatten relocs_byte_range
|
110 |
-
relocs_byte_range = [i for r in relocs_byte_range for i in r]
|
111 |
-
|
112 |
-
|
113 |
-
print(f"relocs: {relocs_byte_range}")
|
114 |
-
summary = print_match_summary(b"test", compiled_bytes, wildcard_offsets_str2=relocs_byte_range)
|
115 |
-
print(f"summary: {summary}")
|
116 |
|
117 |
if result.returncode == 0:
|
118 |
return json_relocs, compiled_bytes, compile_output, disassembly
|
119 |
else:
|
120 |
return None, None, compile_output, disassembly
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
def predict(target_bytes, source, compiler, flags, disasm_arch, disasm_options):
|
123 |
target_bytes = bytes.fromhex(target_bytes)
|
124 |
compiled_relocs, compiled_bytes, compile_output, compiled_disassembly = compile(compiler, flags, source)
|
125 |
target_disassembly = disassemble_bytes(target_bytes, disasm_arch, disasm_options)
|
126 |
|
127 |
if compiled_bytes is not None:
|
|
|
|
|
|
|
|
|
|
|
128 |
return (
|
129 |
hexdump(compiled_bytes, result="return"),
|
130 |
hexdump(target_bytes, result="return"),
|
|
|
96 |
# Filter out .text
|
97 |
json_relocs = [r for r in json_relocs if r["Symbol"]["Name"] != ".text"]
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
|
101 |
if result.returncode == 0:
|
102 |
return json_relocs, compiled_bytes, compile_output, disassembly
|
103 |
else:
|
104 |
return None, None, compile_output, disassembly
|
105 |
|
106 |
+
def _reloc_type2size(s):
|
107 |
+
match s:
|
108 |
+
case "R_X86_64_PC32":
|
109 |
+
return 4
|
110 |
+
case "R_X86_64_PLT32":
|
111 |
+
return 4
|
112 |
+
case _:
|
113 |
+
assert False, f"Unknown reloc {s}"
|
114 |
+
|
115 |
+
def _compute_relocs_byte_range(json_relocs):
|
116 |
+
relocs_byte_range = [range(r["Offset"], r["Offset"] + reloc_type2size(r["Type"]["Name"])) for r in json_relocs]
|
117 |
+
# Flatten relocs_byte_range
|
118 |
+
relocs_byte_range = [i for r in relocs_byte_range for i in r]
|
119 |
+
return relocs_byte_range
|
120 |
+
|
121 |
def predict(target_bytes, source, compiler, flags, disasm_arch, disasm_options):
|
122 |
target_bytes = bytes.fromhex(target_bytes)
|
123 |
compiled_relocs, compiled_bytes, compile_output, compiled_disassembly = compile(compiler, flags, source)
|
124 |
target_disassembly = disassemble_bytes(target_bytes, disasm_arch, disasm_options)
|
125 |
|
126 |
if compiled_bytes is not None:
|
127 |
+
|
128 |
+
reloc_edit_distance, reloc_operations = print_match_summary(target_bytes, compiled_bytes, wildcard_offsets_str2=_compute_relocs_byte_range(compiled_relocs))
|
129 |
+
print(f"reloc_edit_distance: {reloc_edit_distance}")
|
130 |
+
print(f"reloc operations: {reloc_operations}")
|
131 |
+
|
132 |
return (
|
133 |
hexdump(compiled_bytes, result="return"),
|
134 |
hexdump(target_bytes, result="return"),
|