Skip to content

Commit 180adfd

Browse files
CouleeAppsemesare
authored andcommitted
Putting all of these into the base class
1 parent cf01c36 commit 180adfd

File tree

1 file changed

+65
-100
lines changed

1 file changed

+65
-100
lines changed

python/renderlayer.py

Lines changed: 65 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ def __getitem__(self, value):
5050
return CoreRenderLayer(handle)
5151

5252

53-
class BaseRenderLayer(metaclass=_RenderLayerMetaclass):
53+
class RenderLayer(metaclass=_RenderLayerMetaclass):
5454
"""
55-
BaseRenderLayer provides a lower-level interface for manipulating graphs
56-
and lines directly, whereas :py:class:`RenderLayer` provides an interface where you
57-
can choose to target a specific IL level or a couple different Linear View specific
58-
line constructs.
55+
RenderLayer is a plugin class that allows you to customize the presentation of
56+
Linear and Graph view output, adding, changing, or removing lines before they are
57+
presented in the UI.
5958
"""
6059

6160
name = None
@@ -88,7 +87,7 @@ def register(cls):
8887
cls._registered_layers.append(layer)
8988

9089
def __eq__(self, other):
91-
if not isinstance(other, BaseRenderLayer):
90+
if not isinstance(other, RenderLayer):
9291
return False
9392
return self.name == other.name
9493

@@ -138,87 +137,6 @@ def _free_lines(self, ctxt, lines, count):
138137
except:
139138
log_error(traceback.format_exc())
140139

141-
def apply_to_flow_graph(self, graph: 'binaryninja.FlowGraph') -> None:
142-
"""
143-
Apply this Render Layer to a Flow Graph, potentially modifying its nodes,
144-
their edges, their lines, and their lines' content.
145-
146-
:param graph: Graph to modify
147-
"""
148-
pass
149-
150-
def apply_to_linear_view_object(
151-
self,
152-
obj: 'binaryninja.LinearViewObject',
153-
prev: Optional['binaryninja.LinearViewObject'],
154-
next: Optional['binaryninja.LinearViewObject'],
155-
lines: List['binaryninja.LinearDisassemblyLine']
156-
) -> List['binaryninja.LinearDisassemblyLine']:
157-
"""
158-
Apply this Render Layer to the lines produced by a LinearViewObject for rendering
159-
in Linear View, potentially modifying the lines and their contents.
160-
161-
:param obj: Linear View Object being rendered
162-
:param prev: Linear View Object located directly above this one
163-
:param next: Linear View Object located directly below this one
164-
:param lines: Lines originally rendered by the Linear View Object
165-
:return: Updated set of lines to display in Linear View
166-
"""
167-
return lines
168-
169-
170-
class CoreRenderLayer(BaseRenderLayer):
171-
172-
def apply_to_flow_graph(self, graph: 'binaryninja.FlowGraph') -> None:
173-
core.BNApplyRenderLayerToFlowGraph(self.handle, graph.handle)
174-
175-
def apply_to_linear_view_object(
176-
self,
177-
obj: 'binaryninja.LinearViewObject',
178-
prev: Optional['binaryninja.LinearViewObject'],
179-
next: Optional['binaryninja.LinearViewObject'],
180-
lines: List['binaryninja.LinearDisassemblyLine']
181-
) -> List['binaryninja.LinearDisassemblyLine']:
182-
183-
in_lines_buf = (core.BNLinearDisassemblyLine * len(lines))()
184-
for i, r in enumerate(lines):
185-
in_lines_buf[i] = r._to_core_struct()
186-
187-
out_lines = ctypes.POINTER(core.BNLinearDisassemblyLine)()
188-
out_line_count = ctypes.c_size_t(0)
189-
190-
core.BNApplyRenderLayerToLinearViewObject(
191-
self.handle,
192-
obj.handle,
193-
prev.handle if prev is not None else None,
194-
next.handle if next is not None else None,
195-
in_lines_buf,
196-
len(lines),
197-
out_lines,
198-
out_line_count
199-
)
200-
201-
result = []
202-
for i in range(out_line_count.value):
203-
result.append(binaryninja.LinearDisassemblyLine._from_core_struct(out_lines[i], obj=obj))
204-
205-
core.BNFreeLinearDisassemblyLines(out_lines, out_line_count.value)
206-
207-
return result
208-
209-
210-
class RenderLayer(BaseRenderLayer):
211-
"""
212-
RenderLayer is a plugin class that allows you to customize the presentation of
213-
Linear and Graph view output, adding, changing, or removing lines before they are
214-
presented in the UI.
215-
216-
BaseRenderLayer provides a lower-level interface for manipulating the graphs
217-
and lines directly, whereas RenderLayer provides an interface where you can
218-
choose to target a specific IL level or a couple different Linear View specific
219-
line constructs.
220-
"""
221-
222140
def apply_to_disassembly_block(
223141
self,
224142
block: 'binaryninja.BasicBlock',
@@ -286,15 +204,15 @@ def apply_to_high_level_il_block(
286204
You can use the block's `function_graph_type` property to determine which is being handled.
287205
288206
.. warning:: This function will NOT apply to High Level IL bodies as displayed \
289-
in Linear View! Those are handled by `apply_to_hlil_body` instead as they \
207+
in Linear View! Those are handled by `apply_to_high_level_il_body` instead as they \
290208
do not have a Basic Block associated with them.
291209
292210
:param block: Basic Block containing those lines
293211
:param lines: Lines of text for the block, to be modified by this function
294212
"""
295213
pass
296214

297-
def apply_to_hlil_body(
215+
def apply_to_high_level_il_body(
298216
self,
299217
function: 'binaryninja.Function',
300218
lines: List['binaryninja.LinearDisassemblyLine']
@@ -338,7 +256,7 @@ def apply_to_block(
338256
lines: List['binaryninja.DisassemblyTextLine'],
339257
):
340258
"""
341-
Apply to lines generated by a Basic Block, of any type. If not overriden, this
259+
Apply to lines generated by a Basic Block, of any type. If not overridden, this
342260
function will call the appropriate apply_to_X_level_il_block function.
343261
344262
:param block: Basic Block containing those lines
@@ -358,12 +276,16 @@ def apply_to_block(
358276

359277
def apply_to_flow_graph(self, graph: 'binaryninja.FlowGraph') -> None:
360278
"""
361-
Overridden from BaseRenderLayer to provide more specific callbacks. Check
362-
:py:func:`BaseRenderLayer.apply_to_flow_graph` in case you want to
363-
override this directly.
279+
Apply this Render Layer to a Flow Graph, potentially modifying its nodes,
280+
their edges, their lines, and their lines' content.
281+
282+
.. note:: If you override this function, you will need to call the ``super()`` \
283+
implementation if you want to use the higher level ``apply_to_X_level_il_block`` \
284+
functionality.
364285
365286
:param graph: Graph to modify
366287
"""
288+
pass
367289
for i, node in enumerate(graph.nodes):
368290
lines = node.lines
369291
if node.basic_block is not None and isinstance(node.basic_block, binaryninja.BasicBlock):
@@ -378,9 +300,12 @@ def apply_to_linear_view_object(
378300
lines: List['binaryninja.LinearDisassemblyLine']
379301
) -> List['binaryninja.LinearDisassemblyLine']:
380302
"""
381-
Overridden from BaseRenderLayer to provide more specific callbacks. Check
382-
:py:func:`BaseRenderLayer.apply_to_linear_view_object` in case you want to
383-
override this directly.
303+
Apply this Render Layer to the lines produced by a LinearViewObject for rendering
304+
in Linear View, potentially modifying the lines and their contents.
305+
306+
.. note:: If you override this function, you will need to call the ``super()`` \
307+
implementation if you want to use the higher level ``apply_to_X_level_il_block`` \
308+
functionality.
384309
385310
:param obj: Linear View Object being rendered
386311
:param prev: Linear View Object located directly above this one
@@ -389,12 +314,12 @@ def apply_to_linear_view_object(
389314
:return: Updated set of lines to display in Linear View
390315
"""
391316
# Hack: HLIL bodies don't have basic blocks
392-
if obj.identifier.name in [
317+
if len(lines) > 0 and obj.identifier.name in [
393318
"HLIL Function Body",
394319
"HLIL SSA Function Body",
395320
"Language Representation Function Body"
396321
]:
397-
self.apply_to_hlil_body(lines[0].function, lines)
322+
self.apply_to_high_level_il_body(lines[0].function, lines)
398323
return lines
399324

400325
block_lines = []
@@ -407,8 +332,8 @@ def apply_to_linear_view_object(
407332
if last_block is not None:
408333
# Convert linear lines to disassembly lines for the apply()
409334
# and then convert back for linear view
410-
disasm_lines = [line.contents for line in block_lines]
411-
disasm_map = {id(line.contents): line for line in block_lines}
335+
disasm_lines = [block_line.contents for block_line in block_lines]
336+
disasm_map = {id(block_line.contents): block_line for block_line in block_lines}
412337
self.apply_to_block(last_block, disasm_lines)
413338
block_lines = [
414339
LinearDisassemblyLine(
@@ -445,3 +370,43 @@ def apply_to_linear_view_object(
445370
self.apply_to_misc_linear_lines(obj, prev, next, block_lines)
446371
final_lines += block_lines
447372
return final_lines
373+
374+
375+
class CoreRenderLayer(RenderLayer):
376+
377+
def apply_to_flow_graph(self, graph: 'binaryninja.FlowGraph') -> None:
378+
core.BNApplyRenderLayerToFlowGraph(self.handle, graph.handle)
379+
380+
def apply_to_linear_view_object(
381+
self,
382+
obj: 'binaryninja.LinearViewObject',
383+
prev: Optional['binaryninja.LinearViewObject'],
384+
next: Optional['binaryninja.LinearViewObject'],
385+
lines: List['binaryninja.LinearDisassemblyLine']
386+
) -> List['binaryninja.LinearDisassemblyLine']:
387+
388+
in_lines_buf = (core.BNLinearDisassemblyLine * len(lines))()
389+
for i, r in enumerate(lines):
390+
in_lines_buf[i] = r._to_core_struct()
391+
392+
out_lines = ctypes.POINTER(core.BNLinearDisassemblyLine)()
393+
out_line_count = ctypes.c_size_t(0)
394+
395+
core.BNApplyRenderLayerToLinearViewObject(
396+
self.handle,
397+
obj.handle,
398+
prev.handle if prev is not None else None,
399+
next.handle if next is not None else None,
400+
in_lines_buf,
401+
len(lines),
402+
out_lines,
403+
out_line_count
404+
)
405+
406+
result = []
407+
for i in range(out_line_count.value):
408+
result.append(binaryninja.LinearDisassemblyLine._from_core_struct(out_lines[i], obj=obj))
409+
410+
core.BNFreeLinearDisassemblyLines(out_lines, out_line_count.value)
411+
412+
return result

0 commit comments

Comments
 (0)