Skip to content

Commit

Permalink
GUI: Fix bug with pos vs neg diff.
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-carciofini committed Jan 21, 2025
1 parent 8e7d4d7 commit 067044a
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions pate_binja/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,6 @@ def ask_user(self, prompt: str, choices: list[str], replay: bool):
self.cmd_field.setEnabled(True)
self.cmd_field.setFocus()

def injectBinjaDissembly(self, original_lines, patched_lines):

obv = self.getOriginalBinaryView()
original_lines = list(map(lambda line: subDisassemblyForInstAddr(line, obv), original_lines))

pbv = self.getPatchedBinaryView()
patched_lines = list(map(lambda line: subDisassemblyForInstAddr(line, pbv), patched_lines))

return original_lines, patched_lines

def mcad_annotate_inst_tree(self, inst_tree: Optional[dict], bv: BinaryView):
"""Add MCAD cycle counts to instruction tree. NOOP if cycle counts all ready exist."""
if not inst_tree:
Expand Down Expand Up @@ -372,8 +362,14 @@ def paintEvent(self, e: QPaintEvent):
def redisplay(self):
if self.linesA is not None and self.linesB is not None:
# Show diff
html = generateHtmlDiff(self.linesA, self.labelA, self.linesB, self.labelB)
self.diffField.setHtml(html)
if self.linesA == self.linesB:
text = ''
text += f'No differences for {self.labelA} vs {self.labelB}\n'
text += '\n'.join(self.linesA)
self.diffField.setText(text)
else:
html = generateHtmlDiff(self.linesA, self.labelA, self.linesB, self.labelB)
self.diffField.setHtml(html)
elif self.linesA is not None and self.linesB is None:
# Just linesA, no diff
text = ''
Expand Down Expand Up @@ -430,7 +426,10 @@ def setTrace(self, trace: Optional[dict], label: str = None):

# Replace addr lines with binja disassembly
if pw:
originalLines, patchedLines = pw.injectBinjaDissembly(originalLines, patchedLines)
obv = pw.getOriginalBinaryView()
originalLines = list(map(lambda line: subDisassemblyForInstAddr(line, obv), originalLines))
pbv = pw.getPatchedBinaryView()
patchedLines = list(map(lambda line: subDisassemblyForInstAddr(line, pbv), patchedLines))

if label is None:
originalLabel = 'Original'
Expand All @@ -450,7 +449,7 @@ def setTrace(self, trace: Optional[dict], label: str = None):
# Only patched
self.traceDiff.setLinesNoDiff(patchedLines, patchedLabel)

def setTracePosVsNeg(self, posTrace: Optional[dict], negTrace: Optional[dict]):
def setTracePosVsNeg(self, posTrace: Optional[dict], negTrace: Optional[dict], bv: BinaryView) -> None:
if not (posTrace or negTrace):
# Nothing to show
self.traceDiff.clear('None')
Expand All @@ -477,8 +476,9 @@ def setTracePosVsNeg(self, posTrace: Optional[dict], negTrace: Optional[dict]):
negLines = out.getvalue().splitlines()

# Replace addr lines with binja disassembly
if pw:
posLines, negLines = pw.injectBinjaDissembly(posLines, negLines)
if bv:
posLines = list(map(lambda line: subDisassemblyForInstAddr(line, bv), posLines))
negLines = list(map(lambda line: subDisassemblyForInstAddr(line, bv), negLines))

posLabel = 'EQUIVALENT'
negLabel = 'DIFFERENT'
Expand Down Expand Up @@ -661,10 +661,18 @@ def updateFromCfarNode(self):
if self.conditionTrace.trace_false:
patchedNegTrace = self.conditionTrace.trace_false.get('traces', {}).get('patched')

self.trueTraceWidget.setTracePosVsNeg(originalPosTrace, originalNegTrace)
pw: Optional[PateWidget] = getAncestorInstanceOf(self, PateWidget)
if pw:
obv = pw.getOriginalBinaryView()
pbv = pw.getPatchedBinaryView()
else:
obv = None
pbv = None

self.trueTraceWidget.setTracePosVsNeg(originalPosTrace, originalNegTrace, pbv)
self.trueTraceLabel.setText('Trace showing original program, EQUIVALENT vs DIFFERENT behaviour:')
self.falseTraceWidget.setTracePosVsNeg(patchedPosTrace, patchedNegTrace)
self.falseTraceLabel.setText('Trace showing patched program, EQUIVALENT vs different behaviour:')
self.falseTraceWidget.setTracePosVsNeg(patchedPosTrace, patchedNegTrace, obv)
self.falseTraceLabel.setText('Trace showing patched program, EQUIVALENT vs DIFFERENT behaviour:')

def diffModeChanged(self, text):
print('Difference mode:', self.diffModeComboBox.currentText())
Expand Down

0 comments on commit 067044a

Please sign in to comment.