Skip to content

Commit 5957643

Browse files
committed
Add "highlight returns" render layer example
1 parent 480f8b5 commit 5957643

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from typing import List, Optional
2+
3+
import binaryninja
4+
from binaryninja import RenderLayer, InstructionTextToken, \
5+
InstructionTextTokenType, HighlightColor, ThemeColor
6+
from binaryninjaui import getThemeColor
7+
8+
9+
"""
10+
Render Layer that highlights all lines in Linear View that have a return statement
11+
"""
12+
13+
14+
class HighlightReturnsLayer(RenderLayer):
15+
name = "Highlight Returns"
16+
17+
# Highlighting in all ILs includes handling both normal blocks (up to MLIL) and HLIL bodies,
18+
# since HLIL is special and gets its own handler.
19+
def apply_to_high_level_il_body(
20+
self,
21+
function: 'binaryninja.Function',
22+
lines: List['binaryninja.LinearDisassemblyLine']
23+
):
24+
ret_color = getThemeColor(ThemeColor.GraphExitNodeIndicatorColor)
25+
noret_color = getThemeColor(ThemeColor.GraphExitNoreturnNodeIndicatorColor)
26+
ret_highlight = HighlightColor(red=ret_color.red(), green=ret_color.green(), blue=ret_color.blue())
27+
noret_highlight = HighlightColor(red=noret_color.red(), green=noret_color.green(), blue=noret_color.blue())
28+
29+
# Basic check: if the line has a keyword token which is "return" or "noreturn"
30+
for i, line in enumerate(lines):
31+
if any(token.type == InstructionTextTokenType.KeywordToken and token.text.startswith("return") for token in line.contents.tokens):
32+
line.contents.highlight = ret_highlight
33+
elif any(token.type == InstructionTextTokenType.KeywordToken and token.text == "noreturn" for token in line.contents.tokens):
34+
line.contents.highlight = noret_highlight
35+
return lines
36+
37+
# Applies to MLIL and lower
38+
def apply_to_block(
39+
self,
40+
block: 'binaryninja.BasicBlock',
41+
lines: List['binaryninja.DisassemblyTextLine']
42+
):
43+
ret_color = getThemeColor(ThemeColor.GraphExitNodeIndicatorColor)
44+
noret_color = getThemeColor(ThemeColor.GraphExitNoreturnNodeIndicatorColor)
45+
ret_highlight = HighlightColor(red=ret_color.red(), green=ret_color.green(), blue=ret_color.blue())
46+
noret_highlight = HighlightColor(red=noret_color.red(), green=noret_color.green(), blue=noret_color.blue())
47+
48+
# Basic check: if the line has a keyword token which is "return" or "noreturn"
49+
for i, line in enumerate(lines):
50+
if any(token.type == InstructionTextTokenType.KeywordToken and token.text.startswith("return") for token in line.tokens):
51+
line.highlight = ret_highlight
52+
elif any(token.type == InstructionTextTokenType.KeywordToken and token.text == "noreturn" for token in line.tokens):
53+
line.highlight = noret_highlight
54+
return lines
55+
56+
def apply_to_flow_graph(self, graph: 'binaryninja.FlowGraph'):
57+
# Ignore flow graphs, as this Render Layer should only apply to Linear View.
58+
pass
59+
60+
61+
HighlightReturnsLayer.register()

0 commit comments

Comments
 (0)