Skip to content

HowTo correctly trace x86 CPU instructions? #2173

Open
@Alexco500

Description

@Alexco500

Hi there,
I have a rather unusual x86 code, which needs to be run in Unicorn. The code needs protected mode (32 bit) and switches to 16 bit segments. If I execute the code, I get an "unmapped memory error", but I don't know why. So I started to single step debugging into Unicorn, but as soon as the translation block is executed, I end up with the error. Means, I can't single step into ret = tcg_qemu_tb_exec(env, tb_ptr);, in line 60 of cpu_exec.c.

The situation is as follows:
Register Dump

EAX: 0x00272024   EBX: 0x00000064   ECX: 0x00000027   EDX: 0x00000000
ESI: 0x00042068   EDI: 0x0004202c   EBP: 0x0000202c   ESP: 0x00042024
EIP: 0x00000006   EFL: 0x00000002
  Reserved (Always 1) (Bit 1)


GS: 0x0000   ES: 0x0027   FS: 0x150b
DS: 0x0027   SS: 0x0027   CS: 0x000f

00010006  ff 5e 00           call far [bp]
00010009  66 ea 0b 01 02 00 5b 00  jmp far 0x005B:0x0002010B
00010011  8c d1              mov cx, ss
00010013  8e d9              mov ds, cx
00010015  8e c1              mov es, cx
00010017  ff 5e 00           call far [bp]`

so the call far [bp] creates the memory error:

Tracing instruction at 0x10000, instruction size = 0x2
--- EFLAGS is 0x2
Tracing instruction at 0x10002, instruction size = 0x2
--- EFLAGS is 0x2
Tracing instruction at 0x10004, instruction size = 0x2
--- EFLAGS is 0x2
Tracing instruction at 0x10006, instruction size = 0x3
--- EFLAGS is 0x2
mem invalid, type 19  @ 0x00000006, address 0x00082068

Failed on uc_emu_start() with error returned 6: Invalid memory read (UC_ERR_READ_UNMAPPED)

PC: 10006 == 0000000f:00000006

I don't know why the error occurs while accessing address 0x82068. If I look at the registers, the call should access 0x4202c, which is inside the stack and looks like that:

0x00042040:    0x00000000
0x0004203c:    0x00000053
0x00042038:    0x00000053
0x00042034:    0x00000053
0x00042030:    0x00042038
0x0004202c:    0xffff0037
0x00042028:    0x00272054
0x00042024:    0x00010000
0x00042024    ESP

So theoretical it should use ffff:0037 as target for the call, but it breaks before.
Any idea how I can get deeper into that?
Maybe the GDT/LDT setup is wrong?
Can I enable some kind of trace for further debugging?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions