Unicorn CPU emulator framework (ARM, AArch64, M68K, Mips, Sparc, PowerPC, RiscV, S390x, TriCore, X86)
GNU General Public License v2.0
7.68k
stars
1.35k
forks
source link
Memory hooks cause incorrect emulation of the carry flag for the SAR instrution on x86_64 #1933
Closed
epuni closed 7 months ago
While emulating obfuscated code i encountered this snippet (cleaned up manually to a minimal test case)
The result of this should be RSI = 0x31, however when a memory hook is set the
sar
instruction sets the carry flag causing the value to be incorrect.The issue seems to be related to the alignment of the instructions, as moving around the bytes fixes the issue, see the example below.
Repro code:
Running this code on my ubuntu machine with python 3.10.12 and latest unicorn
2.0.1.post1
printsRSI = 0x32
.Observations:
mov rcx, 0xffffffff
tomov rcx, 0xffff
, the output is correctnop
at the start of the instructions, the output is correctuc.hook_add(UC_HOOK_MEM_READ | UC_HOOK_MEM_WRITE, _mem_hook)
line always produces the correct outputIs this a known issue and is there a workaround ?