I am disassembling the memory of the following Python program. The disassembly should start with int 3
instruction, or 0xcc
, but GDB (running through either clion or qt develop) does not see it and instead shows some kind of add
instruction that results from decoding one previous byte with it, that is 0x00 0xcc
.
I am aware that x86 assembly is not self-synchronizing (What happens if `objdump -d --start-address` starts printing from the middle of an x86 instruction?).
How can I help GDB to synchronize this properly?
import ctypes
import mmap
buf = mmap.mmap(-1, mmap.PAGESIZE, prot=mmap.PROT_READ | mmap.PROT_WRITE | mmap.PROT_EXEC)
buf.write(
b'\xcc' # int 3
b'\x8b\xc7' # mov eax, edi
b'\x83\xc0\x01' # add eax, 1
b'\xc3' # ret
)
f_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
f_pointer = ctypes.c_void_p.from_buffer(buf)
f = f_type(ctypes.addressof(f_pointer))
result = f(42)
print(f"{result=}")
del f_pointer
buf.close()