Search for a sequence of bytes in memory. Args: needle: bytes sequence or regex pattern to look for begin: search starting address (or None to start at lowest avaiable address) end: search ending address (or None to end at highest avaiable address)
(self, needle: Union[bytes, Pattern[bytes]], begin: Optional[int] = None, end: Optional[int] = None)
| 412 | self.write(addr, _pack(value)) |
| 413 | |
| 414 | def search(self, needle: Union[bytes, Pattern[bytes]], begin: Optional[int] = None, end: Optional[int] = None) -> List[int]: |
| 415 | """Search for a sequence of bytes in memory. |
| 416 | |
| 417 | Args: |
| 418 | needle: bytes sequence or regex pattern to look for |
| 419 | begin: search starting address (or None to start at lowest avaiable address) |
| 420 | end: search ending address (or None to end at highest avaiable address) |
| 421 | |
| 422 | Returns: addresses of all matches |
| 423 | """ |
| 424 | |
| 425 | # if starting point not set, search from the first mapped region |
| 426 | if begin is None: |
| 427 | begin = self.map_info[0][0] |
| 428 | |
| 429 | # if ending point not set, search till the last mapped region |
| 430 | if end is None: |
| 431 | end = self.map_info[-1][1] |
| 432 | |
| 433 | assert begin < end, 'search arguments do not make sense' |
| 434 | |
| 435 | # narrow the search down to relevant ranges; mmio ranges are excluded due to potential read side effects |
| 436 | ranges = [(max(begin, lbound), min(ubound, end)) for lbound, ubound, _, _, mmio_ctx in self.map_info if not (end < lbound or ubound < begin or mmio_ctx is not None)] |
| 437 | results = [] |
| 438 | |
| 439 | # if needle is a bytes sequence use it verbatim, not as a pattern |
| 440 | if type(needle) is bytes: |
| 441 | needle = re.escape(needle) |
| 442 | |
| 443 | for lbound, ubound in ranges: |
| 444 | haystack = self.read(lbound, ubound - lbound) |
| 445 | local_results = (match.start(0) + lbound for match in re.finditer(needle, haystack)) |
| 446 | |
| 447 | results.extend(local_results) |
| 448 | |
| 449 | return results |
| 450 | |
| 451 | def unmap(self, addr: int, size: int) -> None: |
| 452 | """Reclaim a memory range. |