summaryrefslogtreecommitdiff
path: root/python/examples/highlight_returns_render_layer.py
blob: 11665aa6832a2e7c0d01542c50af6aa555cd982b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from typing import List, Optional

import binaryninja
from binaryninja import RenderLayer, InstructionTextToken, \
    InstructionTextTokenType, HighlightColor, ThemeColor
from binaryninjaui import getThemeColor


"""
Render Layer that highlights all lines in Linear View that have a return statement
"""


class HighlightReturnsLayer(RenderLayer):
    name = "Highlight Returns"

    # Highlighting in all ILs includes handling both normal blocks (up to MLIL) and HLIL bodies,
    # since HLIL is special and gets its own handler.
    def apply_to_high_level_il_body(
            self,
            function: 'binaryninja.Function',
            lines: List['binaryninja.LinearDisassemblyLine']
    ):
        ret_color = getThemeColor(ThemeColor.GraphExitNodeIndicatorColor)
        noret_color = getThemeColor(ThemeColor.GraphExitNoreturnNodeIndicatorColor)
        ret_highlight = HighlightColor(red=ret_color.red(), green=ret_color.green(), blue=ret_color.blue())
        noret_highlight = HighlightColor(red=noret_color.red(), green=noret_color.green(), blue=noret_color.blue())

        # Basic check: if the line has a keyword token which is "return" or "noreturn"
        for i, line in enumerate(lines):
            if any(token.type == InstructionTextTokenType.KeywordToken and token.text.startswith("return") for token in line.contents.tokens):
                line.contents.highlight = ret_highlight
            elif any(token.type == InstructionTextTokenType.KeywordToken and token.text == "noreturn" for token in line.contents.tokens):
                line.contents.highlight = noret_highlight
        return lines

    # Applies to MLIL and lower
    def apply_to_block(
            self,
            block: 'binaryninja.BasicBlock',
            lines: List['binaryninja.DisassemblyTextLine']
    ):
        ret_color = getThemeColor(ThemeColor.GraphExitNodeIndicatorColor)
        noret_color = getThemeColor(ThemeColor.GraphExitNoreturnNodeIndicatorColor)
        ret_highlight = HighlightColor(red=ret_color.red(), green=ret_color.green(), blue=ret_color.blue())
        noret_highlight = HighlightColor(red=noret_color.red(), green=noret_color.green(), blue=noret_color.blue())

        # Basic check: if the line has a keyword token which is "return" or "noreturn"
        for i, line in enumerate(lines):
            if any(token.type == InstructionTextTokenType.KeywordToken and token.text.startswith("return") for token in line.tokens):
                line.highlight = ret_highlight
            elif any(token.type == InstructionTextTokenType.KeywordToken and token.text == "noreturn" for token in line.tokens):
                line.highlight = noret_highlight
        return lines

    def apply_to_flow_graph(self, graph: 'binaryninja.FlowGraph'):
        # Ignore flow graphs, as this Render Layer should only apply to Linear View.
        pass


HighlightReturnsLayer.register()