Skip to content

Commit

Permalink
Code reorg/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-carciofini committed Feb 21, 2024
1 parent 3b32083 commit 18a90ad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
24 changes: 13 additions & 11 deletions pate_binja/pate.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,6 @@ def show_message(self, rec: Any):


class CFARNode:

id: str
desc: str
data = None
predomain = None
postdomain = None
external_postdomain = None
exits: list[CFARNode]

def __init__(self, id: str, desc: str, data: dict):
Expand All @@ -444,6 +437,10 @@ def __init__(self, id: str, desc: str, data: dict):
self.update_node(desc, data)
self.desc = desc
self.data = data
self.predomain = None
self.postdomain = None
self.external_postdomain = None
self.addr = None

def update_node(self, desc: str, data: dict):
self.desc = desc
Expand Down Expand Up @@ -528,7 +525,6 @@ def pprint_node_event_trace_patched(self, trace, label: str, pre: str = '', out:
pprint_event_trace(f'{label} Patched', trace['traces']['patched'], pre, out)

class CFARGraph:

nodes: dict[str, CFARNode]

def __init__(self):
Expand Down Expand Up @@ -1265,10 +1261,15 @@ def run_replay(file: str) -> Popen:
)


def run_pate_config(file):
def get_run_config(file: os.PathLike) -> dict:
with open(file, 'r') as f:
config = json.load(f)
cwd = os.path.dirname(file)
config['cwd'] = os.path.dirname(file)
return config


def run_config(config: dict):
cwd = config.get('cwd')
original = config.get('original')
patched = config.get('patched')
rawargs = config.get('args')
Expand All @@ -1292,7 +1293,8 @@ def run_pate(cwd: str, original: str, patched: str, args: list[str]) -> Popen:

def run_pate_config_or_replay_file(f: str) -> Popen:
if f.endswith(".run-config.json"):
test_live(lambda ignore: run_pate_config(f))
config = get_run_config(f)
test_live(lambda ignore: run_config(config))
elif f.endswith(".replay"):
test_replay(lambda ignore: run_replay(f))

Expand Down
23 changes: 17 additions & 6 deletions pate_binja/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@


class PateWidget(QWidget):
def __init__(self, parent: QWidget, filename: str) -> None:
def __init__(self, parent: QWidget, filename: str, config: dict) -> None:
super().__init__(parent)

self.filename = filename
self.config = config
self.pate_thread: PateThread = None

self.flow_graph_widget = MyFlowGraphWidget(self)
Expand Down Expand Up @@ -125,6 +126,10 @@ def show_cfar_graph(self, graph: pate.CFARGraph) -> None:


class PateThread(Thread):
proc: Popen or None
oBv: BinaryView | None
pBv: BinaryView | None

# TODO: Look at interaction.run_progress_dialog
# handle cancel and restart
def __init__(self, bv, run_fn, pate_widget: PateWidget, replay=False, show_ce_trace=True, trace_file=None):
Expand All @@ -135,7 +140,9 @@ def __init__(self, bv, run_fn, pate_widget: PateWidget, replay=False, show_ce_tr
self.run_fn = run_fn
self.trace_file = trace_file
self.show_ce_trace = show_ce_trace
self.proc: Popen = None
self.proc = None
self.oBv = None
self.pBv = None

def run(self):

Expand Down Expand Up @@ -352,16 +359,20 @@ def launch_pate(context: UIActionContext):
if f.endswith(".run-config.json"):
replay = False
trace_file = os.path.join(os.path.dirname(f), 'lastrun.replay')
fn = lambda ignore: pate.run_pate_config(f)
config = pate.get_run_config(f)
elif f.endswith(".replay"):
replay = True
trace_file = None
fn = lambda ignore: pate.run_replay(f)
config = {} # TODO: need config for replay. Save it in replay file as first line?

pate_widget = PateWidget(context.widget, f)
pate_widget = PateWidget(context.widget, f, config)
tab = context.context.createTabForWidget("PATE " + os.path.basename(f), pate_widget)

pt = PateThread(None, fn, pate_widget, replay=replay, trace_file=trace_file)
if replay:
pt = PateThread(None, lambda i: pate.run_replay(f), pate_widget, replay=replay, trace_file=trace_file)
else:
pt = PateThread(None, lambda i: pate.run_config(config), pate_widget, replay=replay, trace_file=trace_file)

pate_widget.pate_thread = pt
pt.start()

Expand Down

0 comments on commit 18a90ad

Please sign in to comment.