blob: 5d0c5528ba1f0ba8eeac814392bc4bb0e1b6733a [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)
Satoshi Tandaf177f922016-09-27 08:08:58 -07004#pragma warning(disable:4996) // disable MSVC's warning on strcpy()
5#pragma warning(disable:28719) // disable MSVC's warning on strcpy()
Axel 0vercl0k Souchet605faf12014-05-09 20:40:00 +01006#endif
reverserbcf09f42015-04-09 18:28:19 +01007#if defined(CAPSTONE_HAS_OSXKERNEL)
8#include <libkern/libkern.h>
9#else
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080010#include <stddef.h>
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080011#include <stdio.h>
12#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +010013#endif
14
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080015#include <string.h>
16#include <capstone.h>
17
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018#include "utils.h"
Nguyen Anh Quynhc7404072014-01-05 11:35:47 +080019#include "MCRegisterInfo.h"
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020
tandasat1dd89872016-04-23 15:51:24 -070021#if defined(_KERNEL_MODE)
22#include "windows\winkernel_mm.h"
23#endif
24
tandasat2729f3b2016-05-16 08:32:58 -070025// Issue #681: Windows kernel does not support formatting float point
26#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
27#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64)
28#define CAPSTONE_STR_INTERNAL(x) #x
29#define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
30#define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
31
32#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.")
33
34#undef CAPSTONE_MSVC_WRANING_PREFIX
35#undef CAPSTONE_STR
36#undef CAPSTONE_STR_INTERNAL
37#endif
38#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
39
tandasat1dd89872016-04-23 15:51:24 -070040#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
Nguyen Anh Quynh472a4a42014-03-06 09:13:04 +080041#define INSN_CACHE_SIZE 32
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080042#else
Nguyen Anh Quynh5ee2b452014-03-07 08:40:35 +080043// reduce stack variable size for kernel/firmware
Nguyen Anh Quynha836b752014-03-06 03:36:03 +080044#define INSN_CACHE_SIZE 8
45#endif
Nguyen Anh Quynhc34959b2014-01-22 09:46:42 +080046
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080047// default SKIPDATA mnemonic
Nguyen Anh Quynhc75a9092014-04-10 10:26:49 +080048#define SKIPDATA_MNEM ".byte"
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +080049
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080050cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +080051cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
52void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +080053
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080054extern void ARM_enable(void);
55extern void AArch64_enable(void);
56extern void Mips_enable(void);
57extern void X86_enable(void);
58extern void PPC_enable(void);
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080059extern void Sparc_enable(void);
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080060extern void SystemZ_enable(void);
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080061extern void XCore_enable(void);
danghvu701b8502014-01-09 11:06:44 +070062
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080063static void archs_enable(void)
Nguyen Anh Quynh625b5bc2014-01-09 14:33:56 +080064{
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080065 static bool initialized = false;
66
67 if (initialized)
68 return;
69
danghvub33bd2c2014-01-09 12:22:56 +070070#ifdef CAPSTONE_HAS_ARM
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080071 ARM_enable();
danghvub33bd2c2014-01-09 12:22:56 +070072#endif
73#ifdef CAPSTONE_HAS_ARM64
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080074 AArch64_enable();
danghvub33bd2c2014-01-09 12:22:56 +070075#endif
76#ifdef CAPSTONE_HAS_MIPS
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080077 Mips_enable();
danghvub33bd2c2014-01-09 12:22:56 +070078#endif
danghvub33bd2c2014-01-09 12:22:56 +070079#ifdef CAPSTONE_HAS_POWERPC
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080080 PPC_enable();
danghvub33bd2c2014-01-09 12:22:56 +070081#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080082#ifdef CAPSTONE_HAS_SPARC
83 Sparc_enable();
84#endif
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +080085#ifdef CAPSTONE_HAS_SYSZ
86 SystemZ_enable();
87#endif
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +080088#ifdef CAPSTONE_HAS_X86
89 X86_enable();
90#endif
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +080091#ifdef CAPSTONE_HAS_XCORE
92 XCore_enable();
93#endif
94
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +080095
96 initialized = true;
danghvub33bd2c2014-01-09 12:22:56 +070097}
98
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +080099unsigned int all_arch = 0;
Nguyen Anh Quynhf1851802013-12-21 12:16:47 +0800100
tandasat1dd89872016-04-23 15:51:24 -0700101#if defined(CAPSTONE_USE_SYS_DYN_MEM)
102#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800103cs_malloc_t cs_mem_malloc = malloc;
104cs_calloc_t cs_mem_calloc = calloc;
105cs_realloc_t cs_mem_realloc = realloc;
106cs_free_t cs_mem_free = free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800107cs_vsnprintf_t cs_vsnprintf = vsnprintf;
tandasat1dd89872016-04-23 15:51:24 -0700108#elif defined(_KERNEL_MODE)
109cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
110cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
111cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
112cs_free_t cs_mem_free = cs_winkernel_free;
113cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800114#else
Pbb8741bd2015-11-10 23:02:26 +0100115extern void* kern_os_malloc(size_t size);
116extern void kern_os_free(void* addr);
117extern void* kern_os_realloc(void* addr, size_t nsize);
118
119static void* cs_kern_os_calloc(size_t num, size_t size)
120{
121 return kern_os_malloc(num * size); // malloc bzeroes the buffer
122}
123
124cs_malloc_t cs_mem_malloc = kern_os_malloc;
125cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
126cs_realloc_t cs_mem_realloc = kern_os_realloc;
127cs_free_t cs_mem_free = kern_os_free;
128cs_vsnprintf_t cs_vsnprintf = vsnprintf;
129#endif
130#else
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800131cs_malloc_t cs_mem_malloc = NULL;
132cs_calloc_t cs_mem_calloc = NULL;
133cs_realloc_t cs_mem_realloc = NULL;
134cs_free_t cs_mem_free = NULL;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800135cs_vsnprintf_t cs_vsnprintf = NULL;
Nguyen Anh Quynh59492c22014-01-05 23:41:31 +0800136#endif
Nguyen Anh Quynhb8806782013-12-22 15:20:07 +0800137
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800138CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700139unsigned int CAPSTONE_API cs_version(int *major, int *minor)
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800140{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800141 archs_enable();
142
Nguyen Anh Quynh08777472013-12-22 14:16:28 +0800143 if (major != NULL && minor != NULL) {
144 *major = CS_API_MAJOR;
145 *minor = CS_API_MINOR;
146 }
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800147
148 return (CS_API_MAJOR << 8) + CS_API_MINOR;
149}
150
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800151CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700152bool CAPSTONE_API cs_support(int query)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800153{
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800154 archs_enable();
155
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800156 if (query == CS_ARCH_ALL)
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800157 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800158 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800159 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800160 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE));
Nguyen Anh Quynh39a42ed2013-12-22 10:40:58 +0800161
Nguyen Anh Quynh48d58322014-02-27 13:54:28 +0800162 if ((unsigned int)query < CS_ARCH_MAX)
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800163 return all_arch & (1 << query);
164
165 if (query == CS_SUPPORT_DIET) {
166#ifdef CAPSTONE_DIET
167 return true;
168#else
169 return false;
170#endif
171 }
172
Nguyen Anh Quynh59b54892014-03-27 10:54:44 +0800173 if (query == CS_SUPPORT_X86_REDUCE) {
174#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
Nguyen Anh Quynh95181482014-03-25 23:20:41 +0800175 return true;
176#else
177 return false;
178#endif
179 }
180
Nguyen Anh Quynhb2870e42014-02-22 23:41:16 +0800181 // unsupported query
182 return false;
Nguyen Anh Quynh36df4bb2013-12-10 13:31:20 +0800183}
184
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800185CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700186cs_err CAPSTONE_API cs_errno(csh handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800187{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800188 struct cs_struct *ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800189 if (!handle)
190 return CS_ERR_CSH;
191
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100192 ud = (struct cs_struct *)(uintptr_t)handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800194 return ud->errnum;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800195}
196
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800197CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700198const char * CAPSTONE_API cs_strerror(cs_err code)
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800199{
200 switch(code) {
201 default:
202 return "Unknown error code";
203 case CS_ERR_OK:
204 return "OK (CS_ERR_OK)";
205 case CS_ERR_MEM:
206 return "Out of memory (CS_ERR_MEM)";
207 case CS_ERR_ARCH:
208 return "Invalid architecture (CS_ERR_ARCH)";
209 case CS_ERR_HANDLE:
210 return "Invalid handle (CS_ERR_HANDLE)";
211 case CS_ERR_CSH:
212 return "Invalid csh (CS_ERR_CSH)";
213 case CS_ERR_MODE:
214 return "Invalid mode (CS_ERR_MODE)";
215 case CS_ERR_OPTION:
216 return "Invalid option (CS_ERR_OPTION)";
217 case CS_ERR_DETAIL:
218 return "Details are unavailable (CS_ERR_DETAIL)";
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800219 case CS_ERR_MEMSETUP:
220 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
Nguyen Anh Quynh4f220282014-02-09 00:19:15 +0800221 case CS_ERR_VERSION:
222 return "Different API version between core & binding (CS_ERR_VERSION)";
Nguyen Anh Quynhf7cdbdf2014-02-24 16:47:36 +0800223 case CS_ERR_DIET:
224 return "Information irrelevant in diet engine (CS_ERR_DIET)";
Nguyen Anh Quynh11ec8812014-04-10 17:20:01 +0800225 case CS_ERR_SKIPDATA:
226 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
Nguyen Anh Quynh34f96382014-01-03 22:49:07 +0800227 }
228}
229
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800230CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700231cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800232{
Nguyen Anh Quynh42706a32014-05-09 07:33:35 +0800233 cs_err err;
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800234 struct cs_struct *ud;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800235 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
Nguyen Anh Quynhc52352d2014-01-06 09:06:30 +0800236 // Error: before cs_open(), dynamic memory management must be initialized
237 // with cs_option(CS_OPT_MEM)
238 return CS_ERR_MEMSETUP;
239
Nguyen Anh Quynhc272e9d2014-01-19 12:03:22 +0800240 archs_enable();
241
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800242 if (arch < CS_ARCH_MAX && arch_init[arch]) {
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800243 ud = cs_mem_calloc(1, sizeof(*ud));
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800244 if (!ud) {
245 // memory insufficient
246 return CS_ERR_MEM;
247 }
248
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800249 ud->errnum = CS_ERR_OK;
250 ud->arch = arch;
251 ud->mode = mode;
tandasatb3d38cf2016-05-06 17:03:57 -0700252 ud->big_endian = (mode & CS_MODE_BIG_ENDIAN) != 0;
Nguyen Anh Quynh39b812d2014-01-07 23:36:26 +0800253 // by default, do not break instruction into details
254 ud->detail = CS_OPT_OFF;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800255
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800256 // default skipdata setup
257 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
258
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100259 err = arch_init[ud->arch](ud);
Nguyen Anh Quynh06b3c052014-01-21 15:26:02 +0800260 if (err) {
261 cs_mem_free(ud);
262 *handle = 0;
263 return err;
264 }
Nguyen Anh Quynh8f7ab492014-01-06 09:52:57 +0800265
266 *handle = (uintptr_t)ud;
267
268 return CS_ERR_OK;
Nguyen Anh Quynh9a197b32013-12-22 13:41:38 +0800269 } else {
270 *handle = 0;
271 return CS_ERR_ARCH;
272 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800273}
274
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800275CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700276cs_err CAPSTONE_API cs_close(csh *handle)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800277{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800278 struct cs_struct *ud;
279
Nguyen Anh Quynhef3d04d2014-02-27 23:42:49 +0800280 if (*handle == 0)
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800281 // invalid handle
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800282 return CS_ERR_CSH;
283
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100284 ud = (struct cs_struct *)(*handle);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800285
Nguyen Anh Quynhfbe10a52014-02-27 23:59:08 +0800286 if (ud->printer_info)
287 cs_mem_free(ud->printer_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800288
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800289 cs_mem_free(ud->insn_cache);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800290
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800291 memset(ud, 0, sizeof(*ud));
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800292 cs_mem_free(ud);
Nguyen Anh Quynhee143c82014-01-03 21:51:59 +0800293
Nguyen Anh Quynhc8e07852014-02-28 09:38:11 +0800294 // invalidate this handle by ZERO out its value.
295 // this is to make sure it is unusable after cs_close()
Nguyen Anh Quynh226d7dc2014-02-27 22:20:39 +0800296 *handle = 0;
297
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800298 return CS_ERR_OK;
299}
300
301// fill insn with mnemonic & operands info
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800302static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800303 PostPrinter_t postprinter, const uint8_t *code)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800304{
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800305#ifndef CAPSTONE_DIET
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700306 char *sp, *mnem;
Nguyen Anh Quynh8c2e2db2014-05-14 07:32:56 +0800307#endif
tandasata6609fe2016-05-04 05:54:28 -0700308 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800309
Nguyen Anh Quynh6c0dd632014-10-30 20:34:22 +0800310 // fill the instruction bytes.
311 // we might skip some redundant bytes in front in the case of X86
312 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
313 insn->size = copy_size;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800314
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800315 // alias instruction might have ID saved in OpcodePub
316 if (MCInst_getOpcodePub(mci))
317 insn->id = MCInst_getOpcodePub(mci);
318
319 // post printer handles some corner cases (hacky)
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800320 if (postprinter)
Nguyen Anh Quynh64564812014-05-19 16:46:31 +0800321 postprinter((csh)handle, insn, buffer, mci);
Nguyen Anh Quynh4d3e8522013-12-14 10:45:09 +0800322
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800323#ifndef CAPSTONE_DIET
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800324 // fill in mnemonic & operands
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800325 // find first space or tab
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100326 sp = buffer;
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800327 mnem = insn->mnemonic;
Nguyen Anh Quynh25b7f762014-07-02 12:22:39 +0800328 for (sp = buffer; *sp; sp++) {
329 if (*sp == ' '|| *sp == '\t')
330 break;
331 if (*sp == '|') // lock|rep prefix for x86
332 *sp = ' ';
333 // copy to @mnemonic
334 *mnem = *sp;
335 mnem++;
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800336 }
337
Nguyen Anh Quynh7566a2d2014-06-08 22:09:31 +0800338 *mnem = '\0';
339
340 // copy @op_str
Nguyen Anh Quynhdefb9bc2013-12-12 14:00:12 +0800341 if (*sp) {
Nguyen Anh Quynh86dc3932013-12-12 14:43:39 +0800342 // find the next non-space char
343 sp++;
344 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
345 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800346 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
347 } else
348 insn->op_str[0] = '\0';
Nguyen Anh Quynhfc83a432014-02-22 23:26:27 +0800349#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800350}
351
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800352// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800353// this very much depends on instruction alignment requirement of each arch.
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800354static uint8_t skipdata_size(cs_struct *handle)
355{
356 switch(handle->arch) {
357 default:
358 // should never reach
tandasat59df5502016-05-14 16:04:28 -0700359 return (uint8_t)-1;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800360 case CS_ARCH_ARM:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800361 // skip 2 bytes on Thumb mode.
362 if (handle->mode & CS_MODE_THUMB)
363 return 2;
364 // otherwise, skip 4 bytes
365 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800366 case CS_ARCH_ARM64:
367 case CS_ARCH_MIPS:
368 case CS_ARCH_PPC:
369 case CS_ARCH_SPARC:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800370 // skip 4 bytes
371 return 4;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800372 case CS_ARCH_SYSZ:
Nguyen Anh Quynha89383e2014-04-10 11:53:46 +0800373 // SystemZ instruction's length can be 2, 4 or 6 bytes,
374 // so we just skip 2 bytes
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800375 return 2;
376 case CS_ARCH_X86:
377 // X86 has no restriction on instruction alignment
378 return 1;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800379 case CS_ARCH_XCORE:
380 // XCore instruction's length can be 2 or 4 bytes,
381 // so we just skip 2 bytes
382 return 2;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800383 }
384}
385
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800386CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700387cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800388{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800389 struct cs_struct *handle;
Nguyen Anh Quynhc46c35d2014-03-05 00:21:53 +0800390 archs_enable();
391
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800392 // cs_option() can be called with NULL handle just for CS_OPT_MEM
393 // This is supposed to be executed before all other APIs (even cs_open())
394 if (type == CS_OPT_MEM) {
395 cs_opt_mem *mem = (cs_opt_mem *)value;
396
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800397 cs_mem_malloc = mem->malloc;
398 cs_mem_calloc = mem->calloc;
399 cs_mem_realloc = mem->realloc;
400 cs_mem_free = mem->free;
Nguyen Anh Quynhedeeb042014-01-15 20:44:03 +0800401 cs_vsnprintf = mem->vsnprintf;
Nguyen Anh Quynh24bf0d92014-01-05 11:19:04 +0800402
403 return CS_ERR_OK;
404 }
405
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100406 handle = (struct cs_struct *)(uintptr_t)ud;
danghvu2b192962013-12-19 22:40:28 -0600407 if (!handle)
408 return CS_ERR_CSH;
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800409
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800410 switch(type) {
411 default:
412 break;
413 case CS_OPT_DETAIL:
Félix Cloutierc141af92015-03-02 22:06:56 -0500414 handle->detail = (cs_opt_value)value;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800415 return CS_ERR_OK;
416 case CS_OPT_SKIPDATA:
417 handle->skipdata = (value == CS_OPT_ON);
418 if (handle->skipdata) {
419 if (handle->skipdata_size == 0) {
420 // set the default skipdata size
421 handle->skipdata_size = skipdata_size(handle);
422 }
423 }
424 return CS_ERR_OK;
425 case CS_OPT_SKIPDATA_SETUP:
426 if (value)
427 handle->skipdata_setup = *((cs_opt_skipdata *)value);
428 return CS_ERR_OK;
Nguyen Anh Quynh7d02c922013-12-21 09:59:31 +0800429 }
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800430
Nguyen Anh Quynhd3458392013-12-22 11:10:56 +0800431 return arch_option[handle->arch](handle, type, value);
Nguyen Anh Quynh01aba002013-12-03 21:00:09 +0800432}
433
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800434// generate @op_str for data instruction of SKIPDATA
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800435static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
436{
437 char *p = opstr;
438 int len;
439 size_t i;
tandasat760940f2016-03-31 18:14:43 -0700440 size_t available = sizeof(((cs_insn*)NULL)->op_str);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800441
442 if (!size) {
443 opstr[0] = '\0';
444 return;
445 }
446
tandasat760940f2016-03-31 18:14:43 -0700447 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800448 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700449 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800450
451 for(i = 1; i < size; i++) {
tandasat760940f2016-03-31 18:14:43 -0700452 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
453 if (len < 0) {
454 break;
455 }
456 if ((size_t)len > available - 1) {
457 break;
458 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800459 p+= len;
tandasat760940f2016-03-31 18:14:43 -0700460 available -= len;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800461 }
462}
463
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800464// dynamicly allocate memory to contain disasm insn
465// NOTE: caller must free() the allocated memory itself to avoid memory leaking
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800466CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700467size_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 +0800468{
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800469 struct cs_struct *handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800470 MCInst mci;
471 uint16_t insn_size;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800472 size_t c = 0, i;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800473 unsigned int f = 0; // index of the next instruction in the cache
474 cs_insn *insn_cache; // cache contains disassembled instructions
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800475 void *total = NULL;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800476 size_t total_size = 0; // total size of output buffer containing all insns
Nguyen Anh Quynh034a7482014-03-06 22:40:08 +0800477 bool r;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800478 void *tmp;
479 size_t skipdata_bytes;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800480 uint64_t offset_org; // save all the original info of the buffer
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800481 size_t size_org;
482 const uint8_t *buffer_org;
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800483 unsigned int cache_size = INSN_CACHE_SIZE;
danghvu0d1aad12014-10-01 23:12:18 -0500484 size_t next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800485
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800486 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh5848aaa2014-02-27 11:10:41 +0800487 if (!handle) {
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800488 // FIXME: how to handle this case:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800489 // handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800490 return 0;
491 }
492
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800493 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800494
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800495 // reset IT block of ARM structure
Nguyen Anh Quynh0c864402016-04-27 14:47:05 +0800496 if (handle->arch == CS_ARCH_ARM)
497 handle->ITBlock.size = 0;
Nguyen Anh Quynhc2cb0e72016-04-27 14:43:10 +0800498
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800499#ifdef CAPSTONE_USE_SYS_DYN_MEM
Nguyen Anh Quynh16f330c2014-10-02 10:09:59 +0800500 if (count > 0 && count <= INSN_CACHE_SIZE)
flyingsymbolsd91f9642014-10-22 03:21:43 -0400501 cache_size = (unsigned int) count;
Nguyen Anh Quynh50eeba22014-09-30 13:28:02 +0800502#endif
Nguyen Anh Quynhac98ca02014-09-30 13:17:36 +0800503
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800504 // save the original offset for SKIPDATA
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800505 buffer_org = buffer;
Nguyen Anh Quynh07ffd642014-04-10 14:36:08 +0800506 offset_org = offset;
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800507 size_org = size;
Nguyen Anh Quynh523ca992014-10-01 10:46:37 +0800508
509 total_size = sizeof(cs_insn) * cache_size;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700510 total = cs_mem_malloc(total_size);
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800511 if (total == NULL) {
512 // insufficient memory
Edward Williamsonf1e49752014-12-14 20:45:19 -0500513 handle->errnum = CS_ERR_MEM;
514 return 0;
515 }
Nguyen Anh Quynh611b0c52014-12-15 11:22:46 +0800516
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700517 insn_cache = total;
Nguyen Anh Quynh2cff6f62014-04-28 11:19:44 +0800518
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800519 while (size > 0) {
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +0800520 MCInst_Init(&mci);
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800521 mci.csh = handle;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800522
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700523 // relative branches need to know the address & size of current insn
524 mci.address = offset;
525
526 if (handle->detail) {
527 // allocate memory for @detail pointer
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700528 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700529 } else {
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700530 insn_cache->detail = NULL;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700531 }
532
533 // save all the information for non-detailed mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700534 mci.flat_insn = insn_cache;
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700535 mci.flat_insn->address = offset;
Nguyen Anh Quynh0c07cc92014-08-27 22:31:54 +0800536#ifdef CAPSTONE_DIET
537 // zero out mnemonic & op_str
538 mci.flat_insn->mnemonic[0] = '\0';
539 mci.flat_insn->op_str[0] = '\0';
540#endif
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700541
Nguyen Anh Quynha5ffdc32014-05-07 08:25:24 +0800542 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800543 if (r) {
544 SStream ss;
545 SStream_Init(&ss);
546
Nguyen Anh Quynh5329a6f2014-06-08 23:35:52 +0700547 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800548
549 // map internal instruction opcode to public insn ID
550 handle->insn_id(handle, insn_cache, mci.Opcode);
551
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800552 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800553
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700554 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800555
danghvu0d1aad12014-10-01 23:12:18 -0500556 next_offset = insn_size;
Nguyen Anh Quynh8f13f3c2013-12-04 22:57:04 +0800557 } else {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800558 // encounter a broken instruction
559
560 // free memory of @detail pointer
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800561 if (handle->detail) {
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800562 cs_mem_free(insn_cache->detail);
563 }
564
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800565 // if there is no request to skip data, or remaining data is too small,
566 // then bail out
567 if (!handle->skipdata || handle->skipdata_size > size)
568 break;
569
570 if (handle->skipdata_setup.callback) {
Nguyen Anh Quynh0df7e932014-07-10 15:42:16 +0800571 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
Nguyen Anh Quynh4b6b15f2014-08-26 15:57:04 +0800572 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800573 if (skipdata_bytes > size)
574 // remaining data is not enough
575 break;
576
577 if (!skipdata_bytes)
578 // user requested not to skip data, so bail out
579 break;
580 } else
581 skipdata_bytes = handle->skipdata_size;
582
583 // we have to skip some amount of data, depending on arch & mode
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700584 insn_cache->id = 0; // invalid ID for this "data" instruction
585 insn_cache->address = offset;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800586 insn_cache->size = (uint16_t)skipdata_bytes;
Nguyen Anh Quynhee583942014-06-09 00:13:31 +0700587 memcpy(insn_cache->bytes, buffer, skipdata_bytes);
588 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
589 sizeof(insn_cache->mnemonic) - 1);
590 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
591 insn_cache->detail = NULL;
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800592
danghvu0d1aad12014-10-01 23:12:18 -0500593 next_offset = skipdata_bytes;
594 }
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800595
danghvu0d1aad12014-10-01 23:12:18 -0500596 // one more instruction entering the cache
597 f++;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800598
danghvu0d1aad12014-10-01 23:12:18 -0500599 // one more instruction disassembled
600 c++;
601 if (count > 0 && c == count)
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800602 // already got requested number of instructions
danghvu0d1aad12014-10-01 23:12:18 -0500603 break;
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800604
danghvu0d1aad12014-10-01 23:12:18 -0500605 if (f == cache_size) {
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800606 // full cache, so expand the cache to contain incoming insns
danghvu2fb7c8e2014-10-02 07:38:24 -0500607 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
danghvu0d1aad12014-10-01 23:12:18 -0500608 total_size += (sizeof(cs_insn) * cache_size);
609 tmp = cs_mem_realloc(total, total_size);
610 if (tmp == NULL) { // insufficient memory
611 if (handle->detail) {
612 insn_cache = (cs_insn *)total;
613 for (i = 0; i < c; i++, insn_cache++)
614 cs_mem_free(insn_cache->detail);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800615 }
616
danghvu0d1aad12014-10-01 23:12:18 -0500617 cs_mem_free(total);
618 *insn = NULL;
619 handle->errnum = CS_ERR_MEM;
620 return 0;
621 }
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800622
danghvu0d1aad12014-10-01 23:12:18 -0500623 total = tmp;
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800624 // continue to fill in the cache after the last instruction
625 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
Nguyen Anh Quynhd3ffe372014-04-09 23:49:30 +0800626
Nguyen Anh Quynh4d3ccf42014-10-03 00:39:56 +0800627 // reset f back to 0, so we fill in the cache from begining
danghvu0d1aad12014-10-01 23:12:18 -0500628 f = 0;
629 } else
630 insn_cache++;
631
632 buffer += next_offset;
633 size -= next_offset;
634 offset += next_offset;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800635 }
636
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800637 if (!c) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800638 // we did not disassemble any instruction
Nguyen Anh Quynh27a4a082014-10-02 10:31:37 +0800639 cs_mem_free(total);
640 total = NULL;
641 } else if (f != cache_size) {
Nguyen Anh Quynhf9d8a892014-10-02 12:37:32 +0800642 // total did not fully use the last cache, so downsize it
Tyler J. Stachecki30d11672015-10-23 20:59:20 -0400643 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800644 if (tmp == NULL) { // insufficient memory
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800645 // free all detail pointers
646 if (handle->detail) {
647 insn_cache = (cs_insn *)total;
648 for (i = 0; i < c; i++, insn_cache++)
649 cs_mem_free(insn_cache->detail);
650 }
651
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800652 cs_mem_free(total);
Nguyen Anh Quynhdab17fd2014-06-19 11:15:18 +0800653 *insn = NULL;
Nguyen Anh Quynh6c182ae2014-06-18 21:50:25 +0800654
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800655 handle->errnum = CS_ERR_MEM;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800656 return 0;
657 }
658
659 total = tmp;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800660 }
661
662 *insn = total;
663
664 return c;
665}
666
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800667CAPSTONE_EXPORT
Nguyen Anh Quynh0beb0d42014-08-27 22:55:29 +0800668CAPSTONE_DEPRECATED
tandasat760940f2016-03-31 18:14:43 -0700669size_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 +0800670{
671 return cs_disasm(ud, buffer, size, offset, count, insn);
672}
673
674CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700675void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800676{
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800677 size_t i;
678
679 // free all detail pointers
680 for (i = 0; i < count; i++)
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800681 cs_mem_free(insn[i].detail);
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800682
683 // then free pointer to cs_insn array
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800684 cs_mem_free(insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800685}
686
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800687CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700688cs_insn * CAPSTONE_API cs_malloc(csh ud)
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800689{
690 cs_insn *insn;
691 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
692
693 insn = cs_mem_malloc(sizeof(cs_insn));
694 if (!insn) {
695 // insufficient memory
696 handle->errnum = CS_ERR_MEM;
697 return NULL;
698 } else {
699 if (handle->detail) {
700 // allocate memory for @detail pointer
701 insn->detail = cs_mem_malloc(sizeof(cs_detail));
702 if (insn->detail == NULL) { // insufficient memory
703 cs_mem_free(insn);
704 handle->errnum = CS_ERR_MEM;
705 return NULL;
706 }
707 } else
708 insn->detail = NULL;
709 }
710
711 return insn;
712}
713
hlide993f3622014-10-05 18:14:40 +0200714// iterator for instruction "single-stepping"
715CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700716bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800717 uint64_t *address, cs_insn *insn)
hlide993f3622014-10-05 18:14:40 +0200718{
719 struct cs_struct *handle;
hlide993f3622014-10-05 18:14:40 +0200720 uint16_t insn_size;
721 MCInst mci;
722 bool r;
723
724 handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800725 if (!handle) {
Nguyen Anh Quynh29ce6c32014-10-12 15:28:34 +0800726 return false;
hlide993f3622014-10-05 18:14:40 +0200727 }
728
729 handle->errnum = CS_ERR_OK;
730
hlide993f3622014-10-05 18:14:40 +0200731 MCInst_Init(&mci);
732 mci.csh = handle;
733
734 // relative branches need to know the address & size of current insn
735 mci.address = *address;
736
737 // save all the information for non-detailed mode
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800738 mci.flat_insn = insn;
hlide993f3622014-10-05 18:14:40 +0200739 mci.flat_insn->address = *address;
740#ifdef CAPSTONE_DIET
741 // zero out mnemonic & op_str
742 mci.flat_insn->mnemonic[0] = '\0';
743 mci.flat_insn->op_str[0] = '\0';
744#endif
745
746 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800747 if (r) {
hlide993f3622014-10-05 18:14:40 +0200748 SStream ss;
749 SStream_Init(&ss);
750
751 mci.flat_insn->size = insn_size;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800752
753 // map internal instruction opcode to public insn ID
754 handle->insn_id(handle, insn, mci.Opcode);
755
hlide993f3622014-10-05 18:14:40 +0200756 handle->printer(&mci, &ss, handle->printer_info);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800757
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800758 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +0800759
hlide993f3622014-10-05 18:14:40 +0200760 *code += insn_size;
761 *size -= insn_size;
762 *address += insn_size;
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800763 } else { // encounter a broken instruction
764 size_t skipdata_bytes;
765
766 // if there is no request to skip data, or remaining data is too small,
767 // then bail out
768 if (!handle->skipdata || handle->skipdata_size > *size)
769 return false;
770
771 if (handle->skipdata_setup.callback) {
772 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
773 0, handle->skipdata_setup.user_data);
774 if (skipdata_bytes > *size)
775 // remaining data is not enough
776 return false;
777
778 if (!skipdata_bytes)
779 // user requested not to skip data, so bail out
780 return false;
781 } else
782 skipdata_bytes = handle->skipdata_size;
783
784 // we have to skip some amount of data, depending on arch & mode
785 insn->id = 0; // invalid ID for this "data" instruction
786 insn->address = *address;
787 insn->size = (uint16_t)skipdata_bytes;
788 memcpy(insn->bytes, *code, skipdata_bytes);
789 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
790 sizeof(insn->mnemonic) - 1);
791 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
792
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800793 *code += skipdata_bytes;
794 *size -= skipdata_bytes;
795 *address += skipdata_bytes;
hlide993f3622014-10-05 18:14:40 +0200796 }
Nguyen Anh Quynh0a2eca72014-10-11 00:36:16 +0800797
798 return true;
hlide993f3622014-10-05 18:14:40 +0200799}
800
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800801// return friendly name of regiser in a string
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800802CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700803const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800804{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800805 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800806
807 if (!handle || handle->reg_name == NULL) {
808 return NULL;
809 }
810
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800811 return handle->reg_name(ud, reg);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800812}
813
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800814CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700815const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800816{
Nguyen Anh Quynha82a0892014-01-23 23:42:40 +0800817 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800818
819 if (!handle || handle->insn_name == NULL) {
820 return NULL;
821 }
822
Nguyen Anh Quynha253c7a2013-12-09 10:26:18 +0800823 return handle->insn_name(ud, insn);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800824}
825
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800826CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700827const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Nguyen Anh Quynh650f96c2014-07-08 08:59:27 +0800828{
829 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
830
831 if (!handle || handle->group_name == NULL) {
832 return NULL;
833 }
834
835 return handle->group_name(ud, group);
836}
837
Nguyen Anh Quynh70083562013-12-20 22:02:20 +0800838static bool arr_exist(unsigned char *arr, unsigned char max, unsigned int id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800839{
840 int i;
841
842 for (i = 0; i < max; i++) {
843 if (arr[i] == id)
844 return true;
845 }
846
847 return false;
848}
849
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800850CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700851bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800852{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800853 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800854 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800855 return false;
856
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100857 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200858
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800859 if (!handle->detail) {
860 handle->errnum = CS_ERR_DETAIL;
861 return false;
862 }
863
Giovanni Condello95657e02014-05-07 17:31:27 +0200864 if(!insn->id) {
865 handle->errnum = CS_ERR_SKIPDATA;
866 return false;
867 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200868
Giovanni Condello95657e02014-05-07 17:31:27 +0200869 if(!insn->detail) {
870 handle->errnum = CS_ERR_DETAIL;
871 return false;
872 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200873
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800874 return arr_exist(insn->detail->groups, insn->detail->groups_count, group_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800875}
876
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800877CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700878bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800879{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800880 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800881 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800882 return false;
883
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100884 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200885
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800886 if (!handle->detail) {
887 handle->errnum = CS_ERR_DETAIL;
888 return false;
889 }
890
Giovanni Condello95657e02014-05-07 17:31:27 +0200891 if(!insn->id) {
892 handle->errnum = CS_ERR_SKIPDATA;
893 return false;
894 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200895
Giovanni Condello95657e02014-05-07 17:31:27 +0200896 if(!insn->detail) {
897 handle->errnum = CS_ERR_DETAIL;
898 return false;
899 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200900
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800901 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800902}
903
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800904CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700905bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800906{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800907 struct cs_struct *handle;
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800908 if (!ud)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800909 return false;
910
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100911 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condello95657e02014-05-07 17:31:27 +0200912
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800913 if (!handle->detail) {
914 handle->errnum = CS_ERR_DETAIL;
915 return false;
916 }
917
Giovanni Condello95657e02014-05-07 17:31:27 +0200918 if(!insn->id) {
919 handle->errnum = CS_ERR_SKIPDATA;
920 return false;
921 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200922
Giovanni Condello95657e02014-05-07 17:31:27 +0200923 if(!insn->detail) {
924 handle->errnum = CS_ERR_DETAIL;
925 return false;
926 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200927
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800928 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800929}
930
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +0800931CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -0700932int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800933{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +0800934 struct cs_struct *handle;
935 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800936 if (!ud)
937 return -1;
938
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +0100939 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +0200940
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +0800941 if (!handle->detail) {
942 handle->errnum = CS_ERR_DETAIL;
943 return -1;
944 }
945
Giovanni Condello95657e02014-05-07 17:31:27 +0200946 if(!insn->id) {
947 handle->errnum = CS_ERR_SKIPDATA;
948 return -1;
949 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200950
Giovanni Condello95657e02014-05-07 17:31:27 +0200951 if(!insn->detail) {
952 handle->errnum = CS_ERR_DETAIL;
953 return -1;
954 }
Giovanni Condelloa715df12014-05-07 17:25:38 +0200955
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800956 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800957
958 switch (handle->arch) {
959 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +0800960 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800961 return -1;
962 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800963 for (i = 0; i < insn->detail->arm.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800964 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800965 count++;
966 break;
967 case CS_ARCH_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800968 for (i = 0; i < insn->detail->arm64.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800969 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800970 count++;
971 break;
972 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800973 for (i = 0; i < insn->detail->x86.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800974 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800975 count++;
976 break;
977 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800978 for (i = 0; i < insn->detail->mips.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800979 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800980 count++;
981 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800982 case CS_ARCH_PPC:
983 for (i = 0; i < insn->detail->ppc.op_count; i++)
Alex Ionescu46018db2014-01-22 09:45:00 -0800984 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800985 count++;
986 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +0800987 case CS_ARCH_SPARC:
988 for (i = 0; i < insn->detail->sparc.op_count; i++)
989 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
990 count++;
991 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800992 case CS_ARCH_SYSZ:
993 for (i = 0; i < insn->detail->sysz.op_count; i++)
994 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
995 count++;
996 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +0800997 case CS_ARCH_XCORE:
998 for (i = 0; i < insn->detail->xcore.op_count; i++)
999 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1000 count++;
1001 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001002 }
1003
1004 return count;
1005}
1006
Nguyen Anh Quynh07c36932014-06-03 18:33:15 +08001007CAPSTONE_EXPORT
tandasat760940f2016-03-31 18:14:43 -07001008int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001009 unsigned int post)
1010{
Nguyen Anh Quynhbb0744d2014-05-12 13:41:49 +08001011 struct cs_struct *handle;
1012 unsigned int count = 0, i;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001013 if (!ud)
1014 return -1;
1015
Axel 0vercl0k Souchet779d4c72014-05-08 23:44:49 +01001016 handle = (struct cs_struct *)(uintptr_t)ud;
Giovanni Condelloa715df12014-05-07 17:25:38 +02001017
Nguyen Anh Quynh24e12272014-01-15 21:27:23 +08001018 if (!handle->detail) {
1019 handle->errnum = CS_ERR_DETAIL;
1020 return -1;
1021 }
1022
Giovanni Condello95657e02014-05-07 17:31:27 +02001023 if(!insn->id) {
1024 handle->errnum = CS_ERR_SKIPDATA;
1025 return -1;
1026 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001027
Giovanni Condello95657e02014-05-07 17:31:27 +02001028 if(!insn->detail) {
1029 handle->errnum = CS_ERR_DETAIL;
1030 return -1;
1031 }
Giovanni Condelloa715df12014-05-07 17:25:38 +02001032
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001033 handle->errnum = CS_ERR_OK;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001034
1035 switch (handle->arch) {
1036 default:
Nguyen Anh Quynh3eb9ac92013-11-27 15:24:47 +08001037 handle->errnum = CS_ERR_HANDLE;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001038 return -1;
1039 case CS_ARCH_ARM:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001040 for (i = 0; i < insn->detail->arm.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001041 if (insn->detail->arm.operands[i].type == (arm_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_ARM64:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001048 for (i = 0; i < insn->detail->arm64.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001049 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001050 count++;
1051 if (count == post)
1052 return i;
1053 }
1054 break;
1055 case CS_ARCH_X86:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001056 for (i = 0; i < insn->detail->x86.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001057 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001058 count++;
1059 if (count == post)
1060 return i;
1061 }
1062 break;
1063 case CS_ARCH_MIPS:
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +08001064 for (i = 0; i < insn->detail->mips.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001065 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001066 count++;
1067 if (count == post)
1068 return i;
1069 }
1070 break;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001071 case CS_ARCH_PPC:
1072 for (i = 0; i < insn->detail->ppc.op_count; i++) {
Alex Ionescu46018db2014-01-22 09:45:00 -08001073 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +08001074 count++;
1075 if (count == post)
1076 return i;
1077 }
1078 break;
Nguyen Anh Quynh05e27132014-03-10 11:58:57 +08001079 case CS_ARCH_SPARC:
1080 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1081 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1082 count++;
1083 if (count == post)
1084 return i;
1085 }
1086 break;
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +08001087 case CS_ARCH_SYSZ:
1088 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1089 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1090 count++;
1091 if (count == post)
1092 return i;
1093 }
1094 break;
Nguyen Anh Quynhc80d8402014-05-26 23:02:48 +08001095 case CS_ARCH_XCORE:
1096 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1097 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1098 count++;
1099 if (count == post)
1100 return i;
1101 }
1102 break;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001103 }
1104
1105 return -1;
1106}