Skip to content

Commit

Permalink
enabling calling counts
Browse files Browse the repository at this point in the history
  • Loading branch information
clearbluejar committed Dec 15, 2023
1 parent 863bb1e commit 0269420
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
8 changes: 5 additions & 3 deletions ghidriff/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import json

from ghidriff import GhidraDiffEngine
from .parser import get_parser,get_engine_classes
from .parser import get_parser, get_engine_classes


def main():
"""
ghidriff - GhidraDiffEngine module main function
"""

parser : ArgumentParser = get_parser()
parser: ArgumentParser = get_parser()

GhidraDiffEngine.add_ghidra_args_to_parser(parser)

Expand Down Expand Up @@ -62,7 +63,8 @@ def main():
engine_log_path=engine_log_path,
engine_log_level=args.log_level,
engine_file_log_level=args.file_log_level,
min_func_len=args.min_func_len
min_func_len=args.min_func_len,
use_calling_counts=args.use_calling_counts
)

d.setup_project(binary_paths, project_path, project_name, symbols_path)
Expand Down
48 changes: 27 additions & 21 deletions ghidriff/ghidra_diff_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def __init__(
engine_log_level: int = logging.INFO,
engine_file_log_level: int = logging.INFO,
max_section_funcs: int = 200,
min_func_len: int = 10) -> None:
min_func_len: int = 10,
use_calling_counts: bool = True) -> None:

# setup engine logging
self.logger = self.setup_logger(engine_log_level)
Expand Down Expand Up @@ -149,7 +150,7 @@ def __init__(
self.no_symbols = no_symbols

# if looking up more than calling_count_funcs_limit symbols, skip function counts
self.calling_count_funcs_limit = 500
self.use_calling_counts = use_calling_counts

self.logger.debug(f'{vars(self)}')

Expand Down Expand Up @@ -180,7 +181,8 @@ def add_ghidra_args_to_parser(parser: argparse.ArgumentParser) -> None:
group.add_argument('--va', '--verbose-analysis',
help='Verbose logging for analysis step.', action='store_true')
group.add_argument('--min-func-len', help='Minimum function length to consider for diff',
type=int, default=10)
type=int, default=10),
group.add_argument('--use-calling-counts', help='Add calling/called reference counts', default=True)

# TODO add following option
# group.add_argument('--exact-matches', help='Only consider exact matches', action='store_true')
Expand Down Expand Up @@ -265,9 +267,6 @@ def enhance_sym(self, sym: 'ghidra.program.model.symbol.Symbol', thread_id: int
# key = f'{sym.iD}-{sym.program.name}-{get_decomp_info}-{use_calling_counts}'
key = f'{sym.iD}-{sym.program.name}'

# if sym.getName() == 'SepAppendAceToTokenObjectAcl':
# print('hi')

if key not in self.esym_memo:

from ghidra.util.task import ConsoleTaskMonitor
Expand Down Expand Up @@ -341,23 +340,32 @@ def enhance_sym(self, sym: 'ghidra.program.model.symbol.Symbol', thread_id: int
self.logger.warn(err)
code = err

# if use_calling_counts:
if False:
if use_calling_counts:
MAX_FUNC_REFS = 2000

for f in func.getCalledFunctions(monitor):
count = 0
print(len(f.symbol.references))
for ref in f.symbol.references:
if func.getBody().contains(ref.fromAddress, ref.fromAddress):
count += 1
called_funcs.append(f'{f}-{count}')
count = 0
if len(f.symbol.references) > MAX_FUNC_REFS:
self.logger.debug(f'Skipping {func} count calling, too many refs')
called_funcs.append(f'{f}')
else:

for ref in f.symbol.references:
if func.getBody().contains(ref.fromAddress, ref.fromAddress):
count += 1
called_funcs.append(f'{f}-{count}')

for f in func.getCallingFunctions(monitor):
count = 0
print(len(func.symbol.references))
for ref in func.symbol.references:
if f.getBody().contains(ref.fromAddress, ref.fromAddress):
count += 1
called_funcs.append(f'{f}-{count}')
if len(f.symbol.references) > MAX_FUNC_REFS:
self.logger.debug(f'Skipping {func} count calling, too many refs')
calling_funcs.append(f'{f}')
else:
for ref in func.symbol.references:
if f.getBody().contains(ref.fromAddress, ref.fromAddress):
count += 1
calling_funcs.append(f'{f}-{count}')
else:
for f in func.getCalledFunctions(monitor):
called_funcs.append(f'{f}')
Expand Down Expand Up @@ -1301,8 +1309,6 @@ def diff_bins(

esym_lookups.extend(funcs_need_decomp)

use_calling_counts = len(funcs_need_decomp) < self.calling_count_funcs_limit

# TODO add code to symbols!

# there can be duplicate multiple function matches, just do this once
Expand All @@ -1313,7 +1319,7 @@ def diff_bins(
completed = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=self.max_workers) as executor:
# futures = (executor.submit(self.enhance_sym, sym, thread_id % self.max_workers, 15, (sym in funcs_need_decomp), (use_calling_counts and sym in funcs_need_decomp))
futures = (executor.submit(self.enhance_sym, sym, thread_id % self.max_workers, 60, (sym in funcs_need_decomp), (use_calling_counts and sym in funcs_need_decomp))
futures = (executor.submit(self.enhance_sym, sym, thread_id % self.max_workers, 60, (sym in funcs_need_decomp), (self.use_calling_counts and sym in funcs_need_decomp))
for thread_id, sym in enumerate(esym_lookups))

for future in concurrent.futures.as_completed(futures):
Expand Down

0 comments on commit 0269420

Please sign in to comment.