Skip to content

Commit 4e851c2

Browse files
committed
Add constant renderer API
1 parent 457fb75 commit 4e851c2

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

binaryninjacore.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ extern "C"
309309
typedef struct BNStringRef BNStringRef;
310310
typedef struct BNIndirectBranchInfo BNIndirectBranchInfo;
311311
typedef struct BNArchitectureAndAddress BNArchitectureAndAddress;
312+
typedef struct BNConstantRenderer BNConstantRenderer;
312313

313314
typedef bool(*BNProgressFunction)(void*, size_t, size_t);
314315

@@ -3778,6 +3779,15 @@ extern "C"
37783779
char* value;
37793780
} BNTypeAttribute;
37803781

3782+
typedef struct BNCustomConstantRenderer
3783+
{
3784+
void* context;
3785+
void (*freeObject)(void* ctxt);
3786+
bool (*isValidForType)(void* ctxt, BNHighLevelILFunction* hlil, BNType* type);
3787+
bool (*renderConstantPointer)(void* ctxt, BNHighLevelILFunction* hlil, size_t expr, BNType* type, int64_t val,
3788+
BNHighLevelILTokenEmitter* tokens, BNDisassemblySettings* settings, BNOperatorPrecedence precedence);
3789+
} BNCustomConstantRenderer;
3790+
37813791
BINARYNINJACOREAPI char* BNAllocString(const char* contents);
37823792
BINARYNINJACOREAPI char* BNAllocStringWithLength(const char* contents, size_t len);
37833793
BINARYNINJACOREAPI void BNFreeString(char* str);
@@ -8584,6 +8594,14 @@ extern "C"
85848594
BINARYNINJACOREAPI const char* BNGetStringRefContents(BNStringRef* ref);
85858595
BINARYNINJACOREAPI size_t BNGetStringRefSize(BNStringRef* ref);
85868596

8597+
// Constant Renderers
8598+
BINARYNINJACOREAPI BNConstantRenderer* BNRegisterConstantRenderer(
8599+
const char* name, BNCustomConstantRenderer* renderer);
8600+
BINARYNINJACOREAPI BNConstantRenderer* BNGetConstantRendererByName(const char* name);
8601+
BINARYNINJACOREAPI BNConstantRenderer** BNGetConstantRendererList(size_t* count);
8602+
BINARYNINJACOREAPI void BNFreeConstantRendererList(BNLanguageRepresentationFunctionType** renderers);
8603+
BINARYNINJACOREAPI char* BNGetConstantRendererName(BNConstantRenderer* renderer);
8604+
85878605
#ifdef __cplusplus
85888606
}
85898607
#endif

python/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
from .languagerepresentation import *
8383
from .lineformatter import *
8484
from .renderlayer import *
85+
from .constantrenderer import *
8586
# We import each of these by name to prevent conflicts between
8687
# log.py and the function 'log' which we don't import below
8788
from .log import (

python/constantrenderer.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) 2015-2025 Vector 35 Inc
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to
5+
# deal in the Software without restriction, including without limitation the
6+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7+
# sell copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19+
# IN THE SOFTWARE.
20+
21+
import traceback
22+
import ctypes
23+
from typing import Optional
24+
25+
import binaryninja
26+
from . import _binaryninjacore as core
27+
from . import function
28+
from . import enums
29+
from .log import log_error_for_exception
30+
from . import types
31+
from . import highlevelil
32+
from . import languagerepresentation
33+
34+
35+
class ConstantRenderer:
36+
_registered_renderers = []
37+
renderer_name = None
38+
39+
def __init__(self, handle=None):
40+
if handle is not None:
41+
self.handle = core.handle_of_type(handle, core.BNConstantRenderer)
42+
43+
def register(self):
44+
if self.__class__.renderer_name is None:
45+
raise ValueError("Renderer name is missing")
46+
self._cb = core.BNCustomConstantRenderer()
47+
self._cb.context = 0
48+
self._cb.isValidForType = self._cb.isValidForType.__class__(self._is_valid_for_type)
49+
self._cb.renderConstantPointer = self._cb.renderConstantPointer.__class__(self._render_constant_pointer)
50+
self.handle = core.BNRegisterConstantRenderer(self.__class__.renderer_name, self._cb)
51+
self.__class__._registered_renderers.append(self)
52+
53+
def _is_valid_for_type(self, ctxt, hlil, type):
54+
try:
55+
hlil = highlevelil.HighLevelILFunction(handle=core.BNNewHighLevelILFunctionReference(hlil))
56+
type = types.Type.create(handle=core.BNNewTypeReference(type))
57+
return self.is_valid_for_type(hlil, type)
58+
except:
59+
log_error_for_exception("Unhandled Python exception in ConstantRenderer._is_valid_for_type")
60+
return False
61+
62+
def _render_constant_pointer(self, ctxt, hlil, expr, type, val, tokens, settings, precedence):
63+
try:
64+
hlil = highlevelil.HighLevelILFunction(handle=core.BNNewHighLevelILFunctionReference(hlil))
65+
type = types.Type.create(handle=core.BNNewTypeReference(type))
66+
tokens = languagerepresentation.HighLevelILTokenEmitter(core.BNNewHighLevelILTokenEmitterReference(tokens))
67+
if settings is not None:
68+
settings = function.DisassemblySettings(core.BNNewDisassemblySettingsReference(settings))
69+
instr = hlil.get_expr(highlevelil.ExpressionIndex(expr))
70+
return self.render_constant_pointer(instr, type, val, tokens, settings, precedence)
71+
except:
72+
log_error_for_exception("Unhandled Python exception in ConstantRenderer._render_constant_pointer")
73+
return False
74+
75+
def is_valid_for_type(self, func: 'highlevelil.HighLevelILInstruction', type: 'types.Type') -> bool:
76+
return False
77+
78+
def render_constant_pointer(
79+
self, instr: 'highlevelil.HighLevelILInstruction', type: 'types.Type', val: int,
80+
tokens: 'languagerepresentation.HighLevelILTokenEmitter',
81+
settings: Optional['function.DisassemblySettings'], precedence: 'enums.OperatorPrecedence'
82+
) -> bool:
83+
return False

python/examples/xor_strings.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from binaryninja import (ConstantRenderer, PointerType, InstructionTextToken, InstructionTextTokenType, DataBuffer)
2+
3+
class XorStringConstantRenderer(ConstantRenderer):
4+
renderer_name = "xor_strings"
5+
6+
def is_valid_for_type(self, func, type):
7+
if not isinstance(type, PointerType):
8+
return False
9+
return "xor_encoded" in type.target.attributes
10+
11+
def render_constant_pointer(self, instr, type, val, tokens, settings, precedence):
12+
if not isinstance(type, PointerType):
13+
return False
14+
try:
15+
xor_value = int(type.target.attributes["xor_encoded"], 16)
16+
except:
17+
return False
18+
19+
result = b""
20+
i = 0
21+
while True:
22+
byte = instr.function.view.read(val + i, 1)
23+
if len(byte) != 1:
24+
return False
25+
byte = byte[0] ^ xor_value
26+
if byte == 0:
27+
break
28+
result += bytes([byte])
29+
i += 1
30+
31+
tokens.append(InstructionTextToken(InstructionTextTokenType.BraceToken, "\""))
32+
tokens.append(InstructionTextToken(InstructionTextTokenType.StringToken, DataBuffer(result).escape()))
33+
tokens.append(InstructionTextToken(InstructionTextTokenType.BraceToken, "\"_enc"))
34+
return True
35+
36+
XorStringConstantRenderer().register()

0 commit comments

Comments
 (0)