stop emulation when hitting invalid code address. this fixes issue #82

This commit is contained in:
Nguyen Anh Quynh 2015-09-01 00:17:55 +08:00
parent 12019dba40
commit bea73ef213
2 changed files with 7 additions and 1 deletions

View file

@ -205,6 +205,8 @@ int cpu_exec(struct uc_struct *uc, CPUArchState *env) // qq
have_tb_lock = true; have_tb_lock = true;
tb = tb_find_fast(env); // qq tb = tb_find_fast(env); // qq
if (!tb) { // invalid TB due to invalid code? if (!tb) { // invalid TB due to invalid code?
uc->invalid_error = UC_ERR_CODE_INVALID;
ret = EXCP_HLT;
break; break;
} }
/* Note: we do it here to avoid a gcc bug on Mac OS X when /* Note: we do it here to avoid a gcc bug on Mac OS X when

View file

@ -4,20 +4,23 @@
import unicorn import unicorn
CODE_ADDR = 0x10101000 CODE_ADDR = 0x10101000
CODE = b'\xff\xe3' CODE = b'\xff\xe3' # jmp ebx
mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32) mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32)
mu.mem_map(CODE_ADDR, 1024 * 4) mu.mem_map(CODE_ADDR, 1024 * 4)
mu.mem_write(CODE_ADDR, CODE) mu.mem_write(CODE_ADDR, CODE)
# If EBX is zero then an exception is raised, as expected # If EBX is zero then an exception is raised, as expected
mu.reg_write(unicorn.x86_const.UC_X86_REG_EBX, 0x0) mu.reg_write(unicorn.x86_const.UC_X86_REG_EBX, 0x0)
print(">>> jmp ebx (ebx = 0)");
try: try:
mu.emu_start(CODE_ADDR, CODE_ADDR + 2, count=1) mu.emu_start(CODE_ADDR, CODE_ADDR + 2, count=1)
except unicorn.UcError as e: except unicorn.UcError as e:
print("ERROR: %s" % e)
assert(e.errno == unicorn.UC_ERR_CODE_INVALID) assert(e.errno == unicorn.UC_ERR_CODE_INVALID)
else: else:
assert(False) assert(False)
print(">>> jmp ebx (ebx = 0xaa96a47f)");
mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32) mu = unicorn.Uc(unicorn.UC_ARCH_X86, unicorn.UC_MODE_32)
mu.mem_map(CODE_ADDR, 1024 * 4) mu.mem_map(CODE_ADDR, 1024 * 4)
# If we write this address to EBX then the emulator hangs on emu_start # If we write this address to EBX then the emulator hangs on emu_start
@ -26,6 +29,7 @@ mu.mem_write(CODE_ADDR, CODE)
try: try:
mu.emu_start(CODE_ADDR, CODE_ADDR + 2, count=1) mu.emu_start(CODE_ADDR, CODE_ADDR + 2, count=1)
except unicorn.UcError as e: except unicorn.UcError as e:
print("ERROR: %s" % e)
assert(e.errno == unicorn.UC_ERR_CODE_INVALID) assert(e.errno == unicorn.UC_ERR_CODE_INVALID)
else: else:
assert(False) assert(False)