summaryrefslogtreecommitdiff
path: root/src/cli/disasm.c
blob: 6f2ffa482618ae36e150e5e2d764091035be4f0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// The one thing asked of capstone, in C so that its structs stay its own.
#include <capstone/capstone.h>
#include <stdio.h>

static csh handle;

// A memory operand of an instruction, worked out against the registers.
struct zt_touch {
    uint64_t addr;
    uint8_t size;
    // 1 the instruction reads it, 2 it writes it, 3 both.
    uint8_t how;
};

// Where an instruction that is a call, a jump or a return goes.
struct zt_branch {
    // The target; with `indirect`, the address the target is read from.
    uint64_t target;
    // 0 not a branch, 1 call, 2 jump, 3 return (the target is on the stack).
    uint8_t kind;
    // A conditional jump whose condition does not hold is not taken.
    uint8_t taken;
    uint8_t indirect;
    // A jump on a condition.
    uint8_t conditional;
};

// Whether the flags (and rcx) make a conditional jump go.
static int holds(unsigned id, uint64_t flags, uint64_t rcx) {
    int cf = flags & 1, pf = flags >> 2 & 1, zf = flags >> 6 & 1, sf = flags >> 7 & 1, of = flags >> 11 & 1;
    switch (id) {
    case X86_INS_JO: return of;
    case X86_INS_JNO: return !of;
    case X86_INS_JB: return cf;
    case X86_INS_JAE: return !cf;
    case X86_INS_JE: return zf;
    case X86_INS_JNE: return !zf;
    case X86_INS_JBE: return cf || zf;
    case X86_INS_JA: return !cf && !zf;
    case X86_INS_JS: return sf;
    case X86_INS_JNS: return !sf;
    case X86_INS_JP: return pf;
    case X86_INS_JNP: return !pf;
    case X86_INS_JL: return sf != of;
    case X86_INS_JGE: return sf == of;
    case X86_INS_JLE: return zf || sf != of;
    case X86_INS_JG: return !zf && sf == of;
    case X86_INS_JRCXZ: return rcx == 0;
    case X86_INS_JECXZ: return (uint32_t)rcx == 0;
    default: return 1;
    }
}

// Where `reg` is in the kernel's user_regs_struct, as 64-bit words; -1 for
// a register an address is not made of here.
static int slot(x86_reg reg) {
    switch (reg) {
    case X86_REG_R15: return 0;
    case X86_REG_R14: return 1;
    case X86_REG_R13: return 2;
    case X86_REG_R12: return 3;
    case X86_REG_RBP: return 4;
    case X86_REG_RBX: return 5;
    case X86_REG_R11: return 6;
    case X86_REG_R10: return 7;
    case X86_REG_R9: return 8;
    case X86_REG_R8: return 9;
    case X86_REG_RAX: return 10;
    case X86_REG_RCX: return 11;
    case X86_REG_RDX: return 12;
    case X86_REG_RSI: return 13;
    case X86_REG_RDI: return 14;
    case X86_REG_RSP: return 19;
    default: return -1;
    }
}

// The instruction at `code`, which is at `addr`: its text into `text`, its
// length returned; 0 where there is no instruction. With `regs` (the
// thread's, about to run it), the memory it touches into `touches`, at
// most `*count` of them, and how many into `*count`. A branch is told
// without them too, its target where the instruction alone says it.
size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap, const uint64_t *regs, struct zt_touch *touches, size_t *count, struct zt_branch *branch) {
    if (!handle) {
        if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return 0;
        cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL);
        cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
    }
    size_t room = count ? *count : 0;
    if (count) *count = 0;
    cs_insn *insn;
    if (cs_disasm(handle, code, len, addr, 1, &insn) != 1) return 0;
    size_t size = insn->size;
    snprintf(text, cap, insn->op_str[0] ? "%s %s" : "%s", insn->mnemonic, insn->op_str);
    if (branch && insn->detail) {
        const cs_x86 *x = &insn->detail->x86;
        branch->kind = cs_insn_group(handle, insn, CS_GRP_RET) ? 3 : cs_insn_group(handle, insn, CS_GRP_CALL) ? 1 : cs_insn_group(handle, insn, CS_GRP_JUMP) ? 2 : 0;
        branch->taken = regs ? holds(insn->id, regs[18], regs[11]) : 1;
        branch->indirect = 0;
        branch->target = 0;
        branch->conditional = branch->kind == 2 && insn->id != X86_INS_JMP && insn->id != X86_INS_LJMP;
        if (branch->kind == 1 || branch->kind == 2) {
            const cs_x86_op *op = &x->operands[0];
            if (x->op_count < 1) branch->kind = 0;
            else if (op->type == X86_OP_IMM) branch->target = (uint64_t)op->imm;
            else if (op->type == X86_OP_REG && slot(op->reg) >= 0) branch->target = regs ? regs[slot(op->reg)] : 0;
            else if (op->type == X86_OP_MEM) {
                // Its address is the first touch below: the target is what is there.
                branch->indirect = 1;
            } else branch->kind = 0;
        }
    }
    // 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++) {
            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;
            if (op->mem.base == X86_REG_RIP) at += addr + size;
            else if (op->mem.base != X86_REG_INVALID) {
                if (slot(op->mem.base) < 0) continue;
                at += regs[slot(op->mem.base)];
            }
            if (op->mem.index != X86_REG_INVALID) {
                if (slot(op->mem.index) < 0) continue;
                at += regs[slot(op->mem.index)] * (uint64_t)op->mem.scale;
            }
            if (op->mem.segment == X86_REG_FS) at += regs[21];
            if (op->mem.segment == X86_REG_GS) at += regs[22];
            touches[*count].addr = at;
            touches[*count].size = op->size;
            touches[*count].how = (op->access & CS_AC_READ ? 1 : 0) | (op->access & CS_AC_WRITE ? 2 : 0);
            ++*count;
        }
    }
    cs_free(insn, 1);
    return size;
}