Coverage Report

Created: 2023-04-25 07:07

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