Skip to content

Commit 8862696

Browse files
committed
Render Layers
1 parent 5a5426d commit 8862696

35 files changed

+3112
-122
lines changed

basicblock.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,12 @@ bool BasicBlock::IsMediumLevelILBlock() const
592592
}
593593

594594

595+
bool BasicBlock::IsHighLevelILBlock() const
596+
{
597+
return BNIsHighLevelILBasicBlock(m_object);
598+
}
599+
600+
595601
Ref<LowLevelILFunction> BasicBlock::GetLowLevelILFunction() const
596602
{
597603
BNLowLevelILFunction* func = BNGetBasicBlockLowLevelILFunction(m_object);

binaryninjaapi.h

Lines changed: 312 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,7 +4073,9 @@ namespace BinaryNinja {
40734073
Ref<BasicBlock> block;
40744074
DisassemblyTextLine contents;
40754075

4076-
static LinearDisassemblyLine FromAPIObject(BNLinearDisassemblyLine* line);
4076+
BNLinearDisassemblyLine GetAPIObject() const;
4077+
static LinearDisassemblyLine FromAPIObject(const BNLinearDisassemblyLine* line);
4078+
static void FreeAPIObject(BNLinearDisassemblyLine* line);
40774079
};
40784080

40794081
class NamedTypeReference;
@@ -10414,17 +10416,23 @@ namespace BinaryNinja {
1041410416
*/
1041510417
bool IsILBlock() const;
1041610418

10419+
/*! Whether the basic block contains Low Level IL
10420+
10421+
\return Whether the basic block contains Low Level IL
10422+
*/
10423+
bool IsLowLevelILBlock() const;
10424+
1041710425
/*! Whether the basic block contains Medium Level IL
1041810426

1041910427
\return Whether the basic block contains Medium Level IL
1042010428
*/
10421-
bool IsLowLevelILBlock() const;
10429+
bool IsMediumLevelILBlock() const;
1042210430

1042310431
/*! Whether the basic block contains High Level IL
1042410432

1042510433
\return Whether the basic block contains High Level IL
1042610434
*/
10427-
bool IsMediumLevelILBlock() const;
10435+
bool IsHighLevelILBlock() const;
1042810436

1042910437
/*! Get the Low Level IL Function for this basic block
1043010438

@@ -11443,6 +11451,12 @@ namespace BinaryNinja {
1144311451
*/
1144411452
Ref<FlowGraphNode> GetNode(size_t i);
1144511453

11454+
/*! Get the total number of nodes in the graph
11455+
11456+
\return Node count
11457+
*/
11458+
size_t GetNodeCount() const;
11459+
1144611460
/*! Whether the FlowGraph has any nodes added
1144711461

1144811462
\return Whether the FlowGraph has any nodes added
@@ -11451,12 +11465,29 @@ namespace BinaryNinja {
1145111465

1145211466
/*! Add a node to this flowgraph
1145311467

11468+
\note After the graph has completed layout, this function has no effect.
11469+
1145411470
\param node Node to be added.
1145511471
\return Index of the node
1145611472
*/
1145711473
size_t AddNode(FlowGraphNode* node);
1145811474

11475+
/*! Replace an existing node in the graph with a new node.
11476+
Any existing edges referencing the old node will be updated to point to
11477+
the new node.
1145911478

11479+
\note After the graph has completed layout, this function has no effect.
11480+
11481+
\param i Index of the node to replace
11482+
\param newNode New node with which to replace the old node
11483+
*/
11484+
void ReplaceNode(size_t i, FlowGraphNode* newNode);
11485+
11486+
/*! Clear all the nodes in the graph
11487+
11488+
\note After the graph has completed layout, this function has no effect.
11489+
*/
11490+
void ClearNodes();
1146011491

1146111492
/*! Flow graph width
1146211493

@@ -11548,6 +11579,26 @@ namespace BinaryNinja {
1154811579

1154911580
void SetOption(BNFlowGraphOption option, bool value = true);
1155011581
bool IsOptionSet(BNFlowGraphOption option);
11582+
11583+
/*! Get the list of Render Layers which will be applied to this Flow Graph,
11584+
after it calls PopulateNodes.
11585+
11586+
\return List of Render Layers
11587+
*/
11588+
std::vector<class RenderLayer*> GetRenderLayers() const;
11589+
11590+
/*! Add a Render Layer to be applied to this Flow Graph. Note that layers will
11591+
be applied in the order in which they are added.
11592+
11593+
\param layer Render Layer to add
11594+
*/
11595+
void AddRenderLayer(class RenderLayer* layer);
11596+
11597+
/*! Remove a Render Layer from being applied to this Flow Graph
11598+
11599+
\param layer Render Layer to remove
11600+
*/
11601+
void RemoveRenderLayer(class RenderLayer* layer);
1155111602
};
1155211603

1155311604
/*!
@@ -17101,6 +17152,26 @@ namespace BinaryNinja {
1710117152

1710217153
Ref<LinearViewCursor> Duplicate();
1710317154

17155+
/*! Get the list of Render Layers which will be applied to this cursor, at the
17156+
end of calls to GetLines.
17157+
17158+
\return List of Render Layers
17159+
*/
17160+
std::vector<class RenderLayer*> GetRenderLayers() const;
17161+
17162+
/*! Add a Render Layer to be applied to this cursor. Note that layers will
17163+
be applied in the order in which they are added.
17164+
17165+
\param layer Render Layer to add
17166+
*/
17167+
void AddRenderLayer(class RenderLayer* layer);
17168+
17169+
/*! Remove a Render Layer from being applied to this cursor
17170+
17171+
\param layer Render Layer to remove
17172+
*/
17173+
void RemoveRenderLayer(class RenderLayer* layer);
17174+
1710417175
static int Compare(LinearViewCursor* a, LinearViewCursor* b);
1710517176
};
1710617177

@@ -19007,6 +19078,244 @@ namespace BinaryNinja {
1900719078
static void AddNamesForOuterStructureMembers(
1900819079
BinaryView* data, Type* type, const HighLevelILInstruction& var, std::vector<std::string>& nameList);
1900919080
};
19081+
19082+
/*! RenderLayer is a plugin class that allows you to customize the presentation of
19083+
Linear and Graph view output, adding, changing, or removing lines before they are
19084+
presented in the UI.
19085+
*/
19086+
class RenderLayer: public StaticCoreRefCountObject<BNRenderLayer>
19087+
{
19088+
std::string m_nameForRegister;
19089+
static std::unordered_map<BNRenderLayer*, RenderLayer*> g_registeredInstances;
19090+
19091+
protected:
19092+
explicit RenderLayer(const std::string& name);
19093+
RenderLayer(BNRenderLayer* layer);
19094+
virtual ~RenderLayer() = default;
19095+
static void ApplyToFlowGraphCallback(void* ctxt, BNFlowGraph* graph);
19096+
static void ApplyToLinearViewObjectCallback(
19097+
void* ctxt,
19098+
BNLinearViewObject* obj,
19099+
BNLinearViewObject* prev,
19100+
BNLinearViewObject* next,
19101+
BNLinearDisassemblyLine* inLines,
19102+
size_t inLineCount,
19103+
BNLinearDisassemblyLine** outLines,
19104+
size_t* outLineCount
19105+
);
19106+
static void FreeLinesCallback(void* ctxt, BNLinearDisassemblyLine* lines, size_t count);
19107+
19108+
public:
19109+
/*! Register a custom Render Layer.
19110+
19111+
\param layer Render Layer to register
19112+
\param enableState Whether the layer should be enabled by default
19113+
*/
19114+
static void Register(RenderLayer* layer, BNRenderLayerDefaultEnableState enableState = DisabledByDefaultRenderLayerDefaultEnableState);
19115+
19116+
/*! Get the list of all currently registered Render Layers.
19117+
19118+
\return List of Render Layers
19119+
*/
19120+
static std::vector<Ref<RenderLayer>> GetList();
19121+
19122+
/*! Look up a Render Layer by its name
19123+
19124+
\param name Name of Render Layer
19125+
\return Render Layer, if it exists. Otherwise, nullptr.
19126+
*/
19127+
static Ref<RenderLayer> GetByName(const std::string& name);
19128+
19129+
/*! Get the name of a Render Layer
19130+
19131+
\return Render Layer's name
19132+
*/
19133+
std::string GetName() const;
19134+
19135+
/*! Get whether the Render Layer is enabled by default
19136+
19137+
\return Default enable state
19138+
*/
19139+
BNRenderLayerDefaultEnableState GetDefaultEnableState() const;
19140+
19141+
/*! Apply this Render Layer to a single Basic Block of Disassembly lines.
19142+
Subclasses should modify the input `lines` list to make modifications to
19143+
the presentation of the block.
19144+
19145+
\note This function will only handle Disassembly lines, and not any ILs.
19146+
19147+
\param block Basic Block containing those lines
19148+
\param lines Lines of text for the block, to be modified by this function
19149+
*/
19150+
virtual void ApplyToDisassemblyBlock(
19151+
Ref<BasicBlock> block,
19152+
std::vector<DisassemblyTextLine>& lines
19153+
)
19154+
{
19155+
(void)block;
19156+
(void)lines;
19157+
}
19158+
19159+
/*! Apply this Render Layer to a single Basic Block of Low Level IL lines.
19160+
Subclasses should modify the input `lines` list to make modifications to
19161+
the presentation of the block.
19162+
19163+
\note This function will only handle Lifted IL/LLIL/LLIL(SSA) lines.
19164+
You can use the block's `function_graph_type` property to determine which is being handled.
19165+
19166+
\param block Basic Block containing those lines
19167+
\param lines Lines of text for the block, to be modified by this function
19168+
*/
19169+
virtual void ApplyToLowLevelILBlock(
19170+
Ref<BasicBlock> block,
19171+
std::vector<DisassemblyTextLine>& lines
19172+
)
19173+
{
19174+
(void)block;
19175+
(void)lines;
19176+
}
19177+
19178+
/*! Apply this Render Layer to a single Basic Block of Medium Level IL lines.
19179+
Subclasses should modify the input `lines` list to make modifications to
19180+
the presentation of the block.
19181+
19182+
\note This function will only handle MLIL/MLIL(SSA)/Mapped MLIL/Mapped MLIL(SSA) lines.
19183+
You can use the block's `function_graph_type` property to determine which is being handled.
19184+
19185+
\param block Basic Block containing those lines
19186+
\param lines Lines of text for the block, to be modified by this function
19187+
*/
19188+
virtual void ApplyToMediumLevelILBlock(
19189+
Ref<BasicBlock> block,
19190+
std::vector<DisassemblyTextLine>& lines
19191+
)
19192+
{
19193+
(void)block;
19194+
(void)lines;
19195+
}
19196+
19197+
/*! Apply this Render Layer to a single Basic Block of High Level IL lines.
19198+
Subclasses should modify the input `lines` list to make modifications to
19199+
the presentation of the block.
19200+
19201+
\note This function will only handle HLIL/HLIL(SSA)/Language Representation lines.
19202+
You can use the block's `function_graph_type` property to determine which is being handled.
19203+
19204+
\warning This function will NOT apply to High Level IL bodies as displayed
19205+
in Linear View! Those are handled by `ApplyToHighLevelILBody` instead as they
19206+
do not have a Basic Block associated with them.
19207+
19208+
\param block Basic Block containing those lines
19209+
\param lines Lines of text for the block, to be modified by this function
19210+
*/
19211+
virtual void ApplyToHighLevelILBlock(
19212+
Ref<BasicBlock> block,
19213+
std::vector<DisassemblyTextLine>& lines
19214+
)
19215+
{
19216+
(void)block;
19217+
(void)lines;
19218+
}
19219+
19220+
/*! Apply this Render Layer to the entire body of a High Level IL function.
19221+
Subclasses should modify the input `lines` list to make modifications to
19222+
the presentation of the function.
19223+
19224+
\warning This function only applies to Linear View, and not to Graph View!
19225+
If you want to handle Graph View too, you will need to use `ApplyToHighLevelILBlock`
19226+
and handle the lines one block at a time.
19227+
19228+
\param function Function containing those lines
19229+
\param lines Lines of text for the function, to be modified by this function
19230+
*/
19231+
virtual void ApplyToHighLevelILBody(
19232+
Ref<Function> function,
19233+
std::vector<LinearDisassemblyLine>& lines
19234+
)
19235+
{
19236+
(void)function;
19237+
(void)lines;
19238+
}
19239+
19240+
/*! Apply to lines generated by Linear View that are not part of a function.
19241+
It is up to your implementation to figure out which type of Linear View Object
19242+
lines these are, and what to do with them.
19243+
19244+
\param obj Linear View Object being rendered
19245+
\param prev Linear View Object located directly above this one
19246+
\param next Linear View Object located directly below this one
19247+
\param lines Lines rendered by `obj`, to be modified by this function
19248+
*/
19249+
virtual void ApplyToMiscLinearLines(
19250+
Ref<LinearViewObject> obj,
19251+
Ref<LinearViewObject> prev,
19252+
Ref<LinearViewObject> next,
19253+
std::vector<LinearDisassemblyLine>& lines
19254+
)
19255+
{
19256+
(void)obj;
19257+
(void)prev;
19258+
(void)next;
19259+
(void)lines;
19260+
}
19261+
19262+
/*! Apply to lines generated by a Basic Block, of any type. If not overridden, this
19263+
function will call the appropriate ApplyToXLevelILBlock function.
19264+
19265+
\param block Basic Block containing those lines
19266+
\param lines Lines of text for the block, to be modified by this function
19267+
*/
19268+
virtual void ApplyToBlock(
19269+
Ref<BasicBlock> block,
19270+
std::vector<DisassemblyTextLine>& lines
19271+
);
19272+
19273+
/*! Apply this Render Layer to a Flow Graph, potentially modifying its nodes,
19274+
their edges, their lines, and their lines' content.
19275+
19276+
\note If you override this function, you will need to call the parent
19277+
implementation if you want to use the higher level ApplyToXLevelILBlock
19278+
functionality.
19279+
19280+
\param graph Graph to modify
19281+
*/
19282+
virtual void ApplyToFlowGraph(Ref<FlowGraph> graph);
19283+
19284+
/*! Apply this Render Layer to the lines produced by a LinearViewObject for rendering
19285+
in Linear View, potentially modifying the lines and their contents.
19286+
19287+
\note If you override this function, you will need to call the parent
19288+
implementation if you want to use the higher level ApplyToXLevelILBlock
19289+
functionality.
19290+
19291+
\param obj Linear View Object being rendered
19292+
\param prev Linear View Object located directly above this one
19293+
\param next Linear View Object located directly below this one
19294+
\param lines Lines originally rendered by the Linear View Object
19295+
\return Updated set of lines to display in Linear View
19296+
*/
19297+
virtual void ApplyToLinearViewObject(
19298+
Ref<LinearViewObject> obj,
19299+
Ref<LinearViewObject> prev,
19300+
Ref<LinearViewObject> next,
19301+
std::vector<LinearDisassemblyLine>& lines
19302+
);
19303+
};
19304+
19305+
class CoreRenderLayer: public RenderLayer
19306+
{
19307+
public:
19308+
CoreRenderLayer(BNRenderLayer* layer);
19309+
virtual ~CoreRenderLayer() = default;
19310+
19311+
virtual void ApplyToFlowGraph(Ref<FlowGraph> graph) override;
19312+
virtual void ApplyToLinearViewObject(
19313+
Ref<LinearViewObject> obj,
19314+
Ref<LinearViewObject> prev,
19315+
Ref<LinearViewObject> next,
19316+
std::vector<LinearDisassemblyLine>& lines
19317+
) override;
19318+
};
1901019319
} // namespace BinaryNinja
1901119320

1901219321

0 commit comments

Comments
 (0)