Skip to content

Commit 1699c71

Browse files
committed
Add line formatter API and a generic line formatter plugin
1 parent 62cb5ab commit 1699c71

18 files changed

+1758
-50
lines changed

basicblock.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ DisassemblySettings::DisassemblySettings(BNDisassemblySettings* settings)
3636
}
3737

3838

39+
Ref<DisassemblySettings> DisassemblySettings::GetDefaultSettings()
40+
{
41+
return new DisassemblySettings(BNDefaultDisassemblySettings());
42+
}
43+
44+
45+
Ref<DisassemblySettings> DisassemblySettings::GetDefaultGraphSettings()
46+
{
47+
return new DisassemblySettings(BNDefaultGraphDisassemblySettings());
48+
}
49+
50+
51+
Ref<DisassemblySettings> DisassemblySettings::GetDefaultLinearSettings()
52+
{
53+
return new DisassemblySettings(BNDefaultLinearDisassemblySettings());
54+
}
55+
56+
3957
DisassemblySettings* DisassemblySettings::Duplicate()
4058
{
4159
return new DisassemblySettings(BNDuplicateDisassemblySettings(m_object));

binaryninjaapi.h

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10171,6 +10171,10 @@ namespace BinaryNinja {
1017110171
DisassemblySettings(BNDisassemblySettings* settings);
1017210172
DisassemblySettings* Duplicate();
1017310173

10174+
static Ref<DisassemblySettings> GetDefaultSettings();
10175+
static Ref<DisassemblySettings> GetDefaultGraphSettings();
10176+
static Ref<DisassemblySettings> GetDefaultLinearSettings();
10177+
1017410178
bool IsOptionSet(BNDisassemblyOption option) const;
1017510179
void SetOption(BNDisassemblyOption option, bool state = true);
1017610180

@@ -13694,6 +13698,82 @@ namespace BinaryNinja {
1369413698
std::set<SSAVariable> GetSSAVariables();
1369513699
};
1369613700

13701+
struct LineFormatterSettings
13702+
{
13703+
Ref<HighLevelILFunction> highLevelIL;
13704+
size_t desiredLineLength;
13705+
size_t minimumContentLength;
13706+
size_t tabWidth;
13707+
std::string languageName;
13708+
std::string commentStartString;
13709+
std::string commentEndString;
13710+
std::string annotationStartString;
13711+
std::string annotationEndString;
13712+
13713+
/*! Gets the default line formatter settings for High Level IL code.
13714+
13715+
\param settings The settings for reformatting.
13716+
\param func High Level IL function to be reformatted.
13717+
\return Settings for reformatting.
13718+
*/
13719+
static LineFormatterSettings GetDefault(DisassemblySettings* settings, HighLevelILFunction* func);
13720+
13721+
/*! Gets the default line formatter settings for a language representation function.
13722+
13723+
\param settings The settings for reformatting.
13724+
\param func Language representation function to be reformatted.
13725+
\return Settings for reformatting.
13726+
*/
13727+
static LineFormatterSettings GetLanguageRepresentationSettings(
13728+
DisassemblySettings* settings, LanguageRepresentationFunction* func);
13729+
13730+
static LineFormatterSettings FromAPIObject(const BNLineFormatterSettings* settings);
13731+
BNLineFormatterSettings ToAPIObject() const;
13732+
};
13733+
13734+
class LineFormatter : public StaticCoreRefCountObject<BNLineFormatter>
13735+
{
13736+
std::string m_nameForRegister;
13737+
13738+
static BNDisassemblyTextLine* FormatLinesCallback(void* ctxt, BNDisassemblyTextLine* inLines, size_t inCount,
13739+
const BNLineFormatterSettings* settings, size_t* outCount);
13740+
static void FreeLinesCallback(void* ctxt, BNDisassemblyTextLine* lines, size_t count);
13741+
13742+
public:
13743+
LineFormatter(const std::string& name);
13744+
LineFormatter(BNLineFormatter* formatter);
13745+
13746+
/*! Registers the line formatter.
13747+
13748+
\param formatter The line formatter to register.
13749+
*/
13750+
static void Register(LineFormatter* formatter);
13751+
13752+
static std::vector<Ref<LineFormatter>> GetList();
13753+
static Ref<LineFormatter> GetByName(const std::string& name);
13754+
static Ref<LineFormatter> GetDefault();
13755+
13756+
/*! Reformats the given list of lines. Returns a new list of lines containing the reformatted code.
13757+
13758+
\param lines The lines to reformat.
13759+
\param settings The settings for reformatting.
13760+
\return A new list of reformatted lines.
13761+
*/
13762+
virtual std::vector<DisassemblyTextLine> FormatLines(
13763+
const std::vector<DisassemblyTextLine>& lines, const LineFormatterSettings& settings) = 0;
13764+
};
13765+
13766+
class CoreLineFormatter : public LineFormatter
13767+
{
13768+
public:
13769+
CoreLineFormatter(BNLineFormatter* formatter);
13770+
13771+
std::vector<DisassemblyTextLine> FormatLines(
13772+
const std::vector<DisassemblyTextLine>& lines, const LineFormatterSettings& settings) override;
13773+
};
13774+
13775+
class LanguageRepresentationFunctionType;
13776+
1369713777
/*! LanguageRepresentationFunction represents a single function in a registered high level language.
1369813778

1369913779
\ingroup highlevelil
@@ -13703,7 +13783,8 @@ namespace BinaryNinja {
1370313783
BNFreeLanguageRepresentationFunction>
1370413784
{
1370513785
public:
13706-
LanguageRepresentationFunction(Architecture* arch, Function* func, HighLevelILFunction* highLevelIL);
13786+
LanguageRepresentationFunction(LanguageRepresentationFunctionType* type, Architecture* arch, Function* func,
13787+
HighLevelILFunction* highLevelIL);
1370713788
LanguageRepresentationFunction(BNLanguageRepresentationFunction* func);
1370813789

1370913790
/*! Gets the lines of tokens for a given High Level IL instruction.
@@ -13742,6 +13823,7 @@ namespace BinaryNinja {
1374213823
*/
1374313824
BNHighlightColor GetHighlight(BasicBlock* block);
1374413825

13826+
Ref<LanguageRepresentationFunctionType> GetLanguage() const;
1374513827
Ref<Architecture> GetArchitecture() const;
1374613828
Ref<Function> GetFunction() const;
1374713829
Ref<HighLevelILFunction> GetHighLevelILFunction() const;
@@ -13890,6 +13972,13 @@ namespace BinaryNinja {
1389013972
*/
1389113973
virtual Ref<TypeParser> GetTypeParser() { return nullptr; }
1389213974

13975+
/*! Returns the line formatter for formatting code in this language. If NULL is returned, the default
13976+
formatter will be used.
13977+
13978+
\return The optional formatter for formatting code in this language.
13979+
*/
13980+
virtual Ref<LineFormatter> GetLineFormatter() { return nullptr; }
13981+
1389313982
/*! Returns a list of lines representing a function prototype in this language. If no lines are returned, the
1389413983
default C-style prototype will be used.
1389513984

@@ -13916,6 +14005,7 @@ namespace BinaryNinja {
1391614005
static bool IsValidCallback(void* ctxt, BNBinaryView* view);
1391714006
static BNTypePrinter* GetTypePrinterCallback(void* ctxt);
1391814007
static BNTypeParser* GetTypeParserCallback(void* ctxt);
14008+
static BNLineFormatter* GetLineFormatterCallback(void* ctxt);
1391914009
static BNDisassemblyTextLine* GetFunctionTypeTokensCallback(
1392014010
void* ctxt, BNFunction* func, BNDisassemblySettings* settings, size_t* count);
1392114011
static void FreeLinesCallback(void* ctxt, BNDisassemblyTextLine* lines, size_t count);
@@ -13930,6 +14020,7 @@ namespace BinaryNinja {
1393014020
bool IsValid(BinaryView* view) override;
1393114021
Ref<TypePrinter> GetTypePrinter() override;
1393214022
Ref<TypeParser> GetTypeParser() override;
14023+
Ref<LineFormatter> GetLineFormatter() override;
1393314024
std::vector<DisassemblyTextLine> GetFunctionTypeTokens(
1393414025
Function* func, DisassemblySettings* settings = nullptr) override;
1393514026
};

binaryninjacore.h

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
3737
// Current ABI version for linking to the core. This is incremented any time
3838
// there are changes to the API that affect linking, including new functions,
3939
// new types, or modifications to existing functions or types.
40-
#define BN_CURRENT_CORE_ABI_VERSION 88
40+
#define BN_CURRENT_CORE_ABI_VERSION 89
4141

4242
// Minimum ABI version that is supported for loading of plugins. Plugins that
4343
// are linked to an ABI version less than this will not be able to load and
4444
// will require rebuilding. The minimum version is increased when there are
4545
// incompatible changes that break binary compatibility, such as changes to
4646
// existing types or functions.
47-
#define BN_MINIMUM_CORE_ABI_VERSION 86
47+
#define BN_MINIMUM_CORE_ABI_VERSION 89
4848

4949
#ifdef __GNUC__
5050
#ifdef BINARYNINJACORE_LIBRARY
@@ -302,6 +302,7 @@ extern "C"
302302
typedef struct BNDemangler BNDemangler;
303303
typedef struct BNFirmwareNinja BNFirmwareNinja;
304304
typedef struct BNFirmwareNinjaReferenceNode BNFirmwareNinjaReferenceNode;
305+
typedef struct BNLineFormatter BNLineFormatter;
305306

306307
//! Console log levels
307308
typedef enum BNLogLevel
@@ -727,6 +728,7 @@ extern "C"
727728
HighLevelILLinearDisassembly = 65,
728729
WaitForIL = 66,
729730
IndentHLILBody = 67,
731+
DisableLineFormatting = 68,
730732

731733
// Debugging options
732734
ShowFlagUsage = 128,
@@ -3439,6 +3441,7 @@ extern "C"
34393441
bool (*isValid)(void* ctxt, BNBinaryView* view);
34403442
BNTypePrinter* (*getTypePrinter)(void* ctxt);
34413443
BNTypeParser* (*getTypeParser)(void* ctxt);
3444+
BNLineFormatter* (*getLineFormatter)(void* ctxt);
34423445
BNDisassemblyTextLine* (*getFunctionTypeTokens)(
34433446
void* ctxt, BNFunction* func, BNDisassemblySettings* settings, size_t* count);
34443447
void (*freeLines)(void* ctxt, BNDisassemblyTextLine* lines, size_t count);
@@ -3538,6 +3541,28 @@ extern "C"
35383541
size_t unique;
35393542
} BNFirmwareNinjaDeviceAccesses;
35403543

3544+
typedef struct BNLineFormatterSettings
3545+
{
3546+
BNHighLevelILFunction* highLevelIL;
3547+
size_t desiredLineLength;
3548+
size_t minimumContentLength;
3549+
size_t tabWidth;
3550+
char* languageName;
3551+
char* commentStartString;
3552+
char* commentEndString;
3553+
char* annotationStartString;
3554+
char* annotationEndString;
3555+
} BNLineFormatterSettings;
3556+
3557+
typedef struct BNCustomLineFormatter
3558+
{
3559+
void* context;
3560+
BNDisassemblyTextLine* (*formatLines)(void* ctxt, BNDisassemblyTextLine* inLines, size_t inCount,
3561+
const BNLineFormatterSettings* settings, size_t* outCount);
3562+
void (*freeLines)(void* ctxt, BNDisassemblyTextLine* lines, size_t count);
3563+
} BNCustomLineFormatter;
3564+
3565+
35413566
BINARYNINJACOREAPI char* BNAllocString(const char* contents);
35423567
BINARYNINJACOREAPI char* BNAllocStringWithLength(const char* contents, size_t len);
35433568
BINARYNINJACOREAPI void BNFreeString(char* str);
@@ -5453,6 +5478,9 @@ extern "C"
54535478

54545479
// Disassembly settings
54555480
BINARYNINJACOREAPI BNDisassemblySettings* BNCreateDisassemblySettings(void);
5481+
BINARYNINJACOREAPI BNDisassemblySettings* BNDefaultDisassemblySettings(void);
5482+
BINARYNINJACOREAPI BNDisassemblySettings* BNDefaultGraphDisassemblySettings(void);
5483+
BINARYNINJACOREAPI BNDisassemblySettings* BNDefaultLinearDisassemblySettings(void);
54565484
BINARYNINJACOREAPI BNDisassemblySettings* BNNewDisassemblySettingsReference(BNDisassemblySettings* settings);
54575485
BINARYNINJACOREAPI BNDisassemblySettings* BNDuplicateDisassemblySettings(BNDisassemblySettings* settings);
54585486
BINARYNINJACOREAPI void BNFreeDisassemblySettings(BNDisassemblySettings* settings);
@@ -6143,15 +6171,19 @@ extern "C"
61436171
BNLanguageRepresentationFunctionType* type, BNBinaryView* view);
61446172
BINARYNINJACOREAPI BNTypePrinter* BNGetLanguageRepresentationFunctionTypePrinter(BNLanguageRepresentationFunctionType* type);
61456173
BINARYNINJACOREAPI BNTypeParser* BNGetLanguageRepresentationFunctionTypeParser(BNLanguageRepresentationFunctionType* type);
6174+
BINARYNINJACOREAPI BNLineFormatter* BNGetLanguageRepresentationFunctionTypeLineFormatter(
6175+
BNLanguageRepresentationFunctionType* type);
61466176
BINARYNINJACOREAPI BNDisassemblyTextLine* BNGetLanguageRepresentationFunctionTypeFunctionTypeTokens(
61476177
BNLanguageRepresentationFunctionType* type, BNFunction* func, BNDisassemblySettings* settings, size_t* count);
61486178

61496179
BINARYNINJACOREAPI BNLanguageRepresentationFunction* BNCreateCustomLanguageRepresentationFunction(
6150-
BNArchitecture* arch, BNFunction* func, BNHighLevelILFunction* highLevelIL,
6151-
BNCustomLanguageRepresentationFunction* callbacks);
6180+
BNLanguageRepresentationFunctionType* type, BNArchitecture* arch, BNFunction* func,
6181+
BNHighLevelILFunction* highLevelIL, BNCustomLanguageRepresentationFunction* callbacks);
61526182
BINARYNINJACOREAPI BNLanguageRepresentationFunction* BNNewLanguageRepresentationFunctionReference(
61536183
BNLanguageRepresentationFunction* func);
61546184
BINARYNINJACOREAPI void BNFreeLanguageRepresentationFunction(BNLanguageRepresentationFunction* func);
6185+
BINARYNINJACOREAPI BNLanguageRepresentationFunctionType* BNGetLanguageRepresentationType(
6186+
BNLanguageRepresentationFunction* func);
61556187
BINARYNINJACOREAPI BNArchitecture* BNGetLanguageRepresentationArchitecture(BNLanguageRepresentationFunction* func);
61566188
BINARYNINJACOREAPI BNFunction* BNGetLanguageRepresentationOwnerFunction(BNLanguageRepresentationFunction* func);
61576189
BINARYNINJACOREAPI BNHighLevelILFunction* BNGetLanguageRepresentationILFunction(BNLanguageRepresentationFunction* func);
@@ -8056,6 +8088,25 @@ extern "C"
80568088
BINARYNINJACOREAPI void BNFreeFirmwareNinjaReferenceNode(BNFirmwareNinjaReferenceNode* node);
80578089
BINARYNINJACOREAPI BNFirmwareNinjaReferenceNode* BNNewFirmwareNinjaReferenceNodeReference(BNFirmwareNinjaReferenceNode* node);
80588090
BINARYNINJACOREAPI void BNFreeFirmwareNinjaReferenceNodes(BNFirmwareNinjaReferenceNode** nodes, size_t count);
8091+
8092+
// Line formatters
8093+
BINARYNINJACOREAPI BNLineFormatter* BNRegisterLineFormatter(const char* name, BNCustomLineFormatter* callbacks);
8094+
BINARYNINJACOREAPI BNLineFormatter** BNGetLineFormatterList(size_t* count);
8095+
BINARYNINJACOREAPI void BNFreeLineFormatterList(BNLineFormatter** formatters);
8096+
BINARYNINJACOREAPI BNLineFormatter* BNGetLineFormatterByName(const char* name);
8097+
BINARYNINJACOREAPI BNLineFormatter* BNGetDefaultLineFormatter();
8098+
8099+
BINARYNINJACOREAPI char* BNGetLineFormatterName(BNLineFormatter* formatter);
8100+
8101+
BINARYNINJACOREAPI BNDisassemblyTextLine* BNFormatLines(BNLineFormatter* formatter, BNDisassemblyTextLine* inLines,
8102+
size_t inCount, const BNLineFormatterSettings* settings, size_t* outCount);
8103+
8104+
BINARYNINJACOREAPI BNLineFormatterSettings* BNGetDefaultLineFormatterSettings(
8105+
BNDisassemblySettings* settings, BNHighLevelILFunction* func);
8106+
BINARYNINJACOREAPI BNLineFormatterSettings* BNGetLanguageRepresentationLineFormatterSettings(
8107+
BNDisassemblySettings* settings, BNLanguageRepresentationFunction* func);
8108+
BINARYNINJACOREAPI void BNFreeLineFormatterSettings(BNLineFormatterSettings* settings);
8109+
80598110
#ifdef __cplusplus
80608111
}
80618112
#endif

formatter/generic/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)
2+
3+
project(formatter_generic)
4+
5+
if(NOT BN_INTERNAL_BUILD)
6+
add_subdirectory(${PROJECT_SOURCE_DIR}/../.. ${PROJECT_BINARY_DIR}/api)
7+
endif()
8+
9+
file(GLOB SOURCES
10+
*.cpp
11+
*.h)
12+
13+
if(DEMO)
14+
add_library(formatter_generic STATIC ${SOURCES})
15+
else()
16+
add_library(formatter_generic SHARED ${SOURCES})
17+
endif()
18+
19+
target_include_directories(formatter_generic
20+
PRIVATE ${PROJECT_SOURCE_DIR})
21+
22+
if(WIN32)
23+
target_link_directories(formatter_generic
24+
PRIVATE ${BN_INSTALL_DIR})
25+
target_link_libraries(formatter_generic binaryninjaapi binaryninjacore)
26+
else()
27+
target_link_libraries(formatter_generic binaryninjaapi)
28+
endif()
29+
30+
set_target_properties(formatter_generic PROPERTIES
31+
CXX_STANDARD 17
32+
CXX_VISIBILITY_PRESET hidden
33+
CXX_STANDARD_REQUIRED ON
34+
C_STANDARD 99
35+
C_STANDARD_REQUIRED ON
36+
C_VISIBILITY_PRESET hidden
37+
VISIBILITY_INLINES_HIDDEN ON
38+
POSITION_INDEPENDENT_CODE ON)
39+
40+
if(BN_INTERNAL_BUILD)
41+
plugin_rpath(formatter_generic)
42+
set_target_properties(formatter_generic PROPERTIES
43+
LIBRARY_OUTPUT_DIRECTORY ${BN_CORE_PLUGIN_DIR}
44+
RUNTIME_OUTPUT_DIRECTORY ${BN_CORE_PLUGIN_DIR})
45+
endif()

0 commit comments

Comments
 (0)