blob: bf4fd267a254663e52d1607ff659711e386de6c1 [file] [log] [blame]
Nguyen Anh Quynh6023ef72014-04-29 11:21:04 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01003#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
4#pragma warning(disable:4996)
5#endif
reverserbcf09f42015-04-09 18:28:19 +01006#if defined(CAPSTONE_HAS_OSXKERNEL)
7#include <libkern/libkern.h>
8#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08009#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include <stdio.h>
11#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +010012#endif
13
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014#include <string.h>
15#include <capstone.h>
16
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080017#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080018#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019
tandasat1dd89872016-04-23 15:51:24 -070020#if defined(_KERNEL_MODE)
21#include "windows\winkernel_mm.h"
22#endif
23
24#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080025#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080026#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080027// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080028#define INSN_CACHE_SIZE 8
29#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080030
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080031// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080032#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080033
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080034cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080035cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
36void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080037
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080038extern void ARM_enable(void);
39extern void AArch64_enable(void);
40extern void Mips_enable(void);
41extern void X86_enable(void);
42extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080043extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080044extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080045extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070046
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080047static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080048{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080049 static bool initialized = false;
50
51 if (initialized)
52 return;
53
danghvub33bd2c2014-01-09 12:22:56 +070054#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080055 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070056#endif
57#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080058 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070059#endif
60#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080061 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070062#endif
danghvub33bd2c2014-01-09 12:22:56 +070063#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080064 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070065#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080066#ifdef CAPSTONE_HAS_SPARC
67 Sparc_enable();
68#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080069#ifdef CAPSTONE_HAS_SYSZ
70 SystemZ_enable();
71#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080072#ifdef CAPSTONE_HAS_X86
73 X86_enable();
74#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080075#ifdef CAPSTONE_HAS_XCORE
76 XCore_enable();
77#endif
78
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080079
80 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070081}
82
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080083unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080084
tandasat1dd89872016-04-23 15:51:24 -070085#if defined(CAPSTONE_USE_SYS_DYN_MEM)
86#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080087cs_malloc_t cs_mem_malloc = malloc;
88cs_calloc_t cs_mem_calloc = calloc;
89cs_realloc_t cs_mem_realloc = realloc;
90cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +080091cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasat1dd89872016-04-23 15:51:24 -070092#elif defined(_KERNEL_MODE)
93cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
94cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
95cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
96cs_free_t cs_mem_free = cs_winkernel_free;
97cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +080098#else
Pbb8741bd2015-11-10 23:02:26 +010099extern void* kern_os_malloc(size_t size);
100extern void kern_os_free(void* addr);
101extern void* kern_os_realloc(void* addr, size_t nsize);
102
103static void* cs_kern_os_calloc(size_t num, size_t size)
104{
105 return kern_os_malloc(num * size); // malloc bzeroes the buffer
106}
107
108cs_malloc_t cs_mem_malloc = kern_os_malloc;
109cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
110cs_realloc_t cs_mem_realloc = kern_os_realloc;
111cs_free_t cs_mem_free = kern_os_free;
112cs_vsnprintf_t cs_vsnprintf = vsnprintf;
113#endif
114#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800115cs_malloc_t cs_mem_malloc = NULL;
116cs_calloc_t cs_mem_calloc = NULL;
117cs_realloc_t cs_mem_realloc = NULL;
118cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800119cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800120#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800121
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800122CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700123unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800124{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800125 archs_enable();
126
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800127 if (major != NULL && minor != NULL) {
128 *major = CS_API_MAJOR;
129 *minor = CS_API_MINOR;
130 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800131
132 return (CS_API_MAJOR << 8) + CS_API_MINOR;
133}
134
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800135CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700136bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800137{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800138 archs_enable();
139
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800140 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800141 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800142 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800143 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800144 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800145
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800146 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800147 return all_arch & (1 << query);
148
149 if (query == CS_SUPPORT_DIET) {
150#ifdef CAPSTONE_DIET
151 return true;
152#else
153 return false;
154#endif
155 }
156
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800157 if (query == CS_SUPPORT_X86_REDUCE) {
158#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800159 return true;
160#else
161 return false;
162#endif
163 }
164
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800165 // unsupported query
166 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800167}
168
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800169CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700170cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800171{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800172 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800173 if (!handle)
174 return CS_ERR_CSH;
175
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100176 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800177
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800178 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800179}
180
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800181CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700182const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800183{
184 switch(code) {
185 default:
186 return "Unknown error code";
187 case CS_ERR_OK:
188 return "OK (CS_ERR_OK)";
189 case CS_ERR_MEM:
190 return "Out of memory (CS_ERR_MEM)";
191 case CS_ERR_ARCH:
192 return "Invalid architecture (CS_ERR_ARCH)";
193 case CS_ERR_HANDLE:
194 return "Invalid handle (CS_ERR_HANDLE)";
195 case CS_ERR_CSH:
196 return "Invalid csh (CS_ERR_CSH)";
197 case CS_ERR_MODE:
198 return "Invalid mode (CS_ERR_MODE)";
199 case CS_ERR_OPTION:
200 return "Invalid option (CS_ERR_OPTION)";
201 case CS_ERR_DETAIL:
202 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800203 case CS_ERR_MEMSETUP:
204 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800205 case CS_ERR_VERSION:
206 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800207 case CS_ERR_DIET:
208 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800209 case CS_ERR_SKIPDATA:
210 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800211 }
212}
213
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800214CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700215cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800216{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800217 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800218 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800219 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800220 // Error: before cs_open(), dynamic memory management must be initialized
221 // with cs_option(CS_OPT_MEM)
222 return CS_ERR_MEMSETUP;
223
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800224 archs_enable();
225
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800226 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800227 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800228 if (!ud) {
229 // memory insufficient
230 return CS_ERR_MEM;
231 }
232
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800233 ud->errnum = CS_ERR_OK;
234 ud->arch = arch;
235 ud->mode = mode;
tandasatb3d38cf2016-05-06 17:03:57 -0700236 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800237 // by default, do not break instruction into details
238 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800239
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800240 // default skipdata setup
241 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
242
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100243 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800244 if (err) {
245 cs_mem_free(ud);
246 *handle = 0;
247 return err;
248 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800249
250 *handle = (uintptr_t)ud;
251
252 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800253 } else {
254 *handle = 0;
255 return CS_ERR_ARCH;
256 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800257}
258
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800259CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700260cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800261{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800262 struct cs_struct *ud;
263
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800264 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800265 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800266 return CS_ERR_CSH;
267
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100268 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800269
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800270 if (ud->printer_info)
271 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800272
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800273 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800274
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800275 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800276 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800277
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800278 // invalidate this handle by ZERO out its value.
279 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800280 *handle = 0;
281
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282 return CS_ERR_OK;
283}
284
285// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800286static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800287 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800288{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800289#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700290 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800291#endif
tandasata6609fe2016-05-04 05:54:28 -0700292 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800293
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800294 // fill the instruction bytes.
295 // we might skip some redundant bytes in front in the case of X86
296 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
297 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800298
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800299 // alias instruction might have ID saved in OpcodePub
300 if (MCInst_getOpcodePub(mci))
301 insn->id = MCInst_getOpcodePub(mci);
302
303 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800304 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800305 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800306
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800307#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800309 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100310 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800311 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800312 for (sp = buffer; *sp; sp++) {
313 if (*sp == ' '|| *sp == '\t')
314 break;
315 if (*sp == '|') // lock|rep prefix for x86
316 *sp = ' ';
317 // copy to @mnemonic
318 *mnem = *sp;
319 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800320 }
321
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800322 *mnem = '\0';
323
324 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800325 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800326 // find the next non-space char
327 sp++;
328 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
329 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800330 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
331 } else
332 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800333#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800334}
335
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800336// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800337// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800338static uint8_t skipdata_size(cs_struct *handle)
339{
340 switch(handle->arch) {
341 default:
342 // should never reach
343 return -1;
344 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800345 // skip 2 bytes on Thumb mode.
346 if (handle->mode & CS_MODE_THUMB)
347 return 2;
348 // otherwise, skip 4 bytes
349 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800350 case CS_ARCH_ARM64:
351 case CS_ARCH_MIPS:
352 case CS_ARCH_PPC:
353 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800354 // skip 4 bytes
355 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800356 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800357 // SystemZ instruction's length can be 2, 4 or 6 bytes,
358 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800359 return 2;
360 case CS_ARCH_X86:
361 // X86 has no restriction on instruction alignment
362 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800363 case CS_ARCH_XCORE:
364 // XCore instruction's length can be 2 or 4 bytes,
365 // so we just skip 2 bytes
366 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800367 }
368}
369
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800370CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700371cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800372{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800373 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800374 archs_enable();
375
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800376 // cs_option() can be called with NULL handle just for CS_OPT_MEM
377 // This is supposed to be executed before all other APIs (even cs_open())
378 if (type == CS_OPT_MEM) {
379 cs_opt_mem *mem = (cs_opt_mem *)value;
380
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800381 cs_mem_malloc = mem->malloc;
382 cs_mem_calloc = mem->calloc;
383 cs_mem_realloc = mem->realloc;
384 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800385 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800386
387 return CS_ERR_OK;
388 }
389
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100390 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600391 if (!handle)
392 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800393
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800394 switch(type) {
395 default:
396 break;
397 case CS_OPT_DETAIL:
Félix Cloutierc141af92015-03-02 22:06:56 -0500398 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800399 return CS_ERR_OK;
400 case CS_OPT_SKIPDATA:
401 handle->skipdata = (value == CS_OPT_ON);
402 if (handle->skipdata) {
403 if (handle->skipdata_size == 0) {
404 // set the default skipdata size
405 handle->skipdata_size = skipdata_size(handle);
406 }
407 }
408 return CS_ERR_OK;
409 case CS_OPT_SKIPDATA_SETUP:
410 if (value)
411 handle->skipdata_setup = *((cs_opt_skipdata *)value);
412 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800413 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800414
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800415 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800416}
417
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800418// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800419static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
420{
421 char *p = opstr;
422 int len;
423 size_t i;
tandasat760940f2016-03-31 18:14:43 -0700424 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800425
426 if (!size) {
427 opstr[0] = '\0';
428 return;
429 }
430
tandasat760940f2016-03-31 18:14:43 -0700431 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800432 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700433 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800434
435 for(i = 1; i < size; i++) {
tandasat760940f2016-03-31 18:14:43 -0700436 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
437 if (len < 0) {
438 break;
439 }
440 if ((size_t)len > available - 1) {
441 break;
442 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800443 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700444 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800445 }
446}
447
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800448// dynamicly allocate memory to contain disasm insn
449// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800450CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700451size_t CAPSTONE_API cs_disasm(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 +0800452{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800453 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800454 MCInst mci;
455 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800456 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800457 unsigned int f = 0; // index of the next instruction in the cache
458 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800459 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800460 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800461 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800462 void *tmp;
463 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800464 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800465 size_t size_org;
466 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800467 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500468 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800469
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800470 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800471 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800472 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800473 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800474 return 0;
475 }
476
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800477 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800478
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800479 // reset IT block of ARM structure
Nguyen Anh Quynh0c864402016-04-27 14:47:05 +0800480 if (handle->arch == CS_ARCH_ARM)
481 handle->ITBlock.size = 0;
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800482
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800483#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800484 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400485 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800486#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800487
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800488 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800489 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800490 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800491 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800492
493 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700494 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800495 if (total == NULL) {
496 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500497 handle->errnum = CS_ERR_MEM;
498 return 0;
499 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800500
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700501 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800502
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800503 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800504 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800505 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800506
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700507 // relative branches need to know the address & size of current insn
508 mci.address = offset;
509
510 if (handle->detail) {
511 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700512 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700513 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700514 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700515 }
516
517 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700518 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700519 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800520#ifdef CAPSTONE_DIET
521 // zero out mnemonic & op_str
522 mci.flat_insn->mnemonic[0] = '\0';
523 mci.flat_insn->op_str[0] = '\0';
524#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700525
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800526 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800527 if (r) {
528 SStream ss;
529 SStream_Init(&ss);
530
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700531 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800532
533 // map internal instruction opcode to public insn ID
534 handle->insn_id(handle, insn_cache, mci.Opcode);
535
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800536 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800537
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700538 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800539
danghvu0d1aad12014-10-01 23:12:18 -0500540 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800541 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800542 // encounter a broken instruction
543
544 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800545 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800546 cs_mem_free(insn_cache->detail);
547 }
548
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800549 // if there is no request to skip data, or remaining data is too small,
550 // then bail out
551 if (!handle->skipdata || handle->skipdata_size > size)
552 break;
553
554 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800555 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800556 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800557 if (skipdata_bytes > size)
558 // remaining data is not enough
559 break;
560
561 if (!skipdata_bytes)
562 // user requested not to skip data, so bail out
563 break;
564 } else
565 skipdata_bytes = handle->skipdata_size;
566
567 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700568 insn_cache->id = 0; // invalid ID for this "data" instruction
569 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800570 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700571 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
572 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
573 sizeof(insn_cache->mnemonic) - 1);
574 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
575 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800576
danghvu0d1aad12014-10-01 23:12:18 -0500577 next_offset = skipdata_bytes;
578 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800579
danghvu0d1aad12014-10-01 23:12:18 -0500580 // one more instruction entering the cache
581 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800582
danghvu0d1aad12014-10-01 23:12:18 -0500583 // one more instruction disassembled
584 c++;
585 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800586 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500587 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800588
danghvu0d1aad12014-10-01 23:12:18 -0500589 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800590 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500591 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500592 total_size += (sizeof(cs_insn) * cache_size);
593 tmp = cs_mem_realloc(total, total_size);
594 if (tmp == NULL) { // insufficient memory
595 if (handle->detail) {
596 insn_cache = (cs_insn *)total;
597 for (i = 0; i < c; i++, insn_cache++)
598 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800599 }
600
danghvu0d1aad12014-10-01 23:12:18 -0500601 cs_mem_free(total);
602 *insn = NULL;
603 handle->errnum = CS_ERR_MEM;
604 return 0;
605 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800606
danghvu0d1aad12014-10-01 23:12:18 -0500607 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800608 // continue to fill in the cache after the last instruction
609 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800610
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800611 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500612 f = 0;
613 } else
614 insn_cache++;
615
616 buffer += next_offset;
617 size -= next_offset;
618 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800619 }
620
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800621 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800622 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800623 cs_mem_free(total);
624 total = NULL;
625 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800626 // total did not fully use the last cache, so downsize it
Tyler J. Stachecki30d11672015-10-23 20:59:20 -0400627 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800628 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800629 // free all detail pointers
630 if (handle->detail) {
631 insn_cache = (cs_insn *)total;
632 for (i = 0; i < c; i++, insn_cache++)
633 cs_mem_free(insn_cache->detail);
634 }
635
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800636 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800637 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800638
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800639 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800640 return 0;
641 }
642
643 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800644 }
645
646 *insn = total;
647
648 return c;
649}
650
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800651CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800652CAPSTONE_DEPRECATED
tandasat760940f2016-03-31 18:14:43 -0700653size_t CAPSTONE_API cs_disasm_ex(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800654{
655 return cs_disasm(ud, buffer, size, offset, count, insn);
656}
657
658CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700659void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800660{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800661 size_t i;
662
663 // free all detail pointers
664 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800665 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800666
667 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800668 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800669}
670
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800671CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700672cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800673{
674 cs_insn *insn;
675 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
676
677 insn = cs_mem_malloc(sizeof(cs_insn));
678 if (!insn) {
679 // insufficient memory
680 handle->errnum = CS_ERR_MEM;
681 return NULL;
682 } else {
683 if (handle->detail) {
684 // allocate memory for @detail pointer
685 insn->detail = cs_mem_malloc(sizeof(cs_detail));
686 if (insn->detail == NULL) { // insufficient memory
687 cs_mem_free(insn);
688 handle->errnum = CS_ERR_MEM;
689 return NULL;
690 }
691 } else
692 insn->detail = NULL;
693 }
694
695 return insn;
696}
697
hlide993f3622014-10-05 18:14:40 +0200698// iterator for instruction "single-stepping"
699CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700700bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800701 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200702{
703 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200704 uint16_t insn_size;
705 MCInst mci;
706 bool r;
707
708 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800709 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800710 return false;
hlide993f3622014-10-05 18:14:40 +0200711 }
712
713 handle->errnum = CS_ERR_OK;
714
hlide993f3622014-10-05 18:14:40 +0200715 MCInst_Init(&mci);
716 mci.csh = handle;
717
718 // relative branches need to know the address & size of current insn
719 mci.address = *address;
720
721 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800722 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200723 mci.flat_insn->address = *address;
724#ifdef CAPSTONE_DIET
725 // zero out mnemonic & op_str
726 mci.flat_insn->mnemonic[0] = '\0';
727 mci.flat_insn->op_str[0] = '\0';
728#endif
729
730 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800731 if (r) {
hlide993f3622014-10-05 18:14:40 +0200732 SStream ss;
733 SStream_Init(&ss);
734
735 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800736
737 // map internal instruction opcode to public insn ID
738 handle->insn_id(handle, insn, mci.Opcode);
739
hlide993f3622014-10-05 18:14:40 +0200740 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800741
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800742 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800743
hlide993f3622014-10-05 18:14:40 +0200744 *code += insn_size;
745 *size -= insn_size;
746 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800747 } else { // encounter a broken instruction
748 size_t skipdata_bytes;
749
750 // if there is no request to skip data, or remaining data is too small,
751 // then bail out
752 if (!handle->skipdata || handle->skipdata_size > *size)
753 return false;
754
755 if (handle->skipdata_setup.callback) {
756 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
757 0, handle->skipdata_setup.user_data);
758 if (skipdata_bytes > *size)
759 // remaining data is not enough
760 return false;
761
762 if (!skipdata_bytes)
763 // user requested not to skip data, so bail out
764 return false;
765 } else
766 skipdata_bytes = handle->skipdata_size;
767
768 // we have to skip some amount of data, depending on arch & mode
769 insn->id = 0; // invalid ID for this "data" instruction
770 insn->address = *address;
771 insn->size = (uint16_t)skipdata_bytes;
772 memcpy(insn->bytes, *code, skipdata_bytes);
773 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
774 sizeof(insn->mnemonic) - 1);
775 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
776
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800777 *code += skipdata_bytes;
778 *size -= skipdata_bytes;
779 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200780 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800781
782 return true;
hlide993f3622014-10-05 18:14:40 +0200783}
784
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800785// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800786CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700787const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800788{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800789 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800790
791 if (!handle || handle->reg_name == NULL) {
792 return NULL;
793 }
794
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800795 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800796}
797
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800798CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700799const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800800{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800801 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800802
803 if (!handle || handle->insn_name == NULL) {
804 return NULL;
805 }
806
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800807 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800808}
809
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800810CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700811const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800812{
813 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
814
815 if (!handle || handle->group_name == NULL) {
816 return NULL;
817 }
818
819 return handle->group_name(ud, group);
820}
821
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800822static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800823{
824 int i;
825
826 for (i = 0; i < max; i++) {
827 if (arr[i] == id)
828 return true;
829 }
830
831 return false;
832}
833
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800834CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700835bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800836{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800837 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800838 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800839 return false;
840
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100841 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200842
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800843 if (!handle->detail) {
844 handle->errnum = CS_ERR_DETAIL;
845 return false;
846 }
847
Giovanni Condello95657e02014-05-07 17:31:27 +0200848 if(!insn->id) {
849 handle->errnum = CS_ERR_SKIPDATA;
850 return false;
851 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200852
Giovanni Condello95657e02014-05-07 17:31:27 +0200853 if(!insn->detail) {
854 handle->errnum = CS_ERR_DETAIL;
855 return false;
856 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200857
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800858 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800859}
860
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800861CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700862bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800863{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800864 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800865 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800866 return false;
867
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100868 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200869
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800870 if (!handle->detail) {
871 handle->errnum = CS_ERR_DETAIL;
872 return false;
873 }
874
Giovanni Condello95657e02014-05-07 17:31:27 +0200875 if(!insn->id) {
876 handle->errnum = CS_ERR_SKIPDATA;
877 return false;
878 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200879
Giovanni Condello95657e02014-05-07 17:31:27 +0200880 if(!insn->detail) {
881 handle->errnum = CS_ERR_DETAIL;
882 return false;
883 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200884
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800885 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800886}
887
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800888CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700889bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800890{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800891 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800892 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800893 return false;
894
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100895 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200896
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800897 if (!handle->detail) {
898 handle->errnum = CS_ERR_DETAIL;
899 return false;
900 }
901
Giovanni Condello95657e02014-05-07 17:31:27 +0200902 if(!insn->id) {
903 handle->errnum = CS_ERR_SKIPDATA;
904 return false;
905 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200906
Giovanni Condello95657e02014-05-07 17:31:27 +0200907 if(!insn->detail) {
908 handle->errnum = CS_ERR_DETAIL;
909 return false;
910 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200911
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800912 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800913}
914
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800915CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700916int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800917{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800918 struct cs_struct *handle;
919 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800920 if (!ud)
921 return -1;
922
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100923 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200924
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800925 if (!handle->detail) {
926 handle->errnum = CS_ERR_DETAIL;
927 return -1;
928 }
929
Giovanni Condello95657e02014-05-07 17:31:27 +0200930 if(!insn->id) {
931 handle->errnum = CS_ERR_SKIPDATA;
932 return -1;
933 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200934
Giovanni Condello95657e02014-05-07 17:31:27 +0200935 if(!insn->detail) {
936 handle->errnum = CS_ERR_DETAIL;
937 return -1;
938 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200939
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800940 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800941
942 switch (handle->arch) {
943 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800944 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800945 return -1;
946 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800947 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800948 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800949 count++;
950 break;
951 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800952 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800953 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800954 count++;
955 break;
956 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800957 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800958 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800959 count++;
960 break;
961 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800962 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800963 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800964 count++;
965 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800966 case CS_ARCH_PPC:
967 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800968 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800969 count++;
970 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800971 case CS_ARCH_SPARC:
972 for (i = 0; i < insn->detail->sparc.op_count; i++)
973 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
974 count++;
975 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800976 case CS_ARCH_SYSZ:
977 for (i = 0; i < insn->detail->sysz.op_count; i++)
978 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
979 count++;
980 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800981 case CS_ARCH_XCORE:
982 for (i = 0; i < insn->detail->xcore.op_count; i++)
983 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
984 count++;
985 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800986 }
987
988 return count;
989}
990
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800991CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700992int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800993 unsigned int post)
994{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800995 struct cs_struct *handle;
996 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800997 if (!ud)
998 return -1;
999
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001000 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001001
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001002 if (!handle->detail) {
1003 handle->errnum = CS_ERR_DETAIL;
1004 return -1;
1005 }
1006
Giovanni Condello95657e02014-05-07 17:31:27 +02001007 if(!insn->id) {
1008 handle->errnum = CS_ERR_SKIPDATA;
1009 return -1;
1010 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001011
Giovanni Condello95657e02014-05-07 17:31:27 +02001012 if(!insn->detail) {
1013 handle->errnum = CS_ERR_DETAIL;
1014 return -1;
1015 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001016
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001017 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001018
1019 switch (handle->arch) {
1020 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001021 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001022 return -1;
1023 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001024 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001025 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001026 count++;
1027 if (count == post)
1028 return i;
1029 }
1030 break;
1031 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001032 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001033 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001034 count++;
1035 if (count == post)
1036 return i;
1037 }
1038 break;
1039 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001040 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001041 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001042 count++;
1043 if (count == post)
1044 return i;
1045 }
1046 break;
1047 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001048 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001049 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001050 count++;
1051 if (count == post)
1052 return i;
1053 }
1054 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001055 case CS_ARCH_PPC:
1056 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001057 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001058 count++;
1059 if (count == post)
1060 return i;
1061 }
1062 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001063 case CS_ARCH_SPARC:
1064 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1065 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1066 count++;
1067 if (count == post)
1068 return i;
1069 }
1070 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001071 case CS_ARCH_SYSZ:
1072 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1073 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1074 count++;
1075 if (count == post)
1076 return i;
1077 }
1078 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001079 case CS_ARCH_XCORE:
1080 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1081 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1082 count++;
1083 if (count == post)
1084 return i;
1085 }
1086 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001087 }
1088
1089 return -1;
1090}