Coverage Report

Created: 2026-09-28 06:34

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
35.2k
#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
35.2k
#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 | CS_MODE_X86_INTEL | CS_MODE_X86_AMD), \
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
35.2k
{
806
35.2k
  cs_err err = CS_ERR_ARCH;
807
35.2k
  struct cs_struct *ud = NULL;
808
809
35.2k
  if (!cs_mem_is_setup()) {
810
    // Error: before cs_open(), dynamic memory management must be initialized
811
    // with cs_option(CS_OPT_MEM)
812
0
    err = CS_ERR_MEMSETUP;
813
0
    goto fail;
814
0
  }
815
816
35.2k
  if (arch >= CS_ARCH_MAX || !arch_configs[arch].arch_init) {
817
0
    err = CS_ERR_ARCH;
818
0
    goto fail;
819
0
  }
820
821
  // verify if requested mode is valid
822
35.2k
  if (mode & arch_configs[arch].arch_disallowed_mode_mask) {
823
7
    err = CS_ERR_MODE;
824
7
    goto fail;
825
7
  }
826
827
35.2k
  ud = cs_mem_calloc(1, sizeof(*ud));
828
35.2k
  if (!ud) {
829
0
    err = CS_ERR_MEM;
830
0
    goto fail;
831
0
  }
832
833
35.2k
  ud->errnum = CS_ERR_OK;
834
35.2k
  ud->arch = arch;
835
35.2k
  ud->mode = mode;
836
  // by default, do not break instruction into details
837
35.2k
  ud->detail_opt = CS_OPT_OFF;
838
35.2k
  ud->PrintBranchImmAsAddress = true;
839
840
  // default skipdata setup
841
35.2k
  ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
842
843
35.2k
  if ((err = arch_configs[ud->arch].arch_init(ud)))
844
0
    goto fail;
845
846
35.2k
  if ((err = populate_insn_map_cache(ud)))
847
0
    goto fail;
848
849
35.2k
  *handle = (uintptr_t)ud;
850
35.2k
  return CS_ERR_OK;
851
852
7
fail:
853
7
  if (ud) {
854
0
    cs_mem_free(ud);
855
0
  }
856
7
  *handle = 0;
857
7
  return err;
858
35.2k
}
859
860
CAPSTONE_EXPORT
861
cs_err CAPSTONE_API cs_close(csh *handle)
862
35.2k
{
863
35.2k
  struct cs_struct *ud = NULL;
864
35.2k
  struct insn_mnem *next = NULL, *tmp = NULL;
865
866
35.2k
  if (*handle == 0)
867
    // invalid handle
868
0
    return CS_ERR_CSH;
869
870
35.2k
  ud = (struct cs_struct *)(*handle);
871
872
35.2k
  if (ud->printer_info)
873
33.2k
    cs_mem_free(ud->printer_info);
874
875
  // free the linked list of customized mnemonic
876
35.2k
  tmp = ud->mnem_list;
877
35.2k
  while (tmp) {
878
0
    next = tmp->next;
879
0
    cs_mem_free(tmp);
880
0
    tmp = next;
881
0
  }
882
883
35.2k
  cs_mem_free(ud->insn_cache);
884
35.2k
  cs_mem_free(ud->x86_insn_lut);
885
35.2k
  cs_mem_free(ud->x86_insn_reg_lut);
886
887
35.2k
  memset(ud, 0, sizeof(*ud));
888
35.2k
  cs_mem_free(ud);
889
890
  // invalidate this handle by ZERO out its value.
891
  // this is to make sure it is unusable after cs_close()
892
35.2k
  *handle = 0;
893
894
35.2k
  return CS_ERR_OK;
895
35.2k
}
896
897
/// replace str1 in target with str2; target starts with str1
898
/// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
899
/// return 0 on success, -1 on failure
900
#ifndef CAPSTONE_DIET
901
static int str_replace(char *result, char *target, const char *str1, char *str2)
902
0
{
903
0
  size_t target_len = strlen(target);
904
0
  size_t str1_len = strlen(str1);
905
0
  if (target_len < str1_len) {
906
0
    return -1;
907
0
  }
908
909
  // only perform replacement if the output fits into result
910
0
  if (target_len - str1_len + strlen(str2) <= CS_MNEMONIC_SIZE - 1) {
911
    // copy str2 to beginning of result
912
    // skip str1 - already replaced by str2
913
0
    snprintf(result, CS_MNEMONIC_SIZE, "%s%s", str2,
914
0
       target + str1_len);
915
916
0
    return 0;
917
0
  } else
918
0
    return -1;
919
0
}
920
#endif
921
922
// fill insn with mnemonic & operands info
923
static void fill_insn(struct cs_struct *handle, cs_insn *insn, SStream *OS,
924
          MCInst *mci, PostPrinter_t postprinter,
925
          const uint8_t *code)
926
2.45M
{
927
2.45M
#ifndef CAPSTONE_DIET
928
2.45M
  char *sp;
929
2.45M
#endif
930
2.45M
  SStream_trimls(OS);
931
2.45M
  uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
932
933
  // fill the instruction bytes.
934
  // we might skip some redundant bytes in front in the case of X86
935
2.45M
  memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
936
2.45M
  insn->op_str[0] = '\0';
937
2.45M
  insn->size = copy_size;
938
939
  // alias instruction might have ID saved in OpcodePub
940
2.45M
  if (MCInst_getOpcodePub(mci))
941
76.0k
    insn->id = MCInst_getOpcodePub(mci);
942
943
  // post printer handles some corner cases (hacky)
944
2.45M
  if (postprinter)
945
659k
    postprinter((csh)handle, insn, OS, mci);
946
947
2.45M
#ifndef CAPSTONE_DIET
948
2.45M
  memset(insn->mnemonic, '\0', sizeof(insn->mnemonic));
949
2.45M
  memset(insn->op_str, '\0', sizeof(insn->op_str));
950
2.45M
  SStream_extract_mnem_opstr(OS, insn->mnemonic, sizeof(insn->mnemonic),
951
2.45M
           insn->op_str, sizeof(insn->op_str));
952
13.7M
  for (sp = insn->mnemonic; *sp; sp++) {
953
11.2M
    if (*sp == '|') // lock|rep prefix for x86
954
59.7k
      *sp = ' ';
955
11.2M
  }
956
957
  // we might have customized mnemonic
958
2.45M
  if (handle->mnem_list) {
959
0
    struct insn_mnem *tmp = handle->mnem_list;
960
0
    while (tmp) {
961
0
      if (tmp->insn.id == insn->id) {
962
0
        char str[CS_MNEMONIC_SIZE] = { 0 };
963
964
0
        if (!str_replace(str, insn->mnemonic,
965
0
             cs_insn_name((csh)handle,
966
0
                    insn->id),
967
0
             tmp->insn.mnemonic)) {
968
          // copy result to mnemonic
969
0
          CS_ASSERT_RET(sizeof(insn->mnemonic) ==
970
0
                  sizeof(str));
971
0
          (void)memcpy(insn->mnemonic, str,
972
0
                 sizeof(insn->mnemonic));
973
0
          insn->mnemonic[sizeof(insn->mnemonic) -
974
0
                   1] = '\0';
975
0
        }
976
977
0
        break;
978
0
      }
979
0
      tmp = tmp->next;
980
0
    }
981
0
  }
982
2.45M
#endif
983
2.45M
}
984
985
// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
986
// this very much depends on instruction alignment requirement of each arch.
987
static uint8_t skipdata_size(cs_struct *handle)
988
0
{
989
0
  switch (handle->arch) {
990
0
  default:
991
    // should never reach
992
0
    return (uint8_t)-1;
993
0
  case CS_ARCH_ARM:
994
    // skip 2 bytes on Thumb mode.
995
0
    if (handle->mode & CS_MODE_THUMB)
996
0
      return 2;
997
    // otherwise, skip 4 bytes
998
0
    return 4;
999
0
  case CS_ARCH_AARCH64:
1000
0
  case CS_ARCH_MIPS:
1001
0
  case CS_ARCH_PPC:
1002
0
  case CS_ARCH_SPARC:
1003
    // skip 4 bytes
1004
0
    return 4;
1005
0
  case CS_ARCH_SYSTEMZ:
1006
    // SystemZ instruction's length can be 2, 4 or 6 bytes,
1007
    // so we just skip 2 bytes
1008
0
    return 2;
1009
0
  case CS_ARCH_X86:
1010
    // X86 has no restriction on instruction alignment
1011
0
    return 1;
1012
0
  case CS_ARCH_XCORE:
1013
    // XCore instruction's length can be 2 or 4 bytes,
1014
    // so we just skip 2 bytes
1015
0
    return 2;
1016
0
  case CS_ARCH_M68K:
1017
    // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
1018
0
    return 2;
1019
0
  case CS_ARCH_TMS320C64X:
1020
    // TMS320C64x alignment is 4.
1021
0
    return 4;
1022
0
  case CS_ARCH_M680X:
1023
    // M680X alignment is 1.
1024
0
    return 1;
1025
0
  case CS_ARCH_EVM:
1026
    // EVM alignment is 1.
1027
0
    return 1;
1028
0
  case CS_ARCH_WASM:
1029
    //WASM alignment is 1
1030
0
    return 1;
1031
0
  case CS_ARCH_MOS65XX:
1032
    // MOS65XX alignment is 1.
1033
0
    return 1;
1034
0
  case CS_ARCH_BPF:
1035
    // both classic and extended BPF have alignment 8.
1036
0
    return 8;
1037
0
  case CS_ARCH_RISCV:
1038
    // special compress mode
1039
0
    if (handle->mode & CS_MODE_RISCV_C)
1040
0
      return 2;
1041
0
    return 4;
1042
0
  case CS_ARCH_SH:
1043
0
    return 2;
1044
0
  case CS_ARCH_TRICORE:
1045
    // TriCore instruction's length can be 2 or 4 bytes,
1046
    // so we just skip 2 bytes
1047
0
    return 2;
1048
0
  case CS_ARCH_ALPHA:
1049
    // Alpha alignment is 4.
1050
0
    return 4;
1051
0
  case CS_ARCH_HPPA:
1052
    // Hppa alignment is 4.
1053
0
    return 4;
1054
0
  case CS_ARCH_LOONGARCH:
1055
    // LoongArch alignment is 4.
1056
0
    return 4;
1057
0
  case CS_ARCH_ARC:
1058
    // ARC instruction's length can be 2, 4, 6 or 8 bytes,
1059
    // therefore, skip 2 bytes
1060
0
    return 2;
1061
0
  }
1062
0
}
1063
1064
CAPSTONE_EXPORT
1065
cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, uintptr_t value)
1066
42.1k
{
1067
42.1k
  struct cs_struct *handle;
1068
42.1k
  cs_opt_mnem *opt;
1069
1070
  // cs_option() can be called with NULL handle just for CS_OPT_MEM
1071
  // This is supposed to be executed before all other APIs (even cs_open())
1072
42.1k
  if (type == CS_OPT_MEM) {
1073
0
    cs_opt_mem *mem = (cs_opt_mem *)value;
1074
1075
0
    cs_mem_malloc = mem->malloc;
1076
0
    cs_mem_calloc = mem->calloc;
1077
0
    cs_mem_realloc = mem->realloc;
1078
0
    cs_mem_free = mem->free;
1079
0
    cs_vsnprintf = mem->vsnprintf;
1080
1081
0
    return cs_mem_is_setup() ? CS_ERR_OK : CS_ERR_MEMSETUP;
1082
0
  }
1083
1084
42.1k
  handle = (struct cs_struct *)(uintptr_t)ud;
1085
42.1k
  if (!handle)
1086
0
    return CS_ERR_CSH;
1087
1088
42.1k
  switch (type) {
1089
6.88k
  default:
1090
6.88k
    break;
1091
1092
6.88k
  case CS_OPT_UNSIGNED:
1093
0
    handle->imm_unsigned = (cs_opt_value)value;
1094
0
    return CS_ERR_OK;
1095
1096
35.2k
  case CS_OPT_DETAIL:
1097
35.2k
    handle->detail_opt |= (cs_opt_value)value;
1098
35.2k
    return CS_ERR_OK;
1099
1100
0
  case CS_OPT_SKIPDATA:
1101
0
    handle->skipdata = (value == CS_OPT_ON);
1102
0
    if (handle->skipdata) {
1103
0
      if (handle->skipdata_size == 0) {
1104
        // set the default skipdata size
1105
0
        handle->skipdata_size = skipdata_size(handle);
1106
0
      }
1107
0
    }
1108
0
    return CS_ERR_OK;
1109
1110
0
  case CS_OPT_SKIPDATA_SETUP:
1111
0
    if (value) {
1112
0
      handle->skipdata_setup = *((cs_opt_skipdata *)value);
1113
0
      if (handle->skipdata_setup.mnemonic == NULL) {
1114
0
        handle->skipdata_setup.mnemonic = SKIPDATA_MNEM;
1115
0
      }
1116
0
    }
1117
0
    return CS_ERR_OK;
1118
1119
0
  case CS_OPT_MNEMONIC:
1120
0
    opt = (cs_opt_mnem *)value;
1121
0
    if (opt->id) {
1122
0
      if (opt->mnemonic) {
1123
0
        struct insn_mnem *tmp;
1124
1125
        // add new instruction, or replace existing instruction
1126
        // 1. find if we already had this insn in the linked list
1127
0
        tmp = handle->mnem_list;
1128
0
        while (tmp) {
1129
0
          if (tmp->insn.id == opt->id) {
1130
            // found this instruction, so replace its mnemonic
1131
0
            (void)strncpy(
1132
0
              tmp->insn.mnemonic,
1133
0
              opt->mnemonic,
1134
0
              sizeof(tmp->insn
1135
0
                       .mnemonic) -
1136
0
                1);
1137
0
            tmp->insn.mnemonic
1138
0
              [sizeof(tmp->insn.mnemonic) -
1139
0
               1] = '\0';
1140
0
            break;
1141
0
          }
1142
0
          tmp = tmp->next;
1143
0
        }
1144
1145
        // 2. add this instruction if we have not had it yet
1146
0
        if (!tmp) {
1147
0
          tmp = cs_mem_malloc(sizeof(*tmp));
1148
0
          if (!tmp) {
1149
0
            return CS_ERR_MEM;
1150
0
          }
1151
1152
0
          tmp->insn.id = opt->id;
1153
0
          (void)strncpy(
1154
0
            tmp->insn.mnemonic,
1155
0
            opt->mnemonic,
1156
0
            sizeof(tmp->insn.mnemonic) - 1);
1157
0
          tmp->insn.mnemonic
1158
0
            [sizeof(tmp->insn.mnemonic) - 1] =
1159
0
            '\0';
1160
          // this new instruction is heading the list
1161
0
          tmp->next = handle->mnem_list;
1162
0
          handle->mnem_list = tmp;
1163
0
        }
1164
0
        return CS_ERR_OK;
1165
0
      } else {
1166
0
        struct insn_mnem *prev, *tmp;
1167
1168
        // we want to delete an existing instruction
1169
        // iterate the list to find the instruction to remove it
1170
0
        tmp = handle->mnem_list;
1171
0
        prev = tmp;
1172
0
        while (tmp) {
1173
0
          if (tmp->insn.id == opt->id) {
1174
            // delete this instruction
1175
0
            if (tmp == prev) {
1176
              // head of the list
1177
0
              handle->mnem_list =
1178
0
                tmp->next;
1179
0
            } else {
1180
0
              prev->next = tmp->next;
1181
0
            }
1182
0
            cs_mem_free(tmp);
1183
0
            break;
1184
0
          }
1185
0
          prev = tmp;
1186
0
          tmp = tmp->next;
1187
0
        }
1188
0
      }
1189
0
    }
1190
0
    return CS_ERR_OK;
1191
1192
0
  case CS_OPT_MODE:
1193
    // verify if requested mode is valid
1194
0
    if (value &
1195
0
        arch_configs[handle->arch].arch_disallowed_mode_mask) {
1196
0
      return CS_ERR_OPTION;
1197
0
    }
1198
0
    break;
1199
0
  case CS_OPT_ONLY_OFFSET_BRANCH:
1200
0
    handle->PrintBranchImmAsAddress = value == CS_OPT_ON ? false :
1201
0
                       true;
1202
0
    return CS_ERR_OK;
1203
42.1k
  }
1204
1205
6.88k
  if (!arch_configs[handle->arch].arch_option)
1206
0
    return CS_ERR_ARCH;
1207
1208
6.88k
  return arch_configs[handle->arch].arch_option(handle, type, value);
1209
6.88k
}
1210
1211
// generate @op_str for data instruction of SKIPDATA
1212
#ifndef CAPSTONE_DIET
1213
static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
1214
0
{
1215
0
  char *p = opstr;
1216
0
  int len;
1217
0
  size_t i;
1218
0
  size_t available = sizeof(((cs_insn *)NULL)->op_str);
1219
1220
0
  if (!size) {
1221
0
    opstr[0] = '\0';
1222
0
    return;
1223
0
  }
1224
1225
0
  len = cs_snprintf(p, available, "0x%02x", buffer[0]);
1226
0
  if (len < 0 || (size_t)len > available - 1) {
1227
0
    opstr[0] = '\0';
1228
0
    return;
1229
0
  }
1230
0
  p += len;
1231
0
  available -= len;
1232
1233
0
  for (i = 1; i < size; i++) {
1234
0
    len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
1235
0
    if (len < 0) {
1236
0
      break;
1237
0
    }
1238
0
    if ((size_t)len > available - 1) {
1239
0
      break;
1240
0
    }
1241
0
    p += len;
1242
0
    available -= len;
1243
0
  }
1244
0
}
1245
#endif
1246
1247
// dynamically allocate memory to contain disasm insn
1248
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
1249
CAPSTONE_EXPORT
1250
size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size,
1251
            uint64_t offset, size_t count, cs_insn **insn)
1252
35.2k
{
1253
35.2k
  struct cs_struct *handle;
1254
35.2k
  MCInst mci;
1255
35.2k
  uint16_t insn_size;
1256
35.2k
  size_t c = 0, i;
1257
35.2k
  unsigned int f = 0; // index of the next instruction in the cache
1258
35.2k
  cs_insn *insn_cache; // cache contains disassembled instructions
1259
35.2k
  void *total = NULL;
1260
35.2k
  size_t total_size =
1261
35.2k
    0; // total size of output buffer containing all insns
1262
35.2k
  bool r;
1263
35.2k
  void *tmp;
1264
35.2k
  size_t skipdata_bytes;
1265
35.2k
  uint64_t offset_org; // save all the original info of the buffer
1266
35.2k
  size_t size_org;
1267
35.2k
  const uint8_t *buffer_org;
1268
35.2k
  unsigned int cache_size = INSN_CACHE_SIZE;
1269
35.2k
  size_t next_offset;
1270
1271
35.2k
  handle = (struct cs_struct *)(uintptr_t)ud;
1272
35.2k
  if (!handle) {
1273
    // FIXME: how to handle this case:
1274
    // handle->errnum = CS_ERR_HANDLE;
1275
0
    return 0;
1276
0
  }
1277
1278
35.2k
  handle->errnum = CS_ERR_OK;
1279
1280
35.2k
#ifdef CAPSTONE_USE_SYS_DYN_MEM
1281
35.2k
  if (count > 0 && count <= INSN_CACHE_SIZE)
1282
0
    cache_size = (unsigned int)count;
1283
35.2k
#endif
1284
1285
  // save the original offset for SKIPDATA
1286
35.2k
  buffer_org = buffer;
1287
35.2k
  offset_org = offset;
1288
35.2k
  size_org = size;
1289
1290
35.2k
  total_size = sizeof(cs_insn) * cache_size;
1291
35.2k
  total = cs_mem_calloc(sizeof(cs_insn), cache_size);
1292
35.2k
  if (total == NULL) {
1293
    // insufficient memory
1294
0
    handle->errnum = CS_ERR_MEM;
1295
0
    return 0;
1296
0
  }
1297
1298
35.2k
  insn_cache = total;
1299
1300
2.49M
  while (size > 0) {
1301
2.47M
    MCInst_Init(&mci, handle->arch);
1302
2.47M
    mci.csh = handle;
1303
1304
    // relative branches need to know the address & size of current insn
1305
2.47M
    mci.address = offset;
1306
1307
2.47M
    if (handle->detail_opt) {
1308
      // allocate memory for @detail pointer
1309
2.47M
      insn_cache->detail =
1310
2.47M
        cs_mem_calloc(1, sizeof(cs_detail));
1311
2.47M
      if (!insn_cache->detail) {
1312
0
        insn_cache = (cs_insn *)total;
1313
0
        for (i = 0; i < c; i++, insn_cache++)
1314
0
          cs_mem_free(insn_cache->detail);
1315
1316
0
        cs_mem_free(total);
1317
0
        *insn = NULL;
1318
1319
0
        handle->errnum = CS_ERR_MEM;
1320
0
        return 0;
1321
0
      }
1322
2.47M
    } else {
1323
0
      insn_cache->detail = NULL;
1324
0
    }
1325
1326
    // save all the information for non-detailed mode
1327
2.47M
    mci.flat_insn = insn_cache;
1328
2.47M
    mci.flat_insn->address = offset;
1329
#ifdef CAPSTONE_DIET
1330
    // zero out mnemonic & op_str
1331
    mci.flat_insn->mnemonic[0] = '\0';
1332
    mci.flat_insn->op_str[0] = '\0';
1333
#endif
1334
1335
2.47M
    r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset,
1336
2.47M
           handle->getinsn_info);
1337
2.47M
    if (r) {
1338
2.45M
      SStream ss;
1339
2.45M
      SStream_Init(&ss);
1340
1341
2.45M
      mci.flat_insn->size = insn_size;
1342
1343
      // map internal instruction opcode to public insn ID
1344
1345
2.45M
      handle->insn_id(handle, insn_cache, mci.Opcode);
1346
1347
2.45M
      SStream_opt_unum(&ss, handle->imm_unsigned);
1348
2.45M
      handle->printer(&mci, &ss, handle->printer_info);
1349
2.45M
      fill_insn(handle, insn_cache, &ss, &mci,
1350
2.45M
          handle->post_printer, buffer);
1351
1352
      // adjust for pseudo opcode (X86)
1353
2.45M
      if (handle->arch == CS_ARCH_X86 &&
1354
602k
          insn_cache->id != X86_INS_VCMP)
1355
599k
        insn_cache->id += mci.popcode_adjust;
1356
1357
2.45M
      next_offset = insn_size;
1358
2.45M
    } else {
1359
      // encounter a broken instruction
1360
1361
      // free memory of @detail pointer
1362
21.7k
      if (handle->detail_opt) {
1363
21.7k
        cs_mem_free(insn_cache->detail);
1364
21.7k
      }
1365
1366
      // if there is no request to skip data, or remaining data is too small,
1367
      // then bail out
1368
21.7k
      if (!handle->skipdata || handle->skipdata_size > size)
1369
21.7k
        break;
1370
1371
0
      if (handle->skipdata_setup.callback) {
1372
0
        skipdata_bytes = handle->skipdata_setup.callback(
1373
0
          buffer_org, size_org,
1374
0
          (size_t)(offset - offset_org),
1375
0
          handle->skipdata_setup.user_data);
1376
0
        if (skipdata_bytes > size)
1377
          // remaining data is not enough
1378
0
          break;
1379
1380
0
        if (!skipdata_bytes)
1381
          // user requested not to skip data, so bail out
1382
0
          break;
1383
0
      } else
1384
0
        skipdata_bytes = handle->skipdata_size;
1385
1386
      // we have to skip some amount of data, depending on arch & mode
1387
      // invalid ID for this "data" instruction
1388
0
      insn_cache->id = 0;
1389
0
      insn_cache->address = offset;
1390
0
      insn_cache->size = (uint16_t)MIN(
1391
0
        skipdata_bytes, sizeof(insn_cache->bytes));
1392
0
      memcpy(insn_cache->bytes, buffer,
1393
0
             MIN(skipdata_bytes, sizeof(insn_cache->bytes)));
1394
#ifdef CAPSTONE_DIET
1395
      insn_cache->mnemonic[0] = '\0';
1396
      insn_cache->op_str[0] = '\0';
1397
#else
1398
0
      strncpy(insn_cache->mnemonic,
1399
0
        handle->skipdata_setup.mnemonic,
1400
0
        sizeof(insn_cache->mnemonic) - 1);
1401
0
      skipdata_opstr(insn_cache->op_str, buffer,
1402
0
               skipdata_bytes);
1403
0
#endif
1404
0
      insn_cache->detail = NULL;
1405
1406
0
      next_offset = skipdata_bytes;
1407
0
    }
1408
1409
    // one more instruction entering the cache
1410
2.45M
    f++;
1411
1412
    // one more instruction disassembled
1413
2.45M
    c++;
1414
2.45M
    if (count > 0 && c == count)
1415
      // already got requested number of instructions
1416
0
      break;
1417
1418
2.45M
    if (f == cache_size) {
1419
      // full cache, so expand the cache to contain incoming insns
1420
24.9k
      cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
1421
24.9k
      size_t old_total_size = total_size;
1422
24.9k
      total_size += (sizeof(cs_insn) * cache_size);
1423
24.9k
      tmp = cs_mem_realloc(total, total_size);
1424
24.9k
      if (tmp == NULL) { // insufficient memory
1425
0
        if (handle->detail_opt) {
1426
0
          insn_cache = (cs_insn *)total;
1427
0
          for (i = 0; i < c; i++, insn_cache++)
1428
0
            cs_mem_free(insn_cache->detail);
1429
0
        }
1430
1431
0
        cs_mem_free(total);
1432
0
        *insn = NULL;
1433
0
        handle->errnum = CS_ERR_MEM;
1434
0
        return 0;
1435
0
      }
1436
      // Zero reallocated memory to prevent
1437
      // access to uninitialized memory down the line.
1438
24.9k
      memset(((uint8_t *)tmp) + old_total_size, 0,
1439
24.9k
             total_size - old_total_size);
1440
1441
24.9k
      total = tmp;
1442
      // continue to fill in the cache after the last instruction
1443
24.9k
      insn_cache = (cs_insn *)((char *)total +
1444
24.9k
             sizeof(cs_insn) * c);
1445
1446
      // reset f back to 0, so we fill in the cache from beginning
1447
24.9k
      f = 0;
1448
24.9k
    } else
1449
2.43M
      insn_cache++;
1450
1451
2.45M
    buffer += next_offset;
1452
2.45M
    size -= next_offset;
1453
2.45M
    offset += next_offset;
1454
2.45M
  }
1455
1456
35.2k
  if (!c) {
1457
    // we did not disassemble any instruction
1458
795
    cs_mem_free(total);
1459
795
    total = NULL;
1460
34.4k
  } else if (f != cache_size) {
1461
    // total did not fully use the last cache, so downsize it
1462
34.4k
    tmp = cs_mem_realloc(total,
1463
34.4k
             total_size - (cache_size - f) *
1464
34.4k
                sizeof(*insn_cache));
1465
34.4k
    if (tmp == NULL) { // insufficient memory
1466
      // free all detail pointers
1467
0
      if (handle->detail_opt) {
1468
0
        insn_cache = (cs_insn *)total;
1469
0
        for (i = 0; i < c; i++, insn_cache++)
1470
0
          cs_mem_free(insn_cache->detail);
1471
0
      }
1472
1473
0
      cs_mem_free(total);
1474
0
      *insn = NULL;
1475
1476
0
      handle->errnum = CS_ERR_MEM;
1477
0
      return 0;
1478
0
    }
1479
1480
34.4k
    total = tmp;
1481
34.4k
  }
1482
1483
35.2k
  *insn = total;
1484
1485
35.2k
  return c;
1486
35.2k
}
1487
1488
CAPSTONE_EXPORT
1489
void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
1490
34.4k
{
1491
34.4k
  size_t i;
1492
1493
  // free all detail pointers
1494
2.49M
  for (i = 0; i < count; i++)
1495
2.45M
    cs_mem_free(insn[i].detail);
1496
1497
  // then free pointer to cs_insn array
1498
34.4k
  cs_mem_free(insn);
1499
34.4k
}
1500
1501
CAPSTONE_EXPORT
1502
cs_insn *CAPSTONE_API cs_malloc(csh ud)
1503
0
{
1504
0
  cs_insn *insn;
1505
0
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1506
1507
0
  insn = cs_mem_malloc(sizeof(cs_insn));
1508
0
  if (!insn) {
1509
    // insufficient memory
1510
0
    handle->errnum = CS_ERR_MEM;
1511
0
    return NULL;
1512
0
  } else {
1513
0
    if (handle->detail_opt) {
1514
      // allocate memory for @detail pointer
1515
0
      insn->detail = cs_mem_malloc(sizeof(cs_detail));
1516
0
      if (insn->detail == NULL) { // insufficient memory
1517
0
        cs_mem_free(insn);
1518
0
        handle->errnum = CS_ERR_MEM;
1519
0
        return NULL;
1520
0
      }
1521
0
    } else
1522
0
      insn->detail = NULL;
1523
0
  }
1524
1525
0
  return insn;
1526
0
}
1527
1528
// iterator for instruction "single-stepping"
1529
CAPSTONE_EXPORT
1530
bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
1531
         uint64_t *address, cs_insn *insn)
1532
0
{
1533
0
  if (*size == 0)
1534
0
    return false;
1535
1536
0
  struct cs_struct *handle;
1537
0
  uint16_t insn_size;
1538
0
  MCInst mci;
1539
0
  bool r;
1540
1541
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1542
0
  if (!handle) {
1543
0
    return false;
1544
0
  }
1545
1546
0
  handle->errnum = CS_ERR_OK;
1547
1548
0
  MCInst_Init(&mci, handle->arch);
1549
0
  mci.csh = handle;
1550
1551
  // relative branches need to know the address & size of current insn
1552
0
  mci.address = *address;
1553
1554
  // save all the information for non-detailed mode
1555
0
  mci.flat_insn = insn;
1556
0
  mci.flat_insn->address = *address;
1557
#ifdef CAPSTONE_DIET
1558
  // zero out mnemonic & op_str
1559
  mci.flat_insn->mnemonic[0] = '\0';
1560
  mci.flat_insn->op_str[0] = '\0';
1561
#endif
1562
1563
0
  r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address,
1564
0
         handle->getinsn_info);
1565
0
  if (r) {
1566
0
    SStream ss;
1567
0
    SStream_Init(&ss);
1568
1569
0
    mci.flat_insn->size = insn_size;
1570
1571
    // map internal instruction opcode to public insn ID
1572
0
    handle->insn_id(handle, insn, mci.Opcode);
1573
1574
0
    SStream_opt_unum(&ss, handle->imm_unsigned);
1575
0
    handle->printer(&mci, &ss, handle->printer_info);
1576
1577
0
    fill_insn(handle, insn, &ss, &mci, handle->post_printer, *code);
1578
1579
    // adjust for pseudo opcode (X86)
1580
0
    if (handle->arch == CS_ARCH_X86)
1581
0
      insn->id += mci.popcode_adjust;
1582
1583
0
    *code += insn_size;
1584
0
    *size -= insn_size;
1585
0
    *address += insn_size;
1586
0
  } else { // encounter a broken instruction
1587
0
    size_t skipdata_bytes;
1588
1589
    // if there is no request to skip data, or remaining data is too small,
1590
    // then bail out
1591
0
    if (!handle->skipdata || handle->skipdata_size > *size)
1592
0
      return false;
1593
1594
0
    if (handle->skipdata_setup.callback) {
1595
0
      skipdata_bytes = handle->skipdata_setup.callback(
1596
0
        *code, *size, 0,
1597
0
        handle->skipdata_setup.user_data);
1598
0
      if (skipdata_bytes > *size)
1599
        // remaining data is not enough
1600
0
        return false;
1601
1602
0
      if (!skipdata_bytes)
1603
        // user requested not to skip data, so bail out
1604
0
        return false;
1605
0
    } else
1606
0
      skipdata_bytes = handle->skipdata_size;
1607
1608
    // we have to skip some amount of data, depending on arch & mode
1609
0
    insn->id = 0; // invalid ID for this "data" instruction
1610
0
    insn->address = *address;
1611
0
    insn->size = (uint16_t)MIN(skipdata_bytes, sizeof(insn->bytes));
1612
0
    memcpy(insn->bytes, *code,
1613
0
           MIN(skipdata_bytes, sizeof(insn->bytes)));
1614
#ifdef CAPSTONE_DIET
1615
    insn->mnemonic[0] = '\0';
1616
    insn->op_str[0] = '\0';
1617
#else
1618
0
    strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1619
0
      sizeof(insn->mnemonic) - 1);
1620
0
    skipdata_opstr(insn->op_str, *code, skipdata_bytes);
1621
0
#endif
1622
1623
0
    *code += skipdata_bytes;
1624
0
    *size -= skipdata_bytes;
1625
0
    *address += skipdata_bytes;
1626
0
  }
1627
1628
0
  return true;
1629
0
}
1630
1631
// return friendly name of register in a string
1632
CAPSTONE_EXPORT
1633
const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
1634
2.52M
{
1635
2.52M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1636
1637
2.52M
  if (!handle || handle->reg_name == NULL) {
1638
0
    return NULL;
1639
0
  }
1640
1641
2.52M
  return handle->reg_name(ud, reg);
1642
2.52M
}
1643
1644
CAPSTONE_EXPORT
1645
const char *CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
1646
2.45M
{
1647
2.45M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1648
1649
2.45M
  if (!handle || handle->insn_name == NULL) {
1650
0
    return NULL;
1651
0
  }
1652
1653
2.45M
  return handle->insn_name(ud, insn);
1654
2.45M
}
1655
1656
CAPSTONE_EXPORT
1657
const char *CAPSTONE_API cs_group_name(csh ud, unsigned int group)
1658
2.04M
{
1659
2.04M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1660
1661
2.04M
  if (!handle || handle->group_name == NULL) {
1662
0
    return NULL;
1663
0
  }
1664
1665
2.04M
  return handle->group_name(ud, group);
1666
2.04M
}
1667
1668
CAPSTONE_EXPORT
1669
bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn,
1670
        unsigned int group_id)
1671
0
{
1672
0
  struct cs_struct *handle;
1673
0
  if (!ud)
1674
0
    return false;
1675
1676
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1677
1678
0
  if (!handle->detail_opt) {
1679
0
    handle->errnum = CS_ERR_DETAIL;
1680
0
    return false;
1681
0
  }
1682
1683
0
  if (!insn->id) {
1684
0
    handle->errnum = CS_ERR_SKIPDATA;
1685
0
    return false;
1686
0
  }
1687
1688
0
  if (!insn->detail) {
1689
0
    handle->errnum = CS_ERR_DETAIL;
1690
0
    return false;
1691
0
  }
1692
1693
0
  return arr_exist8(insn->detail->groups, insn->detail->groups_count,
1694
0
        group_id);
1695
0
}
1696
1697
CAPSTONE_EXPORT
1698
bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
1699
0
{
1700
0
  struct cs_struct *handle;
1701
0
  if (!ud)
1702
0
    return false;
1703
1704
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1705
1706
0
  if (!handle->detail_opt) {
1707
0
    handle->errnum = CS_ERR_DETAIL;
1708
0
    return false;
1709
0
  }
1710
1711
0
  if (!insn->id) {
1712
0
    handle->errnum = CS_ERR_SKIPDATA;
1713
0
    return false;
1714
0
  }
1715
1716
0
  if (!insn->detail) {
1717
0
    handle->errnum = CS_ERR_DETAIL;
1718
0
    return false;
1719
0
  }
1720
1721
0
  return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count,
1722
0
       reg_id);
1723
0
}
1724
1725
CAPSTONE_EXPORT
1726
bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
1727
0
{
1728
0
  struct cs_struct *handle;
1729
0
  if (!ud)
1730
0
    return false;
1731
1732
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1733
1734
0
  if (!handle->detail_opt) {
1735
0
    handle->errnum = CS_ERR_DETAIL;
1736
0
    return false;
1737
0
  }
1738
1739
0
  if (!insn->id) {
1740
0
    handle->errnum = CS_ERR_SKIPDATA;
1741
0
    return false;
1742
0
  }
1743
1744
0
  if (!insn->detail) {
1745
0
    handle->errnum = CS_ERR_DETAIL;
1746
0
    return false;
1747
0
  }
1748
1749
0
  return arr_exist(insn->detail->regs_write,
1750
0
       insn->detail->regs_write_count, reg_id);
1751
0
}
1752
1753
CAPSTONE_EXPORT
1754
int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
1755
0
{
1756
0
  struct cs_struct *handle;
1757
0
  unsigned int count = 0, i;
1758
0
  if (!ud)
1759
0
    return -1;
1760
1761
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1762
1763
0
  if (!handle->detail_opt) {
1764
0
    handle->errnum = CS_ERR_DETAIL;
1765
0
    return -1;
1766
0
  }
1767
1768
0
  if (!insn->id) {
1769
0
    handle->errnum = CS_ERR_SKIPDATA;
1770
0
    return -1;
1771
0
  }
1772
1773
0
  if (!insn->detail) {
1774
0
    handle->errnum = CS_ERR_DETAIL;
1775
0
    return -1;
1776
0
  }
1777
1778
0
  handle->errnum = CS_ERR_OK;
1779
1780
0
  switch (handle->arch) {
1781
0
  default:
1782
0
    handle->errnum = CS_ERR_HANDLE;
1783
0
    return -1;
1784
0
  case CS_ARCH_ARM:
1785
0
    for (i = 0; i < insn->detail->arm.op_count; i++)
1786
0
      if (insn->detail->arm.operands[i].type ==
1787
0
          (arm_op_type)op_type)
1788
0
        count++;
1789
0
    break;
1790
0
  case CS_ARCH_AARCH64:
1791
0
    for (i = 0; i < insn->detail->aarch64.op_count; i++)
1792
0
      if (insn->detail->aarch64.operands[i].type ==
1793
0
          (aarch64_op_type)op_type)
1794
0
        count++;
1795
0
    break;
1796
0
  case CS_ARCH_X86:
1797
0
    for (i = 0; i < insn->detail->x86.op_count; i++)
1798
0
      if (insn->detail->x86.operands[i].type ==
1799
0
          (x86_op_type)op_type)
1800
0
        count++;
1801
0
    break;
1802
0
  case CS_ARCH_MIPS:
1803
0
    for (i = 0; i < insn->detail->mips.op_count; i++)
1804
0
      if (insn->detail->mips.operands[i].type ==
1805
0
          (mips_op_type)op_type)
1806
0
        count++;
1807
0
    break;
1808
0
  case CS_ARCH_PPC:
1809
0
    for (i = 0; i < insn->detail->ppc.op_count; i++)
1810
0
      if (insn->detail->ppc.operands[i].type ==
1811
0
          (ppc_op_type)op_type)
1812
0
        count++;
1813
0
    break;
1814
0
  case CS_ARCH_SPARC:
1815
0
    for (i = 0; i < insn->detail->sparc.op_count; i++)
1816
0
      if (insn->detail->sparc.operands[i].type ==
1817
0
          (sparc_op_type)op_type)
1818
0
        count++;
1819
0
    break;
1820
0
  case CS_ARCH_SYSTEMZ:
1821
0
    for (i = 0; i < insn->detail->systemz.op_count; i++)
1822
0
      if (insn->detail->systemz.operands[i].type ==
1823
0
          (systemz_op_type)op_type)
1824
0
        count++;
1825
0
    break;
1826
0
  case CS_ARCH_XCORE:
1827
0
    for (i = 0; i < insn->detail->xcore.op_count; i++)
1828
0
      if (insn->detail->xcore.operands[i].type ==
1829
0
          (xcore_op_type)op_type)
1830
0
        count++;
1831
0
    break;
1832
0
  case CS_ARCH_M68K:
1833
0
    for (i = 0; i < insn->detail->m68k.op_count; i++)
1834
0
      if (insn->detail->m68k.operands[i].type ==
1835
0
          (m68k_op_type)op_type)
1836
0
        count++;
1837
0
    break;
1838
0
  case CS_ARCH_TMS320C64X:
1839
0
    for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1840
0
      if (insn->detail->tms320c64x.operands[i].type ==
1841
0
          (tms320c64x_op_type)op_type)
1842
0
        count++;
1843
0
    break;
1844
0
  case CS_ARCH_M680X:
1845
0
    for (i = 0; i < insn->detail->m680x.op_count; i++)
1846
0
      if (insn->detail->m680x.operands[i].type ==
1847
0
          (m680x_op_type)op_type)
1848
0
        count++;
1849
0
    break;
1850
0
  case CS_ARCH_EVM:
1851
0
    break;
1852
0
  case CS_ARCH_MOS65XX:
1853
0
    for (i = 0; i < insn->detail->mos65xx.op_count; i++)
1854
0
      if (insn->detail->mos65xx.operands[i].type ==
1855
0
          (mos65xx_op_type)op_type)
1856
0
        count++;
1857
0
    break;
1858
0
  case CS_ARCH_WASM:
1859
0
    for (i = 0; i < insn->detail->wasm.op_count; i++)
1860
0
      if (insn->detail->wasm.operands[i].type ==
1861
0
          (wasm_op_type)op_type)
1862
0
        count++;
1863
0
    break;
1864
0
  case CS_ARCH_BPF:
1865
0
    for (i = 0; i < insn->detail->bpf.op_count; i++)
1866
0
      if (insn->detail->bpf.operands[i].type ==
1867
0
          (bpf_op_type)op_type)
1868
0
        count++;
1869
0
    break;
1870
0
  case CS_ARCH_RISCV:
1871
0
    for (i = 0; i < insn->detail->riscv.op_count; i++)
1872
0
      if (insn->detail->riscv.operands[i].type ==
1873
0
          (riscv_op_type)op_type)
1874
0
        count++;
1875
0
    break;
1876
0
  case CS_ARCH_TRICORE:
1877
0
    for (i = 0; i < insn->detail->tricore.op_count; i++)
1878
0
      if (insn->detail->tricore.operands[i].type ==
1879
0
          (tricore_op_type)op_type)
1880
0
        count++;
1881
0
    break;
1882
0
  case CS_ARCH_ALPHA:
1883
0
    for (i = 0; i < insn->detail->alpha.op_count; i++)
1884
0
      if (insn->detail->alpha.operands[i].type ==
1885
0
          (alpha_op_type)op_type)
1886
0
        count++;
1887
0
    break;
1888
0
  case CS_ARCH_HPPA:
1889
0
    for (i = 0; i < insn->detail->hppa.op_count; i++)
1890
0
      if (insn->detail->hppa.operands[i].type ==
1891
0
          (hppa_op_type)op_type)
1892
0
        count++;
1893
0
    break;
1894
0
  case CS_ARCH_LOONGARCH:
1895
0
    for (i = 0; i < insn->detail->loongarch.op_count; i++)
1896
0
      if (insn->detail->loongarch.operands[i].type ==
1897
0
          (loongarch_op_type)op_type)
1898
0
        count++;
1899
0
    break;
1900
0
  case CS_ARCH_ARC:
1901
0
    for (i = 0; i < insn->detail->arc.op_count; i++)
1902
0
      if (insn->detail->arc.operands[i].type ==
1903
0
          (arc_op_type)op_type)
1904
0
        count++;
1905
0
    break;
1906
0
  }
1907
1908
0
  return count;
1909
0
}
1910
1911
CAPSTONE_EXPORT
1912
int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
1913
           unsigned int post)
1914
0
{
1915
0
  struct cs_struct *handle;
1916
0
  unsigned int count = 0, i;
1917
0
  if (!ud)
1918
0
    return -1;
1919
1920
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1921
1922
0
  if (!handle->detail_opt) {
1923
0
    handle->errnum = CS_ERR_DETAIL;
1924
0
    return -1;
1925
0
  }
1926
1927
0
  if (!insn->id) {
1928
0
    handle->errnum = CS_ERR_SKIPDATA;
1929
0
    return -1;
1930
0
  }
1931
1932
0
  if (!insn->detail) {
1933
0
    handle->errnum = CS_ERR_DETAIL;
1934
0
    return -1;
1935
0
  }
1936
1937
0
  handle->errnum = CS_ERR_OK;
1938
1939
0
  switch (handle->arch) {
1940
0
  default:
1941
0
    handle->errnum = CS_ERR_HANDLE;
1942
0
    return -1;
1943
0
  case CS_ARCH_ARM:
1944
0
    for (i = 0; i < insn->detail->arm.op_count; i++) {
1945
0
      if (insn->detail->arm.operands[i].type ==
1946
0
          (arm_op_type)op_type)
1947
0
        count++;
1948
0
      if (count == post)
1949
0
        return i;
1950
0
    }
1951
0
    break;
1952
0
  case CS_ARCH_AARCH64:
1953
0
    for (i = 0; i < insn->detail->aarch64.op_count; i++) {
1954
0
      if (insn->detail->aarch64.operands[i].type ==
1955
0
          (aarch64_op_type)op_type)
1956
0
        count++;
1957
0
      if (count == post)
1958
0
        return i;
1959
0
    }
1960
0
    break;
1961
0
  case CS_ARCH_X86:
1962
0
    for (i = 0; i < insn->detail->x86.op_count; i++) {
1963
0
      if (insn->detail->x86.operands[i].type ==
1964
0
          (x86_op_type)op_type)
1965
0
        count++;
1966
0
      if (count == post)
1967
0
        return i;
1968
0
    }
1969
0
    break;
1970
0
  case CS_ARCH_MIPS:
1971
0
    for (i = 0; i < insn->detail->mips.op_count; i++) {
1972
0
      if (insn->detail->mips.operands[i].type ==
1973
0
          (mips_op_type)op_type)
1974
0
        count++;
1975
0
      if (count == post)
1976
0
        return i;
1977
0
    }
1978
0
    break;
1979
0
  case CS_ARCH_PPC:
1980
0
    for (i = 0; i < insn->detail->ppc.op_count; i++) {
1981
0
      if (insn->detail->ppc.operands[i].type ==
1982
0
          (ppc_op_type)op_type)
1983
0
        count++;
1984
0
      if (count == post)
1985
0
        return i;
1986
0
    }
1987
0
    break;
1988
0
  case CS_ARCH_SPARC:
1989
0
    for (i = 0; i < insn->detail->sparc.op_count; i++) {
1990
0
      if (insn->detail->sparc.operands[i].type ==
1991
0
          (sparc_op_type)op_type)
1992
0
        count++;
1993
0
      if (count == post)
1994
0
        return i;
1995
0
    }
1996
0
    break;
1997
0
  case CS_ARCH_SYSTEMZ:
1998
0
    for (i = 0; i < insn->detail->systemz.op_count; i++) {
1999
0
      if (insn->detail->systemz.operands[i].type ==
2000
0
          (systemz_op_type)op_type)
2001
0
        count++;
2002
0
      if (count == post)
2003
0
        return i;
2004
0
    }
2005
0
    break;
2006
0
  case CS_ARCH_XCORE:
2007
0
    for (i = 0; i < insn->detail->xcore.op_count; i++) {
2008
0
      if (insn->detail->xcore.operands[i].type ==
2009
0
          (xcore_op_type)op_type)
2010
0
        count++;
2011
0
      if (count == post)
2012
0
        return i;
2013
0
    }
2014
0
    break;
2015
0
  case CS_ARCH_TRICORE:
2016
0
    for (i = 0; i < insn->detail->tricore.op_count; i++) {
2017
0
      if (insn->detail->tricore.operands[i].type ==
2018
0
          (tricore_op_type)op_type)
2019
0
        count++;
2020
0
      if (count == post)
2021
0
        return i;
2022
0
    }
2023
0
    break;
2024
0
  case CS_ARCH_M68K:
2025
0
    for (i = 0; i < insn->detail->m68k.op_count; i++) {
2026
0
      if (insn->detail->m68k.operands[i].type ==
2027
0
          (m68k_op_type)op_type)
2028
0
        count++;
2029
0
      if (count == post)
2030
0
        return i;
2031
0
    }
2032
0
    break;
2033
0
  case CS_ARCH_TMS320C64X:
2034
0
    for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
2035
0
      if (insn->detail->tms320c64x.operands[i].type ==
2036
0
          (tms320c64x_op_type)op_type)
2037
0
        count++;
2038
0
      if (count == post)
2039
0
        return i;
2040
0
    }
2041
0
    break;
2042
0
  case CS_ARCH_M680X:
2043
0
    for (i = 0; i < insn->detail->m680x.op_count; i++) {
2044
0
      if (insn->detail->m680x.operands[i].type ==
2045
0
          (m680x_op_type)op_type)
2046
0
        count++;
2047
0
      if (count == post)
2048
0
        return i;
2049
0
    }
2050
0
    break;
2051
0
  case CS_ARCH_EVM:
2052
#if 0
2053
      for (i = 0; i < insn->detail->evm.op_count; i++) {
2054
        if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
2055
          count++;
2056
        if (count == post)
2057
          return i;
2058
      }
2059
#endif
2060
0
    break;
2061
0
  case CS_ARCH_MOS65XX:
2062
0
    for (i = 0; i < insn->detail->mos65xx.op_count; i++) {
2063
0
      if (insn->detail->mos65xx.operands[i].type ==
2064
0
          (mos65xx_op_type)op_type)
2065
0
        count++;
2066
0
      if (count == post)
2067
0
        return i;
2068
0
    }
2069
0
    break;
2070
0
  case CS_ARCH_WASM:
2071
0
    for (i = 0; i < insn->detail->wasm.op_count; i++) {
2072
0
      if (insn->detail->wasm.operands[i].type ==
2073
0
          (wasm_op_type)op_type)
2074
0
        count++;
2075
0
      if (count == post)
2076
0
        return i;
2077
0
    }
2078
0
    break;
2079
0
  case CS_ARCH_BPF:
2080
0
    for (i = 0; i < insn->detail->bpf.op_count; i++) {
2081
0
      if (insn->detail->bpf.operands[i].type ==
2082
0
          (bpf_op_type)op_type)
2083
0
        count++;
2084
0
      if (count == post)
2085
0
        return i;
2086
0
    }
2087
0
    break;
2088
0
  case CS_ARCH_RISCV:
2089
0
    for (i = 0; i < insn->detail->riscv.op_count; i++) {
2090
0
      if (insn->detail->riscv.operands[i].type ==
2091
0
          (riscv_op_type)op_type)
2092
0
        count++;
2093
0
      if (count == post)
2094
0
        return i;
2095
0
    }
2096
0
    break;
2097
0
  case CS_ARCH_SH:
2098
0
    for (i = 0; i < insn->detail->sh.op_count; i++) {
2099
0
      if (insn->detail->sh.operands[i].type ==
2100
0
          (sh_op_type)op_type)
2101
0
        count++;
2102
0
      if (count == post)
2103
0
        return i;
2104
0
    }
2105
0
    break;
2106
0
  case CS_ARCH_ALPHA:
2107
0
    for (i = 0; i < insn->detail->alpha.op_count; i++) {
2108
0
      if (insn->detail->alpha.operands[i].type ==
2109
0
          (alpha_op_type)op_type)
2110
0
        count++;
2111
0
      if (count == post)
2112
0
        return i;
2113
0
    }
2114
0
    break;
2115
0
  case CS_ARCH_HPPA:
2116
0
    for (i = 0; i < insn->detail->hppa.op_count; i++) {
2117
0
      if (insn->detail->hppa.operands[i].type ==
2118
0
          (hppa_op_type)op_type)
2119
0
        count++;
2120
0
      if (count == post)
2121
0
        return i;
2122
0
    }
2123
0
    break;
2124
0
  case CS_ARCH_LOONGARCH:
2125
0
    for (i = 0; i < insn->detail->loongarch.op_count; i++) {
2126
0
      if (insn->detail->loongarch.operands[i].type ==
2127
0
          (loongarch_op_type)op_type)
2128
0
        count++;
2129
0
      if (count == post)
2130
0
        return i;
2131
0
    }
2132
0
    break;
2133
0
  case CS_ARCH_ARC:
2134
0
    for (i = 0; i < insn->detail->arc.op_count; i++) {
2135
0
      if (insn->detail->arc.operands[i].type ==
2136
0
          (arc_op_type)op_type)
2137
0
        count++;
2138
0
      if (count == post)
2139
0
        return i;
2140
0
    }
2141
0
    break;
2142
0
  }
2143
2144
0
  return -1;
2145
0
}
2146
2147
CAPSTONE_EXPORT
2148
cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
2149
           cs_regs regs_read, uint8_t *regs_read_count,
2150
           cs_regs regs_write,
2151
           uint8_t *regs_write_count)
2152
0
{
2153
0
  struct cs_struct *handle;
2154
2155
0
  if (!ud)
2156
0
    return CS_ERR_CSH;
2157
2158
0
  handle = (struct cs_struct *)(uintptr_t)ud;
2159
2160
#ifdef CAPSTONE_DIET
2161
  // This API does not work in DIET mode
2162
  handle->errnum = CS_ERR_DIET;
2163
  return CS_ERR_DIET;
2164
#else
2165
0
  if (!handle->detail_opt) {
2166
0
    handle->errnum = CS_ERR_DETAIL;
2167
0
    return CS_ERR_DETAIL;
2168
0
  }
2169
2170
0
  if (!insn->id) {
2171
0
    handle->errnum = CS_ERR_SKIPDATA;
2172
0
    return CS_ERR_SKIPDATA;
2173
0
  }
2174
2175
0
  if (!insn->detail) {
2176
0
    handle->errnum = CS_ERR_DETAIL;
2177
0
    return CS_ERR_DETAIL;
2178
0
  }
2179
2180
0
  if (handle->reg_access) {
2181
0
    handle->reg_access(insn, regs_read, regs_read_count, regs_write,
2182
0
           regs_write_count);
2183
0
  } else {
2184
    // this arch is unsupported yet
2185
0
    handle->errnum = CS_ERR_ARCH;
2186
0
    return CS_ERR_ARCH;
2187
0
  }
2188
2189
0
  return CS_ERR_OK;
2190
0
#endif
2191
0
}