Skip to content

Commit 6b57ef1

Browse files
committed
Enhance MemoryMap bindings and add support to re-enable disabled regions in the UI.
1 parent b8fdf80 commit 6b57ef1

File tree

6 files changed

+637
-60
lines changed

6 files changed

+637
-60
lines changed

binaryninjaapi.h

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8211,6 +8211,34 @@ namespace BinaryNinja {
82118211
segmentation is automatically managed. If multiple regions overlap, the most recently added region takes
82128212
precedence by default.
82138213
*/
8214+
8215+
struct MemoryRegionInfo
8216+
{
8217+
std::string name;
8218+
std::string displayName;
8219+
uint64_t start;
8220+
uint64_t length;
8221+
uint32_t flags;
8222+
bool enabled;
8223+
bool rebaseable;
8224+
uint8_t fill;
8225+
bool hasTarget;
8226+
bool absoluteAddressMode;
8227+
bool local;
8228+
};
8229+
8230+
struct ResolvedMemoryRange
8231+
{
8232+
uint64_t start;
8233+
uint64_t length;
8234+
std::vector<MemoryRegionInfo> regions;
8235+
8236+
uint64_t End() const { return start + length; }
8237+
const MemoryRegionInfo* ActiveRegion() const { return regions.empty() ? nullptr : &regions.front(); }
8238+
std::string Name() const { auto* r = ActiveRegion(); return r ? r->name : std::string(); }
8239+
uint32_t Flags() const { auto* r = ActiveRegion(); return r ? r->flags : 0; }
8240+
};
8241+
82148242
class MemoryMap
82158243
{
82168244
BNBinaryView* m_object;
@@ -8336,6 +8364,103 @@ namespace BinaryNinja {
83368364
return BNIsMemoryRegionLocal(m_object, name.c_str());
83378365
}
83388366

8367+
std::optional<MemoryRegionInfo> GetMemoryRegionInfo(const std::string& name)
8368+
{
8369+
BNMemoryRegionInfo info;
8370+
if (!BNGetMemoryRegionInfo(m_object, name.c_str(), &info))
8371+
return std::nullopt;
8372+
MemoryRegionInfo result {info.name, info.displayName, info.start, info.length,
8373+
info.flags, info.enabled, info.rebaseable, info.fill,
8374+
info.hasTarget, info.absoluteAddressMode, info.local};
8375+
BNFreeMemoryRegionInfo(&info);
8376+
return result;
8377+
}
8378+
8379+
std::optional<MemoryRegionInfo> GetActiveMemoryRegionInfoAt(uint64_t addr)
8380+
{
8381+
BNMemoryRegionInfo info;
8382+
if (!BNGetActiveMemoryRegionInfoAt(m_object, addr, &info))
8383+
return std::nullopt;
8384+
MemoryRegionInfo result {info.name, info.displayName, info.start, info.length,
8385+
info.flags, info.enabled, info.rebaseable, info.fill,
8386+
info.hasTarget, info.absoluteAddressMode, info.local};
8387+
BNFreeMemoryRegionInfo(&info);
8388+
return result;
8389+
}
8390+
8391+
std::optional<ResolvedMemoryRange> GetResolvedMemoryRangeAt(uint64_t addr)
8392+
{
8393+
BNResolvedMemoryRange raw;
8394+
if (!BNGetResolvedMemoryRangeAt(m_object, addr, &raw))
8395+
return std::nullopt;
8396+
ResolvedMemoryRange result;
8397+
result.start = raw.start;
8398+
result.length = raw.length;
8399+
result.regions.reserve(raw.regionCount);
8400+
for (size_t j = 0; j < raw.regionCount; j++)
8401+
{
8402+
auto& r = raw.regions[j];
8403+
result.regions.push_back({r.name, r.displayName, r.start, r.length,
8404+
r.flags, r.enabled, r.rebaseable, r.fill,
8405+
r.hasTarget, r.absoluteAddressMode, r.local});
8406+
}
8407+
BNFreeResolvedMemoryRange(&raw);
8408+
return result;
8409+
}
8410+
8411+
std::vector<MemoryRegionInfo> GetMemoryRegions()
8412+
{
8413+
size_t count = 0;
8414+
BNMemoryRegionInfo* regions = BNGetMemoryRegions(m_object, &count);
8415+
std::vector<MemoryRegionInfo> result;
8416+
result.reserve(count);
8417+
for (size_t i = 0; i < count; i++)
8418+
{
8419+
result.push_back({
8420+
regions[i].name,
8421+
regions[i].displayName,
8422+
regions[i].start,
8423+
regions[i].length,
8424+
regions[i].flags,
8425+
regions[i].enabled,
8426+
regions[i].rebaseable,
8427+
regions[i].fill,
8428+
regions[i].hasTarget,
8429+
regions[i].absoluteAddressMode,
8430+
regions[i].local,
8431+
});
8432+
}
8433+
BNFreeMemoryRegions(regions, count);
8434+
return result;
8435+
}
8436+
8437+
std::vector<ResolvedMemoryRange> GetResolvedRanges()
8438+
{
8439+
size_t count = 0;
8440+
BNResolvedMemoryRange* ranges = BNGetResolvedMemoryRanges(m_object, &count);
8441+
std::vector<ResolvedMemoryRange> result;
8442+
result.reserve(count);
8443+
for (size_t i = 0; i < count; i++)
8444+
{
8445+
ResolvedMemoryRange range;
8446+
range.start = ranges[i].start;
8447+
range.length = ranges[i].length;
8448+
range.regions.reserve(ranges[i].regionCount);
8449+
for (size_t j = 0; j < ranges[i].regionCount; j++)
8450+
{
8451+
auto& r = ranges[i].regions[j];
8452+
range.regions.push_back({
8453+
r.name, r.displayName, r.start, r.length,
8454+
r.flags, r.enabled, r.rebaseable, r.fill,
8455+
r.hasTarget, r.absoluteAddressMode, r.local,
8456+
});
8457+
}
8458+
result.push_back(std::move(range));
8459+
}
8460+
BNFreeResolvedMemoryRanges(ranges, count);
8461+
return result;
8462+
}
8463+
83398464
void Reset()
83408465
{
83418466
BNResetMemoryMap(m_object);
@@ -20044,7 +20169,7 @@ namespace BinaryNinja {
2004420169
\return True if the type library was successfully decompressed
2004520170
*/
2004620171
bool DecompressToFile(const std::string& path);
20047-
20172+
2004820173
/*! The Architecture this type library is associated with
2004920174

2005020175
\return

binaryninjacore.h

Lines changed: 32 additions & 2 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 158
40+
#define BN_CURRENT_CORE_ABI_VERSION 159
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 158
47+
#define BN_MINIMUM_CORE_ABI_VERSION 159
4848

4949
#ifdef __GNUC__
5050
#ifdef BINARYNINJACORE_LIBRARY
@@ -3803,6 +3803,27 @@ extern "C"
38033803
uint64_t infoData;
38043804
} BNSectionInfo;
38053805

3806+
typedef struct BNMemoryRegionInfo {
3807+
char* name;
3808+
char* displayName;
3809+
uint64_t start;
3810+
uint64_t length;
3811+
uint32_t flags;
3812+
bool enabled;
3813+
bool rebaseable;
3814+
uint8_t fill;
3815+
bool hasTarget;
3816+
bool absoluteAddressMode;
3817+
bool local;
3818+
} BNMemoryRegionInfo;
3819+
3820+
typedef struct BNResolvedMemoryRange {
3821+
uint64_t start;
3822+
uint64_t length;
3823+
BNMemoryRegionInfo* regions;
3824+
size_t regionCount;
3825+
} BNResolvedMemoryRange;
3826+
38063827
typedef bool(*BNCollaborationAnalysisConflictHandler)(void*, const char** keys, BNAnalysisMergeConflict** conflicts, size_t conflictCount);
38073828
typedef bool(*BNCollaborationNameChangesetFunction)(void*, BNCollaborationChangeset*);
38083829

@@ -4531,6 +4552,15 @@ extern "C"
45314552
BINARYNINJACOREAPI char* BNGetMemoryRegionDisplayName(BNBinaryView* view, const char* name);
45324553
BINARYNINJACOREAPI bool BNSetMemoryRegionDisplayName(BNBinaryView* view, const char* name, const char* displayName);
45334554
BINARYNINJACOREAPI bool BNIsMemoryRegionLocal(BNBinaryView* view, const char* name);
4555+
BINARYNINJACOREAPI bool BNGetMemoryRegionInfo(BNBinaryView* view, const char* name, BNMemoryRegionInfo* result);
4556+
BINARYNINJACOREAPI bool BNGetActiveMemoryRegionInfoAt(BNBinaryView* view, uint64_t addr, BNMemoryRegionInfo* result);
4557+
BINARYNINJACOREAPI bool BNGetResolvedMemoryRangeAt(BNBinaryView* view, uint64_t addr, BNResolvedMemoryRange* result);
4558+
BINARYNINJACOREAPI void BNFreeMemoryRegionInfo(BNMemoryRegionInfo* info);
4559+
BINARYNINJACOREAPI void BNFreeResolvedMemoryRange(BNResolvedMemoryRange* range);
4560+
BINARYNINJACOREAPI BNMemoryRegionInfo* BNGetMemoryRegions(BNBinaryView* view, size_t* count);
4561+
BINARYNINJACOREAPI void BNFreeMemoryRegions(BNMemoryRegionInfo* regions, size_t count);
4562+
BINARYNINJACOREAPI BNResolvedMemoryRange* BNGetResolvedMemoryRanges(BNBinaryView* view, size_t* count);
4563+
BINARYNINJACOREAPI void BNFreeResolvedMemoryRanges(BNResolvedMemoryRange* ranges, size_t count);
45344564
BINARYNINJACOREAPI void BNResetMemoryMap(BNBinaryView* view);
45354565

45364566
// Binary view access

0 commit comments

Comments
 (0)