summaryrefslogtreecommitdiff
path: root/examples/workflows/inliner/inliner.cpp
diff options
context:
space:
mode:
authorBrian Potchik <brian@vector35.com>2021-08-13 18:48:51 -0400
committerBrian Potchik <brian@vector35.com>2021-08-13 18:48:51 -0400
commit801b4138784a45643a7201b7e54105ea5eb3b6c4 (patch)
tree02916c8e665e7dbc16958b55abe08ff48372abf2 /examples/workflows/inliner/inliner.cpp
parentac70b362bb4f15b1d1eeced7a44a222f663e695f (diff)
Workflows early preview.
Diffstat (limited to 'examples/workflows/inliner/inliner.cpp')
-rw-r--r--examples/workflows/inliner/inliner.cpp159
1 files changed, 159 insertions, 0 deletions
diff --git a/examples/workflows/inliner/inliner.cpp b/examples/workflows/inliner/inliner.cpp
new file mode 100644
index 00000000..01eacbd2
--- /dev/null
+++ b/examples/workflows/inliner/inliner.cpp
@@ -0,0 +1,159 @@
+#define _CRT_SECURE_NO_WARNINGS
+#define NOMINMAX
+
+#include <cinttypes>
+#include <cstdint>
+#include <cstdio>
+#include <string>
+#include <tuple>
+#include <unordered_map>
+
+#include "binaryninjaapi.h"
+#include "lowlevelilinstruction.h"
+#include "mediumlevelilinstruction.h"
+
+
+using namespace BinaryNinja;
+using namespace std;
+
+#if defined(_MSC_VER)
+#define snprintf _snprintf
+#endif
+
+
+extern "C"
+{
+ BN_DECLARE_CORE_ABI_VERSION
+
+ unordered_map<uint64_t, set<uint64_t>> g_callSiteInlines;
+
+ void FunctionInliner(Ref<AnalysisContext> analysisContext)
+ {
+ Ref<Function> function = analysisContext->GetFunction();
+ Ref<BinaryView> data = function->GetView();
+
+ set<uint64_t> callSiteInlines;
+ if (const auto& itr = g_callSiteInlines.find(function->GetStart()); itr != g_callSiteInlines.end())
+ callSiteInlines = itr->second;
+
+ bool updated = false;
+ uint8_t opcode[BN_MAX_INSTRUCTION_LENGTH];
+ InstructionInfo iInfo;
+ Ref<LowLevelILFunction> llilFunc = analysisContext->GetLowLevelILFunction();
+ for (const auto inlineAddr : callSiteInlines)
+ {
+ // if (m_owner->IsAborted())
+ // return;
+
+ for (auto& i : llilFunc->GetBasicBlocks())
+ {
+ Ref<Architecture> arch = i->GetArchitecture();
+ for (size_t instrIndex = i->GetStart(); instrIndex < i->GetEnd(); instrIndex++)
+ {
+ LowLevelILInstruction instr = llilFunc->GetInstruction(instrIndex);
+
+ if (instr.address != inlineAddr)
+ continue;
+
+ if (instr.operation != LLIL_CALL)
+ {
+ LogWarn("Failed to inline function at: 0x%" PRIx64 ". Mapping to LLIL_CALL Failed!", instr.address);
+ continue;
+ }
+
+ uint64_t platformAddr;
+ LowLevelILInstruction destExpr = instr.GetDestExpr<LLIL_CALL>();
+ RegisterValue target = destExpr.GetValue();
+ if (target.IsConstant())
+ platformAddr = target.value;
+ else
+ {
+ LogWarn("Failed to inline function at: 0x%" PRIx64 ". Destination not Constant!", instr.address);
+ continue;
+ }
+
+ size_t opLen = data->Read(opcode, instr.address, arch->GetMaxInstructionLength());
+ if (!opLen || !arch->GetInstructionInfo(opcode, instr.address, opLen, iInfo))
+ continue;
+ Ref<Platform> platform = iInfo.archTransitionByTargetAddr ? function->GetPlatform()->GetAssociatedPlatformByAddress(platformAddr) : function->GetPlatform();
+ if (platform)
+ {
+ Ref<Function> targetFunc = data->GetAnalysisFunction(platform, platformAddr);
+ auto targetLlil = targetFunc->GetLowLevelIL();
+ LowLevelILLabel inlineStartLabel;
+ llilFunc->MarkLabel(inlineStartLabel);
+ instr.Replace(llilFunc->Goto(inlineStartLabel));
+
+ llilFunc->PrepareToCopyFunction(targetLlil);
+ for (auto& ti : targetLlil->GetBasicBlocks())
+ {
+ llilFunc->PrepareToCopyBlock(ti);
+ for (size_t tinstrIndex = ti->GetStart(); tinstrIndex < ti->GetEnd(); tinstrIndex++)
+ {
+ LowLevelILInstruction tinstr = targetLlil->GetInstruction(tinstrIndex);
+ if (tinstr.operation == LLIL_RET)
+ {
+ LowLevelILLabel label;
+ label.operand = instrIndex + 1;
+ llilFunc->AddInstruction(llilFunc->Pop(instr.size));
+ llilFunc->AddInstruction(llilFunc->Goto(label));
+ }
+ else
+ llilFunc->AddInstruction(tinstr.CopyTo(llilFunc));
+ }
+ }
+ llilFunc->Finalize();
+ }
+
+ updated = true;
+ break;
+ }
+ }
+ }
+
+ if (!updated)
+ return;
+
+ // Updates found, regenerate SSA form
+ llilFunc->GenerateSSAForm();
+ }
+
+
+ BINARYNINJAPLUGIN bool CorePluginInit()
+ {
+ auto inlinerIsValid = [](BinaryView* view, Function* func) {
+ if (auto workflow = func->GetWorkflow(); workflow)
+ return workflow->Contains("extension.functionInliner");
+ return false;
+ };
+
+ // PluginCommand::RegisterForFunction(
+ // "Optimizer\\Inline All Calls to Current Function",
+ // "Inline all calls to the current function.",
+ // [](BinaryView* view, Function* func) {
+ // LogError("TODO Inline Current Function: %" PRIx64, func->GetStart());
+ // }, inlinerIsValid);
+
+ PluginCommand::RegisterForFunction(
+ "Optimizer\\Inline Function at Current Call Site",
+ "Inline function call at current call site.",
+ [](BinaryView* view, Function* func) {
+ // TODO func->Inform("inlinedCallSites")
+ // TODO resolve multiple embedded inlines
+ g_callSiteInlines[func->GetStart()].insert(view->GetCurrentOffset());
+ func->Reanalyze();
+ }, inlinerIsValid);
+
+ Ref<Workflow> inlinerWorkflow = Workflow::Instance()->Clone("InlinerWorkflow");
+ inlinerWorkflow->RegisterActivity(new Activity("extension.functionInliner", &FunctionInliner));
+ inlinerWorkflow->Insert("core.function.translateTailCalls", "extension.functionInliner");
+ Workflow::RegisterWorkflow(inlinerWorkflow,
+ R"#({
+ "title" : "Function Inliner (Example)",
+ "description" : "This analysis stands in as an example to demonstrate Binary Ninja's extensible analysis APIs. ***Note** this feature is under active development and subject to change without notice.",
+ "capabilities" : []
+ })#");
+
+ return true;
+ }
+}