Files
unicorn/tests/regress/invalid_insn.py
@Antelox 9cfd5cfac3 - Improved the GitHub python binding workflow: (#2072)
- Added fullMode input in workflow_dispatch
    - Take decision whether to build either in debug or release mode and if to build for all python versions according to the commit message patterns
    - Set proper artifact names
    - Removed not needed steps
    - Compacted some steps in order to leverage more the matrix feature
    - Bumped cibuildwheel action to 2.22.0
    - Run actual regress tests in place of sample scripts
- Specify optional test install in pyproject.toml with proper requirements
- Derive package version from git tags
- Add GENERATORS env var support in setup.py to specify cmake generator and minor refactoring
- Minor cleanup/refactoring for the regress test suite
- Marked some regress tests with skipIf to skip them in case of old python versions
- Marked some failing regress tests to be checked with skipIf
2024-12-29 22:24:48 +08:00

68 lines
1.6 KiB
Python

import regress
from unicorn import *
from unicorn.x86_const import *
from capstone import *
CODE = (
b'\x48\x31\xc0' # xor rax,rax
b'\x48\x0f\xc7\xf0' # rdrand rax
b'\xf4' # hlt
)
BASE = 0x100000
PAGE_SIZE = 0x1000
# max possible length of a x86 instruction
MAX_INSN_LEN = 15
def hook_invalid_insn(uc, ud):
regress.logger.debug('entered invalid instruction handler')
pc = uc.reg_read(UC_X86_REG_RIP)
data = uc.mem_read(pc, MAX_INSN_LEN)
md = Cs(CS_ARCH_X86, CS_MODE_64)
insn = next(md.disasm(data, pc, 1))
if insn.mnemonic == 'rdrand':
# chosen by fair dice roll, guaranteed to be random
rax = 4
# set result to rax
uc.reg_write(UC_X86_REG_RAX, rax)
# resume emulation from next instruction
uc.reg_write(UC_X86_REG_RIP, pc + insn.size)
# signal uc we are ok
return True
# not handled, uc will crash
return False
class TestHooks(regress.RegressTest):
def test_invalid_insn_recover(self):
mu = Uc(UC_ARCH_X86, UC_MODE_64)
mu.mem_map(BASE, PAGE_SIZE)
mu.mem_write(BASE, CODE)
mu.hook_add(UC_HOOK_INSN_INVALID, hook_invalid_insn)
try:
mu.emu_start(BASE, BASE + len(CODE))
except UcError as ex:
if ex.errno == UC_ERR_INSN_INVALID:
self.fail('invalid instruction did not recover properly')
# unexpected exception, re-raise
raise
self.assertNotEqual(0, mu.reg_read(UC_X86_REG_RAX))
if __name__ == '__main__':
regress.main()