blob: 7f72597946e41ec50ddf840f3443fbe233991892 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08004#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08005#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <capstone.h>
9
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080011#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080012
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080013#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080014#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080015#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080016// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080017#define INSN_CACHE_SIZE 8
18#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080019
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080020cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080021cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
22void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080023
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080024extern void ARM_enable(void);
25extern void AArch64_enable(void);
26extern void Mips_enable(void);
27extern void X86_enable(void);
28extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080029extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080030extern void SystemZ_enable(void);
danghvu701b8502014-01-09 11:06:44 +070031
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080032static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080033{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080034 static bool initialized = false;
35
36 if (initialized)
37 return;
38
danghvub33bd2c2014-01-09 12:22:56 +070039#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080040 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070041#endif
42#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080043 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070044#endif
45#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080046 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070047#endif
danghvub33bd2c2014-01-09 12:22:56 +070048#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080049 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070050#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080051#ifdef CAPSTONE_HAS_SPARC
52 Sparc_enable();
53#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080054#ifdef CAPSTONE_HAS_SYSZ
55 SystemZ_enable();
56#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080057#ifdef CAPSTONE_HAS_X86
58 X86_enable();
59#endif
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080060
61 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070062}
63
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080064unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080065
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080066#ifdef USE_SYS_DYN_MEM
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080067cs_malloc_t cs_mem_malloc = malloc;
68cs_calloc_t cs_mem_calloc = calloc;
69cs_realloc_t cs_mem_realloc = realloc;
70cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080071cs_vsnprintf_t cs_vsnprintf = vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080072#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080073cs_malloc_t cs_mem_malloc = NULL;
74cs_calloc_t cs_mem_calloc = NULL;
75cs_realloc_t cs_mem_realloc = NULL;
76cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080077cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080078#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +080079
Nguyen Anh Quynhb90cb992013-12-28 13:59:09 +080080unsigned int cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +080081{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080082 archs_enable();
83
Nguyen Anh Quynh08777472013-12-22 14:16:28 +080084 if (major != NULL && minor != NULL) {
85 *major = CS_API_MAJOR;
86 *minor = CS_API_MINOR;
87 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080088
89 return (CS_API_MAJOR << 8) + CS_API_MINOR;
90}
91
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080092bool cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080093{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +080094 archs_enable();
95
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +080096 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080097 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080098 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080099 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
100 (1 << CS_ARCH_SYSZ));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800101
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800102 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800103 return all_arch & (1 << query);
104
105 if (query == CS_SUPPORT_DIET) {
106#ifdef CAPSTONE_DIET
107 return true;
108#else
109 return false;
110#endif
111 }
112
113 // unsupported query
114 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800115}
116
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800117cs_err cs_errno(csh handle)
118{
119 if (!handle)
120 return CS_ERR_CSH;
121
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800122 struct cs_struct *ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800123
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800124 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800125}
126
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800127const char *cs_strerror(cs_err code)
128{
129 switch(code) {
130 default:
131 return "Unknown error code";
132 case CS_ERR_OK:
133 return "OK (CS_ERR_OK)";
134 case CS_ERR_MEM:
135 return "Out of memory (CS_ERR_MEM)";
136 case CS_ERR_ARCH:
137 return "Invalid architecture (CS_ERR_ARCH)";
138 case CS_ERR_HANDLE:
139 return "Invalid handle (CS_ERR_HANDLE)";
140 case CS_ERR_CSH:
141 return "Invalid csh (CS_ERR_CSH)";
142 case CS_ERR_MODE:
143 return "Invalid mode (CS_ERR_MODE)";
144 case CS_ERR_OPTION:
145 return "Invalid option (CS_ERR_OPTION)";
146 case CS_ERR_DETAIL:
147 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800148 case CS_ERR_MEMSETUP:
149 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800150 case CS_ERR_VERSION:
151 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800152 case CS_ERR_DIET:
153 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800154 }
155}
156
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800157cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
158{
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800159 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800160 // Error: before cs_open(), dynamic memory management must be initialized
161 // with cs_option(CS_OPT_MEM)
162 return CS_ERR_MEMSETUP;
163
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800164 archs_enable();
165
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800166 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800167 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800168
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800169 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800170 if (!ud) {
171 // memory insufficient
172 return CS_ERR_MEM;
173 }
174
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800175 ud->errnum = CS_ERR_OK;
176 ud->arch = arch;
177 ud->mode = mode;
178 ud->big_endian = mode & CS_MODE_BIG_ENDIAN;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800179 // by default, do not break instruction into details
180 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800181
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800182 cs_err err = arch_init[ud->arch](ud);
183 if (err) {
184 cs_mem_free(ud);
185 *handle = 0;
186 return err;
187 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800188
189 *handle = (uintptr_t)ud;
190
191 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800192 } else {
193 *handle = 0;
194 return CS_ERR_ARCH;
195 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800196}
197
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800198cs_err cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800199{
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800200 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800201 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800202 return CS_ERR_CSH;
203
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800204 struct cs_struct *ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800205
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800206 if (ud->printer_info)
207 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800208
Nguyen Anh Quynh9fac5122014-01-07 10:56:04 +0800209 // arch_destroy[ud->arch](ud);
Nguyen Anh Quynhb2654062014-01-03 17:08:58 +0800210
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800211 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800212 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800213 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800214
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800215 // invalidate this handle by ZERO out its value.
216 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800217 *handle = 0;
218
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800219 return CS_ERR_OK;
220}
221
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800222#define MIN(x, y) ((x) < (y) ? (x) : (y))
223
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800224// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800225static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800226 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800227{
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800228 if (handle->detail) {
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800229 // avoiding copy insn->detail
230 memcpy(insn, &mci->flat_insn, sizeof(*insn) - sizeof(insn->detail));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800231
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800232 // NOTE: copy details in 2 chunks, since union is always put at address divisible by 8
233 // copy from @regs_read until @arm
234 memcpy(insn->detail, (void *)(&(mci->flat_insn)) + offsetof(cs_insn_flat, regs_read),
235 offsetof(cs_detail, arm) - offsetof(cs_detail, regs_read));
236 // then copy from @arm until end
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800237 memcpy((void *)((uintptr_t)(insn->detail) + offsetof(cs_detail, arm)),
238 (void *)((uintptr_t)(&(mci->flat_insn)) + offsetof(cs_insn_flat, arm)),
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800239 sizeof(cs_detail) - offsetof(cs_detail, arm));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800240 } else {
241 insn->address = mci->address;
Alex Ionescu46018db2014-01-22 09:45:00 -0800242 insn->size = (uint16_t)mci->insn_size;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800243 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800244
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800245 // fill the instruction bytes
246 memcpy(insn->bytes, code, MIN(sizeof(insn->bytes), insn->size));
247
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800248 // map internal instruction opcode to public insn ID
249 if (handle->insn_id)
Nguyen Anh Quynh1acfd0b2014-01-06 10:56:59 +0800250 handle->insn_id(handle, insn, MCInst_getOpcode(mci));
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800251
252 // alias instruction might have ID saved in OpcodePub
253 if (MCInst_getOpcodePub(mci))
254 insn->id = MCInst_getOpcodePub(mci);
255
256 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800257 if (postprinter)
258 postprinter((csh)handle, insn, buffer);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800259
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800260#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800262 // find first space or tab
263 char *sp = buffer;
264 for (sp = buffer; *sp; sp++)
265 if (*sp == ' '||*sp == '\t')
266 break;
267 if (*sp) {
268 *sp = '\0';
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800269 // find the next non-space char
270 sp++;
271 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
272 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800273 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
274 } else
275 insn->op_str[0] = '\0';
276
277 strncpy(insn->mnemonic, buffer, sizeof(insn->mnemonic) - 1);
278 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800279#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800280}
281
Nguyen Anh Quynhda8adad2013-12-04 09:44:07 +0800282cs_err cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800283{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800284 archs_enable();
285
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800286 // cs_option() can be called with NULL handle just for CS_OPT_MEM
287 // This is supposed to be executed before all other APIs (even cs_open())
288 if (type == CS_OPT_MEM) {
289 cs_opt_mem *mem = (cs_opt_mem *)value;
290
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800291 cs_mem_malloc = mem->malloc;
292 cs_mem_calloc = mem->calloc;
293 cs_mem_realloc = mem->realloc;
294 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800295 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800296
297 return CS_ERR_OK;
298 }
299
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800300 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600301 if (!handle)
302 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800303
danghvu0b6ea042013-12-19 23:07:26 -0600304 if (type == CS_OPT_DETAIL) {
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800305 handle->detail = value;
306 return CS_ERR_OK;
307 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800308
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800309 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800310}
311
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800312// get previous instruction, which can be in the cache, or in total buffer
313static cs_insn *get_prev_insn(cs_insn *cache, unsigned int f, void *total, size_t total_size)
314{
315 if (f == 0) {
316 if (total == NULL)
317 return NULL;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800318 // get the trailing insn from total buffer, which is at
319 // the end of the latest cache trunk
Alex Ionescu46018db2014-01-22 09:45:00 -0800320 return (cs_insn *)((void*)((uintptr_t)total + total_size - sizeof(cs_insn)));
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800321 } else
322 return &cache[f - 1];
323}
324
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800325// dynamicly allocate memory to contain disasm insn
326// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh04c19be2013-12-25 13:26:22 +0800327size_t cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800328{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800329 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800330 MCInst mci;
331 uint16_t insn_size;
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800332 size_t c = 0;
333 unsigned int f = 0;
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800334 cs_insn insn_cache[INSN_CACHE_SIZE];
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800335 void *total = NULL;
Nguyen Anh Quynhb42a6572013-11-29 17:40:07 +0800336 size_t total_size = 0;
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800337 bool r;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800338
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800339 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800340 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800341 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800342 return 0;
343 }
344
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800345 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800346
Nguyen Anh Quynh11b05192014-01-22 11:04:25 +0800347 // reset previous prefix for X86
348 handle->prev_prefix = 0;
349
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800350 memset(insn_cache, 0, sizeof(insn_cache));
351
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800352 while (size > 0) {
danghvu2b192962013-12-19 22:40:28 -0600353 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800354 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800355
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800356 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800357 if (r) {
358 SStream ss;
359 SStream_Init(&ss);
360
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800361 // relative branches need to know the address & size of current insn
362 mci.insn_size = insn_size;
363 mci.address = offset;
364
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800365 if (handle->detail) {
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800366 // save all the information for non-detailed mode
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800367 mci.flat_insn.address = offset;
368 mci.flat_insn.size = insn_size;
369 // allocate memory for @detail pointer
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800370 insn_cache[f].detail = cs_mem_calloc(1, sizeof(cs_detail));
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800371 }
372
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800373 handle->printer(&mci, &ss, handle->printer_info);
374
Joxean114df0e2013-12-04 07:11:32 +0100375 fill_insn(handle, &insn_cache[f], ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800376
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800377 if (!handle->check_combine || !handle->check_combine(handle, &insn_cache[f])) {
378 f++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800379
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800380 if (f == ARR_SIZE(insn_cache)) {
381 // resize total to contain newly disasm insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800382 void *tmp;
383
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +0800384 total_size += (sizeof(cs_insn) * INSN_CACHE_SIZE);
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800385 tmp = cs_mem_realloc(total, total_size);
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800386 if (tmp == NULL) { // insufficient memory
387 cs_mem_free(total);
388 handle->errnum = CS_ERR_MEM;
389 return 0;
390 }
391
392 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800393 memcpy((void*)((uintptr_t)total + total_size - sizeof(insn_cache)), insn_cache, sizeof(insn_cache));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800394
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800395 // reset f back to 0
396 f = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800397 }
398
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800399 c++;
400 } else {
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800401 // combine this instruction with previous prefix "instruction"
Nguyen Anh Quynh7772d852014-01-21 11:49:25 +0800402 cs_insn *prev = get_prev_insn(insn_cache, f, total, total_size);
403 handle->combine(handle, &insn_cache[f], prev);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800404 }
405
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800406 buffer += insn_size;
407 size -= insn_size;
408 offset += insn_size;
409
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800410 if (count > 0) {
411 // x86 hacky
412 if (!handle->prev_prefix) {
413 if (c == count)
414 break;
415 } else {
416 // only combine 1 prefix with regular instruction
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800417 if (c == count + 1) {
418 // the last insn is redundant
419 c--;
420 f--;
421 // free allocated detail pointer of the last redundant instruction
422 if (handle->detail)
423 cs_mem_free(insn_cache[f].detail);
424
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800425 break;
Nguyen Anh Quynh8ce50e42014-01-27 18:15:28 +0800426 }
Nguyen Anh Quynh94020d82014-01-25 14:22:15 +0800427 }
428 }
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800429 } else {
430 // encounter a broken instruction
431 // XXX: TODO: JOXEAN continue here
432 break;
433 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800434 }
435
436 if (f) {
437 // resize total to contain newly disasm insns
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800438 void *tmp = cs_mem_realloc(total, total_size + f * sizeof(insn_cache[0]));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800439 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800440 cs_mem_free(total);
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800441 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800442 return 0;
443 }
444
445 total = tmp;
Alex Ionescu46018db2014-01-22 09:45:00 -0800446 memcpy((void*)((uintptr_t)total + total_size), insn_cache, f * sizeof(insn_cache[0]));
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800447
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800448 }
449
450 *insn = total;
451
452 return c;
453}
454
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800455void cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800456{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800457 size_t i;
458
459 // free all detail pointers
460 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800461 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800462
463 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800464 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800465}
466
467// return friendly name of regiser in a string
pancakef0e4eed2013-12-11 22:14:42 +0100468const char *cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800469{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800470 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800471
472 if (!handle || handle->reg_name == NULL) {
473 return NULL;
474 }
475
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800476 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800477}
478
pancakef0e4eed2013-12-11 22:14:42 +0100479const char *cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800480{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800481 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800482
483 if (!handle || handle->insn_name == NULL) {
484 return NULL;
485 }
486
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800487 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800488}
489
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800490static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800491{
492 int i;
493
494 for (i = 0; i < max; i++) {
495 if (arr[i] == id)
496 return true;
497 }
498
499 return false;
500}
501
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800502bool cs_insn_group(csh ud, cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800503{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800504 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800505 return false;
506
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800507 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800508 if (!handle->detail) {
509 handle->errnum = CS_ERR_DETAIL;
510 return false;
511 }
512
513 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800514}
515
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800516bool cs_reg_read(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800517{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800518 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800519 return false;
520
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800521 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800522 if (!handle->detail) {
523 handle->errnum = CS_ERR_DETAIL;
524 return false;
525 }
526
527 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800528}
529
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800530bool cs_reg_write(csh ud, cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800531{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800532 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800533 return false;
534
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800535 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800536 if (!handle->detail) {
537 handle->errnum = CS_ERR_DETAIL;
538 return false;
539 }
540
541 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800542}
543
544int cs_op_count(csh ud, cs_insn *insn, unsigned int op_type)
545{
546 if (!ud)
547 return -1;
548
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800549 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800550 if (!handle->detail) {
551 handle->errnum = CS_ERR_DETAIL;
552 return -1;
553 }
554
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800555 unsigned int count = 0, i;
556
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800557 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800558
559 switch (handle->arch) {
560 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800561 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800562 return -1;
563 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800564 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800565 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800566 count++;
567 break;
568 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800569 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800570 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800571 count++;
572 break;
573 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800574 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800575 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800576 count++;
577 break;
578 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800579 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800580 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800581 count++;
582 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800583 case CS_ARCH_PPC:
584 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800585 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800586 count++;
587 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800588 case CS_ARCH_SPARC:
589 for (i = 0; i < insn->detail->sparc.op_count; i++)
590 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
591 count++;
592 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800593 case CS_ARCH_SYSZ:
594 for (i = 0; i < insn->detail->sysz.op_count; i++)
595 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
596 count++;
597 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800598 }
599
600 return count;
601}
602
603int cs_op_index(csh ud, cs_insn *insn, unsigned int op_type,
604 unsigned int post)
605{
606 if (!ud)
607 return -1;
608
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800609 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800610 if (!handle->detail) {
611 handle->errnum = CS_ERR_DETAIL;
612 return -1;
613 }
614
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800615 unsigned int count = 0, i;
616
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800617 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800618
619 switch (handle->arch) {
620 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800621 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800622 return -1;
623 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800624 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800625 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800626 count++;
627 if (count == post)
628 return i;
629 }
630 break;
631 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800632 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800633 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800634 count++;
635 if (count == post)
636 return i;
637 }
638 break;
639 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800640 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800641 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800642 count++;
643 if (count == post)
644 return i;
645 }
646 break;
647 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800648 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800649 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800650 count++;
651 if (count == post)
652 return i;
653 }
654 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800655 case CS_ARCH_PPC:
656 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -0800657 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800658 count++;
659 if (count == post)
660 return i;
661 }
662 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800663 case CS_ARCH_SPARC:
664 for (i = 0; i < insn->detail->sparc.op_count; i++) {
665 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
666 count++;
667 if (count == post)
668 return i;
669 }
670 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800671 case CS_ARCH_SYSZ:
672 for (i = 0; i < insn->detail->sysz.op_count; i++) {
673 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
674 count++;
675 if (count == post)
676 return i;
677 }
678 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800679 }
680
681 return -1;
682}