1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
|
class Uc(RegStateManager): """Unicorn Engine class. """
@staticmethod def __is_compliant() -> bool: """Checks whether Unicorn binding version complies with Unicorn library.
Returns: `True` if versions match, `False` otherwise """
uc_maj, uc_min, _ = uc_version() bnd_maj, bnd_min, _ = version_bind()
return (uc_maj, uc_min) == (bnd_maj, bnd_min)
def __new__(cls, arch: int, mode: int, cpu: Optional[int] = None): if not Uc.__is_compliant(): raise UcError(uc.UC_ERR_VERSION)
import importlib
def __uc_subclass(pkgname: str, clsname: str): """Use a lazy subclass instantiation to avoid importing unnecessary arch classes. """
def __wrapped() -> Type[Uc]: archmod = importlib.import_module(f'.arch.{pkgname}', 'unicorn.unicorn_py3')
return getattr(archmod, clsname)
return __wrapped
def __uc_generic(): return Uc
wrapped: Callable[[], Type[Uc]] = { uc.UC_ARCH_ARM : __uc_subclass('arm', 'UcAArch32'), uc.UC_ARCH_ARM64 : __uc_subclass('arm64', 'UcAArch64'), uc.UC_ARCH_MIPS : __uc_generic, uc.UC_ARCH_X86 : __uc_subclass('intel', 'UcIntel'), uc.UC_ARCH_PPC : __uc_generic, uc.UC_ARCH_SPARC : __uc_generic, uc.UC_ARCH_M68K : __uc_generic, uc.UC_ARCH_RISCV : __uc_generic, uc.UC_ARCH_S390X : __uc_generic, uc.UC_ARCH_TRICORE : __uc_generic }[arch]
subclass = wrapped()
return super(Uc, cls).__new__(subclass)
def __init__(self, arch: int, mode: int, cpu: Optional[int] = None) -> None: """Initialize a Unicorn engine instance.
Args: arch: emulated architecture identifier (see UC_ARCH_* constants) mode: emulated processor mode (see UC_MODE_* constants) cpu: emulated cpu model (see UC_CPU_* constants) [optional] """
self._arch = arch self._mode = mode
self._uch = uc_engine() status = uclib.uc_open(arch, mode, ctypes.byref(self._uch))
if status != uc.UC_ERR_OK: self._uch = None raise UcError(status)
if cpu is not None: self.ctl_set_cpu_model(cpu)
self._callbacks: Dict[int, ctypes._FuncPointer] = {} self._mmio_callbacks: Dict[Tuple[int, int], Tuple[Optional[MMIO_READ_CFUNC], Optional[MMIO_WRITE_CFUNC]]] = {}
self._hook_exception: Optional[Exception] = None
self.__finalizer = weakref.finalize(self, Uc.release_handle, self._uch)
@staticmethod def release_handle(uch: uc_engine) -> None:
if uch: try: status = uclib.uc_close(uch)
except: pass
else: if status != uc.UC_ERR_OK: raise UcError(status)
@property def errno(self) -> int: """Get last error number.
Returns: error number (see: UC_ERR_*) """
return uclib.uc_errno(self._uch)
def emu_start(self, begin: int, until: int, timeout: int = 0, count: int = 0) -> None: """Start emulation from a specified address to another.
Args: begin : emulation starting address until : emulation ending address timeout : limit emulation to a certain amount of time (milliseconds) count : limit emulation to a certain amount of instructions
Raises: `UcError` : in case emulation could not be started properly `Exception` : in case an error has been encountered during emulation """
self._hook_exception = None status = uclib.uc_emu_start(self._uch, begin, until, timeout, count)
if status != uc.UC_ERR_OK: raise UcError(status)
if self._hook_exception is not None: raise self._hook_exception
def emu_stop(self) -> None: """Stop emulation.
Raises: `UcError` in case emulation could not be stopped properly """
status = uclib.uc_emu_stop(self._uch)
if status != uc.UC_ERR_OK: raise UcError(status)
def _do_reg_read(self, reg_id: int, reg_obj) -> int: """Private register read implementation. Do not call directly. """
return uclib.uc_reg_read(self._uch, reg_id, reg_obj)
def _do_reg_write(self, reg_id: int, reg_obj) -> int: """Private register write implementation. Do not call directly. """
return uclib.uc_reg_write(self._uch, reg_id, reg_obj)
def _do_reg_read_batch(self, reglist, vallist, count) -> int: """Private batch register read implementation. Do not call directly. """
return uclib.uc_reg_read_batch(self._uch, reglist, vallist, count)
def _do_reg_write_batch(self, reglist, vallist, count) -> int: """Private batch register write implementation. Do not call directly. """
return uclib.uc_reg_write_batch(self._uch, reglist, vallist, count)
def mem_map(self, address: int, size: int, perms: int = uc.UC_PROT_ALL) -> None: """Map a memory range.
Args: address : range base address size : range size (in bytes) perms : access protection bitmask
Raises: `UcError` in case memory could not be mapped """
assert (perms & ~uc.UC_PROT_ALL) == 0, 'unexpected perms bitmask'
status = uclib.uc_mem_map(self._uch, address, size, perms)
if status != uc.UC_ERR_OK: raise UcError(status)
def mem_map_ptr(self, address: int, size: int, perms: int, ptr: int) -> None: """Map a memory range and point to existing data on host memory.
Args: address : range base address size : range size (in bytes) perms : access protection bitmask ptr : address of data on host memory
Raises: `UcError` in case memory could not be mapped """
assert (perms & ~uc.UC_PROT_ALL) == 0, 'unexpected perms bitmask'
status = uclib.uc_mem_map_ptr(self._uch, address, size, perms, ptr)
if status != uc.UC_ERR_OK: raise UcError(status)
def mem_unmap(self, address: int, size: int) -> None: """Reclaim a mapped memory range.
Args: address : range base address size : range size (in bytes)
Raises: `UcError` in case memory could not be unmapped """
status = uclib.uc_mem_unmap(self._uch, address, size)
if status != uc.UC_ERR_OK: raise UcError(status)
rng = (address, address + size)
if rng in self._mmio_callbacks: del self._mmio_callbacks[rng]
def mem_protect(self, address: int, size: int, perms: int = uc.UC_PROT_ALL) -> None: """Modify access protection bitmask of a mapped memory range.
Args: address : range base address size : range size (in bytes) perms : new access protection bitmask
Raises: `UcError` in case access protection bitmask could not be changed """
assert (perms & ~uc.UC_PROT_ALL) == 0, 'unexpected perms bitmask'
status = uclib.uc_mem_protect(self._uch, address, size, perms)
if status != uc.UC_ERR_OK: raise UcError(status)
def mmio_map(self, address: int, size: int, read_cb: Optional[UC_MMIO_READ_TYPE], read_ud: Any, write_cb: Optional[UC_MMIO_WRITE_TYPE], write_ud: Any) -> None: """Map an MMIO range. This method binds a memory range to read and write accessors to simulate a hardware device. Unicorn does not allocate memory to back this range.
Args: address : range base address size : range size (in bytes) read_cb : read callback to invoke upon read access. if not specified, reads \ from the mmio range will be silently dropped read_ud : optional context object to pass on to the read callback write_cb : write callback to invoke upon a write access. if not specified, writes \ to the mmio range will be silently dropped write_ud : optional context object to pass on to the write callback """
@uccallback(self, MMIO_READ_CFUNC) def __mmio_map_read_cb(uc: Uc, offset: int, size: int, key: int) -> int: assert read_cb is not None
return read_cb(uc, offset, size, read_ud)
@uccallback(self, MMIO_WRITE_CFUNC) def __mmio_map_write_cb(uc: Uc, offset: int, size: int, value: int, key: int) -> None: assert write_cb is not None
write_cb(uc, offset, size, value, write_ud)
read_cb_fptr = read_cb and __mmio_map_read_cb write_cb_fptr = write_cb and __mmio_map_write_cb
status = uclib.uc_mmio_map(self._uch, address, size, read_cb_fptr, 0, write_cb_fptr, 0)
if status != uc.UC_ERR_OK: raise UcError(status)
rng = (address, address + size)
self._mmio_callbacks[rng] = (read_cb_fptr, write_cb_fptr)
def mem_regions(self) -> Iterator[Tuple[int, int, int]]: """Iterate through mapped memory regions.
Returns: an iterator whose elements contain begin, end and perms properties of each range
Raises: `UcError` in case an internal error has been encountered """
regions = ctypes.POINTER(uc_mem_region)() count = ctypes.c_uint32() status = uclib.uc_mem_regions(self._uch, ctypes.byref(regions), ctypes.byref(count))
if status != uc.UC_ERR_OK: raise UcError(status)
try: for i in range(count.value): yield regions[i].value
finally: uclib.uc_free(regions)
def mem_read(self, address: int, size: int) -> bytearray: """Read data from emulated memory subsystem.
Args: address : source memory location size : amount of bytes to read
Returns: data bytes
Raises: `UcError` in case of an invalid memory access """
data = ctypes.create_string_buffer(size) status = uclib.uc_mem_read(self._uch, address, data, size)
if status != uc.UC_ERR_OK: raise UcError(status, address, size)
return bytearray(data)
def mem_write(self, address: int, data: bytes) -> None: """Write data to emulated memory subsystem.
Args: address : target memory location data : data bytes to write
Raises: `UcError` in case of an invalid memory access """
size = len(data) status = uclib.uc_mem_write(self._uch, address, data, size)
if status != uc.UC_ERR_OK: raise UcError(status, address, size)
def __do_hook_add(self, htype: int, fptr: ctypes._FuncPointer, begin: int, end: int, *args: ctypes.c_int) -> int: handle = uc_hook_h()
dummy = 0
status = uclib.uc_hook_add( self._uch, ctypes.byref(handle), htype, fptr, ctypes.cast(dummy, ctypes.c_void_p), ctypes.c_uint64(begin), ctypes.c_uint64(end), *args )
if status != uc.UC_ERR_OK: raise UcError(status)
self._callbacks[handle.value] = fptr
return handle.value
def hook_add(self, htype: int, callback: Callable, user_data: Any = None, begin: int = 1, end: int = 0, aux1: int = 0, aux2: int = 0) -> int: """Hook emulated events of a certain type.
Args: htype : event type(s) to hook (see UC_HOOK_* constants) callback : a method to call each time the hooked event occurs user_data : an additional context to pass to the callback when it is called begin : address where hook scope starts end : address where hook scope ends aux1 : auxiliary parameter; needed for some hook types aux2 : auxiliary parameter; needed for some hook types
Returns: hook handle
Raises: `UcError` in case of an invalid htype value """
def __hook_intr(): @uccallback(self, HOOK_INTR_CFUNC) def __hook_intr_cb(uc: Uc, intno: int, key: int) -> None: callback(uc, intno, user_data)
return (__hook_intr_cb,)
def __hook_insn(): raise UcError(uc.UC_ERR_ARG)
def __hook_code(): @uccallback(self, HOOK_CODE_CFUNC) def __hook_code_cb(uc: Uc, address: int, size: int, key: int) -> None: callback(uc, address, size, user_data)
return (__hook_code_cb,)
def __hook_invalid_mem(): @uccallback(self, HOOK_MEM_INVALID_CFUNC) def __hook_mem_invalid_cb(uc: Uc, access: int, address: int, size: int, value: int, key: int) -> bool: return callback(uc, access, address, size, value, user_data)
return (__hook_mem_invalid_cb,)
def __hook_mem(): @uccallback(self, HOOK_MEM_ACCESS_CFUNC) def __hook_mem_access_cb(uc: Uc, access: int, address: int, size: int, value: int, key: int) -> None: callback(uc, access, address, size, value, user_data)
return (__hook_mem_access_cb,)
def __hook_invalid_insn(): @uccallback(self, HOOK_INSN_INVALID_CFUNC) def __hook_insn_invalid_cb(uc: Uc, key: int) -> bool: return callback(uc, user_data)
return (__hook_insn_invalid_cb,)
def __hook_edge_gen(): @uccallback(self, HOOK_EDGE_GEN_CFUNC) def __hook_edge_gen_cb(uc: Uc, cur: ctypes._Pointer[uc_tb], prev: ctypes._Pointer[uc_tb], key: int) -> None: callback(uc, cur.contents, prev.contents, user_data)
return (__hook_edge_gen_cb,)
def __hook_tcg_opcode(): @uccallback(self, HOOK_TCG_OPCODE_CFUNC) def __hook_tcg_op_cb(uc: Uc, address: int, arg1: int, arg2: int, size: int, key: int) -> None: callback(uc, address, arg1, arg2, size, user_data)
opcode = ctypes.c_uint64(aux1) flags = ctypes.c_uint64(aux2)
return (__hook_tcg_op_cb, opcode, flags)
def __hook_tlb_fill(): @uccallback(self, HOOK_TLB_FILL_CFUNC) def __hook_tlb_fill_cb(uc: Uc, vaddr: int, access: int, entry: ctypes._Pointer[uc_tlb_entry], key: int) -> bool: return callback(uc, vaddr, access, entry.contents, user_data)
return (__hook_tlb_fill_cb,)
handlers: Dict[int, Callable[[], Tuple]] = { uc.UC_HOOK_INTR : __hook_intr, uc.UC_HOOK_INSN : __hook_insn, uc.UC_HOOK_CODE : __hook_code, uc.UC_HOOK_BLOCK : __hook_code, uc.UC_HOOK_MEM_READ_UNMAPPED : __hook_invalid_mem, uc.UC_HOOK_MEM_WRITE_UNMAPPED : __hook_invalid_mem, uc.UC_HOOK_MEM_FETCH_UNMAPPED : __hook_invalid_mem, uc.UC_HOOK_MEM_READ_PROT : __hook_invalid_mem, uc.UC_HOOK_MEM_WRITE_PROT : __hook_invalid_mem, uc.UC_HOOK_MEM_FETCH_PROT : __hook_invalid_mem, uc.UC_HOOK_MEM_READ : __hook_mem, uc.UC_HOOK_MEM_WRITE : __hook_mem, uc.UC_HOOK_MEM_FETCH : __hook_mem, uc.UC_HOOK_INSN_INVALID : __hook_invalid_insn, uc.UC_HOOK_EDGE_GENERATED : __hook_edge_gen, uc.UC_HOOK_TCG_OPCODE : __hook_tcg_opcode, uc.UC_HOOK_TLB_FILL : __hook_tlb_fill }
matched = set(handlers.get(1 << n) for n in range(32) if htype & (1 << n))
if len(matched) != 1: raise UcError(uc.UC_ERR_ARG)
handler = matched.pop()
if handler is None: raise UcError(uc.UC_ERR_ARG)
fptr, *aux = handler()
return self.__do_hook_add(htype, fptr, begin, end, *aux)
def hook_del(self, handle: int) -> None: """Remove an existing hook.
Args: handle: hook handle """
h = uc_hook_h(handle) status = uclib.uc_hook_del(self._uch, h)
if status != uc.UC_ERR_OK: raise UcError(status)
del self._callbacks[handle]
def query(self, prop: int) -> int: """Query an internal Unicorn property.
Args: prop: property identifier (see: UC_QUERY_* constants)
Returns: property value """
result = ctypes.c_size_t() status = uclib.uc_query(self._uch, prop, ctypes.byref(result))
if status != uc.UC_ERR_OK: raise UcError(status, prop)
return result.value
def context_save(self) -> UcContext: """Save Unicorn instance internal context.
Returns: unicorn context instance """
context = UcContext(self._uch, self._arch, self._mode) status = uclib.uc_context_save(self._uch, context.context)
if status != uc.UC_ERR_OK: raise UcError(status)
return context
def context_update(self, context: UcContext) -> None: """Update Unicorn instance internal context.
Args: context : unicorn context instance to copy data from """
status = uclib.uc_context_save(self._uch, context.context)
if status != uc.UC_ERR_OK: raise UcError(status)
def context_restore(self, context: UcContext) -> None: """Overwrite Unicorn instance internal context.
Args: context : unicorn context instance to copy data from """
status = uclib.uc_context_restore(self._uch, context.context)
if status != uc.UC_ERR_OK: raise UcError(status)
@staticmethod def __ctl_encode(ctl: int, op: int, nargs: int) -> int: assert check_maxbits(nargs, 4), f'nargs must not exceed value of 15 (got {nargs})' assert op and check_maxbits(op, 2), f'op must not exceed value of 3 (got {op})'
return (op << 30) | (nargs << 26) | ctl
def ctl(self, ctl: int, op: int, *args): code = Uc.__ctl_encode(ctl, op, len(args))
status = uclib.uc_ctl(self._uch, code, *args)
if status != uc.UC_ERR_OK: raise UcError(status)
Arg = Tuple[Type, Optional[int]]
def __ctl_r(self, ctl: int, arg0: Arg): atype, _ = arg0 carg = atype()
self.ctl(ctl, uc.UC_CTL_IO_READ, ctypes.byref(carg))
return carg.value
def __ctl_w(self, ctl: int, *args: Arg): cargs = (atype(avalue) for atype, avalue in args)
self.ctl(ctl, uc.UC_CTL_IO_WRITE, *cargs)
def __ctl_wr(self, ctl: int, arg0: Arg, arg1: Arg): atype, avalue = arg0 carg0 = atype(avalue)
atype, _ = arg1 carg1 = atype()
self.ctl(ctl, uc.UC_CTL_IO_READ_WRITE, carg0, ctypes.byref(carg1))
return carg1.value
def ctl_get_mode(self) -> int: """Retrieve current processor mode.
Returns: current mode (see UC_MODE_* constants) """
return self.__ctl_r(uc.UC_CTL_UC_MODE, (ctypes.c_int, None) )
def ctl_get_page_size(self) -> int: """Retrieve target page size.
Returns: page size in bytes """
return self.__ctl_r(uc.UC_CTL_UC_PAGE_SIZE, (ctypes.c_uint32, None) )
def ctl_set_page_size(self, val: int) -> None: """Set target page size.
Args: val: page size to set (in bytes)
Raises: `UcError` in any of the following cases: - Unicorn architecture is not ARM - Unicorn has already completed its initialization - Page size is not a power of 2 """
self.__ctl_w(uc.UC_CTL_UC_PAGE_SIZE, (ctypes.c_uint32, val) )
def ctl_get_arch(self) -> int: """Retrieve target architecture.
Returns: current architecture (see UC_ARCH_* constants) """
return self.__ctl_r(uc.UC_CTL_UC_ARCH, (ctypes.c_int, None) )
def ctl_get_timeout(self) -> int: """Retrieve emulation timeout.
Returns: timeout value set on emulation start """
return self.__ctl_r(uc.UC_CTL_UC_TIMEOUT, (ctypes.c_uint64, None) )
def ctl_exits_enabled(self, enable: bool) -> None: """Instruct Unicorn whether to respect emulation exit points or ignore them.
Args: enable: `True` to enable exit points, `False` to ignore them """
self.__ctl_w(uc.UC_CTL_UC_USE_EXITS, (ctypes.c_int, enable) )
def ctl_get_exits_cnt(self) -> int: """Retrieve emulation exit points count.
Returns: number of emulation exit points
Raises: `UcErro` if Unicorn is set to ignore exits """
return self.__ctl_r(uc.UC_CTL_UC_EXITS_CNT, (ctypes.c_size_t, None) )
def ctl_get_exits(self) -> Sequence[int]: """Retrieve emulation exit points.
Returns: a tuple of all emulation exit points
Raises: `UcErro` if Unicorn is set to ignore exits """
count = self.ctl_get_exits_cnt() arr = (ctypes.c_uint64 * count)()
self.ctl(uc.UC_CTL_UC_EXITS, uc.UC_CTL_IO_READ, ctypes.cast(arr, ctypes.c_void_p), ctypes.c_size_t(count))
return tuple(arr)
def ctl_set_exits(self, exits: Sequence[int]) -> None: """Set emulation exit points.
Args: exits: a list of emulation exit points to set
Raises: `UcErro` if Unicorn is set to ignore exits """
arr = (ctypes.c_uint64 * len(exits))(*exits)
self.ctl(uc.UC_CTL_UC_EXITS, uc.UC_CTL_IO_WRITE, ctypes.cast(arr, ctypes.c_void_p), ctypes.c_size_t(len(arr)))
def ctl_get_cpu_model(self) -> int: """Retrieve target processor model.
Returns: target cpu model (see UC_CPU_* constants) """
return self.__ctl_r(uc.UC_CTL_CPU_MODEL, (ctypes.c_int, None) )
def ctl_set_cpu_model(self, model: int) -> None: """Set target processor model.
Args: model: cpu model to set (see UC_CPU_* constants)
Raises: `UcError` in any of the following cases: - `model` is not a valid cpu model - Requested cpu model is incompatible with current mode - Unicorn has already completed its initialization """
self.__ctl_w(uc.UC_CTL_CPU_MODEL, (ctypes.c_int, model) )
def ctl_remove_cache(self, lbound: int, ubound: int) -> None: """Invalidate translation cache for a specified region.
Args: lbound: region lower bound ubound: region upper bound
Raises: `UcError` in case the provided range bounds are invalid """
self.__ctl_w(uc.UC_CTL_TB_REMOVE_CACHE, (ctypes.c_uint64, lbound), (ctypes.c_uint64, ubound) )
def ctl_request_cache(self, addr: int) -> TBStruct: """Get translation cache info for a specified address.
Args: addr: address to get its translation cache info
Returns: a 3-tuple containing the base address, instructions count and size of the translation block containing the specified address """
return self.__ctl_wr(uc.UC_CTL_TB_REQUEST_CACHE, (ctypes.c_uint64, addr), (uc_tb, None) )
def ctl_flush_tb(self) -> None: """Flush the entire translation cache. """
self.__ctl_w(uc.UC_CTL_TB_FLUSH)
def ctl_set_tlb_mode(self, mode: int) -> None: """Set TLB mode.
Args: mode: tlb mode to use (see UC_TLB_* constants) """
self.__ctl_w(uc.UC_CTL_TLB_TYPE, (ctypes.c_uint, mode) )
def ctl_tlb_mode(self, mode: int) -> None: """Deprecated, please use ctl_set_tlb_mode instead.
Args: mode: tlb mode to use (see UC_TLB_* constants) """ warnings.warn('Deprecated method, use ctl_set_tlb_mode', DeprecationWarning) self.ctl_set_tlb_mode(mode)
def ctl_get_tcg_buffer_size(self) -> int: """Retrieve TCG buffer size.
Returns: buffer size (in bytes) """
return self.__ctl_r(uc.UC_CTL_TCG_BUFFER_SIZE, (ctypes.c_uint32, None) )
def ctl_set_tcg_buffer_size(self, size: int) -> None: """Set TCG buffer size.
Args: size: new size to set """
self.__ctl_w(uc.UC_CTL_TCG_BUFFER_SIZE, (ctypes.c_uint32, size) )
|