summaryrefslogtreecommitdiff
path: root/src/cli/disasm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cli/disasm.c')
-rw-r--r--src/cli/disasm.c32
1 files changed, 29 insertions, 3 deletions
diff --git a/src/cli/disasm.c b/src/cli/disasm.c
index 6f2ffa4..7f9f5e2 100644
--- a/src/cli/disasm.c
+++ b/src/cli/disasm.c
@@ -7,7 +7,8 @@ static csh handle;
// A memory operand of an instruction, worked out against the registers.
struct zt_touch {
uint64_t addr;
- uint8_t size;
+ // A rep string instruction's is the whole range its count covers.
+ uint32_t size;
// 1 the instruction reads it, 2 it writes it, 3 both.
uint8_t how;
};
@@ -113,7 +114,13 @@ size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, siz
// lea and the long nops name memory and touch none.
if (regs && insn->detail && insn->id != X86_INS_LEA && insn->id != X86_INS_NOP) {
const cs_x86 *x = &insn->detail->x86;
- for (int i = 0; i < x->op_count && *count < room; i++) {
+ // A string instruction (movs, cmps, stos, lods, scas: the one-byte
+ // opcodes a4 to af but test's a8 and a9) repeated touches its count's
+ // elements from rsi or rdi on, or down to them with the direction flag set.
+ uint8_t code0 = x->opcode[0];
+ int string = x->opcode[1] == 0 && code0 >= 0xa4 && code0 <= 0xaf && code0 != 0xa8 && code0 != 0xa9;
+ uint64_t times = string && (x->prefix[0] == X86_PREFIX_REP || x->prefix[0] == X86_PREFIX_REPNE) ? regs[11] : 1;
+ for (int i = 0; i < x->op_count && *count < room && times > 0; i++) {
const cs_x86_op *op = &x->operands[i];
if (op->type != X86_OP_MEM || !(op->access & (CS_AC_READ | CS_AC_WRITE))) continue;
uint64_t at = (uint64_t)op->mem.disp;
@@ -128,11 +135,30 @@ size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, siz
}
if (op->mem.segment == X86_REG_FS) at += regs[21];
if (op->mem.segment == X86_REG_GS) at += regs[22];
+ uint64_t size = op->size * times;
+ if (regs[18] & 0x400) at -= (times - 1) * op->size;
touches[*count].addr = at;
- touches[*count].size = op->size;
+ touches[*count].size = size > UINT32_MAX ? UINT32_MAX : (uint32_t)size;
touches[*count].how = (op->access & CS_AC_READ ? 1 : 0) | (op->access & CS_AC_WRITE ? 2 : 0);
++*count;
}
+ // The stack's own instructions touch memory no operand names, after
+ // what their operands touch: a push writes the word under rsp, a
+ // call its return address there, enter the saved rbp; a pop and a
+ // return read the word at rsp, leave the saved rbp at rbp. A word is
+ // 8 bytes, 2 with the operand-size prefix (pushf, push ax).
+ uint64_t rsp = regs[19];
+ uint32_t word = x->prefix[2] == 0x66 ? 2 : 8;
+ struct zt_touch t = { 0, 0, 0 };
+ switch (insn->id) {
+ case X86_INS_PUSH: case X86_INS_PUSHF: case X86_INS_PUSHFQ: t = (struct zt_touch){ rsp - word, word, 2 }; break;
+ case X86_INS_CALL: case X86_INS_ENTER: t = (struct zt_touch){ rsp - 8, 8, 2 }; break;
+ case X86_INS_POP: case X86_INS_POPF: case X86_INS_POPFQ: t = (struct zt_touch){ rsp, word, 1 }; break;
+ case X86_INS_RET: t = (struct zt_touch){ rsp, 8, 1 }; break;
+ case X86_INS_LEAVE: t = (struct zt_touch){ regs[4], 8, 1 }; break;
+ default: break;
+ }
+ if (t.size && *count < room) touches[(*count)++] = t;
}
cs_free(insn, 1);
return size;