Skip to content

Commit 63a4614

Browse files
committed
Added MemoryMap C++ API.
1 parent e824ffd commit 63a4614

File tree

3 files changed

+87
-13
lines changed

3 files changed

+87
-13
lines changed

binaryninjaapi.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4241,6 +4241,7 @@ namespace BinaryNinja {
42414241
class DebugInfo;
42424242
class TypeLibrary;
42434243
class TypeArchive;
4244+
class MemoryMap;
42444245

42454246
class QueryMetadataException : public ExceptionWithStackTrace
42464247
{
@@ -4311,6 +4312,8 @@ namespace BinaryNinja {
43114312
*/
43124313
class BinaryView : public CoreRefCountObject<BNBinaryView, BNNewViewReference, BNFreeBinaryView>
43134314
{
4315+
std::unique_ptr<MemoryMap> m_memoryMap;
4316+
43144317
protected:
43154318
Ref<FileMetadata> m_file; //!< The underlying file
43164319

@@ -6311,6 +6314,12 @@ namespace BinaryNinja {
63116314
bool GetAddressInput(
63126315
uint64_t& result, const std::string& prompt, const std::string& title, uint64_t currentAddress);
63136316

6317+
/*! A mock object that is a placeholder during development of this feature.
6318+
6319+
\return MemoryMap object
6320+
*/
6321+
MemoryMap* GetMemoryMap() { return m_memoryMap.get(); }
6322+
63146323
/*! Add an analysis segment that specifies how data from the raw file is mapped into a virtual address space
63156324

63166325
\param start Starting virtual address
@@ -6651,6 +6660,54 @@ namespace BinaryNinja {
66516660
std::vector<Ref<ExternalLocation>> GetExternalLocations();
66526661
};
66536662

6663+
class MemoryMap
6664+
{
6665+
BNBinaryView* m_object;
6666+
6667+
public:
6668+
MemoryMap(BNBinaryView* view): m_object(view) {}
6669+
~MemoryMap() = default;
6670+
6671+
bool AddBinaryMemoryRegion(const std::string& name, uint64_t start, Ref<BinaryView> source)
6672+
{
6673+
return BNAddBinaryMemoryRegion(m_object, name.c_str(), start, source->GetObject());
6674+
}
6675+
6676+
bool AddDataMemoryRegion(const std::string& name, uint64_t start, const DataBuffer& source)
6677+
{
6678+
return BNAddDataMemoryRegion(m_object, name.c_str(), start, source.GetBufferObject());
6679+
}
6680+
6681+
bool AddRemoteMemoryRegion(const std::string& name, uint64_t start, FileAccessor* source)
6682+
{
6683+
return BNAddRemoteMemoryRegion(m_object, name.c_str(), start, source->GetCallbacks());
6684+
}
6685+
6686+
bool RemoveMemoryRegion(const std::string& name)
6687+
{
6688+
return BNRemoveMemoryRegion(m_object, name.c_str());
6689+
}
6690+
6691+
bool IsMemoryRegionEnabled(const std::string& name, uint64_t start)
6692+
{
6693+
return BNIsMemoryRegionEnabled(m_object, name.c_str(), start);
6694+
}
6695+
6696+
bool SetMemoryRegionEnabled(const std::string& name, uint64_t start, bool enabled)
6697+
{
6698+
return BNSetMemoryRegionEnabled(m_object, name.c_str(), start, enabled);
6699+
}
6700+
6701+
bool SetMemoryRegionFill(const std::string& name, uint64_t start, uint8_t fill)
6702+
{
6703+
return BNSetMemoryRegionFill(m_object, name.c_str(), start, fill);
6704+
}
6705+
6706+
void Reset()
6707+
{
6708+
BNResetMemoryMap(m_object);
6709+
}
6710+
};
66546711

66556712
/*!
66566713
\ingroup binaryview

binaryview.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,13 +1249,15 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryVi
12491249
AddRefForRegistration();
12501250
m_object = BNCreateCustomBinaryView(
12511251
typeName.c_str(), m_file->GetObject(), parentView ? parentView->GetObject() : nullptr, &view);
1252+
m_memoryMap = std::make_unique<MemoryMap>(m_object);
12521253
}
12531254

12541255

12551256
BinaryView::BinaryView(BNBinaryView* view)
12561257
{
12571258
m_object = view;
12581259
m_file = new FileMetadata(BNGetFileForView(m_object));
1260+
m_memoryMap = std::make_unique<MemoryMap>(m_object);
12591261
}
12601262

12611263

python/binaryview.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,8 @@ class MemoryMap:
21192119
BinaryView has a view into the MemoryMap which is described by the Segments defined in that BinaryView. The MemoryMap
21202120
object allows for the addition of multiple, arbitrary overlapping regions of memory. Segmenting of the address space is
21212121
automatically handled when the MemoryMap is modified and in the case where a portion of the system address space has
2122-
multilple defined regions, the default ordering gives priority to the most recently added region.
2122+
multilple defined regions, the default ordering gives priority to the most recently added region. This feature is
2123+
experimental and under active development.
21232124
21242125
:Example:
21252126
@@ -2230,19 +2231,33 @@ def __len__(self):
22302231
def description(self):
22312232
return json.loads(core.BNGetMemoryMapDescription(self.handle))
22322233

2233-
# // LoadBinary:
2234-
# // Loads a file in a loadable binary format.
2235-
# // Provides persistence, indicating that the loaded data will persist across sessions.
2236-
# // Presumably loads the file into memory according to the virtual and physical load addresses specified in the file itself.
2237-
# // LoadFile:
2238-
# // Loads a flat file or bytes directly at the specified address.
2239-
# // Provides persistence, suggesting that the loaded data will be saved and remain accessible across sessions.
2240-
# // Allows for direct loading of files or bytes into memory at a specified location.
2241-
# // LoadRemote:
2242-
# // Loads data from a remote source or interface.
2243-
# // Not persistent, implying that the loaded data is ephemeral and may not be saved across sessions.
2244-
# // Supports a target where bytes are provided/generated upon request, indicating a dynamic and potentially transient data source.
22452234
def add_memory_region(self, name: str, start: int, source: Union['os.PathLike', str, bytes, bytearray, 'BinaryView', 'databuffer.DataBuffer', 'fileaccessor.FileAccessor']) -> bool:
2235+
"""
2236+
Adds a memory region into the memory map. There are three types of memory regions that can be added:
2237+
- BinaryMemoryRegion(*** Unimplemented ***): Creates a memory region from a loadable binary format and provides persistence across sessions.
2238+
- DataMemoryRegion: Creates a memory region from a flat file or bytes and provide persistence across sessions.
2239+
- RemoteMemoryRegion: Creates a memory region from a proxy callback interface. This region is ephemeral and not saved across sessions.
2240+
2241+
The type of memory region added depends on the source parameter:
2242+
- `os.PathLike`, `str`, : Treats the source as a file path which is read and loaded into memory as a DataMemoryRegion.
2243+
- `bytes`, `bytearray`: Directly loads these byte formats into memory as a DataMemoryRegion.
2244+
- `databuffer.DataBuffer`: Directly loads a data buffer into memory as a DataMemoryRegion.
2245+
- `fileaccessor.FileAccessor`: Utilizes a file accessor to establish a RemoteMemoryRegion, managing data fetched from a remote source.
2246+
2247+
Parameters:
2248+
name (str): A unique name of the memory region.
2249+
start (int): The start address in memory where the region will be loaded.
2250+
source (Union[os.PathLike, str, bytes, bytearray, BinaryView, databuffer.DataBuffer, fileaccessor.FileAccessor]): The source from which the memory is loaded.
2251+
2252+
Returns:
2253+
bool: True if the memory region was successfully added, False otherwise.
2254+
2255+
Raises:
2256+
NotImplementedError: If the source type is not supported.
2257+
2258+
Notes:
2259+
If parts of the new memory region do not overlap with existing segments, new segments will be automatically created for each non-overlapping area, each with the SegmentFlag.SegmentReadable flag set.
2260+
"""
22462261
if isinstance(source, os.PathLike):
22472262
source = str(source)
22482263
if isinstance(source, bytes) or isinstance(source, bytearray):

0 commit comments

Comments
 (0)