blob: 4549e2813f14a04b7528c08c00527503828981fd [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
tandasat2729f3b2016-05-16 08:32:58 -070024// Issue #681: Windows kernel does not support formatting float point
25#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
26#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64)
27#define CAPSTONE_STR_INTERNAL(x) #x
28#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
29#define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
30
31#pragma message(CAPSTONE_MSVC_WRANING_PREFIX "Windows driver does not support full features for selected architecture(s). Define CAPSTONE_DIET to compile Capstone with only supported features. See issue #681 for details.")
32
33#undef CAPSTONE_MSVC_WRANING_PREFIX
34#undef CAPSTONE_STR
35#undef CAPSTONE_STR_INTERNAL
36#endif
37#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
38
tandasat1dd89872016-04-23 15:51:24 -070039#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080040#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080041#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080042// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080043#define INSN_CACHE_SIZE 8
44#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080045
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080046// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080047#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080048
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080049cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080050cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
51void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080052
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080053extern void ARM_enable(void);
54extern void AArch64_enable(void);
55extern void Mips_enable(void);
56extern void X86_enable(void);
57extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080058extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080059extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080060extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070061
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080062static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080063{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080064 static bool initialized = false;
65
66 if (initialized)
67 return;
68
danghvub33bd2c2014-01-09 12:22:56 +070069#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080070 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070071#endif
72#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080073 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070074#endif
75#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080076 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070077#endif
danghvub33bd2c2014-01-09 12:22:56 +070078#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080079 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070080#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080081#ifdef CAPSTONE_HAS_SPARC
82 Sparc_enable();
83#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080084#ifdef CAPSTONE_HAS_SYSZ
85 SystemZ_enable();
86#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080087#ifdef CAPSTONE_HAS_X86
88 X86_enable();
89#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080090#ifdef CAPSTONE_HAS_XCORE
91 XCore_enable();
92#endif
93
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080094
95 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070096}
97
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080098unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080099
tandasat1dd89872016-04-23 15:51:24 -0700100#if defined(CAPSTONE_USE_SYS_DYN_MEM)
101#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800102cs_malloc_t cs_mem_malloc = malloc;
103cs_calloc_t cs_mem_calloc = calloc;
104cs_realloc_t cs_mem_realloc = realloc;
105cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800106cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasat1dd89872016-04-23 15:51:24 -0700107#elif defined(_KERNEL_MODE)
108cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
109cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
110cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
111cs_free_t cs_mem_free = cs_winkernel_free;
112cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800113#else
Pbb8741bd2015-11-10 23:02:26 +0100114extern void* kern_os_malloc(size_t size);
115extern void kern_os_free(void* addr);
116extern void* kern_os_realloc(void* addr, size_t nsize);
117
118static void* cs_kern_os_calloc(size_t num, size_t size)
119{
120 return kern_os_malloc(num * size); // malloc bzeroes the buffer
121}
122
123cs_malloc_t cs_mem_malloc = kern_os_malloc;
124cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
125cs_realloc_t cs_mem_realloc = kern_os_realloc;
126cs_free_t cs_mem_free = kern_os_free;
127cs_vsnprintf_t cs_vsnprintf = vsnprintf;
128#endif
129#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800130cs_malloc_t cs_mem_malloc = NULL;
131cs_calloc_t cs_mem_calloc = NULL;
132cs_realloc_t cs_mem_realloc = NULL;
133cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800134cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800135#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800136
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800137CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700138unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800139{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800140 archs_enable();
141
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800142 if (major != NULL && minor != NULL) {
143 *major = CS_API_MAJOR;
144 *minor = CS_API_MINOR;
145 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800146
147 return (CS_API_MAJOR << 8) + CS_API_MINOR;
148}
149
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800150CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700151bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800152{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800153 archs_enable();
154
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800155 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800156 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800157 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800158 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800159 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800160
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800161 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800162 return all_arch & (1 << query);
163
164 if (query == CS_SUPPORT_DIET) {
165#ifdef CAPSTONE_DIET
166 return true;
167#else
168 return false;
169#endif
170 }
171
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800172 if (query == CS_SUPPORT_X86_REDUCE) {
173#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800174 return true;
175#else
176 return false;
177#endif
178 }
179
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800180 // unsupported query
181 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800182}
183
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800184CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700185cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800186{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800187 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800188 if (!handle)
189 return CS_ERR_CSH;
190
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100191 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800192
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800193 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800194}
195
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800196CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700197const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800198{
199 switch(code) {
200 default:
201 return "Unknown error code";
202 case CS_ERR_OK:
203 return "OK (CS_ERR_OK)";
204 case CS_ERR_MEM:
205 return "Out of memory (CS_ERR_MEM)";
206 case CS_ERR_ARCH:
207 return "Invalid architecture (CS_ERR_ARCH)";
208 case CS_ERR_HANDLE:
209 return "Invalid handle (CS_ERR_HANDLE)";
210 case CS_ERR_CSH:
211 return "Invalid csh (CS_ERR_CSH)";
212 case CS_ERR_MODE:
213 return "Invalid mode (CS_ERR_MODE)";
214 case CS_ERR_OPTION:
215 return "Invalid option (CS_ERR_OPTION)";
216 case CS_ERR_DETAIL:
217 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800218 case CS_ERR_MEMSETUP:
219 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800220 case CS_ERR_VERSION:
221 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800222 case CS_ERR_DIET:
223 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800224 case CS_ERR_SKIPDATA:
225 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800226 }
227}
228
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800229CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700230cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800231{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800232 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800233 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800234 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800235 // Error: before cs_open(), dynamic memory management must be initialized
236 // with cs_option(CS_OPT_MEM)
237 return CS_ERR_MEMSETUP;
238
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800239 archs_enable();
240
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800241 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800242 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800243 if (!ud) {
244 // memory insufficient
245 return CS_ERR_MEM;
246 }
247
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800248 ud->errnum = CS_ERR_OK;
249 ud->arch = arch;
250 ud->mode = mode;
tandasatb3d38cf2016-05-06 17:03:57 -0700251 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800252 // by default, do not break instruction into details
253 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800254
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800255 // default skipdata setup
256 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
257
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100258 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800259 if (err) {
260 cs_mem_free(ud);
261 *handle = 0;
262 return err;
263 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800264
265 *handle = (uintptr_t)ud;
266
267 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800268 } else {
269 *handle = 0;
270 return CS_ERR_ARCH;
271 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800272}
273
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800274CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700275cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800276{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800277 struct cs_struct *ud;
278
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800279 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800280 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800281 return CS_ERR_CSH;
282
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100283 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800284
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800285 if (ud->printer_info)
286 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800287
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800288 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800289
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800290 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800291 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800292
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800293 // invalidate this handle by ZERO out its value.
294 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800295 *handle = 0;
296
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800297 return CS_ERR_OK;
298}
299
300// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800301static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800302 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800303{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800304#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700305 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800306#endif
tandasata6609fe2016-05-04 05:54:28 -0700307 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800308
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800309 // fill the instruction bytes.
310 // we might skip some redundant bytes in front in the case of X86
311 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
312 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800313
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800314 // alias instruction might have ID saved in OpcodePub
315 if (MCInst_getOpcodePub(mci))
316 insn->id = MCInst_getOpcodePub(mci);
317
318 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800319 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800320 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800321
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800322#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800323 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800324 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100325 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800326 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800327 for (sp = buffer; *sp; sp++) {
328 if (*sp == ' '|| *sp == '\t')
329 break;
330 if (*sp == '|') // lock|rep prefix for x86
331 *sp = ' ';
332 // copy to @mnemonic
333 *mnem = *sp;
334 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800335 }
336
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800337 *mnem = '\0';
338
339 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800340 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800341 // find the next non-space char
342 sp++;
343 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
344 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800345 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
346 } else
347 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800348#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800349}
350
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800351// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800352// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800353static uint8_t skipdata_size(cs_struct *handle)
354{
355 switch(handle->arch) {
356 default:
357 // should never reach
tandasat59df5502016-05-14 16:04:28 -0700358 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800359 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800360 // skip 2 bytes on Thumb mode.
361 if (handle->mode & CS_MODE_THUMB)
362 return 2;
363 // otherwise, skip 4 bytes
364 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800365 case CS_ARCH_ARM64:
366 case CS_ARCH_MIPS:
367 case CS_ARCH_PPC:
368 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800369 // skip 4 bytes
370 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800371 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800372 // SystemZ instruction's length can be 2, 4 or 6 bytes,
373 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800374 return 2;
375 case CS_ARCH_X86:
376 // X86 has no restriction on instruction alignment
377 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800378 case CS_ARCH_XCORE:
379 // XCore instruction's length can be 2 or 4 bytes,
380 // so we just skip 2 bytes
381 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800382 }
383}
384
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800385CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700386cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800387{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800388 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800389 archs_enable();
390
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800391 // cs_option() can be called with NULL handle just for CS_OPT_MEM
392 // This is supposed to be executed before all other APIs (even cs_open())
393 if (type == CS_OPT_MEM) {
394 cs_opt_mem *mem = (cs_opt_mem *)value;
395
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800396 cs_mem_malloc = mem->malloc;
397 cs_mem_calloc = mem->calloc;
398 cs_mem_realloc = mem->realloc;
399 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800400 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800401
402 return CS_ERR_OK;
403 }
404
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100405 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600406 if (!handle)
407 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800408
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800409 switch(type) {
410 default:
411 break;
412 case CS_OPT_DETAIL:
Félix Cloutierc141af92015-03-02 22:06:56 -0500413 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800414 return CS_ERR_OK;
415 case CS_OPT_SKIPDATA:
416 handle->skipdata = (value == CS_OPT_ON);
417 if (handle->skipdata) {
418 if (handle->skipdata_size == 0) {
419 // set the default skipdata size
420 handle->skipdata_size = skipdata_size(handle);
421 }
422 }
423 return CS_ERR_OK;
424 case CS_OPT_SKIPDATA_SETUP:
425 if (value)
426 handle->skipdata_setup = *((cs_opt_skipdata *)value);
427 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800428 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800429
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800430 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800431}
432
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800433// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800434static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
435{
436 char *p = opstr;
437 int len;
438 size_t i;
tandasat760940f2016-03-31 18:14:43 -0700439 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800440
441 if (!size) {
442 opstr[0] = '\0';
443 return;
444 }
445
tandasat760940f2016-03-31 18:14:43 -0700446 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800447 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700448 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800449
450 for(i = 1; i < size; i++) {
tandasat760940f2016-03-31 18:14:43 -0700451 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
452 if (len < 0) {
453 break;
454 }
455 if ((size_t)len > available - 1) {
456 break;
457 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800458 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700459 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800460 }
461}
462
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800463// dynamicly allocate memory to contain disasm insn
464// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800465CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700466size_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 +0800467{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800468 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800469 MCInst mci;
470 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800471 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800472 unsigned int f = 0; // index of the next instruction in the cache
473 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800474 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800475 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800476 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800477 void *tmp;
478 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800479 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800480 size_t size_org;
481 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800482 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500483 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800484
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800485 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800486 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800487 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800488 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800489 return 0;
490 }
491
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800492 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800493
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800494 // reset IT block of ARM structure
Nguyen Anh Quynh0c864402016-04-27 14:47:05 +0800495 if (handle->arch == CS_ARCH_ARM)
496 handle->ITBlock.size = 0;
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800497
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800498#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800499 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400500 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800501#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800502
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800503 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800504 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800505 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800506 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800507
508 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700509 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800510 if (total == NULL) {
511 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500512 handle->errnum = CS_ERR_MEM;
513 return 0;
514 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800515
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700516 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800517
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800518 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800519 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800520 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800521
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700522 // relative branches need to know the address & size of current insn
523 mci.address = offset;
524
525 if (handle->detail) {
526 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700527 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700528 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700529 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700530 }
531
532 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700533 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700534 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800535#ifdef CAPSTONE_DIET
536 // zero out mnemonic & op_str
537 mci.flat_insn->mnemonic[0] = '\0';
538 mci.flat_insn->op_str[0] = '\0';
539#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700540
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800541 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800542 if (r) {
543 SStream ss;
544 SStream_Init(&ss);
545
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700546 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800547
548 // map internal instruction opcode to public insn ID
549 handle->insn_id(handle, insn_cache, mci.Opcode);
550
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800551 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800552
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700553 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800554
danghvu0d1aad12014-10-01 23:12:18 -0500555 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800556 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800557 // encounter a broken instruction
558
559 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800560 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800561 cs_mem_free(insn_cache->detail);
562 }
563
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800564 // if there is no request to skip data, or remaining data is too small,
565 // then bail out
566 if (!handle->skipdata || handle->skipdata_size > size)
567 break;
568
569 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800570 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800571 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800572 if (skipdata_bytes > size)
573 // remaining data is not enough
574 break;
575
576 if (!skipdata_bytes)
577 // user requested not to skip data, so bail out
578 break;
579 } else
580 skipdata_bytes = handle->skipdata_size;
581
582 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700583 insn_cache->id = 0; // invalid ID for this "data" instruction
584 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800585 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700586 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
587 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
588 sizeof(insn_cache->mnemonic) - 1);
589 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
590 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800591
danghvu0d1aad12014-10-01 23:12:18 -0500592 next_offset = skipdata_bytes;
593 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800594
danghvu0d1aad12014-10-01 23:12:18 -0500595 // one more instruction entering the cache
596 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800597
danghvu0d1aad12014-10-01 23:12:18 -0500598 // one more instruction disassembled
599 c++;
600 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800601 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500602 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800603
danghvu0d1aad12014-10-01 23:12:18 -0500604 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800605 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500606 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500607 total_size += (sizeof(cs_insn) * cache_size);
608 tmp = cs_mem_realloc(total, total_size);
609 if (tmp == NULL) { // insufficient memory
610 if (handle->detail) {
611 insn_cache = (cs_insn *)total;
612 for (i = 0; i < c; i++, insn_cache++)
613 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800614 }
615
danghvu0d1aad12014-10-01 23:12:18 -0500616 cs_mem_free(total);
617 *insn = NULL;
618 handle->errnum = CS_ERR_MEM;
619 return 0;
620 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800621
danghvu0d1aad12014-10-01 23:12:18 -0500622 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800623 // continue to fill in the cache after the last instruction
624 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800625
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800626 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500627 f = 0;
628 } else
629 insn_cache++;
630
631 buffer += next_offset;
632 size -= next_offset;
633 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800634 }
635
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800636 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800637 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800638 cs_mem_free(total);
639 total = NULL;
640 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800641 // total did not fully use the last cache, so downsize it
Tyler J. Stachecki30d11672015-10-23 20:59:20 -0400642 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800643 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800644 // free all detail pointers
645 if (handle->detail) {
646 insn_cache = (cs_insn *)total;
647 for (i = 0; i < c; i++, insn_cache++)
648 cs_mem_free(insn_cache->detail);
649 }
650
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800651 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800652 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800653
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800654 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800655 return 0;
656 }
657
658 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800659 }
660
661 *insn = total;
662
663 return c;
664}
665
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800666CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800667CAPSTONE_DEPRECATED
tandasat760940f2016-03-31 18:14:43 -0700668size_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 +0800669{
670 return cs_disasm(ud, buffer, size, offset, count, insn);
671}
672
673CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700674void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800675{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800676 size_t i;
677
678 // free all detail pointers
679 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800680 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800681
682 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800683 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800684}
685
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800686CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700687cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800688{
689 cs_insn *insn;
690 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
691
692 insn = cs_mem_malloc(sizeof(cs_insn));
693 if (!insn) {
694 // insufficient memory
695 handle->errnum = CS_ERR_MEM;
696 return NULL;
697 } else {
698 if (handle->detail) {
699 // allocate memory for @detail pointer
700 insn->detail = cs_mem_malloc(sizeof(cs_detail));
701 if (insn->detail == NULL) { // insufficient memory
702 cs_mem_free(insn);
703 handle->errnum = CS_ERR_MEM;
704 return NULL;
705 }
706 } else
707 insn->detail = NULL;
708 }
709
710 return insn;
711}
712
hlide993f3622014-10-05 18:14:40 +0200713// iterator for instruction "single-stepping"
714CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700715bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800716 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200717{
718 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200719 uint16_t insn_size;
720 MCInst mci;
721 bool r;
722
723 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800724 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800725 return false;
hlide993f3622014-10-05 18:14:40 +0200726 }
727
728 handle->errnum = CS_ERR_OK;
729
hlide993f3622014-10-05 18:14:40 +0200730 MCInst_Init(&mci);
731 mci.csh = handle;
732
733 // relative branches need to know the address & size of current insn
734 mci.address = *address;
735
736 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800737 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200738 mci.flat_insn->address = *address;
739#ifdef CAPSTONE_DIET
740 // zero out mnemonic & op_str
741 mci.flat_insn->mnemonic[0] = '\0';
742 mci.flat_insn->op_str[0] = '\0';
743#endif
744
745 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800746 if (r) {
hlide993f3622014-10-05 18:14:40 +0200747 SStream ss;
748 SStream_Init(&ss);
749
750 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800751
752 // map internal instruction opcode to public insn ID
753 handle->insn_id(handle, insn, mci.Opcode);
754
hlide993f3622014-10-05 18:14:40 +0200755 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800756
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800757 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800758
hlide993f3622014-10-05 18:14:40 +0200759 *code += insn_size;
760 *size -= insn_size;
761 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800762 } else { // encounter a broken instruction
763 size_t skipdata_bytes;
764
765 // if there is no request to skip data, or remaining data is too small,
766 // then bail out
767 if (!handle->skipdata || handle->skipdata_size > *size)
768 return false;
769
770 if (handle->skipdata_setup.callback) {
771 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
772 0, handle->skipdata_setup.user_data);
773 if (skipdata_bytes > *size)
774 // remaining data is not enough
775 return false;
776
777 if (!skipdata_bytes)
778 // user requested not to skip data, so bail out
779 return false;
780 } else
781 skipdata_bytes = handle->skipdata_size;
782
783 // we have to skip some amount of data, depending on arch & mode
784 insn->id = 0; // invalid ID for this "data" instruction
785 insn->address = *address;
786 insn->size = (uint16_t)skipdata_bytes;
787 memcpy(insn->bytes, *code, skipdata_bytes);
788 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
789 sizeof(insn->mnemonic) - 1);
790 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
791
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800792 *code += skipdata_bytes;
793 *size -= skipdata_bytes;
794 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200795 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800796
797 return true;
hlide993f3622014-10-05 18:14:40 +0200798}
799
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800800// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800801CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700802const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800803{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800804 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800805
806 if (!handle || handle->reg_name == NULL) {
807 return NULL;
808 }
809
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800810 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800811}
812
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800813CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700814const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800815{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800816 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800817
818 if (!handle || handle->insn_name == NULL) {
819 return NULL;
820 }
821
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800822 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800823}
824
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800825CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700826const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800827{
828 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
829
830 if (!handle || handle->group_name == NULL) {
831 return NULL;
832 }
833
834 return handle->group_name(ud, group);
835}
836
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800837static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800838{
839 int i;
840
841 for (i = 0; i < max; i++) {
842 if (arr[i] == id)
843 return true;
844 }
845
846 return false;
847}
848
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800849CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700850bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800851{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800852 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800853 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800854 return false;
855
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100856 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200857
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800858 if (!handle->detail) {
859 handle->errnum = CS_ERR_DETAIL;
860 return false;
861 }
862
Giovanni Condello95657e02014-05-07 17:31:27 +0200863 if(!insn->id) {
864 handle->errnum = CS_ERR_SKIPDATA;
865 return false;
866 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200867
Giovanni Condello95657e02014-05-07 17:31:27 +0200868 if(!insn->detail) {
869 handle->errnum = CS_ERR_DETAIL;
870 return false;
871 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200872
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800873 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800874}
875
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800876CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700877bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800878{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800879 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800880 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800881 return false;
882
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100883 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200884
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800885 if (!handle->detail) {
886 handle->errnum = CS_ERR_DETAIL;
887 return false;
888 }
889
Giovanni Condello95657e02014-05-07 17:31:27 +0200890 if(!insn->id) {
891 handle->errnum = CS_ERR_SKIPDATA;
892 return false;
893 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200894
Giovanni Condello95657e02014-05-07 17:31:27 +0200895 if(!insn->detail) {
896 handle->errnum = CS_ERR_DETAIL;
897 return false;
898 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200899
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800900 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800901}
902
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800903CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700904bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800905{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800906 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800907 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800908 return false;
909
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100910 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200911
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800912 if (!handle->detail) {
913 handle->errnum = CS_ERR_DETAIL;
914 return false;
915 }
916
Giovanni Condello95657e02014-05-07 17:31:27 +0200917 if(!insn->id) {
918 handle->errnum = CS_ERR_SKIPDATA;
919 return false;
920 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200921
Giovanni Condello95657e02014-05-07 17:31:27 +0200922 if(!insn->detail) {
923 handle->errnum = CS_ERR_DETAIL;
924 return false;
925 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200926
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800927 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800928}
929
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800930CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700931int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800932{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800933 struct cs_struct *handle;
934 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800935 if (!ud)
936 return -1;
937
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100938 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200939
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800940 if (!handle->detail) {
941 handle->errnum = CS_ERR_DETAIL;
942 return -1;
943 }
944
Giovanni Condello95657e02014-05-07 17:31:27 +0200945 if(!insn->id) {
946 handle->errnum = CS_ERR_SKIPDATA;
947 return -1;
948 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200949
Giovanni Condello95657e02014-05-07 17:31:27 +0200950 if(!insn->detail) {
951 handle->errnum = CS_ERR_DETAIL;
952 return -1;
953 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200954
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800955 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800956
957 switch (handle->arch) {
958 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800959 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800960 return -1;
961 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800962 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800963 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800964 count++;
965 break;
966 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800967 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800968 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800969 count++;
970 break;
971 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800972 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800973 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800974 count++;
975 break;
976 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800977 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800978 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800979 count++;
980 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800981 case CS_ARCH_PPC:
982 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800983 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800984 count++;
985 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800986 case CS_ARCH_SPARC:
987 for (i = 0; i < insn->detail->sparc.op_count; i++)
988 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
989 count++;
990 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800991 case CS_ARCH_SYSZ:
992 for (i = 0; i < insn->detail->sysz.op_count; i++)
993 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
994 count++;
995 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800996 case CS_ARCH_XCORE:
997 for (i = 0; i < insn->detail->xcore.op_count; i++)
998 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
999 count++;
1000 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001001 }
1002
1003 return count;
1004}
1005
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001006CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001007int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001008 unsigned int post)
1009{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001010 struct cs_struct *handle;
1011 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001012 if (!ud)
1013 return -1;
1014
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001015 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001016
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001017 if (!handle->detail) {
1018 handle->errnum = CS_ERR_DETAIL;
1019 return -1;
1020 }
1021
Giovanni Condello95657e02014-05-07 17:31:27 +02001022 if(!insn->id) {
1023 handle->errnum = CS_ERR_SKIPDATA;
1024 return -1;
1025 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001026
Giovanni Condello95657e02014-05-07 17:31:27 +02001027 if(!insn->detail) {
1028 handle->errnum = CS_ERR_DETAIL;
1029 return -1;
1030 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001031
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001032 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001033
1034 switch (handle->arch) {
1035 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001036 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001037 return -1;
1038 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001039 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001040 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001041 count++;
1042 if (count == post)
1043 return i;
1044 }
1045 break;
1046 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001047 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001048 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001049 count++;
1050 if (count == post)
1051 return i;
1052 }
1053 break;
1054 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001055 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001056 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001057 count++;
1058 if (count == post)
1059 return i;
1060 }
1061 break;
1062 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001063 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001064 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001065 count++;
1066 if (count == post)
1067 return i;
1068 }
1069 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001070 case CS_ARCH_PPC:
1071 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001072 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001073 count++;
1074 if (count == post)
1075 return i;
1076 }
1077 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001078 case CS_ARCH_SPARC:
1079 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1080 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1081 count++;
1082 if (count == post)
1083 return i;
1084 }
1085 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001086 case CS_ARCH_SYSZ:
1087 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1088 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1089 count++;
1090 if (count == post)
1091 return i;
1092 }
1093 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001094 case CS_ARCH_XCORE:
1095 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1096 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1097 count++;
1098 if (count == post)
1099 return i;
1100 }
1101 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001102 }
1103
1104 return -1;
1105}