Revamp Python regression tests suite (#2022)
* Fix Python regression test suite (partial) * Fix Python regression test suite * Add a test for mapping at high addresses * Add ctl tests
This commit is contained in:
@@ -1,90 +1,77 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
from __future__ import print_function
|
||||
import binascii
|
||||
import regress
|
||||
|
||||
from unicorn import *
|
||||
from unicorn.x86_const import *
|
||||
|
||||
|
||||
CODE = binascii.unhexlify(b"".join([
|
||||
b"48c7c003000000", # mov rax, 3 mapped: 0x1000
|
||||
b"0f05", # syscall mapped: 0x1007
|
||||
b"48c7c700400000", # mov rdi, 0x4000 mapped: 0x1009
|
||||
b"488907", # mov [rdi], rdx mapped: 0x1010
|
||||
b"488b07", # mov rdx, [rdi] mapped: 0x1013
|
||||
b"4883c201", # add rdx, 1 mapped: 0x1016
|
||||
]))
|
||||
CODE = (
|
||||
b'\x48\xc7\xc0\x03\x00\x00\x00' # 0x1000: mov rax, 3
|
||||
b'\x0f\x05' # 0x1007: syscall
|
||||
b'\x48\xc7\xc7\x00\x40\x00\x00' # 0x1009: mov rdi, 0x4000
|
||||
b'\x48\x89\x07' # 0x1010: mov [rdi], rdx
|
||||
b'\x48\x8b\x07' # 0x1013: mov rdx, [rdi]
|
||||
b'\x48\x83\xc2\x01' # 0x1016: add rdx, 1
|
||||
)
|
||||
|
||||
BASE = 0x00001000
|
||||
SCRATCH = 0x00004000
|
||||
|
||||
|
||||
class SingleStepper:
|
||||
def __init__(self, emu, test):
|
||||
self._emu = emu
|
||||
self._hit_count = 0
|
||||
self._test = test
|
||||
def __init__(self, uc, test):
|
||||
self.uc = uc
|
||||
self.hits = 0
|
||||
self.test = test
|
||||
|
||||
def _stop_hook(self, uc, address, *args, **kwargs):
|
||||
if self._hit_count == 0:
|
||||
self._hit_count += 1
|
||||
else:
|
||||
self._test.assertEqual(1, self._hit_count, "HOOK_CODE invoked too many times")
|
||||
self.hits += 1
|
||||
|
||||
if self.hits > 1:
|
||||
self.test.assertEqual(2, self.hits, "HOOK_CODE invoked too many times")
|
||||
uc.emu_stop()
|
||||
|
||||
def step(self):
|
||||
self._hit_count = 0
|
||||
h = self._emu.hook_add(UC_HOOK_CODE, self._stop_hook)
|
||||
self.hits = 0
|
||||
h = self.uc.hook_add(UC_HOOK_CODE, self._stop_hook)
|
||||
|
||||
try:
|
||||
pc = self._emu.reg_read(UC_X86_REG_RIP)
|
||||
self._emu.emu_start(pc, pc+0x20)
|
||||
pc = self.uc.reg_read(UC_X86_REG_RIP)
|
||||
self.uc.emu_start(pc, pc + 0x20)
|
||||
finally:
|
||||
self._emu.hook_del(h)
|
||||
self.uc.hook_del(h)
|
||||
|
||||
|
||||
def showpc(mu):
|
||||
pc = mu.reg_read(UC_X86_REG_RIP)
|
||||
print("pc: 0x%x" % (pc))
|
||||
regress.logger.debug("pc: %#x", mu.reg_read(UC_X86_REG_RIP))
|
||||
|
||||
|
||||
class HookCodeStopEmuTest(regress.RegressTest):
|
||||
def test_hook_code_stop_emu(self):
|
||||
try:
|
||||
mu = Uc(UC_ARCH_X86, UC_MODE_64)
|
||||
mu = Uc(UC_ARCH_X86, UC_MODE_64)
|
||||
|
||||
# base of CODE
|
||||
mu.mem_map(0x1000, 0x1000)
|
||||
mu.mem_write(0x1000, CODE)
|
||||
# base of CODE
|
||||
mu.mem_map(BASE, 0x1000)
|
||||
mu.mem_write(BASE, CODE)
|
||||
|
||||
# scratch, used by CODE
|
||||
mu.mem_map(0x4000, 0x1000)
|
||||
# scratch, used by CODE
|
||||
mu.mem_map(SCRATCH, 0x1000)
|
||||
|
||||
mu.reg_write(UC_X86_REG_RDX, 0x1)
|
||||
mu.reg_write(UC_X86_REG_RIP, 0x1000)
|
||||
mu.reg_write(UC_X86_REG_RDX, 0x1)
|
||||
mu.reg_write(UC_X86_REG_RIP, BASE)
|
||||
|
||||
# 0x1000: 48c7c003000000 mov rax, 3
|
||||
# 0x1007: 0f05 syscall
|
||||
# 0x1009: 48c7c700400000 mov rdi, 0x4000
|
||||
# 0x1010: 488907 mov [rdi], rdx
|
||||
# 0x1013: 488b07 mov rdx, [rdi]
|
||||
# 0x1016: 4883c201 add rdx, 1
|
||||
|
||||
stepper = SingleStepper(mu, self)
|
||||
showpc(mu)
|
||||
self.assertEqual(0x1000, mu.reg_read(UC_X86_REG_RIP), "Unexpected PC")
|
||||
stepper = SingleStepper(mu, self)
|
||||
showpc(mu)
|
||||
self.assertEqual(BASE + 0x0, mu.reg_read(UC_X86_REG_RIP), "Unexpected starting PC")
|
||||
|
||||
stepper.step()
|
||||
showpc(mu)
|
||||
self.assertEqual(BASE + 0x7, mu.reg_read(UC_X86_REG_RIP), "Emulator failed to stop after one instruction")
|
||||
|
||||
stepper.step()
|
||||
showpc(mu)
|
||||
self.assertEqual(0x1007, mu.reg_read(UC_X86_REG_RIP),
|
||||
"Emulator failed to stop after one instruction")
|
||||
|
||||
stepper.step()
|
||||
showpc(mu)
|
||||
self.assertEqual(0x1009, mu.reg_read(UC_X86_REG_RIP),
|
||||
"Emulator failed to stop after one instruction")
|
||||
|
||||
except UcError as e:
|
||||
self.assertFalse(0, "ERROR: %s" % e)
|
||||
stepper.step()
|
||||
showpc(mu)
|
||||
self.assertEqual(BASE + 0x9, mu.reg_read(UC_X86_REG_RIP), "Emulator failed to stop after one instruction")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user