From c97fa4fd8a5a1fd9b7f9a65ed8cd3584be8e4879 Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Fri, 13 Nov 2015 08:59:36 -0500 Subject: [PATCH 1/3] add test case to run_across_bb demonstrates that calling emu_start from a BB start to another BB end works fine. --- tests/regress/run_across_bb.py | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/tests/regress/run_across_bb.py b/tests/regress/run_across_bb.py index 9409c06d..2c37f283 100755 --- a/tests/regress/run_across_bb.py +++ b/tests/regress/run_across_bb.py @@ -33,11 +33,67 @@ def showpc(mu): class RunAcrossBBTest(regress.RegressTest): + def test_run_all(self): + try: + ####################################################################### + # emu SETUP + ####################################################################### + print("\n---- test: run_all ----") + + + mu = Uc(UC_ARCH_X86, UC_MODE_32) + + def hook_code(uc, address, size, user_data): + print(">>> Tracing instruction at 0x%x, instruction size = %u" %(address, size)) + mu.hook_add(UC_HOOK_CODE, hook_code) + + # base of CODE + mu.mem_map(0x1000, 0x1000) + mu.mem_write(0x1000, CODE) + + # stack + mu.mem_map(0x2000, 0x1000) + + mu.reg_write(UC_X86_REG_EIP, 0x1000) + mu.reg_write(UC_X86_REG_ESP, 0x2800) + self.assertEqual(0x1000, mu.reg_read(UC_X86_REG_EIP), "unexpected PC") + self.assertEqual(0x2800, mu.reg_read(UC_X86_REG_ESP), "unexpected SP") + showpc(mu) + + mu.emu_start(0x1000, 0x1016) + # should exec the following four instructions: + # 1000: b8 00 00 00 00 mov eax,0x0 < + # 1005: 40 inc eax < + # 1006: 40 inc eax < + # 1007: 68 10 10 00 00 push 0x1010 < + # 100c: c3 ret -----------+ + # 100d: cc int3 | + # 100e: cc int3 | + # 100f: cc int3 | + # 1010: b8 00 00 00 00 mov eax,0x0 <-+ + # 1015: 40 inc eax < + # 1016: 40 inc eax < + self.assertEqual(0x1016, mu.reg_read(UC_X86_REG_EIP), "unexpected PC (2)") + self.assertEqual(0x2800, mu.reg_read(UC_X86_REG_ESP), "unexpected SP (2)") + showpc(mu) + + except UcError as e: + if e.errno == UC_ERR_FETCH_UNMAPPED: + # during initial test dev, bad fetch at 0x1010, but the data is there, + # and this proves it + print("!!! about to bail due to bad fetch... here's the data at PC:") + print(binascii.hexlify(mu.mem_read(mu.reg_read(UC_X86_REG_EIP), 0x8))) + + self.assertFalse(True, "ERROR: %s @ 0x%x" % (e, mu.reg_read(UC_X86_REG_EIP))) + + + def test_run_across_bb(self): try: ####################################################################### # emu SETUP ####################################################################### + print("\n---- test: run_across_bb ----") mu = Uc(UC_ARCH_X86, UC_MODE_32) From d09cc8849529e600d06cf9ef8a3aef09deee4fff Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Fri, 13 Nov 2015 09:13:27 -0500 Subject: [PATCH 2/3] run_across_bb: whitespacing --- tests/regress/run_across_bb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/regress/run_across_bb.py b/tests/regress/run_across_bb.py index 2c37f283..2f969894 100755 --- a/tests/regress/run_across_bb.py +++ b/tests/regress/run_across_bb.py @@ -38,7 +38,7 @@ class RunAcrossBBTest(regress.RegressTest): ####################################################################### # emu SETUP ####################################################################### - print("\n---- test: run_all ----") + print("\n---- test: run_all ----") mu = Uc(UC_ARCH_X86, UC_MODE_32) @@ -93,7 +93,7 @@ class RunAcrossBBTest(regress.RegressTest): ####################################################################### # emu SETUP ####################################################################### - print("\n---- test: run_across_bb ----") + print("\n---- test: run_across_bb ----") mu = Uc(UC_ARCH_X86, UC_MODE_32) From eb7398476332dbd7cb95310b4c70ba19de8221ff Mon Sep 17 00:00:00 2001 From: Willi Ballenthin Date: Fri, 13 Nov 2015 10:14:38 -0500 Subject: [PATCH 3/3] run_across_bb: include expected vs actual PC in assertion error --- tests/regress/run_across_bb.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/regress/run_across_bb.py b/tests/regress/run_across_bb.py index 2f969894..074db4d4 100755 --- a/tests/regress/run_across_bb.py +++ b/tests/regress/run_across_bb.py @@ -176,7 +176,9 @@ class RunAcrossBBTest(regress.RegressTest): # 1010: b8 00 00 00 00 mov eax,0x0 < # 1015: 40 inc eax < # 1016: 40 inc eax < - self.assertEqual(0x1016, mu.reg_read(UC_X86_REG_EIP), "unexpected PC (4)") + self.assertEqual(0x1016, mu.reg_read(UC_X86_REG_EIP), + "unexpected PC (4): 0x%x vs 0x%x" % ( + 0x1016, mu.reg_read(UC_X86_REG_EIP))) showpc(mu) except UcError as e: