Skip to content

New APIs: cs_disasm_alloc() & cs_disasm_buf()

aquynh edited this page Oct 3, 2014 · 21 revisions

Currently cs_disasm API allocate memory for disassembled instructions every time it runs, which is expensive if we need to decode a lot of instructions.

This proposal proposes 2 new API:

  • cs_disasm_malloc: pre-allocate buffer for instructions

  • cs_disasm_buf: disassemble instructions into pre-allocated buffer provided by cs_disasm_malloc


Prototype:

// pre-allocate memory for @count instructions
cs_err cs_disasm_malloc(csh handle, size_t count, cs_insn **insn);

// disassemble @count instructions using buffer pre-allocated
// by cs_disasm_malloc()
size_t cs_disasm_buf(csh handle,
                const uint8_t *code, size_t code_size,
                uint64_t address,
                size_t count,
                cs_insn **insn);

Sample code will be like below (C-pseudo)

if (cs_disasm_malloc(h, count, &insns) == CS_ERR_OK) {
    // mycount < count
    while(c = cs_disasm_buf(h, code, size, address, mycount, &insns)) {
        // analyze *c* instructions in @insns ...

        // then update input code/size/address for the next iteration
        length = CS_INSN_OFFSET(insns, c);
        code += length;
        size -= length;
        address += length;
    }

    // free memory when done
    cs_free(insns, count);
}