Skip to content

Commit f28af4e

Browse files
v-p-bclearbluejar
authored andcommitted
Use hash tables for FullName:Param matching
This way we can reduce the complexity of the matching phase from O(n^2) to O(n).
1 parent 38348e2 commit f28af4e

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

ghidriff/simple_diff.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,23 @@ def _syms_match(esym, esym2) -> Tuple[bool, str]:
157157
for sym in modified_new:
158158
self.logger.info(sym)
159159

160-
p1_modified = []
161-
p2_modified = []
160+
p1_modified = {}
161+
p2_modified = {}
162162

163163
# Find modified functions based on compare_key
164164
for sym in p1.getSymbolTable().getDefinedSymbols():
165165

166166
if "function".lower() in sym.getSymbolType().toString().lower():
167167
func = p1.functionManager.getFunctionAt(sym.getAddress())
168168
if (_get_compare_key(sym, func)) in modified_old:
169-
p1_modified.append(sym)
169+
p1_modified[(sym.getName(True), func.parameterCount)] = sym
170170

171171
for sym in p2.getSymbolTable().getDefinedSymbols():
172172

173173
if "function".lower() in sym.getSymbolType().toString().lower():
174174
func = p2.functionManager.getFunctionAt(sym.getAddress())
175175
if (_get_compare_key(sym, func)) in modified_new:
176-
p2_modified.append(sym)
176+
p2_modified[(sym.getName(True), func.parameterCount)] = sym
177177

178178
self.logger.info("\nmodified_old_modified")
179179
for sym in p1_modified:
@@ -190,23 +190,21 @@ def _syms_match(esym, esym2) -> Tuple[bool, str]:
190190
self.logger.info("\nMatching functions...")
191191

192192
# match by name and paramcount
193-
for sym in p1_modified:
194-
for sym2 in p2_modified:
195-
193+
for sym in p1_modified.values():
194+
func = p1.functionManager.getFunctionAt(sym.getAddress())
195+
matcher = (sym.getName(True), func.parameterCount)
196+
if matcher in p2_modified:
197+
sym2 = p2_modified[matcher]
196198
if sym2 in matched:
197199
continue
198200

199-
func = p1.functionManager.getFunctionAt(sym.getAddress())
200-
func2 = p2.functionManager.getFunctionAt(sym2.getAddress())
201-
202-
if sym.getName(True) == sym2.getName(True) and func.parameterCount == func2.parameterCount:
203-
self.logger.info("FullName + Paramcount {} {}".format(sym.getName(True), sym2.getName(True)))
204-
match_type = 'FullName:Param'
205-
matched.append(sym)
206-
matched.append(sym2)
207-
matches.append([sym, sym2, match_type])
201+
self.logger.info("FullName + Paramcount {} {}".format(sym.getName(True), sym2.getName(True)))
202+
match_type = 'FullName:Param'
203+
matched.append(sym)
204+
matched.append(sym2)
205+
matches.append([sym, sym2, match_type])
208206

209-
for sym in p1_modified:
207+
for sym in p1_modified.values():
210208
found = False
211209

212210
if sym in matched:
@@ -219,7 +217,7 @@ def _syms_match(esym, esym2) -> Tuple[bool, str]:
219217
match_type = 'Direct'
220218
self.logger.info(f"direct getsymbol match {sym.getName(True)} {sym2.getName(True)}")
221219
else:
222-
for sym2 in p2_modified:
220+
for sym2 in p2_modified.values():
223221
if sym2 in matched:
224222
continue
225223

@@ -238,7 +236,7 @@ def _syms_match(esym, esym2) -> Tuple[bool, str]:
238236
self.logger.info(f"Deleted func found: {sym}")
239237
unmatched.append(sym)
240238

241-
for sym in p2_modified:
239+
for sym in p2_modified.values():
242240
found = False
243241
match_type = None
244242

@@ -252,7 +250,7 @@ def _syms_match(esym, esym2) -> Tuple[bool, str]:
252250
match_type = 'Direct'
253251
self.logger.info(f"direct getsymbol match {sym.getName(True)} {sym2.getName(True)}")
254252
else:
255-
for sym2 in p1_modified:
253+
for sym2 in p1_modified.values():
256254
if sym2 in matched:
257255
continue
258256

0 commit comments

Comments
 (0)