diff options
| author | Brian Potchik <brian@vector35.com> | 2021-08-13 18:48:51 -0400 |
|---|---|---|
| committer | Brian Potchik <brian@vector35.com> | 2021-08-13 18:48:51 -0400 |
| commit | 801b4138784a45643a7201b7e54105ea5eb3b6c4 (patch) | |
| tree | 02916c8e665e7dbc16958b55abe08ff48372abf2 /examples/workflows/inliner | |
| parent | ac70b362bb4f15b1d1eeced7a44a222f663e695f (diff) | |
Workflows early preview.
Diffstat (limited to 'examples/workflows/inliner')
| -rw-r--r-- | examples/workflows/inliner/CMakeLists.txt | 39 | ||||
| -rw-r--r-- | examples/workflows/inliner/inliner.cpp | 159 |
2 files changed, 198 insertions, 0 deletions
diff --git a/examples/workflows/inliner/CMakeLists.txt b/examples/workflows/inliner/CMakeLists.txt new file mode 100644 index 00000000..59611afb --- /dev/null +++ b/examples/workflows/inliner/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.9 FATAL_ERROR) + +project(workflow_inliner) + +if((NOT BN_API_PATH) AND (NOT BN_INTERNAL_BUILD)) + set(BN_API_PATH $ENV{BN_API_PATH}) + if(NOT BN_API_PATH) + message(FATAL_ERROR "Provide path to Binary Ninja API source in BN_API_PATH") + endif() +endif() +if(NOT BN_INTERNAL_BUILD) + add_subdirectory(${BN_API_PATH} ${PROJECT_BINARY_DIR}/api) +endif() + +file(GLOB SOURCES + *.cpp + *.c + *.h) + +add_library(workflow_inliner SHARED ${SOURCES}) + +target_link_libraries(workflow_inliner binaryninjaapi) + +set_target_properties(workflow_inliner PROPERTIES + CXX_STANDARD 17 + CXX_VISIBILITY_PRESET hidden + CXX_STANDARD_REQUIRED ON + C_STANDARD 99 + C_STANDARD_REQUIRED ON + C_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN ON + POSITION_INDEPENDENT_CODE ON) + +if(BN_INTERNAL_BUILD) + plugin_rpath(workflow_inliner) + set_target_properties(workflow_inliner PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${BN_CORE_PLUGIN_DIR} + RUNTIME_OUTPUT_DIRECTORY ${BN_CORE_PLUGIN_DIR}) +endif() 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; + } +} |
