Skip to content

Commit 8d91951

Browse files
committed
Add documentation to the constant renderer and string recognizer APIs
1 parent 24ec8c9 commit 8d91951

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed

binaryninjaapi.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5372,6 +5372,11 @@ namespace BinaryNinja {
53725372
std::vector<TypeReferenceSource> typeRefs;
53735373
};
53745374

5375+
/*! Represents a custom string type. String types contain the name of the string type and the prefix
5376+
and postfix used to render them in code.
5377+
5378+
\ingroup stringrecognizer
5379+
*/
53755380
class CustomStringType: public StaticCoreRefCountObject<BNCustomStringType>
53765381
{
53775382
public:
@@ -5384,6 +5389,10 @@ namespace BinaryNinja {
53845389
const std::string& name, const std::string& stringPrefix = "", const std::string& stringPostfix = "");
53855390
};
53865391

5392+
/*! Location associated with a derived string. Locations are optional.
5393+
5394+
\ingroup stringrecognizer
5395+
*/
53875396
struct DerivedStringLocation
53885397
{
53895398
BNDerivedStringLocationType locationType;
@@ -5418,6 +5427,12 @@ namespace BinaryNinja {
54185427
}
54195428
};
54205429

5430+
/*! Contains a string derived from code or data. The string does not need to be directly present in
5431+
the binary in its raw form. Derived strings can have optional locations to data or code. When
5432+
creating new derived strings, a custom type should be registered with \c CustomStringType::register.
5433+
5434+
\ingroup stringrecognizer
5435+
*/
54215436
struct DerivedString
54225437
{
54235438
StringRef value;
@@ -21425,6 +21440,10 @@ namespace BinaryNinja {
2142521440
) override;
2142621441
};
2142721442

21443+
/*! \c ConstantRenderer allows custom rendering of constants in high level representations.
21444+
21445+
\ingroup constantrenderer
21446+
*/
2142821447
class ConstantRenderer : public StaticCoreRefCountObject<BNConstantRenderer>
2142921448
{
2143021449
std::string m_nameForRegister;
@@ -21435,9 +21454,47 @@ namespace BinaryNinja {
2143521454

2143621455
std::string GetName() const;
2143721456

21457+
/*! Determines if the rendering methods should be called for the given expression type. It is optional
21458+
to override this method. If the method isn't overridden, all expression types are passed to the
21459+
rendering methods.
21460+
21461+
\param func \c HighLevelILFunction representing the high level function to be queried
21462+
\param type Type of the expression
21463+
\return \c true if the constant should be passed to the rendering methods, \c false otherwise
21464+
*/
2143821465
virtual bool IsValidForType(HighLevelILFunction* func, Type* type);
21466+
21467+
/*! Can be overridden to render a constant that is not a pointer. The expression type and value of the
21468+
expression are given. If the expression is not handled by this constant renderer, this method should
21469+
return \c false
21470+
21471+
To render a constant, emit the tokens to the tokens object and return \c true
21472+
21473+
\param instr High level expression
21474+
\param type Type of the expression
21475+
\param val Value of the expression
21476+
\param tokens Token emitter for adding the rendered tokens
21477+
\param settings Settings for rendering
21478+
\param precedence Operator precedence of the expression
21479+
\return \c true if the constant was rendered, \c false otherwise
21480+
*/
2143921481
virtual bool RenderConstant(const HighLevelILInstruction& instr, Type* type, int64_t val,
2144021482
HighLevelILTokenEmitter& tokens, DisassemblySettings* settings, BNOperatorPrecedence precedence);
21483+
21484+
/*! Can be overridden to render a constant pointer. The expression type and value of the
21485+
expression are given. If the expression is not handled by this constant renderer, this method should
21486+
return \c false
21487+
21488+
To render a constant, emit the tokens to the tokens object and return \c true
21489+
21490+
\param instr High level expression
21491+
\param type Type of the expression
21492+
\param val Value of the expression
21493+
\param tokens Token emitter for adding the rendered tokens
21494+
\param settings Settings for rendering
21495+
\param precedence Operator precedence of the expression
21496+
\return \c true if the constant was rendered, \c false otherwise
21497+
*/
2144121498
virtual bool RenderConstantPointer(const HighLevelILInstruction& instr, Type* type, int64_t val,
2144221499
HighLevelILTokenEmitter& tokens, DisassemblySettings* settings, BNSymbolDisplayType symbolDisplay,
2144321500
BNOperatorPrecedence precedence);
@@ -21473,6 +21530,10 @@ namespace BinaryNinja {
2147321530
BNOperatorPrecedence precedence) override;
2147421531
};
2147521532

21533+
/*! \c StringRecognizer recognizes custom strings found in high level expressions.
21534+
21535+
\ingroup stringrecognizer
21536+
*/
2147621537
class StringRecognizer : public StaticCoreRefCountObject<BNStringRecognizer>
2147721538
{
2147821539
std::string m_nameForRegister;
@@ -21483,13 +21544,70 @@ namespace BinaryNinja {
2148321544

2148421545
std::string GetName() const;
2148521546

21547+
/*! Determines if the string recognizer should be called for the given expression type. It is optional
21548+
to override this method. If the method isn't overridden, all expression types are passed to the
21549+
string recognizer.
21550+
21551+
\param func \c HighLevelILFunction representing the high level function to be queried
21552+
\param type Type of the expression
21553+
\return \c true if the expression should be passed to the string recognizer, \c false otherwise
21554+
*/
2148621555
virtual bool IsValidForType(HighLevelILFunction* func, Type* type);
21556+
21557+
/*! Can be overridden to recognize strings for a constant that is not a pointer. The expression type and
21558+
value of the expression are given. If no string is found for this expression, this method should
21559+
return \c std::nullopt
21560+
21561+
If a string is found, return a \c DerivedString with the string information.
21562+
21563+
\param instr High level expression
21564+
\param type Type of the expression
21565+
\param val Value of the expression
21566+
\return Optional \c DerivedString for any string that is found
21567+
*/
2148721568
virtual std::optional<DerivedString> RecognizeConstant(
2148821569
const HighLevelILInstruction& instr, Type* type, int64_t val);
21570+
21571+
/*! Can be overridden to recognize strings for a constant pointer. The expression type and
21572+
value of the expression are given. If no string is found for this expression, this method should
21573+
return \c std::nullopt
21574+
21575+
If a string is found, return a \c DerivedString with the string information.
21576+
21577+
\param instr High level expression
21578+
\param type Type of the expression
21579+
\param val Value of the expression
21580+
\return Optional \c DerivedString for any string that is found
21581+
*/
2148921582
virtual std::optional<DerivedString> RecognizeConstantPointer(
2149021583
const HighLevelILInstruction& instr, Type* type, int64_t val);
21584+
21585+
/*! Can be overridden to recognize strings for an external symbol. The expression type and
21586+
value of the expression are given. If no string is found for this expression, this method should
21587+
return \c std::nullopt
21588+
21589+
If a string is found, return a \c DerivedString with the string information.
21590+
21591+
\param instr High level expression
21592+
\param type Type of the expression
21593+
\param val Value of the expression
21594+
\param offset Offset into the external symbol
21595+
\return Optional \c DerivedString for any string that is found
21596+
*/
2149121597
virtual std::optional<DerivedString> RecognizeExternPointer(
2149221598
const HighLevelILInstruction& instr, Type* type, int64_t val, uint64_t offset);
21599+
21600+
/*! Can be overridden to recognize strings for an imported symbol. The expression type and
21601+
value of the expression are given. If no string is found for this expression, this method should
21602+
return \c std::nullopt
21603+
21604+
If a string is found, return a \c DerivedString with the string information.
21605+
21606+
\param instr High level expression
21607+
\param type Type of the expression
21608+
\param val Value of the expression
21609+
\return Optional \c DerivedString for any string that is found
21610+
*/
2149321611
virtual std::optional<DerivedString> RecognizeImport(
2149421612
const HighLevelILInstruction& instr, Type* type, int64_t val);
2149521613

python/binaryview.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ def view(self) -> 'BinaryView':
528528

529529

530530
class StringRef:
531+
"""Deduplicated reference to a string owned by the Binary Ninja core. Use `str` or `bytes` to convert
532+
this to a standard Python string or sequence of bytes."""
531533
def __init__(self, handle):
532534
self.handle = core.handle_of_type(handle, core.BNStringRef)
533535

@@ -589,13 +591,20 @@ def __hash__(self):
589591

590592
@dataclass(frozen=True)
591593
class DerivedStringLocation:
594+
"""Location associated with a derived string. Locations are optional."""
592595
location_type: 'DerivedStringLocationType'
593596
address: int
594597
length: int
595598

596599

597600
@dataclass(frozen=True)
598601
class DerivedString:
602+
"""
603+
Contains a string derived from code or data. The string does not need to be directly present in
604+
the binary in its raw form. Derived strings can have optional locations to data or code. When
605+
creating new derived strings, a custom type should be registered with
606+
:py:func:`~binaryninja.stringrecognizer.CustomStringType.register` on :py:class:`~binaryninja.stringrecognizer.CustomStringType`.
607+
"""
599608
value: 'StringRef'
600609
location: Optional[DerivedStringLocation]
601610
custom_type: Optional[stringrecognizer.CustomStringType]

python/constantrenderer.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ def __getitem__(cls, value):
5353

5454

5555
class ConstantRenderer(metaclass=_ConstantRendererMetaClass):
56+
"""
57+
``class ConstantRenderer`` allows custom rendering of constants in high level representations.
58+
59+
The :py:func:`render_constant` method will be called when rendering constants that aren't pointers, while the
60+
:py:func:`render_constant_pointer` method will be called when rendering constant pointers. The
61+
:py:func:`is_valid_for_type` method can be optionally overridden to call the rendering methods only when
62+
the expression type matches a custom filter.
63+
"""
5664
_registered_renderers = []
5765
renderer_name = None
5866

@@ -61,6 +69,7 @@ def __init__(self, handle=None):
6169
self.handle = core.handle_of_type(handle, core.BNConstantRenderer)
6270

6371
def register(self):
72+
"""Registers the constant renderer."""
6473
if self.__class__.renderer_name is None:
6574
raise ValueError("Renderer name is missing")
6675
self._cb = core.BNCustomConstantRenderer()
@@ -117,13 +126,37 @@ def name(self) -> str:
117126
return self.__class__.renderer_name
118127

119128
def is_valid_for_type(self, func: 'highlevelil.HighLevelILFunction', type: 'types.Type') -> bool:
129+
"""
130+
Determines if the rendering methods should be called for the given expression type. It is optional
131+
to override this method. If the method isn't overridden, all expression types are passed to the
132+
rendering methods.
133+
134+
:param func: `HighLevelILFunction` representing the high level function to be rendered
135+
:param type: Type of the expression
136+
:return: `True` if the constant should be passed to the rendering methods, `False` otherwise
137+
"""
120138
return True
121139

122140
def render_constant(
123141
self, instr: 'highlevelil.HighLevelILInstruction', type: 'types.Type', val: int,
124142
tokens: 'languagerepresentation.HighLevelILTokenEmitter',
125143
settings: Optional['function.DisassemblySettings'], precedence: 'enums.OperatorPrecedence'
126144
) -> bool:
145+
"""
146+
Can be overridden to render a constant that is not a pointer. The expression type and value of the
147+
expression are given. If the expression is not handled by this constant renderer, this method should
148+
return `False`.
149+
150+
To render a constant, emit the tokens to the `tokens` object and return `True`.
151+
152+
:param instr: High level expression
153+
:param type: Type of the expression
154+
:param val: Value of the expression
155+
:param tokens: Token emitter for adding the rendered tokens
156+
:param settings: Settings for rendering
157+
:param precedence: Operator precedence of the expression
158+
:return: `True` if the constant was rendered, `False` otherwise
159+
"""
127160
return False
128161

129162
def render_constant_pointer(
@@ -132,6 +165,21 @@ def render_constant_pointer(
132165
settings: Optional['function.DisassemblySettings'], symbol_display: 'enums.SymbolDisplayType',
133166
precedence: 'enums.OperatorPrecedence'
134167
) -> bool:
168+
"""
169+
Can be overridden to render a constant pointer. The expression type and value of the expression are given.
170+
If the expression is not handled by this constant renderer, this method should return `False`.
171+
172+
To render a constant pointer, emit the tokens to the `tokens` object and return `True`.
173+
174+
:param instr: High level expression
175+
:param type: Type of the expression
176+
:param val: Value of the expression
177+
:param tokens: Token emitter for adding the rendered tokens
178+
:param settings: Settings for rendering
179+
:param symbol_display: Type of symbol to display
180+
:param precedence: Operator precedence of the expression
181+
:return: `True` if the constant was rendered, `False` otherwise
182+
"""
135183
return False
136184

137185

0 commit comments

Comments
 (0)