Skip to content

Commit

Permalink
x86: proper calculation for the trailing instruction in total cache. …
Browse files Browse the repository at this point in the history
…issue reported by Pancake
  • Loading branch information
aquynh committed Jan 22, 2014
1 parent 06b3c05 commit c34959b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion arch/X86/X86Mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -6662,7 +6662,8 @@ bool X86_insn_check_combine(cs_struct *h, cs_insn *insn)
return true;
}

// cannot combine this with a prefix
// neither prefix instruction nor having previous instruction as prefix,
// so we cannot combine this with a prefix
return false;
}

Expand Down
11 changes: 7 additions & 4 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "utils.h"
#include "MCRegisterInfo.h"

#define INSN_CACHE_SIZE 64

cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Expand Down Expand Up @@ -279,8 +281,9 @@ static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_
if (f == 0) {
if (total == NULL)
return NULL;
// get the trailing insn from total buffer
return (cs_insn *)(total + total_size - sizeof(cs_insn));
// get the trailing insn from total buffer, which is at
// the end of the latest cache trunk
return (cs_insn *)(total + total_size - (sizeof(cs_insn) * INSN_CACHE_SIZE));
} else
return &cache[f - 1];
}
Expand All @@ -294,7 +297,7 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,
uint16_t insn_size;
size_t c = 0;
unsigned int f = 0;
cs_insn insn_cache[64];
cs_insn insn_cache[INSN_CACHE_SIZE];
void *total = NULL;
size_t total_size = 0;

Expand Down Expand Up @@ -338,7 +341,7 @@ size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset,

if (f == ARR_SIZE(insn_cache)) {
// resize total to contain newly disasm insns
total_size += sizeof(insn_cache);
total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
void *tmp = cs_mem_realloc(total, total_size);
if (tmp == NULL) { // insufficient memory
cs_mem_free(total);
Expand Down

0 comments on commit c34959b

Please sign in to comment.