MCPcopy Create free account
hub / github.com/Vector35/binaryninja-api / add_memory_region

Method add_memory_region

python/binaryview.py:2384–2431  ·  view source on GitHub ↗

Adds a memory region to the memory map. Depending on the source parameter, the memory region is created as one of the following types: - **BinaryMemoryRegion** (***Unimplemented***): Represents a memory region loaded from a binary format, providing persistence across sessions. - **DataMemory

(self, name: str, start: int, source: Union['os.PathLike', str, bytes, bytearray, 'BinaryView', 'databuffer.DataBuffer', 'fileaccessor.FileAccessor'], flags: SegmentFlag = 0)

Source from the content-addressed store, hash-verified

2382 core.BNSetLogicalMemoryMapEnabled(self.handle, enabled)
2383
2384 def add_memory_region(self, name: str, start: int, source: Union['os.PathLike', str, bytes, bytearray, 'BinaryView', 'databuffer.DataBuffer', 'fileaccessor.FileAccessor'], flags: SegmentFlag = 0) -> bool:
2385 """
2386 Adds a memory region to the memory map. Depending on the source parameter, the memory region is created as one of the following types:
2387
2388 - **BinaryMemoryRegion** (***Unimplemented***): Represents a memory region loaded from a binary format, providing persistence across sessions.
2389 - **DataMemoryRegion**: Represents a memory region loaded from flat files or raw bytes, providing persistence across sessions.
2390 - **RemoteMemoryRegion**: Represents a memory region managed via a proxy callback interface. This region is ephemeral and not persisted across sessions.
2391
2392 The type of memory region created is determined by the `source` parameter:
2393 - `os.PathLike` or `str`: Treated as a file path to be loaded into memory as a `DataMemoryRegion`.
2394 - `bytes` or `bytearray`: Directly loaded into memory as a `DataMemoryRegion`.
2395 - `databuffer.DataBuffer`: Loaded as a `DataMemoryRegion`.
2396 - `fileaccessor.FileAccessor`: Creates a `RemoteMemoryRegion` that fetches data via a remote source.
2397 - `BinaryView`: (Not yet implemented) Intended for future exploration.
2398
2399 .. note:: If no flags are specified and the new memory region overlaps with one or more existing regions, the overlapping portions of the new region will inherit the flags of the respective underlying regions.
2400
2401 Parameters:
2402 name (str): A unique name for the memory region.
2403 start (int): The starting address in memory for the region.
2404 source (Union[os.PathLike, str, bytes, bytearray, BinaryView, databuffer.DataBuffer, fileaccessor.FileAccessor]): The source from which the memory is loaded.
2405 flags (SegmentFlag, optional): Flags to apply to the memory region. Defaults to 0 (no flags).
2406
2407 Returns:
2408 bool: `True` if the memory region was successfully added, `False` otherwise.
2409
2410 Raises:
2411 NotImplementedError: If the specified `source` type is unsupported.
2412 """
2413 if isinstance(source, os.PathLike):
2414 source = str(source)
2415 if isinstance(source, bytes) or isinstance(source, bytearray):
2416 source = databuffer.DataBuffer(source)
2417 if isinstance(source, str):
2418 with open(source, "rb") as f:
2419 source = databuffer.DataBuffer(f.read())
2420
2421 if name is None:
2422 name = ""
2423
2424 if isinstance(source, BinaryView):
2425 return core.BNAddBinaryMemoryRegion(self.handle, name, start, source.handle, flags)
2426 elif isinstance(source, databuffer.DataBuffer):
2427 return core.BNAddDataMemoryRegion(self.handle, name, start, source.handle, flags)
2428 elif isinstance(source, fileaccessor.FileAccessor):
2429 return core.BNAddRemoteMemoryRegion(self.handle, name, start, source._cb, flags)
2430 else:
2431 raise NotImplementedError
2432
2433 def remove_memory_region(self, name: str) -> bool:
2434 return core.BNRemoveMemoryRegion(self.handle, name)

Callers

nothing calls this directly

Calls 2

DataBufferMethod · 0.80
readMethod · 0.45

Tested by

no test coverage detected