Coverage Report

Created: 2026-08-14 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/cs.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3
4
#include "SStream.h"
5
#if defined(CAPSTONE_HAS_OSXKERNEL)
6
#include <Availability.h>
7
#include <libkern/libkern.h>
8
#else
9
#include <stddef.h>
10
#include <stdio.h>
11
#include <stdlib.h>
12
#endif
13
14
#include <capstone/capstone.h>
15
#include <string.h>
16
17
#include "MCRegisterInfo.h"
18
#include "Mapping.h"
19
#include "utils.h"
20
21
#if defined(_KERNEL_MODE)
22
#include "windows\winkernel_mm.h"
23
#endif
24
25
// Issue #681: Windows kernel does not support formatting float point
26
#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
27
#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_AARCH64) || \
28
  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 \
32
  __FILE__ "(" CAPSTONE_STR(__LINE__) ") : warning message : "
33
34
#pragma message( \
35
  CAPSTONE_MSVC_WRANING_PREFIX \
36
  "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.")
37
38
#undef CAPSTONE_MSVC_WRANING_PREFIX
39
#undef CAPSTONE_STR
40
#undef CAPSTONE_STR_INTERNAL
41
#endif
42
#endif // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
43
44
#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && \
45
  !defined(_KERNEL_MODE)
46
42.6k
#define INSN_CACHE_SIZE 32
47
#else
48
// reduce stack variable size for kernel/firmware
49
#define INSN_CACHE_SIZE 8
50
#endif
51
52
// default SKIPDATA mnemonic
53
#ifndef CAPSTONE_DIET
54
42.6k
#define SKIPDATA_MNEM ".byte"
55
#else // No printing is available in diet mode
56
#define SKIPDATA_MNEM NULL
57
#endif
58
59
#include "arch/AArch64/AArch64Module.h"
60
#include "arch/ARC/ARCModule.h"
61
#include "arch/ARM/ARMModule.h"
62
#include "arch/Alpha/AlphaModule.h"
63
#include "arch/BPF/BPFModule.h"
64
#include "arch/EVM/EVMModule.h"
65
#include "arch/HPPA/HPPAModule.h"
66
#include "arch/LoongArch/LoongArchModule.h"
67
#include "arch/M680X/M680XModule.h"
68
#include "arch/M68K/M68KModule.h"
69
#include "arch/MOS65XX/MOS65XXModule.h"
70
#include "arch/Mips/MipsModule.h"
71
#include "arch/PowerPC/PPCModule.h"
72
#include "arch/RISCV/RISCVModule.h"
73
#include "arch/SH/SHModule.h"
74
#include "arch/Sparc/SparcModule.h"
75
#include "arch/SystemZ/SystemZModule.h"
76
#include "arch/TMS320C64x/TMS320C64xModule.h"
77
#include "arch/TriCore/TriCoreModule.h"
78
#include "arch/WASM/WASMModule.h"
79
#include "arch/X86/X86Module.h"
80
#include "arch/XCore/XCoreModule.h"
81
#include "arch/Xtensa/XtensaModule.h"
82
83
typedef struct cs_arch_config {
84
  // constructor initialization
85
  cs_err (*arch_init)(cs_struct *);
86
  // support cs_option()
87
  cs_err (*arch_option)(cs_struct *, cs_opt_type, size_t value);
88
  // bitmask for finding disallowed modes for an arch:
89
  // to be called in cs_open()/cs_option()
90
  cs_mode arch_disallowed_mode_mask;
91
} cs_arch_config;
92
93
#define CS_ARCH_CONFIG_ARM \
94
  { \
95
    ARM_global_init, \
96
    ARM_option, \
97
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_V8 | \
98
      CS_MODE_MCLASS | CS_MODE_THUMB | CS_MODE_BIG_ENDIAN), \
99
  }
100
#define CS_ARCH_CONFIG_AARCH64 \
101
  { \
102
    AArch64_global_init, \
103
    AArch64_option, \
104
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_BIG_ENDIAN | \
105
      CS_MODE_APPLE_PROPRIETARY), \
106
  }
107
#define CS_ARCH_CONFIG_MIPS \
108
  { \
109
    Mips_global_init, \
110
    Mips_option, \
111
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN | \
112
      CS_MODE_MIPS16 | CS_MODE_MIPS32 | CS_MODE_MIPS64 | \
113
      CS_MODE_MICRO | CS_MODE_MIPS1 | CS_MODE_MIPS2 | \
114
      CS_MODE_MIPS32R2 | CS_MODE_MIPS32R3 | CS_MODE_MIPS32R5 | \
115
      CS_MODE_MIPS32R6 | CS_MODE_MIPS3 | CS_MODE_MIPS4 | \
116
      CS_MODE_MIPS5 | CS_MODE_MIPS64R2 | CS_MODE_MIPS64R3 | \
117
      CS_MODE_MIPS64R5 | CS_MODE_MIPS64R6 | CS_MODE_OCTEON | \
118
      CS_MODE_OCTEONP | CS_MODE_NANOMIPS | CS_MODE_NMS1 | \
119
      CS_MODE_I7200 | CS_MODE_MIPS_NOFLOAT | CS_MODE_MIPS_PTR64), \
120
  }
121
#define CS_ARCH_CONFIG_X86 \
122
  { \
123
    X86_global_init, \
124
    X86_option, \
125
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | \
126
      CS_MODE_16), \
127
  }
128
#define CS_ARCH_CONFIG_PPC \
129
  { \
130
    PPC_global_init, \
131
    PPC_option, \
132
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | \
133
      CS_MODE_BIG_ENDIAN | CS_MODE_QPX | CS_MODE_PS | \
134
      CS_MODE_BOOKE | CS_MODE_SPE | CS_MODE_AIX_OS | \
135
      CS_MODE_PWR7 | CS_MODE_PWR8 | CS_MODE_PWR9 | CS_MODE_PWR10 | \
136
      CS_MODE_PPC_ISA_FUTURE | CS_MODE_MSYNC | \
137
      CS_MODE_MODERN_AIX_AS), \
138
  }
139
#define CS_ARCH_CONFIG_SPARC \
140
  { \
141
    Sparc_global_init, \
142
    Sparc_option, \
143
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN | CS_MODE_V9 | \
144
      CS_MODE_64 | CS_MODE_32), \
145
  }
146
#define CS_ARCH_CONFIG_SYSTEMZ \
147
  { \
148
    SystemZ_global_init, \
149
    SystemZ_option, \
150
    ~(CS_MODE_BIG_ENDIAN | CS_MODE_SYSTEMZ_ARCH8 | \
151
      CS_MODE_SYSTEMZ_ARCH9 | CS_MODE_SYSTEMZ_ARCH10 | \
152
      CS_MODE_SYSTEMZ_ARCH11 | CS_MODE_SYSTEMZ_ARCH12 | \
153
      CS_MODE_SYSTEMZ_ARCH13 | CS_MODE_SYSTEMZ_ARCH14 | \
154
      CS_MODE_SYSTEMZ_Z10 | CS_MODE_SYSTEMZ_Z196 | \
155
      CS_MODE_SYSTEMZ_ZEC12 | CS_MODE_SYSTEMZ_Z13 | \
156
      CS_MODE_SYSTEMZ_Z14 | CS_MODE_SYSTEMZ_Z15 | \
157
      CS_MODE_SYSTEMZ_Z16 | CS_MODE_SYSTEMZ_GENERIC), \
158
  }
159
#define CS_ARCH_CONFIG_XCORE \
160
  { \
161
    XCore_global_init, \
162
    XCore_option, \
163
    ~(CS_MODE_BIG_ENDIAN), \
164
  }
165
#define CS_ARCH_CONFIG_M68K \
166
  { \
167
    M68K_global_init, \
168
    M68K_option, \
169
    ~(CS_MODE_BIG_ENDIAN | CS_MODE_M68K_FEATURE_MASK), \
170
  }
171
#define CS_ARCH_CONFIG_TMS320C64X \
172
  { \
173
    TMS320C64x_global_init, \
174
    TMS320C64x_option, \
175
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN), \
176
  }
177
#define CS_ARCH_CONFIG_M680X \
178
  { \
179
    M680X_global_init, \
180
    M680X_option, \
181
    ~(CS_MODE_M680X_6301 | CS_MODE_M680X_6309 | \
182
      CS_MODE_M680X_6800 | CS_MODE_M680X_6801 | \
183
      CS_MODE_M680X_6805 | CS_MODE_M680X_6808 | \
184
      CS_MODE_M680X_6809 | CS_MODE_M680X_6811 | \
185
      CS_MODE_M680X_CPU12 | CS_MODE_M680X_HCS08 | \
186
      CS_MODE_M680X_RS08 | CS_MODE_M680X_HCS12X), \
187
  }
188
#define CS_ARCH_CONFIG_EVM \
189
  { \
190
    EVM_global_init, \
191
    EVM_option, \
192
    0, \
193
  }
194
#define CS_ARCH_CONFIG_MOS65XX \
195
  { \
196
    MOS65XX_global_init, \
197
    MOS65XX_option, \
198
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_MOS65XX_6502 | \
199
      CS_MODE_MOS65XX_65C02 | CS_MODE_MOS65XX_W65C02 | \
200
      CS_MODE_MOS65XX_65816_LONG_MX), \
201
  }
202
#define CS_ARCH_CONFIG_WASM \
203
  { \
204
    WASM_global_init, \
205
    WASM_option, \
206
    0, \
207
  }
208
#define CS_ARCH_CONFIG_BPF \
209
  { \
210
    BPF_global_init, \
211
    BPF_option, \
212
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BPF_CLASSIC | \
213
      CS_MODE_BPF_EXTENDED | CS_MODE_BIG_ENDIAN), \
214
  }
215
#define CS_ARCH_CONFIG_RISCV \
216
  { \
217
    RISCV_global_init, \
218
    RISCV_option, \
219
    ~(CS_MODE_RISCV32 | CS_MODE_RISCV64 | CS_MODE_RISCV_C | \
220
      CS_MODE_RISCV_FD | CS_MODE_RISCV_V | CS_MODE_RISCV_ZFINX | \
221
      CS_MODE_RISCV_ZCMP_ZCMT_ZCE | CS_MODE_RISCV_ZICFISS | \
222
      CS_MODE_RISCV_E | CS_MODE_RISCV_A | CS_MODE_RISCV_COREV | \
223
      CS_MODE_RISCV_SIFIVE | CS_MODE_RISCV_THEAD | \
224
      CS_MODE_RISCV_VENTANA | CS_MODE_RISCV_ZBA | \
225
      CS_MODE_RISCV_ZBB | CS_MODE_RISCV_ZBC | CS_MODE_RISCV_ZBKB | \
226
      CS_MODE_RISCV_ZBKC | CS_MODE_RISCV_ZBKX | \
227
      CS_MODE_RISCV_ZBS), \
228
  }
229
#define CS_ARCH_CONFIG_SH \
230
  { \
231
    SH_global_init, \
232
    SH_option, \
233
    ~(CS_MODE_SH2 | CS_MODE_SH2A | CS_MODE_SH3 | CS_MODE_SH4 | \
234
      CS_MODE_SH4A | CS_MODE_SHFPU | CS_MODE_SHDSP | \
235
      CS_MODE_BIG_ENDIAN), \
236
  }
237
#define CS_ARCH_CONFIG_TRICORE \
238
  { \
239
    TRICORE_global_init, \
240
    TRICORE_option, \
241
    ~(CS_MODE_TRICORE_110 | CS_MODE_TRICORE_120 | \
242
      CS_MODE_TRICORE_130 | CS_MODE_TRICORE_131 | \
243
      CS_MODE_TRICORE_160 | CS_MODE_TRICORE_161 | \
244
      CS_MODE_TRICORE_162 | CS_MODE_TRICORE_180 | \
245
      CS_MODE_LITTLE_ENDIAN), \
246
  }
247
#define CS_ARCH_CONFIG_ALPHA \
248
  { \
249
    ALPHA_global_init, \
250
    ALPHA_option, \
251
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN), \
252
  }
253
#define CS_ARCH_CONFIG_LOONGARCH \
254
  { \
255
    LoongArch_global_init, \
256
    LoongArch_option, \
257
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_LOONGARCH32 | \
258
      CS_MODE_LOONGARCH64), \
259
  }
260
#define CS_ARCH_CONFIG_XTENSA \
261
  { \
262
    Xtensa_global_init, \
263
    Xtensa_option, \
264
    ~(CS_MODE_XTENSA_ESP32 | CS_MODE_XTENSA_ESP32S2 | \
265
      CS_MODE_XTENSA_ESP8266 | CS_MODE_XTENSA_ESP32S3), \
266
  }
267
268
#define CS_ARCH_CONFIG_ARC \
269
  { \
270
    ARC_global_init, \
271
    ARC_option, \
272
    ~(CS_MODE_LITTLE_ENDIAN), \
273
  }
274
275
#ifdef CAPSTONE_USE_ARCH_REGISTRATION
276
static cs_arch_config arch_configs[MAX_ARCH];
277
static uint32_t all_arch;
278
#else
279
static const cs_arch_config arch_configs[MAX_ARCH] = {
280
#ifdef CAPSTONE_HAS_ARM
281
  CS_ARCH_CONFIG_ARM,
282
#else
283
  { NULL, NULL, 0 },
284
#endif
285
#ifdef CAPSTONE_HAS_AARCH64
286
  CS_ARCH_CONFIG_AARCH64,
287
#else
288
  { NULL, NULL, 0 },
289
#endif
290
#ifdef CAPSTONE_HAS_SYSTEMZ
291
  CS_ARCH_CONFIG_SYSTEMZ,
292
#else
293
  { NULL, NULL, 0 },
294
#endif
295
#ifdef CAPSTONE_HAS_MIPS
296
  CS_ARCH_CONFIG_MIPS,
297
#else
298
  { NULL, NULL, 0 },
299
#endif
300
#ifdef CAPSTONE_HAS_X86
301
  CS_ARCH_CONFIG_X86,
302
#else
303
  { NULL, NULL, 0 },
304
#endif
305
#ifdef CAPSTONE_HAS_POWERPC
306
  CS_ARCH_CONFIG_PPC,
307
#else
308
  { NULL, NULL, 0 },
309
#endif
310
#ifdef CAPSTONE_HAS_SPARC
311
  CS_ARCH_CONFIG_SPARC,
312
#else
313
  { NULL, NULL, 0 },
314
#endif
315
#ifdef CAPSTONE_HAS_XCORE
316
  CS_ARCH_CONFIG_XCORE,
317
#else
318
  { NULL, NULL, 0 },
319
#endif
320
#ifdef CAPSTONE_HAS_M68K
321
  CS_ARCH_CONFIG_M68K,
322
#else
323
  { NULL, NULL, 0 },
324
#endif
325
#ifdef CAPSTONE_HAS_TMS320C64X
326
  CS_ARCH_CONFIG_TMS320C64X,
327
#else
328
  { NULL, NULL, 0 },
329
#endif
330
#ifdef CAPSTONE_HAS_M680X
331
  CS_ARCH_CONFIG_M680X,
332
#else
333
  { NULL, NULL, 0 },
334
#endif
335
#ifdef CAPSTONE_HAS_EVM
336
  CS_ARCH_CONFIG_EVM,
337
#else
338
  { NULL, NULL, 0 },
339
#endif
340
#ifdef CAPSTONE_HAS_MOS65XX
341
  CS_ARCH_CONFIG_MOS65XX,
342
#else
343
  { NULL, NULL, 0 },
344
#endif
345
#ifdef CAPSTONE_HAS_WASM
346
  CS_ARCH_CONFIG_WASM,
347
#else
348
  { NULL, NULL, 0 },
349
#endif
350
#ifdef CAPSTONE_HAS_BPF
351
  CS_ARCH_CONFIG_BPF,
352
#else
353
  { NULL, NULL, 0 },
354
#endif
355
#ifdef CAPSTONE_HAS_RISCV
356
  CS_ARCH_CONFIG_RISCV,
357
#else
358
  { NULL, NULL, 0 },
359
#endif
360
#ifdef CAPSTONE_HAS_SH
361
  CS_ARCH_CONFIG_SH,
362
#else
363
  { NULL, NULL, 0 },
364
#endif
365
#ifdef CAPSTONE_HAS_TRICORE
366
  CS_ARCH_CONFIG_TRICORE,
367
#else
368
  { NULL, NULL, 0 },
369
#endif
370
#ifdef CAPSTONE_HAS_ALPHA
371
  CS_ARCH_CONFIG_ALPHA,
372
#else
373
  { NULL, NULL, 0 },
374
#endif
375
#ifdef CAPSTONE_HAS_HPPA
376
  {
377
    HPPA_global_init,
378
    HPPA_option,
379
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN | CS_MODE_HPPA_11 |
380
      CS_MODE_HPPA_20 | CS_MODE_HPPA_20W),
381
  },
382
#else
383
  { NULL, NULL, 0 },
384
#endif
385
#ifdef CAPSTONE_HAS_LOONGARCH
386
  CS_ARCH_CONFIG_LOONGARCH,
387
#else
388
  { NULL, NULL, 0 },
389
#endif
390
#ifdef CAPSTONE_HAS_XTENSA
391
  CS_ARCH_CONFIG_XTENSA,
392
#else
393
  { NULL, NULL, 0 },
394
#endif
395
#ifdef CAPSTONE_HAS_ARC
396
  CS_ARCH_CONFIG_ARC,
397
#else
398
  { NULL, NULL, 0 },
399
#endif
400
};
401
402
// bitmask of enabled architectures
403
static const uint32_t all_arch = 0
404
#ifdef CAPSTONE_HAS_ARM
405
         | (1 << CS_ARCH_ARM)
406
#endif
407
#if defined(CAPSTONE_HAS_AARCH64) || defined(CAPSTONE_HAS_ARM64)
408
         | (1 << CS_ARCH_AARCH64)
409
#endif
410
#ifdef CAPSTONE_HAS_MIPS
411
         | (1 << CS_ARCH_MIPS)
412
#endif
413
#ifdef CAPSTONE_HAS_X86
414
         | (1 << CS_ARCH_X86)
415
#endif
416
#ifdef CAPSTONE_HAS_POWERPC
417
         | (1 << CS_ARCH_PPC)
418
#endif
419
#ifdef CAPSTONE_HAS_SPARC
420
         | (1 << CS_ARCH_SPARC)
421
#endif
422
#ifdef CAPSTONE_HAS_SYSTEMZ
423
         | (1 << CS_ARCH_SYSTEMZ)
424
#endif
425
#ifdef CAPSTONE_HAS_XCORE
426
         | (1 << CS_ARCH_XCORE)
427
#endif
428
#ifdef CAPSTONE_HAS_M68K
429
         | (1 << CS_ARCH_M68K)
430
#endif
431
#ifdef CAPSTONE_HAS_TMS320C64X
432
         | (1 << CS_ARCH_TMS320C64X)
433
#endif
434
#ifdef CAPSTONE_HAS_M680X
435
         | (1 << CS_ARCH_M680X)
436
#endif
437
#ifdef CAPSTONE_HAS_EVM
438
         | (1 << CS_ARCH_EVM)
439
#endif
440
#ifdef CAPSTONE_HAS_MOS65XX
441
         | (1 << CS_ARCH_MOS65XX)
442
#endif
443
#ifdef CAPSTONE_HAS_WASM
444
         | (1 << CS_ARCH_WASM)
445
#endif
446
#ifdef CAPSTONE_HAS_BPF
447
         | (1 << CS_ARCH_BPF)
448
#endif
449
#ifdef CAPSTONE_HAS_RISCV
450
         | (1 << CS_ARCH_RISCV)
451
#endif
452
#ifdef CAPSTONE_HAS_SH
453
         | (1 << CS_ARCH_SH)
454
#endif
455
#ifdef CAPSTONE_HAS_TRICORE
456
         | (1 << CS_ARCH_TRICORE)
457
#endif
458
#ifdef CAPSTONE_HAS_ALPHA
459
         | (1 << CS_ARCH_ALPHA)
460
#endif
461
#ifdef CAPSTONE_HAS_HPPA
462
         | (1 << CS_ARCH_HPPA)
463
#endif
464
#ifdef CAPSTONE_HAS_LOONGARCH
465
         | (1 << CS_ARCH_LOONGARCH)
466
#endif
467
#ifdef CAPSTONE_HAS_XTENSA
468
         | (1 << CS_ARCH_XTENSA)
469
#endif
470
#ifdef CAPSTONE_HAS_ARC
471
         | (1 << CS_ARCH_ARC)
472
#endif
473
  ;
474
#endif
475
476
#if defined(CAPSTONE_USE_SYS_DYN_MEM)
477
#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
478
// default
479
cs_malloc_t cs_mem_malloc = malloc;
480
cs_calloc_t cs_mem_calloc = calloc;
481
cs_realloc_t cs_mem_realloc = realloc;
482
cs_free_t cs_mem_free = free;
483
#if defined(_WIN32_WCE)
484
cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
485
#else
486
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
487
#endif // defined(_WIN32_WCE)
488
489
#elif defined(_KERNEL_MODE)
490
// Windows driver
491
cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
492
cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
493
cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
494
cs_free_t cs_mem_free = cs_winkernel_free;
495
cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
496
#else
497
// OSX kernel
498
extern void *kern_os_malloc(size_t size);
499
extern void kern_os_free(void *addr);
500
extern void *kern_os_realloc(void *addr, size_t nsize);
501
502
static void *cs_kern_os_calloc(size_t num, size_t size)
503
{
504
  size_t alloc = num * size;
505
  if (num && size != alloc / num) {
506
    return NULL; // overflow check
507
  }
508
  return kern_os_malloc(alloc); // malloc bzeroes the buffer
509
}
510
511
cs_malloc_t cs_mem_malloc = kern_os_malloc;
512
cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
513
cs_realloc_t cs_mem_realloc = kern_os_realloc;
514
cs_free_t cs_mem_free = kern_os_free;
515
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
516
#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
517
#else
518
// User-defined
519
cs_malloc_t cs_mem_malloc = NULL;
520
cs_calloc_t cs_mem_calloc = NULL;
521
cs_realloc_t cs_mem_realloc = NULL;
522
cs_free_t cs_mem_free = NULL;
523
cs_vsnprintf_t cs_vsnprintf = NULL;
524
525
#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
526
527
CAPSTONE_EXPORT
528
unsigned int CAPSTONE_API cs_version(int *major, int *minor)
529
0
{
530
0
  if (major != NULL && minor != NULL) {
531
0
    *major = CS_API_MAJOR;
532
0
    *minor = CS_API_MINOR;
533
0
  }
534
535
0
  return (CS_API_MAJOR << 8) + CS_API_MINOR;
536
0
}
537
538
#define CS_ARCH_REGISTER(id) \
539
  cs_arch_config cfg = CS_ARCH_CONFIG_##id; \
540
  arch_configs[CS_ARCH_##id] = cfg; \
541
  all_arch |= 1 << CS_ARCH_##id
542
543
CAPSTONE_EXPORT
544
void CAPSTONE_API cs_arch_register_arm(void)
545
0
{
546
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ARM)
547
  CS_ARCH_REGISTER(ARM);
548
#endif
549
0
}
550
551
CAPSTONE_EXPORT
552
void CAPSTONE_API cs_arch_register_aarch64(void)
553
0
{
554
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_AARCH64)
555
  CS_ARCH_REGISTER(AARCH64);
556
#endif
557
0
}
558
559
CAPSTONE_EXPORT
560
void CAPSTONE_API cs_arch_register_mips(void)
561
0
{
562
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_MIPS)
563
  CS_ARCH_REGISTER(MIPS);
564
#endif
565
0
}
566
567
CAPSTONE_EXPORT
568
void CAPSTONE_API cs_arch_register_x86(void)
569
0
{
570
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_X86)
571
  CS_ARCH_REGISTER(X86);
572
#endif
573
0
}
574
575
CAPSTONE_EXPORT
576
void CAPSTONE_API cs_arch_register_powerpc(void)
577
0
{
578
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_POWERPC)
579
  CS_ARCH_REGISTER(PPC);
580
#endif
581
0
}
582
583
CAPSTONE_EXPORT
584
void CAPSTONE_API cs_arch_register_sparc(void)
585
0
{
586
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SPARC)
587
  CS_ARCH_REGISTER(SPARC);
588
#endif
589
0
}
590
591
CAPSTONE_EXPORT
592
void CAPSTONE_API cs_arch_register_systemz(void)
593
0
{
594
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SYSTEMZ)
595
  CS_ARCH_REGISTER(SYSTEMZ);
596
#endif
597
0
}
598
599
CAPSTONE_EXPORT
600
void CAPSTONE_API cs_arch_register_xcore(void)
601
0
{
602
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_XCORE)
603
  CS_ARCH_REGISTER(XCORE);
604
#endif
605
0
}
606
607
CAPSTONE_EXPORT
608
void CAPSTONE_API cs_arch_register_m68k(void)
609
0
{
610
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_M68K)
611
  CS_ARCH_REGISTER(M68K);
612
#endif
613
0
}
614
615
CAPSTONE_EXPORT
616
void CAPSTONE_API cs_arch_register_tms320c64x(void)
617
0
{
618
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_TMS320C64X)
619
  CS_ARCH_REGISTER(TMS320C64X);
620
#endif
621
0
}
622
623
CAPSTONE_EXPORT
624
void CAPSTONE_API cs_arch_register_m680x(void)
625
0
{
626
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_M680X)
627
  CS_ARCH_REGISTER(M680X);
628
#endif
629
0
}
630
631
CAPSTONE_EXPORT
632
void CAPSTONE_API cs_arch_register_evm(void)
633
0
{
634
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_EVM)
635
  CS_ARCH_REGISTER(EVM);
636
#endif
637
0
}
638
639
CAPSTONE_EXPORT
640
void CAPSTONE_API cs_arch_register_mos65xx(void)
641
0
{
642
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_MOS65XX)
643
  CS_ARCH_REGISTER(MOS65XX);
644
#endif
645
0
}
646
647
CAPSTONE_EXPORT
648
void CAPSTONE_API cs_arch_register_wasm(void)
649
0
{
650
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_WASM)
651
  CS_ARCH_REGISTER(WASM);
652
#endif
653
0
}
654
655
CAPSTONE_EXPORT
656
void CAPSTONE_API cs_arch_register_bpf(void)
657
0
{
658
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_BPF)
659
  CS_ARCH_REGISTER(BPF);
660
#endif
661
0
}
662
663
CAPSTONE_EXPORT
664
void CAPSTONE_API cs_arch_register_riscv(void)
665
0
{
666
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_RISCV)
667
  CS_ARCH_REGISTER(RISCV);
668
#endif
669
0
}
670
671
CAPSTONE_EXPORT
672
void CAPSTONE_API cs_arch_register_sh(void)
673
0
{
674
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SH)
675
  CS_ARCH_REGISTER(SH);
676
#endif
677
0
}
678
679
CAPSTONE_EXPORT
680
void CAPSTONE_API cs_arch_register_tricore(void)
681
0
{
682
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_TRICORE)
683
  CS_ARCH_REGISTER(TRICORE);
684
#endif
685
0
}
686
687
CAPSTONE_EXPORT
688
void CAPSTONE_API cs_arch_register_alpha(void)
689
0
{
690
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ALPHA)
691
  CS_ARCH_REGISTER(ALPHA);
692
#endif
693
0
}
694
695
CAPSTONE_EXPORT
696
void CAPSTONE_API cs_arch_register_loongarch(void)
697
0
{
698
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_LOONGARCH)
699
  CS_ARCH_REGISTER(LOONGARCH);
700
#endif
701
0
}
702
703
CAPSTONE_EXPORT
704
void CAPSTONE_API cs_arch_register_arc(void)
705
0
{
706
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ARC)
707
  CS_ARCH_REGISTER(ARC);
708
#endif
709
0
}
710
711
CAPSTONE_EXPORT
712
bool CAPSTONE_API cs_support(int query)
713
0
{
714
0
  if (query == CS_ARCH_ALL)
715
0
    return all_arch ==
716
0
           ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_AARCH64) |
717
0
      (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
718
0
      (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
719
0
      (1 << CS_ARCH_SYSTEMZ) | (1 << CS_ARCH_XCORE) |
720
0
      (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
721
0
      (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM) |
722
0
      (1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX) |
723
0
      (1 << CS_ARCH_WASM) | (1 << CS_ARCH_BPF) |
724
0
      (1 << CS_ARCH_SH) | (1 << CS_ARCH_TRICORE) |
725
0
      (1 << CS_ARCH_ALPHA) | (1 << CS_ARCH_HPPA) |
726
0
      (1 << CS_ARCH_LOONGARCH) | (1 << CS_ARCH_XTENSA) |
727
0
      (1 << CS_ARCH_ARC));
728
729
0
  if ((unsigned int)query < CS_ARCH_MAX)
730
0
    return all_arch & (1 << query);
731
732
0
  if (query == CS_SUPPORT_DIET) {
733
#ifdef CAPSTONE_DIET
734
    return true;
735
#else
736
0
    return false;
737
0
#endif
738
0
  }
739
740
0
  if (query == CS_SUPPORT_X86_REDUCE) {
741
#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
742
    return true;
743
#else
744
0
    return false;
745
0
#endif
746
0
  }
747
748
  // unsupported query
749
0
  return false;
750
0
}
751
752
CAPSTONE_EXPORT
753
cs_err CAPSTONE_API cs_errno(csh handle)
754
0
{
755
0
  struct cs_struct *ud;
756
0
  if (!handle)
757
0
    return CS_ERR_CSH;
758
759
0
  ud = (struct cs_struct *)(uintptr_t)handle;
760
761
0
  return ud->errnum;
762
0
}
763
764
CAPSTONE_EXPORT
765
const char *CAPSTONE_API cs_strerror(cs_err code)
766
0
{
767
0
  switch (code) {
768
0
  default:
769
0
    return "Unknown error code";
770
0
  case CS_ERR_OK:
771
0
    return "OK (CS_ERR_OK)";
772
0
  case CS_ERR_MEM:
773
0
    return "Out of memory (CS_ERR_MEM)";
774
0
  case CS_ERR_ARCH:
775
0
    return "Invalid/unsupported architecture(CS_ERR_ARCH)";
776
0
  case CS_ERR_HANDLE:
777
0
    return "Invalid handle (CS_ERR_HANDLE)";
778
0
  case CS_ERR_CSH:
779
0
    return "Invalid csh (CS_ERR_CSH)";
780
0
  case CS_ERR_MODE:
781
0
    return "Invalid mode (CS_ERR_MODE)";
782
0
  case CS_ERR_OPTION:
783
0
    return "Invalid option (CS_ERR_OPTION)";
784
0
  case CS_ERR_DETAIL:
785
0
    return "Details are unavailable (CS_ERR_DETAIL)";
786
0
  case CS_ERR_MEMSETUP:
787
0
    return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
788
0
  case CS_ERR_VERSION:
789
0
    return "Different API version between core & binding (CS_ERR_VERSION)";
790
0
  case CS_ERR_DIET:
791
0
    return "Information irrelevant in diet engine (CS_ERR_DIET)";
792
0
  case CS_ERR_SKIPDATA:
793
0
    return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
794
0
  case CS_ERR_X86_ATT:
795
0
    return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
796
0
  case CS_ERR_X86_INTEL:
797
0
    return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
798
0
  case CS_ERR_X86_MASM:
799
0
    return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
800
0
  }
801
0
}
802
803
CAPSTONE_EXPORT
804
cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
805
42.6k
{
806
42.6k
  cs_err err = CS_ERR_ARCH;
807
42.6k
  struct cs_struct *ud = NULL;
808
809
42.6k
  if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc ||
810
42.6k
      !cs_mem_free || !cs_vsnprintf) {
811
    // Error: before cs_open(), dynamic memory management must be initialized
812
    // with cs_option(CS_OPT_MEM)
813
0
    err = CS_ERR_MEMSETUP;
814
0
    goto fail;
815
0
  }
816
817
42.6k
  if (arch >= CS_ARCH_MAX || !arch_configs[arch].arch_init) {
818
0
    err = CS_ERR_ARCH;
819
0
    goto fail;
820
0
  }
821
822
  // verify if requested mode is valid
823
42.6k
  if (mode & arch_configs[arch].arch_disallowed_mode_mask) {
824
7
    err = CS_ERR_MODE;
825
7
    goto fail;
826
7
  }
827
828
42.6k
  ud = cs_mem_calloc(1, sizeof(*ud));
829
42.6k
  if (!ud) {
830
0
    err = CS_ERR_MEM;
831
0
    goto fail;
832
0
  }
833
834
42.6k
  ud->errnum = CS_ERR_OK;
835
42.6k
  ud->arch = arch;
836
42.6k
  ud->mode = mode;
837
  // by default, do not break instruction into details
838
42.6k
  ud->detail_opt = CS_OPT_OFF;
839
42.6k
  ud->PrintBranchImmAsAddress = true;
840
841
  // default skipdata setup
842
42.6k
  ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
843
844
42.6k
  if ((err = arch_configs[ud->arch].arch_init(ud)))
845
0
    goto fail;
846
847
42.6k
  if ((err = populate_insn_map_cache(ud)))
848
0
    goto fail;
849
850
42.6k
  *handle = (uintptr_t)ud;
851
42.6k
  return CS_ERR_OK;
852
853
7
fail:
854
7
  if (ud) {
855
0
    cs_mem_free(ud);
856
0
  }
857
7
  *handle = 0;
858
7
  return err;
859
42.6k
}
860
861
CAPSTONE_EXPORT
862
cs_err CAPSTONE_API cs_close(csh *handle)
863
81.0k
{
864
81.0k
  struct cs_struct *ud = NULL;
865
81.0k
  struct insn_mnem *next = NULL, *tmp = NULL;
866
867
81.0k
  if (*handle == 0)
868
    // invalid handle
869
0
    return CS_ERR_CSH;
870
871
81.0k
  ud = (struct cs_struct *)(*handle);
872
873
81.0k
  if (ud->printer_info)
874
76.7k
    cs_mem_free(ud->printer_info);
875
876
  // free the linked list of customized mnemonic
877
81.0k
  tmp = ud->mnem_list;
878
81.0k
  while (tmp) {
879
0
    next = tmp->next;
880
0
    cs_mem_free(tmp);
881
0
    tmp = next;
882
0
  }
883
884
81.0k
  cs_mem_free(ud->insn_cache);
885
81.0k
  cs_mem_free(ud->x86_insn_lut);
886
81.0k
  cs_mem_free(ud->x86_insn_reg_lut);
887
888
81.0k
  memset(ud, 0, sizeof(*ud));
889
81.0k
  cs_mem_free(ud);
890
891
  // invalidate this handle by ZERO out its value.
892
  // this is to make sure it is unusable after cs_close()
893
81.0k
  *handle = 0;
894
895
81.0k
  return CS_ERR_OK;
896
81.0k
}
897
898
/// replace str1 in target with str2; target starts with str1
899
/// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
900
/// return 0 on success, -1 on failure
901
#ifndef CAPSTONE_DIET
902
static int str_replace(char *result, char *target, const char *str1, char *str2)
903
0
{
904
0
  size_t target_len = strlen(target);
905
0
  size_t str1_len = strlen(str1);
906
0
  if (target_len < str1_len) {
907
0
    return -1;
908
0
  }
909
910
  // only perform replacement if the output fits into result
911
0
  if (target_len - str1_len + strlen(str2) <= CS_MNEMONIC_SIZE - 1) {
912
    // copy str2 to beginning of result
913
    // skip str1 - already replaced by str2
914
0
    snprintf(result, CS_MNEMONIC_SIZE, "%s%s", str2,
915
0
       target + str1_len);
916
917
0
    return 0;
918
0
  } else
919
0
    return -1;
920
0
}
921
#endif
922
923
// fill insn with mnemonic & operands info
924
static void fill_insn(struct cs_struct *handle, cs_insn *insn, SStream *OS,
925
          MCInst *mci, PostPrinter_t postprinter,
926
          const uint8_t *code)
927
3.18M
{
928
3.18M
#ifndef CAPSTONE_DIET
929
3.18M
  char *sp;
930
3.18M
#endif
931
3.18M
  SStream_trimls(OS);
932
3.18M
  uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
933
934
  // fill the instruction bytes.
935
  // we might skip some redundant bytes in front in the case of X86
936
3.18M
  memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
937
3.18M
  insn->op_str[0] = '\0';
938
3.18M
  insn->size = copy_size;
939
940
  // alias instruction might have ID saved in OpcodePub
941
3.18M
  if (MCInst_getOpcodePub(mci))
942
117k
    insn->id = MCInst_getOpcodePub(mci);
943
944
  // post printer handles some corner cases (hacky)
945
3.18M
  if (postprinter)
946
843k
    postprinter((csh)handle, insn, OS, mci);
947
948
3.18M
#ifndef CAPSTONE_DIET
949
3.18M
  memset(insn->mnemonic, '\0', sizeof(insn->mnemonic));
950
3.18M
  memset(insn->op_str, '\0', sizeof(insn->op_str));
951
3.18M
  SStream_extract_mnem_opstr(OS, insn->mnemonic, sizeof(insn->mnemonic),
952
3.18M
           insn->op_str, sizeof(insn->op_str));
953
17.8M
  for (sp = insn->mnemonic; *sp; sp++) {
954
14.6M
    if (*sp == '|') // lock|rep prefix for x86
955
83.8k
      *sp = ' ';
956
14.6M
  }
957
958
  // we might have customized mnemonic
959
3.18M
  if (handle->mnem_list) {
960
0
    struct insn_mnem *tmp = handle->mnem_list;
961
0
    while (tmp) {
962
0
      if (tmp->insn.id == insn->id) {
963
0
        char str[CS_MNEMONIC_SIZE] = { 0 };
964
965
0
        if (!str_replace(str, insn->mnemonic,
966
0
             cs_insn_name((csh)handle,
967
0
                    insn->id),
968
0
             tmp->insn.mnemonic)) {
969
          // copy result to mnemonic
970
0
          CS_ASSERT_RET(sizeof(insn->mnemonic) ==
971
0
                  sizeof(str));
972
0
          (void)memcpy(insn->mnemonic, str,
973
0
                 sizeof(insn->mnemonic));
974
0
          insn->mnemonic[sizeof(insn->mnemonic) -
975
0
                   1] = '\0';
976
0
        }
977
978
0
        break;
979
0
      }
980
0
      tmp = tmp->next;
981
0
    }
982
0
  }
983
3.18M
#endif
984
3.18M
}
985
986
// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
987
// this very much depends on instruction alignment requirement of each arch.
988
static uint8_t skipdata_size(cs_struct *handle)
989
0
{
990
0
  switch (handle->arch) {
991
0
  default:
992
    // should never reach
993
0
    return (uint8_t)-1;
994
0
  case CS_ARCH_ARM:
995
    // skip 2 bytes on Thumb mode.
996
0
    if (handle->mode & CS_MODE_THUMB)
997
0
      return 2;
998
    // otherwise, skip 4 bytes
999
0
    return 4;
1000
0
  case CS_ARCH_AARCH64:
1001
0
  case CS_ARCH_MIPS:
1002
0
  case CS_ARCH_PPC:
1003
0
  case CS_ARCH_SPARC:
1004
    // skip 4 bytes
1005
0
    return 4;
1006
0
  case CS_ARCH_SYSTEMZ:
1007
    // SystemZ instruction's length can be 2, 4 or 6 bytes,
1008
    // so we just skip 2 bytes
1009
0
    return 2;
1010
0
  case CS_ARCH_X86:
1011
    // X86 has no restriction on instruction alignment
1012
0
    return 1;
1013
0
  case CS_ARCH_XCORE:
1014
    // XCore instruction's length can be 2 or 4 bytes,
1015
    // so we just skip 2 bytes
1016
0
    return 2;
1017
0
  case CS_ARCH_M68K:
1018
    // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
1019
0
    return 2;
1020
0
  case CS_ARCH_TMS320C64X:
1021
    // TMS320C64x alignment is 4.
1022
0
    return 4;
1023
0
  case CS_ARCH_M680X:
1024
    // M680X alignment is 1.
1025
0
    return 1;
1026
0
  case CS_ARCH_EVM:
1027
    // EVM alignment is 1.
1028
0
    return 1;
1029
0
  case CS_ARCH_WASM:
1030
    //WASM alignment is 1
1031
0
    return 1;
1032
0
  case CS_ARCH_MOS65XX:
1033
    // MOS65XX alignment is 1.
1034
0
    return 1;
1035
0
  case CS_ARCH_BPF:
1036
    // both classic and extended BPF have alignment 8.
1037
0
    return 8;
1038
0
  case CS_ARCH_RISCV:
1039
    // special compress mode
1040
0
    if (handle->mode & CS_MODE_RISCV_C)
1041
0
      return 2;
1042
0
    return 4;
1043
0
  case CS_ARCH_SH:
1044
0
    return 2;
1045
0
  case CS_ARCH_TRICORE:
1046
    // TriCore instruction's length can be 2 or 4 bytes,
1047
    // so we just skip 2 bytes
1048
0
    return 2;
1049
0
  case CS_ARCH_ALPHA:
1050
    // Alpha alignment is 4.
1051
0
    return 4;
1052
0
  case CS_ARCH_HPPA:
1053
    // Hppa alignment is 4.
1054
0
    return 4;
1055
0
  case CS_ARCH_LOONGARCH:
1056
    // LoongArch alignment is 4.
1057
0
    return 4;
1058
0
  case CS_ARCH_ARC:
1059
    // ARC instruction's length can be 2, 4, 6 or 8 bytes,
1060
    // therefore, skip 2 bytes
1061
0
    return 2;
1062
0
  }
1063
0
}
1064
1065
CAPSTONE_EXPORT
1066
cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, uintptr_t value)
1067
50.3k
{
1068
50.3k
  struct cs_struct *handle;
1069
50.3k
  cs_opt_mnem *opt;
1070
1071
  // cs_option() can be called with NULL handle just for CS_OPT_MEM
1072
  // This is supposed to be executed before all other APIs (even cs_open())
1073
50.3k
  if (type == CS_OPT_MEM) {
1074
0
    cs_opt_mem *mem = (cs_opt_mem *)value;
1075
1076
0
    cs_mem_malloc = mem->malloc;
1077
0
    cs_mem_calloc = mem->calloc;
1078
0
    cs_mem_realloc = mem->realloc;
1079
0
    cs_mem_free = mem->free;
1080
0
    cs_vsnprintf = mem->vsnprintf;
1081
1082
0
    return CS_ERR_OK;
1083
0
  }
1084
1085
50.3k
  handle = (struct cs_struct *)(uintptr_t)ud;
1086
50.3k
  if (!handle)
1087
0
    return CS_ERR_CSH;
1088
1089
50.3k
  switch (type) {
1090
7.78k
  default:
1091
7.78k
    break;
1092
1093
7.78k
  case CS_OPT_UNSIGNED:
1094
0
    handle->imm_unsigned = (cs_opt_value)value;
1095
0
    return CS_ERR_OK;
1096
1097
42.6k
  case CS_OPT_DETAIL:
1098
42.6k
    handle->detail_opt |= (cs_opt_value)value;
1099
42.6k
    return CS_ERR_OK;
1100
1101
0
  case CS_OPT_SKIPDATA:
1102
0
    handle->skipdata = (value == CS_OPT_ON);
1103
0
    if (handle->skipdata) {
1104
0
      if (handle->skipdata_size == 0) {
1105
        // set the default skipdata size
1106
0
        handle->skipdata_size = skipdata_size(handle);
1107
0
      }
1108
0
    }
1109
0
    return CS_ERR_OK;
1110
1111
0
  case CS_OPT_SKIPDATA_SETUP:
1112
0
    if (value) {
1113
0
      handle->skipdata_setup = *((cs_opt_skipdata *)value);
1114
0
      if (handle->skipdata_setup.mnemonic == NULL) {
1115
0
        handle->skipdata_setup.mnemonic = SKIPDATA_MNEM;
1116
0
      }
1117
0
    }
1118
0
    return CS_ERR_OK;
1119
1120
0
  case CS_OPT_MNEMONIC:
1121
0
    opt = (cs_opt_mnem *)value;
1122
0
    if (opt->id) {
1123
0
      if (opt->mnemonic) {
1124
0
        struct insn_mnem *tmp;
1125
1126
        // add new instruction, or replace existing instruction
1127
        // 1. find if we already had this insn in the linked list
1128
0
        tmp = handle->mnem_list;
1129
0
        while (tmp) {
1130
0
          if (tmp->insn.id == opt->id) {
1131
            // found this instruction, so replace its mnemonic
1132
0
            (void)strncpy(
1133
0
              tmp->insn.mnemonic,
1134
0
              opt->mnemonic,
1135
0
              sizeof(tmp->insn
1136
0
                       .mnemonic) -
1137
0
                1);
1138
0
            tmp->insn.mnemonic
1139
0
              [sizeof(tmp->insn.mnemonic) -
1140
0
               1] = '\0';
1141
0
            break;
1142
0
          }
1143
0
          tmp = tmp->next;
1144
0
        }
1145
1146
        // 2. add this instruction if we have not had it yet
1147
0
        if (!tmp) {
1148
0
          tmp = cs_mem_malloc(sizeof(*tmp));
1149
0
          if (!tmp) {
1150
0
            return CS_ERR_MEM;
1151
0
          }
1152
1153
0
          tmp->insn.id = opt->id;
1154
0
          (void)strncpy(
1155
0
            tmp->insn.mnemonic,
1156
0
            opt->mnemonic,
1157
0
            sizeof(tmp->insn.mnemonic) - 1);
1158
0
          tmp->insn.mnemonic
1159
0
            [sizeof(tmp->insn.mnemonic) - 1] =
1160
0
            '\0';
1161
          // this new instruction is heading the list
1162
0
          tmp->next = handle->mnem_list;
1163
0
          handle->mnem_list = tmp;
1164
0
        }
1165
0
        return CS_ERR_OK;
1166
0
      } else {
1167
0
        struct insn_mnem *prev, *tmp;
1168
1169
        // we want to delete an existing instruction
1170
        // iterate the list to find the instruction to remove it
1171
0
        tmp = handle->mnem_list;
1172
0
        prev = tmp;
1173
0
        while (tmp) {
1174
0
          if (tmp->insn.id == opt->id) {
1175
            // delete this instruction
1176
0
            if (tmp == prev) {
1177
              // head of the list
1178
0
              handle->mnem_list =
1179
0
                tmp->next;
1180
0
            } else {
1181
0
              prev->next = tmp->next;
1182
0
            }
1183
0
            cs_mem_free(tmp);
1184
0
            break;
1185
0
          }
1186
0
          prev = tmp;
1187
0
          tmp = tmp->next;
1188
0
        }
1189
0
      }
1190
0
    }
1191
0
    return CS_ERR_OK;
1192
1193
0
  case CS_OPT_MODE:
1194
    // verify if requested mode is valid
1195
0
    if (value &
1196
0
        arch_configs[handle->arch].arch_disallowed_mode_mask) {
1197
0
      return CS_ERR_OPTION;
1198
0
    }
1199
0
    break;
1200
0
  case CS_OPT_ONLY_OFFSET_BRANCH:
1201
0
    handle->PrintBranchImmAsAddress = value == CS_OPT_ON ? false :
1202
0
                       true;
1203
0
    return CS_ERR_OK;
1204
50.3k
  }
1205
1206
7.78k
  if (!arch_configs[handle->arch].arch_option)
1207
0
    return CS_ERR_ARCH;
1208
1209
7.78k
  return arch_configs[handle->arch].arch_option(handle, type, value);
1210
7.78k
}
1211
1212
// generate @op_str for data instruction of SKIPDATA
1213
#ifndef CAPSTONE_DIET
1214
static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
1215
0
{
1216
0
  char *p = opstr;
1217
0
  int len;
1218
0
  size_t i;
1219
0
  size_t available = sizeof(((cs_insn *)NULL)->op_str);
1220
1221
0
  if (!size) {
1222
0
    opstr[0] = '\0';
1223
0
    return;
1224
0
  }
1225
1226
0
  len = cs_snprintf(p, available, "0x%02x", buffer[0]);
1227
0
  p += len;
1228
0
  available -= len;
1229
1230
0
  for (i = 1; i < size; i++) {
1231
0
    len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
1232
0
    if (len < 0) {
1233
0
      break;
1234
0
    }
1235
0
    if ((size_t)len > available - 1) {
1236
0
      break;
1237
0
    }
1238
0
    p += len;
1239
0
    available -= len;
1240
0
  }
1241
0
}
1242
#endif
1243
1244
// dynamically allocate memory to contain disasm insn
1245
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
1246
CAPSTONE_EXPORT
1247
size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size,
1248
            uint64_t offset, size_t count, cs_insn **insn)
1249
42.6k
{
1250
42.6k
  struct cs_struct *handle;
1251
42.6k
  MCInst mci;
1252
42.6k
  uint16_t insn_size;
1253
42.6k
  size_t c = 0, i;
1254
42.6k
  unsigned int f = 0; // index of the next instruction in the cache
1255
42.6k
  cs_insn *insn_cache; // cache contains disassembled instructions
1256
42.6k
  void *total = NULL;
1257
42.6k
  size_t total_size =
1258
42.6k
    0; // total size of output buffer containing all insns
1259
42.6k
  bool r;
1260
42.6k
  void *tmp;
1261
42.6k
  size_t skipdata_bytes;
1262
42.6k
  uint64_t offset_org; // save all the original info of the buffer
1263
42.6k
  size_t size_org;
1264
42.6k
  const uint8_t *buffer_org;
1265
42.6k
  unsigned int cache_size = INSN_CACHE_SIZE;
1266
42.6k
  size_t next_offset;
1267
1268
42.6k
  handle = (struct cs_struct *)(uintptr_t)ud;
1269
42.6k
  if (!handle) {
1270
    // FIXME: how to handle this case:
1271
    // handle->errnum = CS_ERR_HANDLE;
1272
0
    return 0;
1273
0
  }
1274
1275
42.6k
  handle->errnum = CS_ERR_OK;
1276
1277
42.6k
#ifdef CAPSTONE_USE_SYS_DYN_MEM
1278
42.6k
  if (count > 0 && count <= INSN_CACHE_SIZE)
1279
0
    cache_size = (unsigned int)count;
1280
42.6k
#endif
1281
1282
  // save the original offset for SKIPDATA
1283
42.6k
  buffer_org = buffer;
1284
42.6k
  offset_org = offset;
1285
42.6k
  size_org = size;
1286
1287
42.6k
  total_size = sizeof(cs_insn) * cache_size;
1288
42.6k
  total = cs_mem_calloc(sizeof(cs_insn), cache_size);
1289
42.6k
  if (total == NULL) {
1290
    // insufficient memory
1291
0
    handle->errnum = CS_ERR_MEM;
1292
0
    return 0;
1293
0
  }
1294
1295
42.6k
  insn_cache = total;
1296
1297
3.23M
  while (size > 0) {
1298
3.21M
    MCInst_Init(&mci, handle->arch);
1299
3.21M
    mci.csh = handle;
1300
1301
    // relative branches need to know the address & size of current insn
1302
3.21M
    mci.address = offset;
1303
1304
3.21M
    if (handle->detail_opt) {
1305
      // allocate memory for @detail pointer
1306
3.21M
      insn_cache->detail =
1307
3.21M
        cs_mem_calloc(1, sizeof(cs_detail));
1308
3.21M
      if (!insn_cache->detail) {
1309
0
        insn_cache = (cs_insn *)total;
1310
0
        for (i = 0; i < c; i++, insn_cache++)
1311
0
          cs_mem_free(insn_cache->detail);
1312
1313
0
        cs_mem_free(total);
1314
0
        *insn = NULL;
1315
1316
0
        handle->errnum = CS_ERR_MEM;
1317
0
        return 0;
1318
0
      }
1319
3.21M
    } else {
1320
0
      insn_cache->detail = NULL;
1321
0
    }
1322
1323
    // save all the information for non-detailed mode
1324
3.21M
    mci.flat_insn = insn_cache;
1325
3.21M
    mci.flat_insn->address = offset;
1326
#ifdef CAPSTONE_DIET
1327
    // zero out mnemonic & op_str
1328
    mci.flat_insn->mnemonic[0] = '\0';
1329
    mci.flat_insn->op_str[0] = '\0';
1330
#endif
1331
1332
3.21M
    r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset,
1333
3.21M
           handle->getinsn_info);
1334
3.21M
    if (r) {
1335
3.18M
      SStream ss;
1336
3.18M
      SStream_Init(&ss);
1337
1338
3.18M
      mci.flat_insn->size = insn_size;
1339
1340
      // map internal instruction opcode to public insn ID
1341
1342
3.18M
      handle->insn_id(handle, insn_cache, mci.Opcode);
1343
1344
3.18M
      SStream_opt_unum(&ss, handle->imm_unsigned);
1345
3.18M
      handle->printer(&mci, &ss, handle->printer_info);
1346
3.18M
      fill_insn(handle, insn_cache, &ss, &mci,
1347
3.18M
          handle->post_printer, buffer);
1348
1349
      // adjust for pseudo opcode (X86)
1350
3.18M
      if (handle->arch == CS_ARCH_X86 &&
1351
779k
          insn_cache->id != X86_INS_VCMP)
1352
775k
        insn_cache->id += mci.popcode_adjust;
1353
1354
3.18M
      next_offset = insn_size;
1355
3.18M
    } else {
1356
      // encounter a broken instruction
1357
1358
      // free memory of @detail pointer
1359
27.7k
      if (handle->detail_opt) {
1360
27.7k
        cs_mem_free(insn_cache->detail);
1361
27.7k
      }
1362
1363
      // if there is no request to skip data, or remaining data is too small,
1364
      // then bail out
1365
27.7k
      if (!handle->skipdata || handle->skipdata_size > size)
1366
27.7k
        break;
1367
1368
0
      if (handle->skipdata_setup.callback) {
1369
0
        skipdata_bytes = handle->skipdata_setup.callback(
1370
0
          buffer_org, size_org,
1371
0
          (size_t)(offset - offset_org),
1372
0
          handle->skipdata_setup.user_data);
1373
0
        if (skipdata_bytes > size)
1374
          // remaining data is not enough
1375
0
          break;
1376
1377
0
        if (!skipdata_bytes)
1378
          // user requested not to skip data, so bail out
1379
0
          break;
1380
0
      } else
1381
0
        skipdata_bytes = handle->skipdata_size;
1382
1383
      // we have to skip some amount of data, depending on arch & mode
1384
      // invalid ID for this "data" instruction
1385
0
      insn_cache->id = 0;
1386
0
      insn_cache->address = offset;
1387
0
      insn_cache->size = (uint16_t)MIN(
1388
0
        skipdata_bytes, sizeof(insn_cache->bytes));
1389
0
      memcpy(insn_cache->bytes, buffer,
1390
0
             MIN(skipdata_bytes, sizeof(insn_cache->bytes)));
1391
#ifdef CAPSTONE_DIET
1392
      insn_cache->mnemonic[0] = '\0';
1393
      insn_cache->op_str[0] = '\0';
1394
#else
1395
0
      strncpy(insn_cache->mnemonic,
1396
0
        handle->skipdata_setup.mnemonic,
1397
0
        sizeof(insn_cache->mnemonic) - 1);
1398
0
      skipdata_opstr(insn_cache->op_str, buffer,
1399
0
               skipdata_bytes);
1400
0
#endif
1401
0
      insn_cache->detail = NULL;
1402
1403
0
      next_offset = skipdata_bytes;
1404
0
    }
1405
1406
    // one more instruction entering the cache
1407
3.18M
    f++;
1408
1409
    // one more instruction disassembled
1410
3.18M
    c++;
1411
3.18M
    if (count > 0 && c == count)
1412
      // already got requested number of instructions
1413
0
      break;
1414
1415
3.18M
    if (f == cache_size) {
1416
      // full cache, so expand the cache to contain incoming insns
1417
32.4k
      cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
1418
32.4k
      size_t old_total_size = total_size;
1419
32.4k
      total_size += (sizeof(cs_insn) * cache_size);
1420
32.4k
      tmp = cs_mem_realloc(total, total_size);
1421
32.4k
      if (tmp == NULL) { // insufficient memory
1422
0
        if (handle->detail_opt) {
1423
0
          insn_cache = (cs_insn *)total;
1424
0
          for (i = 0; i < c; i++, insn_cache++)
1425
0
            cs_mem_free(insn_cache->detail);
1426
0
        }
1427
1428
0
        cs_mem_free(total);
1429
0
        *insn = NULL;
1430
0
        handle->errnum = CS_ERR_MEM;
1431
0
        return 0;
1432
0
      }
1433
      // Zero reallocated memory to prevent
1434
      // access to uninitialized memory down the line.
1435
32.4k
      memset(((uint8_t *)tmp) + old_total_size, 0,
1436
32.4k
             total_size - old_total_size);
1437
1438
32.4k
      total = tmp;
1439
      // continue to fill in the cache after the last instruction
1440
32.4k
      insn_cache = (cs_insn *)((char *)total +
1441
32.4k
             sizeof(cs_insn) * c);
1442
1443
      // reset f back to 0, so we fill in the cache from beginning
1444
32.4k
      f = 0;
1445
32.4k
    } else
1446
3.15M
      insn_cache++;
1447
1448
3.18M
    buffer += next_offset;
1449
3.18M
    size -= next_offset;
1450
3.18M
    offset += next_offset;
1451
3.18M
  }
1452
1453
42.6k
  if (!c) {
1454
    // we did not disassemble any instruction
1455
859
    cs_mem_free(total);
1456
859
    total = NULL;
1457
41.7k
  } else if (f != cache_size) {
1458
    // total did not fully use the last cache, so downsize it
1459
41.7k
    tmp = cs_mem_realloc(total,
1460
41.7k
             total_size - (cache_size - f) *
1461
41.7k
                sizeof(*insn_cache));
1462
41.7k
    if (tmp == NULL) { // insufficient memory
1463
      // free all detail pointers
1464
0
      if (handle->detail_opt) {
1465
0
        insn_cache = (cs_insn *)total;
1466
0
        for (i = 0; i < c; i++, insn_cache++)
1467
0
          cs_mem_free(insn_cache->detail);
1468
0
      }
1469
1470
0
      cs_mem_free(total);
1471
0
      *insn = NULL;
1472
1473
0
      handle->errnum = CS_ERR_MEM;
1474
0
      return 0;
1475
0
    }
1476
1477
41.7k
    total = tmp;
1478
41.7k
  }
1479
1480
42.6k
  *insn = total;
1481
1482
42.6k
  return c;
1483
42.6k
}
1484
1485
CAPSTONE_EXPORT
1486
void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
1487
79.3k
{
1488
79.3k
  size_t i;
1489
1490
  // free all detail pointers
1491
5.82M
  for (i = 0; i < count; i++)
1492
5.74M
    cs_mem_free(insn[i].detail);
1493
1494
  // then free pointer to cs_insn array
1495
79.3k
  cs_mem_free(insn);
1496
79.3k
}
1497
1498
CAPSTONE_EXPORT
1499
cs_insn *CAPSTONE_API cs_malloc(csh ud)
1500
0
{
1501
0
  cs_insn *insn;
1502
0
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1503
1504
0
  insn = cs_mem_malloc(sizeof(cs_insn));
1505
0
  if (!insn) {
1506
    // insufficient memory
1507
0
    handle->errnum = CS_ERR_MEM;
1508
0
    return NULL;
1509
0
  } else {
1510
0
    if (handle->detail_opt) {
1511
      // allocate memory for @detail pointer
1512
0
      insn->detail = cs_mem_malloc(sizeof(cs_detail));
1513
0
      if (insn->detail == NULL) { // insufficient memory
1514
0
        cs_mem_free(insn);
1515
0
        handle->errnum = CS_ERR_MEM;
1516
0
        return NULL;
1517
0
      }
1518
0
    } else
1519
0
      insn->detail = NULL;
1520
0
  }
1521
1522
0
  return insn;
1523
0
}
1524
1525
// iterator for instruction "single-stepping"
1526
CAPSTONE_EXPORT
1527
bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
1528
         uint64_t *address, cs_insn *insn)
1529
0
{
1530
0
  if (*size == 0)
1531
0
    return false;
1532
1533
0
  struct cs_struct *handle;
1534
0
  uint16_t insn_size;
1535
0
  MCInst mci;
1536
0
  bool r;
1537
1538
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1539
0
  if (!handle) {
1540
0
    return false;
1541
0
  }
1542
1543
0
  handle->errnum = CS_ERR_OK;
1544
1545
0
  MCInst_Init(&mci, handle->arch);
1546
0
  mci.csh = handle;
1547
1548
  // relative branches need to know the address & size of current insn
1549
0
  mci.address = *address;
1550
1551
  // save all the information for non-detailed mode
1552
0
  mci.flat_insn = insn;
1553
0
  mci.flat_insn->address = *address;
1554
#ifdef CAPSTONE_DIET
1555
  // zero out mnemonic & op_str
1556
  mci.flat_insn->mnemonic[0] = '\0';
1557
  mci.flat_insn->op_str[0] = '\0';
1558
#endif
1559
1560
0
  r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address,
1561
0
         handle->getinsn_info);
1562
0
  if (r) {
1563
0
    SStream ss;
1564
0
    SStream_Init(&ss);
1565
1566
0
    mci.flat_insn->size = insn_size;
1567
1568
    // map internal instruction opcode to public insn ID
1569
0
    handle->insn_id(handle, insn, mci.Opcode);
1570
1571
0
    SStream_opt_unum(&ss, handle->imm_unsigned);
1572
0
    handle->printer(&mci, &ss, handle->printer_info);
1573
1574
0
    fill_insn(handle, insn, &ss, &mci, handle->post_printer, *code);
1575
1576
    // adjust for pseudo opcode (X86)
1577
0
    if (handle->arch == CS_ARCH_X86)
1578
0
      insn->id += mci.popcode_adjust;
1579
1580
0
    *code += insn_size;
1581
0
    *size -= insn_size;
1582
0
    *address += insn_size;
1583
0
  } else { // encounter a broken instruction
1584
0
    size_t skipdata_bytes;
1585
1586
    // if there is no request to skip data, or remaining data is too small,
1587
    // then bail out
1588
0
    if (!handle->skipdata || handle->skipdata_size > *size)
1589
0
      return false;
1590
1591
0
    if (handle->skipdata_setup.callback) {
1592
0
      skipdata_bytes = handle->skipdata_setup.callback(
1593
0
        *code, *size, 0,
1594
0
        handle->skipdata_setup.user_data);
1595
0
      if (skipdata_bytes > *size)
1596
        // remaining data is not enough
1597
0
        return false;
1598
1599
0
      if (!skipdata_bytes)
1600
        // user requested not to skip data, so bail out
1601
0
        return false;
1602
0
    } else
1603
0
      skipdata_bytes = handle->skipdata_size;
1604
1605
    // we have to skip some amount of data, depending on arch & mode
1606
0
    insn->id = 0; // invalid ID for this "data" instruction
1607
0
    insn->address = *address;
1608
0
    insn->size = (uint16_t)MIN(skipdata_bytes, sizeof(insn->bytes));
1609
0
    memcpy(insn->bytes, *code,
1610
0
           MIN(skipdata_bytes, sizeof(insn->bytes)));
1611
#ifdef CAPSTONE_DIET
1612
    insn->mnemonic[0] = '\0';
1613
    insn->op_str[0] = '\0';
1614
#else
1615
0
    strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1616
0
      sizeof(insn->mnemonic) - 1);
1617
0
    skipdata_opstr(insn->op_str, *code, skipdata_bytes);
1618
0
#endif
1619
1620
0
    *code += skipdata_bytes;
1621
0
    *size -= skipdata_bytes;
1622
0
    *address += skipdata_bytes;
1623
0
  }
1624
1625
0
  return true;
1626
0
}
1627
1628
// return friendly name of register in a string
1629
CAPSTONE_EXPORT
1630
const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
1631
5.19M
{
1632
5.19M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1633
1634
5.19M
  if (!handle || handle->reg_name == NULL) {
1635
0
    return NULL;
1636
0
  }
1637
1638
5.19M
  return handle->reg_name(ud, reg);
1639
5.19M
}
1640
1641
CAPSTONE_EXPORT
1642
const char *CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
1643
5.74M
{
1644
5.74M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1645
1646
5.74M
  if (!handle || handle->insn_name == NULL) {
1647
0
    return NULL;
1648
0
  }
1649
1650
5.74M
  return handle->insn_name(ud, insn);
1651
5.74M
}
1652
1653
CAPSTONE_EXPORT
1654
const char *CAPSTONE_API cs_group_name(csh ud, unsigned int group)
1655
4.77M
{
1656
4.77M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1657
1658
4.77M
  if (!handle || handle->group_name == NULL) {
1659
0
    return NULL;
1660
0
  }
1661
1662
4.77M
  return handle->group_name(ud, group);
1663
4.77M
}
1664
1665
CAPSTONE_EXPORT
1666
bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn,
1667
        unsigned int group_id)
1668
0
{
1669
0
  struct cs_struct *handle;
1670
0
  if (!ud)
1671
0
    return false;
1672
1673
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1674
1675
0
  if (!handle->detail_opt) {
1676
0
    handle->errnum = CS_ERR_DETAIL;
1677
0
    return false;
1678
0
  }
1679
1680
0
  if (!insn->id) {
1681
0
    handle->errnum = CS_ERR_SKIPDATA;
1682
0
    return false;
1683
0
  }
1684
1685
0
  if (!insn->detail) {
1686
0
    handle->errnum = CS_ERR_DETAIL;
1687
0
    return false;
1688
0
  }
1689
1690
0
  return arr_exist8(insn->detail->groups, insn->detail->groups_count,
1691
0
        group_id);
1692
0
}
1693
1694
CAPSTONE_EXPORT
1695
bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
1696
0
{
1697
0
  struct cs_struct *handle;
1698
0
  if (!ud)
1699
0
    return false;
1700
1701
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1702
1703
0
  if (!handle->detail_opt) {
1704
0
    handle->errnum = CS_ERR_DETAIL;
1705
0
    return false;
1706
0
  }
1707
1708
0
  if (!insn->id) {
1709
0
    handle->errnum = CS_ERR_SKIPDATA;
1710
0
    return false;
1711
0
  }
1712
1713
0
  if (!insn->detail) {
1714
0
    handle->errnum = CS_ERR_DETAIL;
1715
0
    return false;
1716
0
  }
1717
1718
0
  return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count,
1719
0
       reg_id);
1720
0
}
1721
1722
CAPSTONE_EXPORT
1723
bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
1724
1.01M
{
1725
1.01M
  struct cs_struct *handle;
1726
1.01M
  if (!ud)
1727
0
    return false;
1728
1729
1.01M
  handle = (struct cs_struct *)(uintptr_t)ud;
1730
1731
1.01M
  if (!handle->detail_opt) {
1732
0
    handle->errnum = CS_ERR_DETAIL;
1733
0
    return false;
1734
0
  }
1735
1736
1.01M
  if (!insn->id) {
1737
0
    handle->errnum = CS_ERR_SKIPDATA;
1738
0
    return false;
1739
0
  }
1740
1741
1.01M
  if (!insn->detail) {
1742
0
    handle->errnum = CS_ERR_DETAIL;
1743
0
    return false;
1744
0
  }
1745
1746
1.01M
  return arr_exist(insn->detail->regs_write,
1747
1.01M
       insn->detail->regs_write_count, reg_id);
1748
1.01M
}
1749
1750
CAPSTONE_EXPORT
1751
int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
1752
0
{
1753
0
  struct cs_struct *handle;
1754
0
  unsigned int count = 0, i;
1755
0
  if (!ud)
1756
0
    return -1;
1757
1758
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1759
1760
0
  if (!handle->detail_opt) {
1761
0
    handle->errnum = CS_ERR_DETAIL;
1762
0
    return -1;
1763
0
  }
1764
1765
0
  if (!insn->id) {
1766
0
    handle->errnum = CS_ERR_SKIPDATA;
1767
0
    return -1;
1768
0
  }
1769
1770
0
  if (!insn->detail) {
1771
0
    handle->errnum = CS_ERR_DETAIL;
1772
0
    return -1;
1773
0
  }
1774
1775
0
  handle->errnum = CS_ERR_OK;
1776
1777
0
  switch (handle->arch) {
1778
0
  default:
1779
0
    handle->errnum = CS_ERR_HANDLE;
1780
0
    return -1;
1781
0
  case CS_ARCH_ARM:
1782
0
    for (i = 0; i < insn->detail->arm.op_count; i++)
1783
0
      if (insn->detail->arm.operands[i].type ==
1784
0
          (arm_op_type)op_type)
1785
0
        count++;
1786
0
    break;
1787
0
  case CS_ARCH_AARCH64:
1788
0
    for (i = 0; i < insn->detail->aarch64.op_count; i++)
1789
0
      if (insn->detail->aarch64.operands[i].type ==
1790
0
          (aarch64_op_type)op_type)
1791
0
        count++;
1792
0
    break;
1793
0
  case CS_ARCH_X86:
1794
0
    for (i = 0; i < insn->detail->x86.op_count; i++)
1795
0
      if (insn->detail->x86.operands[i].type ==
1796
0
          (x86_op_type)op_type)
1797
0
        count++;
1798
0
    break;
1799
0
  case CS_ARCH_MIPS:
1800
0
    for (i = 0; i < insn->detail->mips.op_count; i++)
1801
0
      if (insn->detail->mips.operands[i].type ==
1802
0
          (mips_op_type)op_type)
1803
0
        count++;
1804
0
    break;
1805
0
  case CS_ARCH_PPC:
1806
0
    for (i = 0; i < insn->detail->ppc.op_count; i++)
1807
0
      if (insn->detail->ppc.operands[i].type ==
1808
0
          (ppc_op_type)op_type)
1809
0
        count++;
1810
0
    break;
1811
0
  case CS_ARCH_SPARC:
1812
0
    for (i = 0; i < insn->detail->sparc.op_count; i++)
1813
0
      if (insn->detail->sparc.operands[i].type ==
1814
0
          (sparc_op_type)op_type)
1815
0
        count++;
1816
0
    break;
1817
0
  case CS_ARCH_SYSTEMZ:
1818
0
    for (i = 0; i < insn->detail->systemz.op_count; i++)
1819
0
      if (insn->detail->systemz.operands[i].type ==
1820
0
          (systemz_op_type)op_type)
1821
0
        count++;
1822
0
    break;
1823
0
  case CS_ARCH_XCORE:
1824
0
    for (i = 0; i < insn->detail->xcore.op_count; i++)
1825
0
      if (insn->detail->xcore.operands[i].type ==
1826
0
          (xcore_op_type)op_type)
1827
0
        count++;
1828
0
    break;
1829
0
  case CS_ARCH_M68K:
1830
0
    for (i = 0; i < insn->detail->m68k.op_count; i++)
1831
0
      if (insn->detail->m68k.operands[i].type ==
1832
0
          (m68k_op_type)op_type)
1833
0
        count++;
1834
0
    break;
1835
0
  case CS_ARCH_TMS320C64X:
1836
0
    for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1837
0
      if (insn->detail->tms320c64x.operands[i].type ==
1838
0
          (tms320c64x_op_type)op_type)
1839
0
        count++;
1840
0
    break;
1841
0
  case CS_ARCH_M680X:
1842
0
    for (i = 0; i < insn->detail->m680x.op_count; i++)
1843
0
      if (insn->detail->m680x.operands[i].type ==
1844
0
          (m680x_op_type)op_type)
1845
0
        count++;
1846
0
    break;
1847
0
  case CS_ARCH_EVM:
1848
0
    break;
1849
0
  case CS_ARCH_MOS65XX:
1850
0
    for (i = 0; i < insn->detail->mos65xx.op_count; i++)
1851
0
      if (insn->detail->mos65xx.operands[i].type ==
1852
0
          (mos65xx_op_type)op_type)
1853
0
        count++;
1854
0
    break;
1855
0
  case CS_ARCH_WASM:
1856
0
    for (i = 0; i < insn->detail->wasm.op_count; i++)
1857
0
      if (insn->detail->wasm.operands[i].type ==
1858
0
          (wasm_op_type)op_type)
1859
0
        count++;
1860
0
    break;
1861
0
  case CS_ARCH_BPF:
1862
0
    for (i = 0; i < insn->detail->bpf.op_count; i++)
1863
0
      if (insn->detail->bpf.operands[i].type ==
1864
0
          (bpf_op_type)op_type)
1865
0
        count++;
1866
0
    break;
1867
0
  case CS_ARCH_RISCV:
1868
0
    for (i = 0; i < insn->detail->riscv.op_count; i++)
1869
0
      if (insn->detail->riscv.operands[i].type ==
1870
0
          (riscv_op_type)op_type)
1871
0
        count++;
1872
0
    break;
1873
0
  case CS_ARCH_TRICORE:
1874
0
    for (i = 0; i < insn->detail->tricore.op_count; i++)
1875
0
      if (insn->detail->tricore.operands[i].type ==
1876
0
          (tricore_op_type)op_type)
1877
0
        count++;
1878
0
    break;
1879
0
  case CS_ARCH_ALPHA:
1880
0
    for (i = 0; i < insn->detail->alpha.op_count; i++)
1881
0
      if (insn->detail->alpha.operands[i].type ==
1882
0
          (alpha_op_type)op_type)
1883
0
        count++;
1884
0
    break;
1885
0
  case CS_ARCH_HPPA:
1886
0
    for (i = 0; i < insn->detail->hppa.op_count; i++)
1887
0
      if (insn->detail->hppa.operands[i].type ==
1888
0
          (hppa_op_type)op_type)
1889
0
        count++;
1890
0
    break;
1891
0
  case CS_ARCH_LOONGARCH:
1892
0
    for (i = 0; i < insn->detail->loongarch.op_count; i++)
1893
0
      if (insn->detail->loongarch.operands[i].type ==
1894
0
          (loongarch_op_type)op_type)
1895
0
        count++;
1896
0
    break;
1897
0
  case CS_ARCH_ARC:
1898
0
    for (i = 0; i < insn->detail->arc.op_count; i++)
1899
0
      if (insn->detail->arc.operands[i].type ==
1900
0
          (arc_op_type)op_type)
1901
0
        count++;
1902
0
    break;
1903
0
  }
1904
1905
0
  return count;
1906
0
}
1907
1908
CAPSTONE_EXPORT
1909
int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
1910
           unsigned int post)
1911
0
{
1912
0
  struct cs_struct *handle;
1913
0
  unsigned int count = 0, i;
1914
0
  if (!ud)
1915
0
    return -1;
1916
1917
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1918
1919
0
  if (!handle->detail_opt) {
1920
0
    handle->errnum = CS_ERR_DETAIL;
1921
0
    return -1;
1922
0
  }
1923
1924
0
  if (!insn->id) {
1925
0
    handle->errnum = CS_ERR_SKIPDATA;
1926
0
    return -1;
1927
0
  }
1928
1929
0
  if (!insn->detail) {
1930
0
    handle->errnum = CS_ERR_DETAIL;
1931
0
    return -1;
1932
0
  }
1933
1934
0
  handle->errnum = CS_ERR_OK;
1935
1936
0
  switch (handle->arch) {
1937
0
  default:
1938
0
    handle->errnum = CS_ERR_HANDLE;
1939
0
    return -1;
1940
0
  case CS_ARCH_ARM:
1941
0
    for (i = 0; i < insn->detail->arm.op_count; i++) {
1942
0
      if (insn->detail->arm.operands[i].type ==
1943
0
          (arm_op_type)op_type)
1944
0
        count++;
1945
0
      if (count == post)
1946
0
        return i;
1947
0
    }
1948
0
    break;
1949
0
  case CS_ARCH_AARCH64:
1950
0
    for (i = 0; i < insn->detail->aarch64.op_count; i++) {
1951
0
      if (insn->detail->aarch64.operands[i].type ==
1952
0
          (aarch64_op_type)op_type)
1953
0
        count++;
1954
0
      if (count == post)
1955
0
        return i;
1956
0
    }
1957
0
    break;
1958
0
  case CS_ARCH_X86:
1959
0
    for (i = 0; i < insn->detail->x86.op_count; i++) {
1960
0
      if (insn->detail->x86.operands[i].type ==
1961
0
          (x86_op_type)op_type)
1962
0
        count++;
1963
0
      if (count == post)
1964
0
        return i;
1965
0
    }
1966
0
    break;
1967
0
  case CS_ARCH_MIPS:
1968
0
    for (i = 0; i < insn->detail->mips.op_count; i++) {
1969
0
      if (insn->detail->mips.operands[i].type ==
1970
0
          (mips_op_type)op_type)
1971
0
        count++;
1972
0
      if (count == post)
1973
0
        return i;
1974
0
    }
1975
0
    break;
1976
0
  case CS_ARCH_PPC:
1977
0
    for (i = 0; i < insn->detail->ppc.op_count; i++) {
1978
0
      if (insn->detail->ppc.operands[i].type ==
1979
0
          (ppc_op_type)op_type)
1980
0
        count++;
1981
0
      if (count == post)
1982
0
        return i;
1983
0
    }
1984
0
    break;
1985
0
  case CS_ARCH_SPARC:
1986
0
    for (i = 0; i < insn->detail->sparc.op_count; i++) {
1987
0
      if (insn->detail->sparc.operands[i].type ==
1988
0
          (sparc_op_type)op_type)
1989
0
        count++;
1990
0
      if (count == post)
1991
0
        return i;
1992
0
    }
1993
0
    break;
1994
0
  case CS_ARCH_SYSTEMZ:
1995
0
    for (i = 0; i < insn->detail->systemz.op_count; i++) {
1996
0
      if (insn->detail->systemz.operands[i].type ==
1997
0
          (systemz_op_type)op_type)
1998
0
        count++;
1999
0
      if (count == post)
2000
0
        return i;
2001
0
    }
2002
0
    break;
2003
0
  case CS_ARCH_XCORE:
2004
0
    for (i = 0; i < insn->detail->xcore.op_count; i++) {
2005
0
      if (insn->detail->xcore.operands[i].type ==
2006
0
          (xcore_op_type)op_type)
2007
0
        count++;
2008
0
      if (count == post)
2009
0
        return i;
2010
0
    }
2011
0
    break;
2012
0
  case CS_ARCH_TRICORE:
2013
0
    for (i = 0; i < insn->detail->tricore.op_count; i++) {
2014
0
      if (insn->detail->tricore.operands[i].type ==
2015
0
          (tricore_op_type)op_type)
2016
0
        count++;
2017
0
      if (count == post)
2018
0
        return i;
2019
0
    }
2020
0
    break;
2021
0
  case CS_ARCH_M68K:
2022
0
    for (i = 0; i < insn->detail->m68k.op_count; i++) {
2023
0
      if (insn->detail->m68k.operands[i].type ==
2024
0
          (m68k_op_type)op_type)
2025
0
        count++;
2026
0
      if (count == post)
2027
0
        return i;
2028
0
    }
2029
0
    break;
2030
0
  case CS_ARCH_TMS320C64X:
2031
0
    for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
2032
0
      if (insn->detail->tms320c64x.operands[i].type ==
2033
0
          (tms320c64x_op_type)op_type)
2034
0
        count++;
2035
0
      if (count == post)
2036
0
        return i;
2037
0
    }
2038
0
    break;
2039
0
  case CS_ARCH_M680X:
2040
0
    for (i = 0; i < insn->detail->m680x.op_count; i++) {
2041
0
      if (insn->detail->m680x.operands[i].type ==
2042
0
          (m680x_op_type)op_type)
2043
0
        count++;
2044
0
      if (count == post)
2045
0
        return i;
2046
0
    }
2047
0
    break;
2048
0
  case CS_ARCH_EVM:
2049
#if 0
2050
      for (i = 0; i < insn->detail->evm.op_count; i++) {
2051
        if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
2052
          count++;
2053
        if (count == post)
2054
          return i;
2055
      }
2056
#endif
2057
0
    break;
2058
0
  case CS_ARCH_MOS65XX:
2059
0
    for (i = 0; i < insn->detail->mos65xx.op_count; i++) {
2060
0
      if (insn->detail->mos65xx.operands[i].type ==
2061
0
          (mos65xx_op_type)op_type)
2062
0
        count++;
2063
0
      if (count == post)
2064
0
        return i;
2065
0
    }
2066
0
    break;
2067
0
  case CS_ARCH_WASM:
2068
0
    for (i = 0; i < insn->detail->wasm.op_count; i++) {
2069
0
      if (insn->detail->wasm.operands[i].type ==
2070
0
          (wasm_op_type)op_type)
2071
0
        count++;
2072
0
      if (count == post)
2073
0
        return i;
2074
0
    }
2075
0
    break;
2076
0
  case CS_ARCH_BPF:
2077
0
    for (i = 0; i < insn->detail->bpf.op_count; i++) {
2078
0
      if (insn->detail->bpf.operands[i].type ==
2079
0
          (bpf_op_type)op_type)
2080
0
        count++;
2081
0
      if (count == post)
2082
0
        return i;
2083
0
    }
2084
0
    break;
2085
0
  case CS_ARCH_RISCV:
2086
0
    for (i = 0; i < insn->detail->riscv.op_count; i++) {
2087
0
      if (insn->detail->riscv.operands[i].type ==
2088
0
          (riscv_op_type)op_type)
2089
0
        count++;
2090
0
      if (count == post)
2091
0
        return i;
2092
0
    }
2093
0
    break;
2094
0
  case CS_ARCH_SH:
2095
0
    for (i = 0; i < insn->detail->sh.op_count; i++) {
2096
0
      if (insn->detail->sh.operands[i].type ==
2097
0
          (sh_op_type)op_type)
2098
0
        count++;
2099
0
      if (count == post)
2100
0
        return i;
2101
0
    }
2102
0
    break;
2103
0
  case CS_ARCH_ALPHA:
2104
0
    for (i = 0; i < insn->detail->alpha.op_count; i++) {
2105
0
      if (insn->detail->alpha.operands[i].type ==
2106
0
          (alpha_op_type)op_type)
2107
0
        count++;
2108
0
      if (count == post)
2109
0
        return i;
2110
0
    }
2111
0
    break;
2112
0
  case CS_ARCH_HPPA:
2113
0
    for (i = 0; i < insn->detail->hppa.op_count; i++) {
2114
0
      if (insn->detail->hppa.operands[i].type ==
2115
0
          (hppa_op_type)op_type)
2116
0
        count++;
2117
0
      if (count == post)
2118
0
        return i;
2119
0
    }
2120
0
    break;
2121
0
  case CS_ARCH_LOONGARCH:
2122
0
    for (i = 0; i < insn->detail->loongarch.op_count; i++) {
2123
0
      if (insn->detail->loongarch.operands[i].type ==
2124
0
          (loongarch_op_type)op_type)
2125
0
        count++;
2126
0
      if (count == post)
2127
0
        return i;
2128
0
    }
2129
0
    break;
2130
0
  case CS_ARCH_ARC:
2131
0
    for (i = 0; i < insn->detail->arc.op_count; i++) {
2132
0
      if (insn->detail->arc.operands[i].type ==
2133
0
          (arc_op_type)op_type)
2134
0
        count++;
2135
0
      if (count == post)
2136
0
        return i;
2137
0
    }
2138
0
    break;
2139
0
  }
2140
2141
0
  return -1;
2142
0
}
2143
2144
CAPSTONE_EXPORT
2145
cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
2146
           cs_regs regs_read, uint8_t *regs_read_count,
2147
           cs_regs regs_write,
2148
           uint8_t *regs_write_count)
2149
0
{
2150
0
  struct cs_struct *handle;
2151
2152
0
  if (!ud)
2153
0
    return CS_ERR_CSH;
2154
2155
0
  handle = (struct cs_struct *)(uintptr_t)ud;
2156
2157
#ifdef CAPSTONE_DIET
2158
  // This API does not work in DIET mode
2159
  handle->errnum = CS_ERR_DIET;
2160
  return CS_ERR_DIET;
2161
#else
2162
0
  if (!handle->detail_opt) {
2163
0
    handle->errnum = CS_ERR_DETAIL;
2164
0
    return CS_ERR_DETAIL;
2165
0
  }
2166
2167
0
  if (!insn->id) {
2168
0
    handle->errnum = CS_ERR_SKIPDATA;
2169
0
    return CS_ERR_SKIPDATA;
2170
0
  }
2171
2172
0
  if (!insn->detail) {
2173
0
    handle->errnum = CS_ERR_DETAIL;
2174
0
    return CS_ERR_DETAIL;
2175
0
  }
2176
2177
0
  if (handle->reg_access) {
2178
0
    handle->reg_access(insn, regs_read, regs_read_count, regs_write,
2179
0
           regs_write_count);
2180
0
  } else {
2181
    // this arch is unsupported yet
2182
0
    handle->errnum = CS_ERR_ARCH;
2183
0
    return CS_ERR_ARCH;
2184
0
  }
2185
2186
0
  return CS_ERR_OK;
2187
0
#endif
2188
0
}