Debian Codegraph @

cs.c

raw symbols SCIP
1 /* Capstone Disassembly Engine */
2 /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
3 #if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
4 #pragma warning(disable:4996) // disable MSVC's warning on strcpy()
5 #pragma warning(disable:28719) // disable MSVC's warning on strcpy()
6 #endif
7 #if defined(CAPSTONE_HAS_OSXKERNEL)
8 #include <Availability.h>
9 #include <libkern/libkern.h>
10 #else
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #endif
15
16 #include <string.h>
17 #include <capstone/capstone.h>
18
19 #include "utils.h"
20 #include "MCRegisterInfo.h"
21
22 #if defined(_KERNEL_MODE)
23 #include "windows\winkernel_mm.h"
24 #endif
25
26 // Issue #681: Windows kernel does not support formatting float point
27 #if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
28 #if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64) || defined(CAPSTONE_HAS_M68K)
29 #define CAPSTONE_STR_INTERNAL(x) #x
30 #define CAPSTONE_STR(x) CAPSTONE_STR_INTERNAL(x)
31 #define CAPSTONE_MSVC_WRANING_PREFIX __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
32
33 #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.")
34
35 #undef CAPSTONE_MSVC_WRANING_PREFIX
36 #undef CAPSTONE_STR
37 #undef CAPSTONE_STR_INTERNAL
38 #endif
39 #endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
40
41 #if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
42 #define INSN_CACHE_SIZE 32
43 #else
44 // reduce stack variable size for kernel/firmware
45 #define INSN_CACHE_SIZE 8
46 #endif
47
48 // default SKIPDATA mnemonic
49 #ifndef CAPSTONE_DIET
50 #define SKIPDATA_MNEM ".byte"
51 #else // No printing is available in diet mode
52 #define SKIPDATA_MNEM NULL
53 #endif
54
55 #include "arch/AArch64/AArch64Module.h"
56 #include "arch/ARM/ARMModule.h"
57 #include "arch/EVM/EVMModule.h"
58 #include "arch/WASM/WASMModule.h"
59 #include "arch/M680X/M680XModule.h"
60 #include "arch/M68K/M68KModule.h"
61 #include "arch/Mips/MipsModule.h"
62 #include "arch/PowerPC/PPCModule.h"
63 #include "arch/Sparc/SparcModule.h"
64 #include "arch/SystemZ/SystemZModule.h"
65 #include "arch/TMS320C64x/TMS320C64xModule.h"
66 #include "arch/X86/X86Module.h"
67 #include "arch/XCore/XCoreModule.h"
68 #include "arch/RISCV/RISCVModule.h"
69 #include "arch/MOS65XX/MOS65XXModule.h"
70 #include "arch/BPF/BPFModule.h"
71 #include "arch/SH/SHModule.h"
72 #include "arch/TriCore/TriCoreModule.h"
73
74 static const struct {
75 // constructor initialization
76 cs_err (*arch_init)(cs_struct *);
77 // support cs_option()
78 cs_err (*arch_option)(cs_struct *, cs_opt_type, size_t value);
79 // bitmask for finding disallowed modes for an arch:
80 // to be called in cs_open()/cs_option()
81 cs_mode arch_disallowed_mode_mask;
82 } arch_configs[MAX_ARCH] = {
83 #ifdef CAPSTONE_HAS_ARM
84 {
85 ARM_global_init,
86 ARM_option,
87 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_V8 | CS_MODE_MCLASS
88 | CS_MODE_THUMB | CS_MODE_BIG_ENDIAN)
89 },
90 #else
91 { NULL, NULL, 0 },
92 #endif
93 #ifdef CAPSTONE_HAS_ARM64
94 {
95 AArch64_global_init,
96 AArch64_option,
97 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_BIG_ENDIAN),
98 },
99 #else
100 { NULL, NULL, 0 },
101 #endif
102 #ifdef CAPSTONE_HAS_MIPS
103 {
104 Mips_global_init,
105 Mips_option,
106 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_MICRO
107 | CS_MODE_MIPS32R6 | CS_MODE_BIG_ENDIAN | CS_MODE_MIPS2 | CS_MODE_MIPS3),
108 },
109 #else
110 { NULL, NULL, 0 },
111 #endif
112 #ifdef CAPSTONE_HAS_X86
113 {
114 X86_global_init,
115 X86_option,
116 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_16),
117 },
118 #else
119 { NULL, NULL, 0 },
120 #endif
121 #ifdef CAPSTONE_HAS_POWERPC
122 {
123 PPC_global_init,
124 PPC_option,
125 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_BIG_ENDIAN
126 | CS_MODE_QPX | CS_MODE_PS),
127 },
128 #else
129 { NULL, NULL, 0 },
130 #endif
131 #ifdef CAPSTONE_HAS_SPARC
132 {
133 Sparc_global_init,
134 Sparc_option,
135 ~(CS_MODE_BIG_ENDIAN | CS_MODE_V9),
136 },
137 #else
138 { NULL, NULL, 0 },
139 #endif
140 #ifdef CAPSTONE_HAS_SYSZ
141 {
142 SystemZ_global_init,
143 SystemZ_option,
144 ~(CS_MODE_BIG_ENDIAN),
145 },
146 #else
147 { NULL, NULL, 0 },
148 #endif
149 #ifdef CAPSTONE_HAS_XCORE
150 {
151 XCore_global_init,
152 XCore_option,
153 ~(CS_MODE_BIG_ENDIAN),
154 },
155 #else
156 { NULL, NULL, 0 },
157 #endif
158 #ifdef CAPSTONE_HAS_M68K
159 {
160 M68K_global_init,
161 M68K_option,
162 ~(CS_MODE_BIG_ENDIAN | CS_MODE_M68K_000 | CS_MODE_M68K_010 | CS_MODE_M68K_020
163 | CS_MODE_M68K_030 | CS_MODE_M68K_040 | CS_MODE_M68K_060),
164 },
165 #else
166 { NULL, NULL, 0 },
167 #endif
168 #ifdef CAPSTONE_HAS_TMS320C64X
169 {
170 TMS320C64x_global_init,
171 TMS320C64x_option,
172 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN),
173 },
174 #else
175 { NULL, NULL, 0 },
176 #endif
177 #ifdef CAPSTONE_HAS_M680X
178 {
179 M680X_global_init,
180 M680X_option,
181 ~(CS_MODE_M680X_6301 | CS_MODE_M680X_6309 | CS_MODE_M680X_6800
182 | CS_MODE_M680X_6801 | CS_MODE_M680X_6805 | CS_MODE_M680X_6808
183 | CS_MODE_M680X_6809 | CS_MODE_M680X_6811 | CS_MODE_M680X_CPU12
184 | CS_MODE_M680X_HCS08),
185 },
186 #else
187 { NULL, NULL, 0 },
188 #endif
189 #ifdef CAPSTONE_HAS_EVM
190 {
191 EVM_global_init,
192 EVM_option,
193 0,
194 },
195 #else
196 { NULL, NULL, 0 },
197 #endif
198 #ifdef CAPSTONE_HAS_MOS65XX
199 {
200 MOS65XX_global_init,
201 MOS65XX_option,
202 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_MOS65XX_6502 | CS_MODE_MOS65XX_65C02
203 | CS_MODE_MOS65XX_W65C02 | CS_MODE_MOS65XX_65816_LONG_MX),
204 },
205 #else
206 { NULL, NULL, 0 },
207 #endif
208 #ifdef CAPSTONE_HAS_WASM
209 {
210 WASM_global_init,
211 WASM_option,
212 0,
213 },
214 #else
215 { NULL, NULL, 0 },
216 #endif
217 #ifdef CAPSTONE_HAS_BPF
218 {
219 BPF_global_init,
220 BPF_option,
221 ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BPF_CLASSIC | CS_MODE_BPF_EXTENDED
222 | CS_MODE_BIG_ENDIAN),
223 },
224 #else
225 { NULL, NULL, 0 },
226 #endif
227 #ifdef CAPSTONE_HAS_RISCV
228 {
229 RISCV_global_init,
230 RISCV_option,
231 ~(CS_MODE_RISCV32 | CS_MODE_RISCV64 | CS_MODE_RISCVC),
232 },
233 #else
234 { NULL, NULL, 0 },
235 #endif
236 #ifdef CAPSTONE_HAS_SH
237 {
238 SH_global_init,
239 SH_option,
240 ~(CS_MODE_SH2 | CS_MODE_SH2A | CS_MODE_SH3 |
241 CS_MODE_SH4 | CS_MODE_SH4A |
242 CS_MODE_SHFPU | CS_MODE_SHDSP|CS_MODE_BIG_ENDIAN),
243 },
244 #else
245 { NULL, NULL, 0 },
246 #endif
247 #ifdef CAPSTONE_HAS_TRICORE
248 {
249 TRICORE_global_init,
250 TRICORE_option,
251 ~(CS_MODE_TRICORE_110 | CS_MODE_TRICORE_120 | CS_MODE_TRICORE_130
252 | CS_MODE_TRICORE_131 | CS_MODE_TRICORE_160 | CS_MODE_TRICORE_161
253 | CS_MODE_TRICORE_162 | CS_MODE_LITTLE_ENDIAN),
254 },
255 #else
256 { NULL, NULL, 0 },
257 #endif
258 };
259
260 // bitmask of enabled architectures
261 static const uint32_t all_arch = 0
262 #ifdef CAPSTONE_HAS_ARM
263 | (1 << CS_ARCH_ARM)
264 #endif
265 #ifdef CAPSTONE_HAS_ARM64
266 | (1 << CS_ARCH_ARM64)
267 #endif
268 #ifdef CAPSTONE_HAS_MIPS
269 | (1 << CS_ARCH_MIPS)
270 #endif
271 #ifdef CAPSTONE_HAS_X86
272 | (1 << CS_ARCH_X86)
273 #endif
274 #ifdef CAPSTONE_HAS_POWERPC
275 | (1 << CS_ARCH_PPC)
276 #endif
277 #ifdef CAPSTONE_HAS_SPARC
278 | (1 << CS_ARCH_SPARC)
279 #endif
280 #ifdef CAPSTONE_HAS_SYSZ
281 | (1 << CS_ARCH_SYSZ)
282 #endif
283 #ifdef CAPSTONE_HAS_XCORE
284 | (1 << CS_ARCH_XCORE)
285 #endif
286 #ifdef CAPSTONE_HAS_M68K
287 | (1 << CS_ARCH_M68K)
288 #endif
289 #ifdef CAPSTONE_HAS_TMS320C64X
290 | (1 << CS_ARCH_TMS320C64X)
291 #endif
292 #ifdef CAPSTONE_HAS_M680X
293 | (1 << CS_ARCH_M680X)
294 #endif
295 #ifdef CAPSTONE_HAS_EVM
296 | (1 << CS_ARCH_EVM)
297 #endif
298 #ifdef CAPSTONE_HAS_MOS65XX
299 | (1 << CS_ARCH_MOS65XX)
300 #endif
301 #ifdef CAPSTONE_HAS_WASM
302 | (1 << CS_ARCH_WASM)
303 #endif
304 #ifdef CAPSTONE_HAS_BPF
305 | (1 << CS_ARCH_BPF)
306 #endif
307 #ifdef CAPSTONE_HAS_RISCV
308 | (1 << CS_ARCH_RISCV)
309 #endif
310 #ifdef CAPSTONE_HAS_SH
311 | (1 << CS_ARCH_SH)
312 #endif
313 #ifdef CAPSTONE_HAS_TRICORE
314 | (1 << CS_ARCH_TRICORE)
315 #endif
316 ;
317
318
319 #if defined(CAPSTONE_USE_SYS_DYN_MEM)
320 #if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
321 // default
322 cs_malloc_t cs_mem_malloc = malloc;
323 cs_calloc_t cs_mem_calloc = calloc;
324 cs_realloc_t cs_mem_realloc = realloc;
325 cs_free_t cs_mem_free = free;
326 #if defined(_WIN32_WCE)
327 cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
328 #else
329 cs_vsnprintf_t cs_vsnprintf = vsnprintf;
330 #endif // defined(_WIN32_WCE)
331
332 #elif defined(_KERNEL_MODE)
333 // Windows driver
334 cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
335 cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
336 cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
337 cs_free_t cs_mem_free = cs_winkernel_free;
338 cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
339 #else
340 // OSX kernel
341 extern void* kern_os_malloc(size_t size);
342 extern void kern_os_free(void* addr);
343 extern void* kern_os_realloc(void* addr, size_t nsize);
344
345 static void* cs_kern_os_calloc(size_t num, size_t size)
346 {
347 return kern_os_malloc(num * size); // malloc bzeroes the buffer
348 }
349
350 cs_malloc_t cs_mem_malloc = kern_os_malloc;
351 cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
352 cs_realloc_t cs_mem_realloc = kern_os_realloc;
353 cs_free_t cs_mem_free = kern_os_free;
354 cs_vsnprintf_t cs_vsnprintf = vsnprintf;
355 #endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
356 #else
357 // User-defined
358 cs_malloc_t cs_mem_malloc = NULL;
359 cs_calloc_t cs_mem_calloc = NULL;
360 cs_realloc_t cs_mem_realloc = NULL;
361 cs_free_t cs_mem_free = NULL;
362 cs_vsnprintf_t cs_vsnprintf = NULL;
363
364 #endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
365
366 CAPSTONE_EXPORT
367 unsigned int CAPSTONE_API cs_version(int *major, int *minor)
368 {
369 if (major != NULL && minor != NULL) {
370 *major = CS_API_MAJOR;
371 *minor = CS_API_MINOR;
372 }
373
374 return (CS_API_MAJOR << 8) + CS_API_MINOR;
375 }
376
377 CAPSTONE_EXPORT
378 bool CAPSTONE_API cs_support(int query)
379 {
380 if (query == CS_ARCH_ALL)
381 return all_arch == ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_ARM64) |
382 (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
383 (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
384 (1 << CS_ARCH_SYSZ) | (1 << CS_ARCH_XCORE) |
385 (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
386 (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM) |
387 (1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX) |
388 (1 << CS_ARCH_WASM) | (1 << CS_ARCH_BPF) |
389 (1 << CS_ARCH_SH) | (1 << CS_ARCH_TRICORE));
390
391 if ((unsigned int)query < CS_ARCH_MAX)
392 return all_arch & (1 << query);
393
394 if (query == CS_SUPPORT_DIET) {
395 #ifdef CAPSTONE_DIET
396 return true;
397 #else
398 return false;
399 #endif
400 }
401
402 if (query == CS_SUPPORT_X86_REDUCE) {
403 #if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
404 return true;
405 #else
406 return false;
407 #endif
408 }
409
410 // unsupported query
411 return false;
412 }
413
414 CAPSTONE_EXPORT
415 cs_err CAPSTONE_API cs_errno(csh handle)
416 {
417 struct cs_struct *ud;
418 if (!handle)
419 return CS_ERR_CSH;
420
421 ud = (struct cs_struct *)(uintptr_t)handle;
422
423 return ud->errnum;
424 }
425
426 CAPSTONE_EXPORT
427 const char * CAPSTONE_API cs_strerror(cs_err code)
428 {
429 switch(code) {
430 default:
431 return "Unknown error code";
432 case CS_ERR_OK:
433 return "OK (CS_ERR_OK)";
434 case CS_ERR_MEM:
435 return "Out of memory (CS_ERR_MEM)";
436 case CS_ERR_ARCH:
437 return "Invalid/unsupported architecture(CS_ERR_ARCH)";
438 case CS_ERR_HANDLE:
439 return "Invalid handle (CS_ERR_HANDLE)";
440 case CS_ERR_CSH:
441 return "Invalid csh (CS_ERR_CSH)";
442 case CS_ERR_MODE:
443 return "Invalid mode (CS_ERR_MODE)";
444 case CS_ERR_OPTION:
445 return "Invalid option (CS_ERR_OPTION)";
446 case CS_ERR_DETAIL:
447 return "Details are unavailable (CS_ERR_DETAIL)";
448 case CS_ERR_MEMSETUP:
449 return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
450 case CS_ERR_VERSION:
451 return "Different API version between core & binding (CS_ERR_VERSION)";
452 case CS_ERR_DIET:
453 return "Information irrelevant in diet engine (CS_ERR_DIET)";
454 case CS_ERR_SKIPDATA:
455 return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
456 case CS_ERR_X86_ATT:
457 return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
458 case CS_ERR_X86_INTEL:
459 return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
460 case CS_ERR_X86_MASM:
461 return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
462 }
463 }
464
465 CAPSTONE_EXPORT
466 cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
467 {
468 cs_err err;
469 struct cs_struct *ud;
470 if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
471 // Error: before cs_open(), dynamic memory management must be initialized
472 // with cs_option(CS_OPT_MEM)
473 return CS_ERR_MEMSETUP;
474
475 if (arch < CS_ARCH_MAX && arch_configs[arch].arch_init) {
476 // verify if requested mode is valid
477 if (mode & arch_configs[arch].arch_disallowed_mode_mask) {
478 *handle = 0;
479 return CS_ERR_MODE;
480 }
481
482 ud = cs_mem_calloc(1, sizeof(*ud));
483 if (!ud) {
484 // memory insufficient
485 return CS_ERR_MEM;
486 }
487
488 ud->errnum = CS_ERR_OK;
489 ud->arch = arch;
490 ud->mode = mode;
491 // by default, do not break instruction into details
492 ud->detail = CS_OPT_OFF;
493
494 // default skipdata setup
495 ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
496
497 err = arch_configs[ud->arch].arch_init(ud);
498 if (err) {
499 cs_mem_free(ud);
500 *handle = 0;
501 return err;
502 }
503
504 *handle = (uintptr_t)ud;
505
506 return CS_ERR_OK;
507 } else {
508 *handle = 0;
509 return CS_ERR_ARCH;
510 }
511 }
512
513 CAPSTONE_EXPORT
514 cs_err CAPSTONE_API cs_close(csh *handle)
515 {
516 struct cs_struct *ud;
517 struct insn_mnem *next, *tmp;
518
519 if (*handle == 0)
520 // invalid handle
521 return CS_ERR_CSH;
522
523 ud = (struct cs_struct *)(*handle);
524
525 if (ud->printer_info)
526 cs_mem_free(ud->printer_info);
527
528 // free the linked list of customized mnemonic
529 tmp = ud->mnem_list;
530 while(tmp) {
531 next = tmp->next;
532 cs_mem_free(tmp);
533 tmp = next;
534 }
535
536 cs_mem_free(ud->insn_cache);
537 cs_mem_free(ud->x86_insn_lut);
538 cs_mem_free(ud->x86_insn_reg_lut);
539
540 memset(ud, 0, sizeof(*ud));
541 cs_mem_free(ud);
542
543 // invalidate this handle by ZERO out its value.
544 // this is to make sure it is unusable after cs_close()
545 *handle = 0;
546
547 return CS_ERR_OK;
548 }
549
550 // replace str1 in target with str2; target starts with str1
551 // output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
552 // return 0 on success, -1 on failure
553 static int str_replace(char *result, char *target, const char *str1, char *str2)
554 {
555 // only perform replacement if the output fits into result
556 if (strlen(target) - strlen(str1) + strlen(str2) < CS_MNEMONIC_SIZE - 1) {
557 // copy str2 to begining of result
558 strcpy(result, str2);
559 // skip str1 - already replaced by str2
560 strcat(result, target + strlen(str1));
561
562 return 0;
563 } else
564 return -1;
565 }
566
567 // fill insn with mnemonic & operands info
568 static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
569 PostPrinter_t postprinter, const uint8_t *code)
570 {
571 #ifndef CAPSTONE_DIET
572 char *sp, *mnem;
573 #endif
574 uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
575
576 // fill the instruction bytes.
577 // we might skip some redundant bytes in front in the case of X86
578 memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
579 insn->op_str[0] = '\0';
580 insn->size = copy_size;
581
582 // alias instruction might have ID saved in OpcodePub
583 if (MCInst_getOpcodePub(mci))
584 insn->id = MCInst_getOpcodePub(mci);
585
586 // post printer handles some corner cases (hacky)
587 if (postprinter)
588 postprinter((csh)handle, insn, buffer, mci);
589
590 #ifndef CAPSTONE_DIET
591 mnem = insn->mnemonic;
592 // memset(mnem, 0, CS_MNEMONIC_SIZE);
593 for (sp = buffer; *sp; sp++) {
594 if (*sp == ' '|| *sp == '\t')
595 break;
596 if (*sp == '|') // lock|rep prefix for x86
597 *sp = ' ';
598 // copy to @mnemonic
599 *mnem = *sp;
600 mnem++;
601 }
602
603 *mnem = '\0';
604
605 // we might have customized mnemonic
606 if (handle->mnem_list) {
607 struct insn_mnem *tmp = handle->mnem_list;
608 while(tmp) {
609 if (tmp->insn.id == insn->id) {
610 char str[CS_MNEMONIC_SIZE];
611
612 if (!str_replace(str, insn->mnemonic, cs_insn_name((csh)handle, insn->id), tmp->insn.mnemonic)) {
613 // copy result to mnemonic
614 (void)strncpy(insn->mnemonic, str, sizeof(insn->mnemonic) - 1);
615 insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
616 }
617
618 break;
619 }
620 tmp = tmp->next;
621 }
622 }
623
624 // copy @op_str
625 if (*sp) {
626 // find the next non-space char
627 sp++;
628 for (; ((*sp == ' ') || (*sp == '\t')); sp++);
629 strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
630 insn->op_str[sizeof(insn->op_str) - 1] = '\0';
631 } else
632 insn->op_str[0] = '\0';
633
634 #endif
635 }
636
637 // how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
638 // this very much depends on instruction alignment requirement of each arch.
639 static uint8_t skipdata_size(cs_struct *handle)
640 {
641 switch(handle->arch) {
642 default:
643 // should never reach
644 return (uint8_t)-1;
645 case CS_ARCH_ARM:
646 // skip 2 bytes on Thumb mode.
647 if (handle->mode & CS_MODE_THUMB)
648 return 2;
649 // otherwise, skip 4 bytes
650 return 4;
651 case CS_ARCH_ARM64:
652 case CS_ARCH_MIPS:
653 case CS_ARCH_PPC:
654 case CS_ARCH_SPARC:
655 // skip 4 bytes
656 return 4;
657 case CS_ARCH_SYSZ:
658 // SystemZ instruction's length can be 2, 4 or 6 bytes,
659 // so we just skip 2 bytes
660 return 2;
661 case CS_ARCH_X86:
662 // X86 has no restriction on instruction alignment
663 return 1;
664 case CS_ARCH_XCORE:
665 // XCore instruction's length can be 2 or 4 bytes,
666 // so we just skip 2 bytes
667 return 2;
668 case CS_ARCH_M68K:
669 // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
670 return 2;
671 case CS_ARCH_TMS320C64X:
672 // TMS320C64x alignment is 4.
673 return 4;
674 case CS_ARCH_M680X:
675 // M680X alignment is 1.
676 return 1;
677 case CS_ARCH_EVM:
678 // EVM alignment is 1.
679 return 1;
680 case CS_ARCH_WASM:
681 //WASM alignment is 1
682 return 1;
683 case CS_ARCH_MOS65XX:
684 // MOS65XX alignment is 1.
685 return 1;
686 case CS_ARCH_BPF:
687 // both classic and extended BPF have alignment 8.
688 return 8;
689 case CS_ARCH_RISCV:
690 // special compress mode
691 if (handle->mode & CS_MODE_RISCVC)
692 return 2;
693 return 4;
694 case CS_ARCH_SH:
695 return 2;
696 case CS_ARCH_TRICORE:
697 // TriCore instruction's length can be 2 or 4 bytes,
698 // so we just skip 2 bytes
699 return 2;
700 }
701 }
702
703 CAPSTONE_EXPORT
704 cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
705 {
706 struct cs_struct *handle;
707 cs_opt_mnem *opt;
708
709 // cs_option() can be called with NULL handle just for CS_OPT_MEM
710 // This is supposed to be executed before all other APIs (even cs_open())
711 if (type == CS_OPT_MEM) {
712 cs_opt_mem *mem = (cs_opt_mem *)value;
713
714 cs_mem_malloc = mem->malloc;
715 cs_mem_calloc = mem->calloc;
716 cs_mem_realloc = mem->realloc;
717 cs_mem_free = mem->free;
718 cs_vsnprintf = mem->vsnprintf;
719
720 return CS_ERR_OK;
721 }
722
723 handle = (struct cs_struct *)(uintptr_t)ud;
724 if (!handle)
725 return CS_ERR_CSH;
726
727 switch(type) {
728 default:
729 break;
730
731 case CS_OPT_UNSIGNED:
732 handle->imm_unsigned = (cs_opt_value)value;
733 return CS_ERR_OK;
734
735 case CS_OPT_DETAIL:
736 handle->detail = (cs_opt_value)value;
737 return CS_ERR_OK;
738
739 case CS_OPT_SKIPDATA:
740 handle->skipdata = (value == CS_OPT_ON);
741 if (handle->skipdata) {
742 if (handle->skipdata_size == 0) {
743 // set the default skipdata size
744 handle->skipdata_size = skipdata_size(handle);
745 }
746 }
747 return CS_ERR_OK;
748
749 case CS_OPT_SKIPDATA_SETUP:
750 if (value) {
751 handle->skipdata_setup = *((cs_opt_skipdata *)value);
752 if (handle->skipdata_setup.mnemonic == NULL) {
753 handle->skipdata_setup.mnemonic = SKIPDATA_MNEM;
754 }
755 }
756 return CS_ERR_OK;
757
758 case CS_OPT_MNEMONIC:
759 opt = (cs_opt_mnem *)value;
760 if (opt->id) {
761 if (opt->mnemonic) {
762 struct insn_mnem *tmp;
763
764 // add new instruction, or replace existing instruction
765 // 1. find if we already had this insn in the linked list
766 tmp = handle->mnem_list;
767 while(tmp) {
768 if (tmp->insn.id == opt->id) {
769 // found this instruction, so replace its mnemonic
770 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
771 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
772 break;
773 }
774 tmp = tmp->next;
775 }
776
777 // 2. add this instruction if we have not had it yet
778 if (!tmp) {
779 tmp = cs_mem_malloc(sizeof(*tmp));
780 tmp->insn.id = opt->id;
781 (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
782 tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
783 // this new instruction is heading the list
784 tmp->next = handle->mnem_list;
785 handle->mnem_list = tmp;
786 }
787 return CS_ERR_OK;
788 } else {
789 struct insn_mnem *prev, *tmp;
790
791 // we want to delete an existing instruction
792 // iterate the list to find the instruction to remove it
793 tmp = handle->mnem_list;
794 prev = tmp;
795 while(tmp) {
796 if (tmp->insn.id == opt->id) {
797 // delete this instruction
798 if (tmp == prev) {
799 // head of the list
800 handle->mnem_list = tmp->next;
801 } else {
802 prev->next = tmp->next;
803 }
804 cs_mem_free(tmp);
805 break;
806 }
807 prev = tmp;
808 tmp = tmp->next;
809 }
810 }
811 }
812 return CS_ERR_OK;
813
814 case CS_OPT_MODE:
815 // verify if requested mode is valid
816 if (value & arch_configs[handle->arch].arch_disallowed_mode_mask) {
817 return CS_ERR_OPTION;
818 }
819 break;
820 }
821
822 return arch_configs[handle->arch].arch_option(handle, type, value);
823 }
824
825 // generate @op_str for data instruction of SKIPDATA
826 #ifndef CAPSTONE_DIET
827 static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
828 {
829 char *p = opstr;
830 int len;
831 size_t i;
832 size_t available = sizeof(((cs_insn*)NULL)->op_str);
833
834 if (!size) {
835 opstr[0] = '\0';
836 return;
837 }
838
839 len = cs_snprintf(p, available, "0x%02x", buffer[0]);
840 p+= len;
841 available -= len;
842
843 for(i = 1; i < size; i++) {
844 len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
845 if (len < 0) {
846 break;
847 }
848 if ((size_t)len > available - 1) {
849 break;
850 }
851 p+= len;
852 available -= len;
853 }
854 }
855 #endif
856
857 // dynamicly allocate memory to contain disasm insn
858 // NOTE: caller must free() the allocated memory itself to avoid memory leaking
859 CAPSTONE_EXPORT
860 size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
861 {
862 struct cs_struct *handle;
863 MCInst mci;
864 uint16_t insn_size;
865 size_t c = 0, i;
866 unsigned int f = 0; // index of the next instruction in the cache
867 cs_insn *insn_cache; // cache contains disassembled instructions
868 void *total = NULL;
869 size_t total_size = 0; // total size of output buffer containing all insns
870 bool r;
871 void *tmp;
872 size_t skipdata_bytes;
873 uint64_t offset_org; // save all the original info of the buffer
874 size_t size_org;
875 const uint8_t *buffer_org;
876 unsigned int cache_size = INSN_CACHE_SIZE;
877 size_t next_offset;
878
879 handle = (struct cs_struct *)(uintptr_t)ud;
880 if (!handle) {
881 // FIXME: how to handle this case:
882 // handle->errnum = CS_ERR_HANDLE;
883 return 0;
884 }
885
886 handle->errnum = CS_ERR_OK;
887
888 // reset IT block of ARM structure
889 if (handle->arch == CS_ARCH_ARM)
890 handle->ITBlock.size = 0;
891
892 #ifdef CAPSTONE_USE_SYS_DYN_MEM
893 if (count > 0 && count <= INSN_CACHE_SIZE)
894 cache_size = (unsigned int) count;
895 #endif
896
897 // save the original offset for SKIPDATA
898 buffer_org = buffer;
899 offset_org = offset;
900 size_org = size;
901
902 total_size = sizeof(cs_insn) * cache_size;
903 total = cs_mem_calloc(sizeof(cs_insn), cache_size);
904 if (total == NULL) {
905 // insufficient memory
906 handle->errnum = CS_ERR_MEM;
907 return 0;
908 }
909
910 insn_cache = total;
911
912 while (size > 0) {
913 MCInst_Init(&mci);
914 mci.csh = handle;
915
916 // relative branches need to know the address & size of current insn
917 mci.address = offset;
918
919 if (handle->detail) {
920 // allocate memory for @detail pointer
921 insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
922 } else {
923 insn_cache->detail = NULL;
924 }
925
926 // save all the information for non-detailed mode
927 mci.flat_insn = insn_cache;
928 mci.flat_insn->address = offset;
929 #ifdef CAPSTONE_DIET
930 // zero out mnemonic & op_str
931 mci.flat_insn->mnemonic[0] = '\0';
932 mci.flat_insn->op_str[0] = '\0';
933 #endif
934
935 r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
936 if (r) {
937 SStream ss;
938 SStream_Init(&ss);
939
940 mci.flat_insn->size = insn_size;
941
942 // map internal instruction opcode to public insn ID
943
944 handle->insn_id(handle, insn_cache, mci.Opcode);
945
946 handle->printer(&mci, &ss, handle->printer_info);
947 fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
948
949 // adjust for pseudo opcode (X86)
950 if (handle->arch == CS_ARCH_X86 && insn_cache->id != X86_INS_VCMP)
951 insn_cache->id += mci.popcode_adjust;
952
953 next_offset = insn_size;
954 } else {
955 // encounter a broken instruction
956
957 // free memory of @detail pointer
958 if (handle->detail) {
959 cs_mem_free(insn_cache->detail);
960 }
961
962 // if there is no request to skip data, or remaining data is too small,
963 // then bail out
964 if (!handle->skipdata || handle->skipdata_size > size)
965 break;
966
967 if (handle->skipdata_setup.callback) {
968 skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
969 (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
970 if (skipdata_bytes > size)
971 // remaining data is not enough
972 break;
973
974 if (!skipdata_bytes)
975 // user requested not to skip data, so bail out
976 break;
977 } else
978 skipdata_bytes = handle->skipdata_size;
979
980 // we have to skip some amount of data, depending on arch & mode
981 // invalid ID for this "data" instruction
982 insn_cache->id = 0;
983 insn_cache->address = offset;
984 insn_cache->size = (uint16_t)MIN(
985 skipdata_bytes, sizeof(insn_cache->bytes));
986 memcpy(insn_cache->bytes, buffer,
987 MIN(skipdata_bytes, sizeof(insn_cache->bytes)));
988 #ifdef CAPSTONE_DIET
989 insn_cache->mnemonic[0] = '\0';
990 insn_cache->op_str[0] = '\0';
991 #else
992 strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
993 sizeof(insn_cache->mnemonic) - 1);
994 skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
995 #endif
996 insn_cache->detail = NULL;
997
998 next_offset = skipdata_bytes;
999 }
1000
1001 // one more instruction entering the cache
1002 f++;
1003
1004 // one more instruction disassembled
1005 c++;
1006 if (count > 0 && c == count)
1007 // already got requested number of instructions
1008 break;
1009
1010 if (f == cache_size) {
1011 // full cache, so expand the cache to contain incoming insns
1012 cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
1013 total_size += (sizeof(cs_insn) * cache_size);
1014 tmp = cs_mem_realloc(total, total_size);
1015 if (tmp == NULL) { // insufficient memory
1016 if (handle->detail) {
1017 insn_cache = (cs_insn *)total;
1018 for (i = 0; i < c; i++, insn_cache++)
1019 cs_mem_free(insn_cache->detail);
1020 }
1021
1022 cs_mem_free(total);
1023 *insn = NULL;
1024 handle->errnum = CS_ERR_MEM;
1025 return 0;
1026 }
1027
1028 total = tmp;
1029 // continue to fill in the cache after the last instruction
1030 insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
1031
1032 // reset f back to 0, so we fill in the cache from begining
1033 f = 0;
1034 } else
1035 insn_cache++;
1036
1037 buffer += next_offset;
1038 size -= next_offset;
1039 offset += next_offset;
1040 }
1041
1042 if (!c) {
1043 // we did not disassemble any instruction
1044 cs_mem_free(total);
1045 total = NULL;
1046 } else if (f != cache_size) {
1047 // total did not fully use the last cache, so downsize it
1048 tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
1049 if (tmp == NULL) { // insufficient memory
1050 // free all detail pointers
1051 if (handle->detail) {
1052 insn_cache = (cs_insn *)total;
1053 for (i = 0; i < c; i++, insn_cache++)
1054 cs_mem_free(insn_cache->detail);
1055 }
1056
1057 cs_mem_free(total);
1058 *insn = NULL;
1059
1060 handle->errnum = CS_ERR_MEM;
1061 return 0;
1062 }
1063
1064 total = tmp;
1065 }
1066
1067 *insn = total;
1068
1069 return c;
1070 }
1071
1072 CAPSTONE_EXPORT
1073 void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
1074 {
1075 size_t i;
1076
1077 // free all detail pointers
1078 for (i = 0; i < count; i++)
1079 cs_mem_free(insn[i].detail);
1080
1081 // then free pointer to cs_insn array
1082 cs_mem_free(insn);
1083 }
1084
1085 CAPSTONE_EXPORT
1086 cs_insn * CAPSTONE_API cs_malloc(csh ud)
1087 {
1088 cs_insn *insn;
1089 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1090
1091 insn = cs_mem_malloc(sizeof(cs_insn));
1092 if (!insn) {
1093 // insufficient memory
1094 handle->errnum = CS_ERR_MEM;
1095 return NULL;
1096 } else {
1097 if (handle->detail) {
1098 // allocate memory for @detail pointer
1099 insn->detail = cs_mem_malloc(sizeof(cs_detail));
1100 if (insn->detail == NULL) { // insufficient memory
1101 cs_mem_free(insn);
1102 handle->errnum = CS_ERR_MEM;
1103 return NULL;
1104 }
1105 } else
1106 insn->detail = NULL;
1107 }
1108
1109 return insn;
1110 }
1111
1112 // iterator for instruction "single-stepping"
1113 CAPSTONE_EXPORT
1114 bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
1115 uint64_t *address, cs_insn *insn)
1116 {
1117 if (*size == 0)
1118 return false;
1119
1120 struct cs_struct *handle;
1121 uint16_t insn_size;
1122 MCInst mci;
1123 bool r;
1124
1125 handle = (struct cs_struct *)(uintptr_t)ud;
1126 if (!handle) {
1127 return false;
1128 }
1129
1130 handle->errnum = CS_ERR_OK;
1131
1132 MCInst_Init(&mci);
1133 mci.csh = handle;
1134
1135 // relative branches need to know the address & size of current insn
1136 mci.address = *address;
1137
1138 // save all the information for non-detailed mode
1139 mci.flat_insn = insn;
1140 mci.flat_insn->address = *address;
1141 #ifdef CAPSTONE_DIET
1142 // zero out mnemonic & op_str
1143 mci.flat_insn->mnemonic[0] = '\0';
1144 mci.flat_insn->op_str[0] = '\0';
1145 #endif
1146
1147 r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
1148 if (r) {
1149 SStream ss;
1150 SStream_Init(&ss);
1151
1152 mci.flat_insn->size = insn_size;
1153
1154 // map internal instruction opcode to public insn ID
1155 handle->insn_id(handle, insn, mci.Opcode);
1156
1157 handle->printer(&mci, &ss, handle->printer_info);
1158
1159 fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
1160
1161 // adjust for pseudo opcode (X86)
1162 if (handle->arch == CS_ARCH_X86)
1163 insn->id += mci.popcode_adjust;
1164
1165 *code += insn_size;
1166 *size -= insn_size;
1167 *address += insn_size;
1168 } else { // encounter a broken instruction
1169 size_t skipdata_bytes;
1170
1171 // if there is no request to skip data, or remaining data is too small,
1172 // then bail out
1173 if (!handle->skipdata || handle->skipdata_size > *size)
1174 return false;
1175
1176 if (handle->skipdata_setup.callback) {
1177 skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
1178 0, handle->skipdata_setup.user_data);
1179 if (skipdata_bytes > *size)
1180 // remaining data is not enough
1181 return false;
1182
1183 if (!skipdata_bytes)
1184 // user requested not to skip data, so bail out
1185 return false;
1186 } else
1187 skipdata_bytes = handle->skipdata_size;
1188
1189 // we have to skip some amount of data, depending on arch & mode
1190 insn->id = 0; // invalid ID for this "data" instruction
1191 insn->address = *address;
1192 insn->size = (uint16_t)MIN(skipdata_bytes, sizeof(insn->bytes));
1193 memcpy(insn->bytes, *code,
1194 MIN(skipdata_bytes, sizeof(insn->bytes)));
1195 #ifdef CAPSTONE_DIET
1196 insn->mnemonic[0] = '\0';
1197 insn->op_str[0] = '\0';
1198 #else
1199 strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1200 sizeof(insn->mnemonic) - 1);
1201 skipdata_opstr(insn->op_str, *code, skipdata_bytes);
1202 #endif
1203
1204 *code += skipdata_bytes;
1205 *size -= skipdata_bytes;
1206 *address += skipdata_bytes;
1207 }
1208
1209 return true;
1210 }
1211
1212 // return friendly name of register in a string
1213 CAPSTONE_EXPORT
1214 const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
1215 {
1216 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1217
1218 if (!handle || handle->reg_name == NULL) {
1219 return NULL;
1220 }
1221
1222 return handle->reg_name(ud, reg);
1223 }
1224
1225 CAPSTONE_EXPORT
1226 const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
1227 {
1228 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1229
1230 if (!handle || handle->insn_name == NULL) {
1231 return NULL;
1232 }
1233
1234 return handle->insn_name(ud, insn);
1235 }
1236
1237 CAPSTONE_EXPORT
1238 const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
1239 {
1240 struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1241
1242 if (!handle || handle->group_name == NULL) {
1243 return NULL;
1244 }
1245
1246 return handle->group_name(ud, group);
1247 }
1248
1249 CAPSTONE_EXPORT
1250 bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
1251 {
1252 struct cs_struct *handle;
1253 if (!ud)
1254 return false;
1255
1256 handle = (struct cs_struct *)(uintptr_t)ud;
1257
1258 if (!handle->detail) {
1259 handle->errnum = CS_ERR_DETAIL;
1260 return false;
1261 }
1262
1263 if (!insn->id) {
1264 handle->errnum = CS_ERR_SKIPDATA;
1265 return false;
1266 }
1267
1268 if (!insn->detail) {
1269 handle->errnum = CS_ERR_DETAIL;
1270 return false;
1271 }
1272
1273 return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
1274 }
1275
1276 CAPSTONE_EXPORT
1277 bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
1278 {
1279 struct cs_struct *handle;
1280 if (!ud)
1281 return false;
1282
1283 handle = (struct cs_struct *)(uintptr_t)ud;
1284
1285 if (!handle->detail) {
1286 handle->errnum = CS_ERR_DETAIL;
1287 return false;
1288 }
1289
1290 if (!insn->id) {
1291 handle->errnum = CS_ERR_SKIPDATA;
1292 return false;
1293 }
1294
1295 if (!insn->detail) {
1296 handle->errnum = CS_ERR_DETAIL;
1297 return false;
1298 }
1299
1300 return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
1301 }
1302
1303 CAPSTONE_EXPORT
1304 bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
1305 {
1306 struct cs_struct *handle;
1307 if (!ud)
1308 return false;
1309
1310 handle = (struct cs_struct *)(uintptr_t)ud;
1311
1312 if (!handle->detail) {
1313 handle->errnum = CS_ERR_DETAIL;
1314 return false;
1315 }
1316
1317 if (!insn->id) {
1318 handle->errnum = CS_ERR_SKIPDATA;
1319 return false;
1320 }
1321
1322 if (!insn->detail) {
1323 handle->errnum = CS_ERR_DETAIL;
1324 return false;
1325 }
1326
1327 return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
1328 }
1329
1330 CAPSTONE_EXPORT
1331 int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
1332 {
1333 struct cs_struct *handle;
1334 unsigned int count = 0, i;
1335 if (!ud)
1336 return -1;
1337
1338 handle = (struct cs_struct *)(uintptr_t)ud;
1339
1340 if (!handle->detail) {
1341 handle->errnum = CS_ERR_DETAIL;
1342 return -1;
1343 }
1344
1345 if (!insn->id) {
1346 handle->errnum = CS_ERR_SKIPDATA;
1347 return -1;
1348 }
1349
1350 if (!insn->detail) {
1351 handle->errnum = CS_ERR_DETAIL;
1352 return -1;
1353 }
1354
1355 handle->errnum = CS_ERR_OK;
1356
1357 switch (handle->arch) {
1358 default:
1359 handle->errnum = CS_ERR_HANDLE;
1360 return -1;
1361 case CS_ARCH_ARM:
1362 for (i = 0; i < insn->detail->arm.op_count; i++)
1363 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
1364 count++;
1365 break;
1366 case CS_ARCH_ARM64:
1367 for (i = 0; i < insn->detail->arm64.op_count; i++)
1368 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
1369 count++;
1370 break;
1371 case CS_ARCH_X86:
1372 for (i = 0; i < insn->detail->x86.op_count; i++)
1373 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
1374 count++;
1375 break;
1376 case CS_ARCH_MIPS:
1377 for (i = 0; i < insn->detail->mips.op_count; i++)
1378 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
1379 count++;
1380 break;
1381 case CS_ARCH_PPC:
1382 for (i = 0; i < insn->detail->ppc.op_count; i++)
1383 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
1384 count++;
1385 break;
1386 case CS_ARCH_SPARC:
1387 for (i = 0; i < insn->detail->sparc.op_count; i++)
1388 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1389 count++;
1390 break;
1391 case CS_ARCH_SYSZ:
1392 for (i = 0; i < insn->detail->sysz.op_count; i++)
1393 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1394 count++;
1395 break;
1396 case CS_ARCH_XCORE:
1397 for (i = 0; i < insn->detail->xcore.op_count; i++)
1398 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1399 count++;
1400 break;
1401 case CS_ARCH_M68K:
1402 for (i = 0; i < insn->detail->m68k.op_count; i++)
1403 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1404 count++;
1405 break;
1406 case CS_ARCH_TMS320C64X:
1407 for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1408 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1409 count++;
1410 break;
1411 case CS_ARCH_M680X:
1412 for (i = 0; i < insn->detail->m680x.op_count; i++)
1413 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1414 count++;
1415 break;
1416 case CS_ARCH_EVM:
1417 break;
1418 case CS_ARCH_MOS65XX:
1419 for (i = 0; i < insn->detail->mos65xx.op_count; i++)
1420 if (insn->detail->mos65xx.operands[i].type == (mos65xx_op_type)op_type)
1421 count++;
1422 break;
1423 case CS_ARCH_WASM:
1424 for (i = 0; i < insn->detail->wasm.op_count; i++)
1425 if (insn->detail->wasm.operands[i].type == (wasm_op_type)op_type)
1426 count++;
1427 break;
1428 case CS_ARCH_BPF:
1429 for (i = 0; i < insn->detail->bpf.op_count; i++)
1430 if (insn->detail->bpf.operands[i].type == (bpf_op_type)op_type)
1431 count++;
1432 break;
1433 case CS_ARCH_RISCV:
1434 for (i = 0; i < insn->detail->riscv.op_count; i++)
1435 if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type)
1436 count++;
1437 break;
1438 case CS_ARCH_TRICORE:
1439 for (i = 0; i < insn->detail->tricore.op_count; i++)
1440 if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
1441 count++;
1442 break;
1443 }
1444
1445 return count;
1446 }
1447
1448 CAPSTONE_EXPORT
1449 int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
1450 unsigned int post)
1451 {
1452 struct cs_struct *handle;
1453 unsigned int count = 0, i;
1454 if (!ud)
1455 return -1;
1456
1457 handle = (struct cs_struct *)(uintptr_t)ud;
1458
1459 if (!handle->detail) {
1460 handle->errnum = CS_ERR_DETAIL;
1461 return -1;
1462 }
1463
1464 if (!insn->id) {
1465 handle->errnum = CS_ERR_SKIPDATA;
1466 return -1;
1467 }
1468
1469 if (!insn->detail) {
1470 handle->errnum = CS_ERR_DETAIL;
1471 return -1;
1472 }
1473
1474 handle->errnum = CS_ERR_OK;
1475
1476 switch (handle->arch) {
1477 default:
1478 handle->errnum = CS_ERR_HANDLE;
1479 return -1;
1480 case CS_ARCH_ARM:
1481 for (i = 0; i < insn->detail->arm.op_count; i++) {
1482 if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
1483 count++;
1484 if (count == post)
1485 return i;
1486 }
1487 break;
1488 case CS_ARCH_ARM64:
1489 for (i = 0; i < insn->detail->arm64.op_count; i++) {
1490 if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
1491 count++;
1492 if (count == post)
1493 return i;
1494 }
1495 break;
1496 case CS_ARCH_X86:
1497 for (i = 0; i < insn->detail->x86.op_count; i++) {
1498 if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
1499 count++;
1500 if (count == post)
1501 return i;
1502 }
1503 break;
1504 case CS_ARCH_MIPS:
1505 for (i = 0; i < insn->detail->mips.op_count; i++) {
1506 if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
1507 count++;
1508 if (count == post)
1509 return i;
1510 }
1511 break;
1512 case CS_ARCH_PPC:
1513 for (i = 0; i < insn->detail->ppc.op_count; i++) {
1514 if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
1515 count++;
1516 if (count == post)
1517 return i;
1518 }
1519 break;
1520 case CS_ARCH_SPARC:
1521 for (i = 0; i < insn->detail->sparc.op_count; i++) {
1522 if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1523 count++;
1524 if (count == post)
1525 return i;
1526 }
1527 break;
1528 case CS_ARCH_SYSZ:
1529 for (i = 0; i < insn->detail->sysz.op_count; i++) {
1530 if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1531 count++;
1532 if (count == post)
1533 return i;
1534 }
1535 break;
1536 case CS_ARCH_XCORE:
1537 for (i = 0; i < insn->detail->xcore.op_count; i++) {
1538 if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1539 count++;
1540 if (count == post)
1541 return i;
1542 }
1543 break;
1544 case CS_ARCH_TRICORE:
1545 for (i = 0; i < insn->detail->tricore.op_count; i++) {
1546 if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
1547 count++;
1548 if (count == post)
1549 return i;
1550 }
1551 break;
1552 case CS_ARCH_M68K:
1553 for (i = 0; i < insn->detail->m68k.op_count; i++) {
1554 if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1555 count++;
1556 if (count == post)
1557 return i;
1558 }
1559 break;
1560 case CS_ARCH_TMS320C64X:
1561 for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1562 if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1563 count++;
1564 if (count == post)
1565 return i;
1566 }
1567 break;
1568 case CS_ARCH_M680X:
1569 for (i = 0; i < insn->detail->m680x.op_count; i++) {
1570 if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1571 count++;
1572 if (count == post)
1573 return i;
1574 }
1575 break;
1576 case CS_ARCH_EVM:
1577 #if 0
1578 for (i = 0; i < insn->detail->evm.op_count; i++) {
1579 if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1580 count++;
1581 if (count == post)
1582 return i;
1583 }
1584 #endif
1585 break;
1586 case CS_ARCH_MOS65XX:
1587 for (i = 0; i < insn->detail->mos65xx.op_count; i++) {
1588 if (insn->detail->mos65xx.operands[i].type == (mos65xx_op_type)op_type)
1589 count++;
1590 if (count == post)
1591 return i;
1592 }
1593 break;
1594 case CS_ARCH_WASM:
1595 for (i = 0; i < insn->detail->wasm.op_count; i++) {
1596 if (insn->detail->wasm.operands[i].type == (wasm_op_type)op_type)
1597 count++;
1598 if (count == post)
1599 return i;
1600 }
1601 break;
1602 case CS_ARCH_BPF:
1603 for (i = 0; i < insn->detail->bpf.op_count; i++) {
1604 if (insn->detail->bpf.operands[i].type == (bpf_op_type)op_type)
1605 count++;
1606 if (count == post)
1607 return i;
1608 }
1609 break;
1610 case CS_ARCH_RISCV:
1611 for (i = 0; i < insn->detail->riscv.op_count; i++) {
1612 if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type)
1613 count++;
1614 if (count == post)
1615 return i;
1616 }
1617 break;
1618 case CS_ARCH_SH:
1619 for (i = 0; i < insn->detail->sh.op_count; i++) {
1620 if (insn->detail->sh.operands[i].type == (sh_op_type)op_type)
1621 count++;
1622 if (count == post)
1623 return i;
1624 }
1625 break;
1626 }
1627
1628 return -1;
1629 }
1630
1631 CAPSTONE_EXPORT
1632 cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
1633 cs_regs regs_read, uint8_t *regs_read_count,
1634 cs_regs regs_write, uint8_t *regs_write_count)
1635 {
1636 struct cs_struct *handle;
1637
1638 if (!ud)
1639 return -1;
1640
1641 handle = (struct cs_struct *)(uintptr_t)ud;
1642
1643 #ifdef CAPSTONE_DIET
1644 // This API does not work in DIET mode
1645 handle->errnum = CS_ERR_DIET;
1646 return CS_ERR_DIET;
1647 #else
1648 if (!handle->detail) {
1649 handle->errnum = CS_ERR_DETAIL;
1650 return CS_ERR_DETAIL;
1651 }
1652
1653 if (!insn->id) {
1654 handle->errnum = CS_ERR_SKIPDATA;
1655 return CS_ERR_SKIPDATA;
1656 }
1657
1658 if (!insn->detail) {
1659 handle->errnum = CS_ERR_DETAIL;
1660 return CS_ERR_DETAIL;
1661 }
1662
1663 if (handle->reg_access) {
1664 handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1665 } else {
1666 // this arch is unsupported yet
1667 handle->errnum = CS_ERR_ARCH;
1668 return CS_ERR_ARCH;
1669 }
1670
1671 return CS_ERR_OK;
1672 #endif
1673 }