diff options
| author | Glenn Smith <glenn@vector35.com> | 2024-12-27 16:14:46 -0500 |
|---|---|---|
| committer | Glenn Smith <glenn@vector35.com> | 2025-01-30 17:20:05 -0500 |
| commit | 8862696926173104957729683832591438161557 (patch) | |
| tree | 78ba6d7dc8144430136086c8dc84726171eec8ab /plugins/stack_render_layer/plugin.cpp | |
| parent | 5a5426d030b6be26d4564ba1eba2d8a275533256 (diff) | |
Render Layers
Diffstat (limited to 'plugins/stack_render_layer/plugin.cpp')
| -rw-r--r-- | plugins/stack_render_layer/plugin.cpp | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/plugins/stack_render_layer/plugin.cpp b/plugins/stack_render_layer/plugin.cpp new file mode 100644 index 00000000..b32ee470 --- /dev/null +++ b/plugins/stack_render_layer/plugin.cpp @@ -0,0 +1,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; + } +}
\ No newline at end of file |
