summaryrefslogtreecommitdiff
path: root/plugins/stack_render_layer/plugin.cpp
blob: b32ee47019a643a110991bca9ea818498b7b741e (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#include <binaryninjaapi.h>
#include <thread>

using namespace BinaryNinja;


class StackRenderLayer: public RenderLayer
{
public:
	StackRenderLayer(): RenderLayer("Annotate Stack Offset") {}

	void ApplyToLines(
		Ref<BasicBlock> block,
		std::vector<DisassemblyTextLine>& lines
	)
	{
		for (auto& line: lines)
		{
			// Skip blank lines (block separators)
			if (line.tokens.empty())
			{
				continue;
			}

			// Insert tokens after the address separator
			int64_t sep = -1;
			for (int64_t i = 0; i < line.tokens.size(); i ++)
			{
				if (line.tokens[i].type == AddressSeparatorToken)
				{
					sep = i;
					break;
				}
			}
			// Don't annotate lines which don't have an address separator
			// (these are usually annotations like { Does not return }
			if (sep == -1)
			{
				continue;
			}

			// Grab stack offset value from function
			auto stackOffset = block->GetFunction()->GetRegisterValueAtInstruction(
				block->GetArchitecture(),
				line.addr,
				block->GetArchitecture()->GetStackPointerRegister()
			);
			auto stackOffsetAfter = block->GetFunction()->GetRegisterValueAfterInstruction(
				block->GetArchitecture(),
				line.addr,
				block->GetArchitecture()->GetStackPointerRegister()
			);
			if (stackOffset.state == StackFrameOffset)
			{
				// Stack pointer is resolved to an offset: show the offset
				// (but negative because that is how other tools do it)
				line.tokens.emplace(
					line.tokens.begin() + sep + 1,
					IntegerToken,
					fmt::format("{:4x}", -stackOffset.value),
					-stackOffset.value
				);
			}
			else
			{
				// Stack pointer is not resolved, show ??
				line.tokens.emplace(
					line.tokens.begin() + sep + 1,
					IntegerToken,
					"  ??",
					0
				);
			}
			// And put a spacer after the offset token
			if (stackOffset != stackOffsetAfter)
			{
				line.tokens.emplace(
					line.tokens.begin() + sep + 2,
					TextToken,
					"* "
				);
			}
			else
			{
				line.tokens.emplace(
					line.tokens.begin() + sep + 2,
					TextToken,
					"  "
				);
			}
		}
	}

	virtual void ApplyToDisassemblyBlock(
		Ref<BasicBlock> block,
		std::vector<DisassemblyTextLine>& lines
	) override
	{
		// Break this out into a helper so we don't have to write it twice
		ApplyToLines(block, lines);
	}

	virtual void ApplyToLowLevelILBlock(
		Ref<BasicBlock> block,
		std::vector<DisassemblyTextLine>& lines
	) override
	{
		// Break this out into a helper so we don't have to write it twice
		ApplyToLines(block, lines);
	}
};


extern "C" {
	BN_DECLARE_CORE_ABI_VERSION

	BINARYNINJAPLUGIN bool CorePluginInit()
	{
		static StackRenderLayer* layer = new StackRenderLayer();
		RenderLayer::Register(layer, DisabledByDefaultRenderLayerDefaultEnableState);
		return true;
	}
}