Coverage Report

Created: 2026-05-30 06:22

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
40.0k
#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
40.0k
#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), \
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
  return kern_os_malloc(num * size); // malloc bzeroes the buffer
505
}
506
507
cs_malloc_t cs_mem_malloc = kern_os_malloc;
508
cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
509
cs_realloc_t cs_mem_realloc = kern_os_realloc;
510
cs_free_t cs_mem_free = kern_os_free;
511
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
512
#endif // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
513
#else
514
// User-defined
515
cs_malloc_t cs_mem_malloc = NULL;
516
cs_calloc_t cs_mem_calloc = NULL;
517
cs_realloc_t cs_mem_realloc = NULL;
518
cs_free_t cs_mem_free = NULL;
519
cs_vsnprintf_t cs_vsnprintf = NULL;
520
521
#endif // defined(CAPSTONE_USE_SYS_DYN_MEM)
522
523
CAPSTONE_EXPORT
524
unsigned int CAPSTONE_API cs_version(int *major, int *minor)
525
0
{
526
0
  if (major != NULL && minor != NULL) {
527
0
    *major = CS_API_MAJOR;
528
0
    *minor = CS_API_MINOR;
529
0
  }
530
531
0
  return (CS_API_MAJOR << 8) + CS_API_MINOR;
532
0
}
533
534
#define CS_ARCH_REGISTER(id) \
535
  cs_arch_config cfg = CS_ARCH_CONFIG_##id; \
536
  arch_configs[CS_ARCH_##id] = cfg; \
537
  all_arch |= 1 << CS_ARCH_##id
538
539
CAPSTONE_EXPORT
540
void CAPSTONE_API cs_arch_register_arm(void)
541
0
{
542
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ARM)
543
  CS_ARCH_REGISTER(ARM);
544
#endif
545
0
}
546
547
CAPSTONE_EXPORT
548
void CAPSTONE_API cs_arch_register_aarch64(void)
549
0
{
550
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_AARCH64)
551
  CS_ARCH_REGISTER(AARCH64);
552
#endif
553
0
}
554
555
CAPSTONE_EXPORT
556
void CAPSTONE_API cs_arch_register_mips(void)
557
0
{
558
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_MIPS)
559
  CS_ARCH_REGISTER(MIPS);
560
#endif
561
0
}
562
563
CAPSTONE_EXPORT
564
void CAPSTONE_API cs_arch_register_x86(void)
565
0
{
566
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_X86)
567
  CS_ARCH_REGISTER(X86);
568
#endif
569
0
}
570
571
CAPSTONE_EXPORT
572
void CAPSTONE_API cs_arch_register_powerpc(void)
573
0
{
574
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_POWERPC)
575
  CS_ARCH_REGISTER(PPC);
576
#endif
577
0
}
578
579
CAPSTONE_EXPORT
580
void CAPSTONE_API cs_arch_register_sparc(void)
581
0
{
582
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SPARC)
583
  CS_ARCH_REGISTER(SPARC);
584
#endif
585
0
}
586
587
CAPSTONE_EXPORT
588
void CAPSTONE_API cs_arch_register_systemz(void)
589
0
{
590
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SYSTEMZ)
591
  CS_ARCH_REGISTER(SYSTEMZ);
592
#endif
593
0
}
594
595
CAPSTONE_EXPORT
596
void CAPSTONE_API cs_arch_register_xcore(void)
597
0
{
598
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_XCORE)
599
  CS_ARCH_REGISTER(XCORE);
600
#endif
601
0
}
602
603
CAPSTONE_EXPORT
604
void CAPSTONE_API cs_arch_register_m68k(void)
605
0
{
606
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_M68K)
607
  CS_ARCH_REGISTER(M68K);
608
#endif
609
0
}
610
611
CAPSTONE_EXPORT
612
void CAPSTONE_API cs_arch_register_tms320c64x(void)
613
0
{
614
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_TMS320C64X)
615
  CS_ARCH_REGISTER(TMS320C64X);
616
#endif
617
0
}
618
619
CAPSTONE_EXPORT
620
void CAPSTONE_API cs_arch_register_m680x(void)
621
0
{
622
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_M680X)
623
  CS_ARCH_REGISTER(M680X);
624
#endif
625
0
}
626
627
CAPSTONE_EXPORT
628
void CAPSTONE_API cs_arch_register_evm(void)
629
0
{
630
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_EVM)
631
  CS_ARCH_REGISTER(EVM);
632
#endif
633
0
}
634
635
CAPSTONE_EXPORT
636
void CAPSTONE_API cs_arch_register_mos65xx(void)
637
0
{
638
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_MOS65XX)
639
  CS_ARCH_REGISTER(MOS65XX);
640
#endif
641
0
}
642
643
CAPSTONE_EXPORT
644
void CAPSTONE_API cs_arch_register_wasm(void)
645
0
{
646
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_WASM)
647
  CS_ARCH_REGISTER(WASM);
648
#endif
649
0
}
650
651
CAPSTONE_EXPORT
652
void CAPSTONE_API cs_arch_register_bpf(void)
653
0
{
654
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_BPF)
655
  CS_ARCH_REGISTER(BPF);
656
#endif
657
0
}
658
659
CAPSTONE_EXPORT
660
void CAPSTONE_API cs_arch_register_riscv(void)
661
0
{
662
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_RISCV)
663
  CS_ARCH_REGISTER(RISCV);
664
#endif
665
0
}
666
667
CAPSTONE_EXPORT
668
void CAPSTONE_API cs_arch_register_sh(void)
669
0
{
670
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_SH)
671
  CS_ARCH_REGISTER(SH);
672
#endif
673
0
}
674
675
CAPSTONE_EXPORT
676
void CAPSTONE_API cs_arch_register_tricore(void)
677
0
{
678
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_TRICORE)
679
  CS_ARCH_REGISTER(TRICORE);
680
#endif
681
0
}
682
683
CAPSTONE_EXPORT
684
void CAPSTONE_API cs_arch_register_alpha(void)
685
0
{
686
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ALPHA)
687
  CS_ARCH_REGISTER(ALPHA);
688
#endif
689
0
}
690
691
CAPSTONE_EXPORT
692
void CAPSTONE_API cs_arch_register_loongarch(void)
693
0
{
694
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_LOONGARCH)
695
  CS_ARCH_REGISTER(LOONGARCH);
696
#endif
697
0
}
698
699
CAPSTONE_EXPORT
700
void CAPSTONE_API cs_arch_register_arc(void)
701
0
{
702
#if defined(CAPSTONE_USE_ARCH_REGISTRATION) && defined(CAPSTONE_HAS_ARC)
703
  CS_ARCH_REGISTER(ARC);
704
#endif
705
0
}
706
707
CAPSTONE_EXPORT
708
bool CAPSTONE_API cs_support(int query)
709
0
{
710
0
  if (query == CS_ARCH_ALL)
711
0
    return all_arch ==
712
0
           ((1 << CS_ARCH_ARM) | (1 << CS_ARCH_AARCH64) |
713
0
      (1 << CS_ARCH_MIPS) | (1 << CS_ARCH_X86) |
714
0
      (1 << CS_ARCH_PPC) | (1 << CS_ARCH_SPARC) |
715
0
      (1 << CS_ARCH_SYSTEMZ) | (1 << CS_ARCH_XCORE) |
716
0
      (1 << CS_ARCH_M68K) | (1 << CS_ARCH_TMS320C64X) |
717
0
      (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM) |
718
0
      (1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX) |
719
0
      (1 << CS_ARCH_WASM) | (1 << CS_ARCH_BPF) |
720
0
      (1 << CS_ARCH_SH) | (1 << CS_ARCH_TRICORE) |
721
0
      (1 << CS_ARCH_ALPHA) | (1 << CS_ARCH_HPPA) |
722
0
      (1 << CS_ARCH_LOONGARCH) | (1 << CS_ARCH_XTENSA) |
723
0
      (1 << CS_ARCH_ARC));
724
725
0
  if ((unsigned int)query < CS_ARCH_MAX)
726
0
    return all_arch & (1 << query);
727
728
0
  if (query == CS_SUPPORT_DIET) {
729
#ifdef CAPSTONE_DIET
730
    return true;
731
#else
732
0
    return false;
733
0
#endif
734
0
  }
735
736
0
  if (query == CS_SUPPORT_X86_REDUCE) {
737
#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
738
    return true;
739
#else
740
0
    return false;
741
0
#endif
742
0
  }
743
744
  // unsupported query
745
0
  return false;
746
0
}
747
748
CAPSTONE_EXPORT
749
cs_err CAPSTONE_API cs_errno(csh handle)
750
0
{
751
0
  struct cs_struct *ud;
752
0
  if (!handle)
753
0
    return CS_ERR_CSH;
754
755
0
  ud = (struct cs_struct *)(uintptr_t)handle;
756
757
0
  return ud->errnum;
758
0
}
759
760
CAPSTONE_EXPORT
761
const char *CAPSTONE_API cs_strerror(cs_err code)
762
0
{
763
0
  switch (code) {
764
0
  default:
765
0
    return "Unknown error code";
766
0
  case CS_ERR_OK:
767
0
    return "OK (CS_ERR_OK)";
768
0
  case CS_ERR_MEM:
769
0
    return "Out of memory (CS_ERR_MEM)";
770
0
  case CS_ERR_ARCH:
771
0
    return "Invalid/unsupported architecture(CS_ERR_ARCH)";
772
0
  case CS_ERR_HANDLE:
773
0
    return "Invalid handle (CS_ERR_HANDLE)";
774
0
  case CS_ERR_CSH:
775
0
    return "Invalid csh (CS_ERR_CSH)";
776
0
  case CS_ERR_MODE:
777
0
    return "Invalid mode (CS_ERR_MODE)";
778
0
  case CS_ERR_OPTION:
779
0
    return "Invalid option (CS_ERR_OPTION)";
780
0
  case CS_ERR_DETAIL:
781
0
    return "Details are unavailable (CS_ERR_DETAIL)";
782
0
  case CS_ERR_MEMSETUP:
783
0
    return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
784
0
  case CS_ERR_VERSION:
785
0
    return "Different API version between core & binding (CS_ERR_VERSION)";
786
0
  case CS_ERR_DIET:
787
0
    return "Information irrelevant in diet engine (CS_ERR_DIET)";
788
0
  case CS_ERR_SKIPDATA:
789
0
    return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
790
0
  case CS_ERR_X86_ATT:
791
0
    return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
792
0
  case CS_ERR_X86_INTEL:
793
0
    return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
794
0
  case CS_ERR_X86_MASM:
795
0
    return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
796
0
  }
797
0
}
798
799
CAPSTONE_EXPORT
800
cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
801
40.0k
{
802
40.0k
  cs_err err = CS_ERR_ARCH;
803
40.0k
  struct cs_struct *ud = NULL;
804
805
40.0k
  if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc ||
806
40.0k
      !cs_mem_free || !cs_vsnprintf) {
807
    // Error: before cs_open(), dynamic memory management must be initialized
808
    // with cs_option(CS_OPT_MEM)
809
0
    err = CS_ERR_MEMSETUP;
810
0
    goto fail;
811
0
  }
812
813
40.0k
  if (arch >= CS_ARCH_MAX || !arch_configs[arch].arch_init) {
814
0
    err = CS_ERR_ARCH;
815
0
    goto fail;
816
0
  }
817
818
  // verify if requested mode is valid
819
40.0k
  if (mode & arch_configs[arch].arch_disallowed_mode_mask) {
820
7
    err = CS_ERR_MODE;
821
7
    goto fail;
822
7
  }
823
824
40.0k
  ud = cs_mem_calloc(1, sizeof(*ud));
825
40.0k
  if (!ud) {
826
0
    err = CS_ERR_MEM;
827
0
    goto fail;
828
0
  }
829
830
40.0k
  ud->errnum = CS_ERR_OK;
831
40.0k
  ud->arch = arch;
832
40.0k
  ud->mode = mode;
833
  // by default, do not break instruction into details
834
40.0k
  ud->detail_opt = CS_OPT_OFF;
835
40.0k
  ud->PrintBranchImmAsAddress = true;
836
837
  // default skipdata setup
838
40.0k
  ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
839
840
40.0k
  if ((err = arch_configs[ud->arch].arch_init(ud)))
841
0
    goto fail;
842
843
40.0k
  if ((err = populate_insn_map_cache(ud)))
844
0
    goto fail;
845
846
40.0k
  *handle = (uintptr_t)ud;
847
40.0k
  return CS_ERR_OK;
848
849
7
fail:
850
7
  if (ud) {
851
0
    cs_mem_free(ud);
852
0
  }
853
7
  *handle = 0;
854
7
  return err;
855
40.0k
}
856
857
CAPSTONE_EXPORT
858
cs_err CAPSTONE_API cs_close(csh *handle)
859
40.0k
{
860
40.0k
  struct cs_struct *ud = NULL;
861
40.0k
  struct insn_mnem *next = NULL, *tmp = NULL;
862
863
40.0k
  if (*handle == 0)
864
    // invalid handle
865
0
    return CS_ERR_CSH;
866
867
40.0k
  ud = (struct cs_struct *)(*handle);
868
869
40.0k
  if (ud->printer_info)
870
37.8k
    cs_mem_free(ud->printer_info);
871
872
  // free the linked list of customized mnemonic
873
40.0k
  tmp = ud->mnem_list;
874
40.0k
  while (tmp) {
875
0
    next = tmp->next;
876
0
    cs_mem_free(tmp);
877
0
    tmp = next;
878
0
  }
879
880
40.0k
  cs_mem_free(ud->insn_cache);
881
40.0k
  cs_mem_free(ud->x86_insn_lut);
882
40.0k
  cs_mem_free(ud->x86_insn_reg_lut);
883
884
40.0k
  memset(ud, 0, sizeof(*ud));
885
40.0k
  cs_mem_free(ud);
886
887
  // invalidate this handle by ZERO out its value.
888
  // this is to make sure it is unusable after cs_close()
889
40.0k
  *handle = 0;
890
891
40.0k
  return CS_ERR_OK;
892
40.0k
}
893
894
/// replace str1 in target with str2; target starts with str1
895
/// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
896
/// return 0 on success, -1 on failure
897
#ifndef CAPSTONE_DIET
898
static int str_replace(char *result, char *target, const char *str1, char *str2)
899
0
{
900
0
  size_t target_len = strlen(target);
901
0
  size_t str1_len = strlen(str1);
902
0
  if (target_len < str1_len) {
903
0
    return -1;
904
0
  }
905
906
  // only perform replacement if the output fits into result
907
0
  if (target_len - str1_len + strlen(str2) <= CS_MNEMONIC_SIZE - 1) {
908
    // copy str2 to beginning of result
909
    // skip str1 - already replaced by str2
910
0
    snprintf(result, CS_MNEMONIC_SIZE, "%s%s", str2,
911
0
       target + str1_len);
912
913
0
    return 0;
914
0
  } else
915
0
    return -1;
916
0
}
917
#endif
918
919
// fill insn with mnemonic & operands info
920
static void fill_insn(struct cs_struct *handle, cs_insn *insn, SStream *OS,
921
          MCInst *mci, PostPrinter_t postprinter,
922
          const uint8_t *code)
923
3.03M
{
924
3.03M
#ifndef CAPSTONE_DIET
925
3.03M
  char *sp;
926
3.03M
#endif
927
3.03M
  SStream_trimls(OS);
928
3.03M
  uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
929
930
  // fill the instruction bytes.
931
  // we might skip some redundant bytes in front in the case of X86
932
3.03M
  memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
933
3.03M
  insn->op_str[0] = '\0';
934
3.03M
  insn->size = copy_size;
935
936
  // alias instruction might have ID saved in OpcodePub
937
3.03M
  if (MCInst_getOpcodePub(mci))
938
111k
    insn->id = MCInst_getOpcodePub(mci);
939
940
  // post printer handles some corner cases (hacky)
941
3.03M
  if (postprinter)
942
823k
    postprinter((csh)handle, insn, OS, mci);
943
944
3.03M
#ifndef CAPSTONE_DIET
945
3.03M
  memset(insn->mnemonic, '\0', sizeof(insn->mnemonic));
946
3.03M
  memset(insn->op_str, '\0', sizeof(insn->op_str));
947
3.03M
  SStream_extract_mnem_opstr(OS, insn->mnemonic, sizeof(insn->mnemonic),
948
3.03M
           insn->op_str, sizeof(insn->op_str));
949
17.1M
  for (sp = insn->mnemonic; *sp; sp++) {
950
14.1M
    if (*sp == '|') // lock|rep prefix for x86
951
74.8k
      *sp = ' ';
952
14.1M
  }
953
954
  // we might have customized mnemonic
955
3.03M
  if (handle->mnem_list) {
956
0
    struct insn_mnem *tmp = handle->mnem_list;
957
0
    while (tmp) {
958
0
      if (tmp->insn.id == insn->id) {
959
0
        char str[CS_MNEMONIC_SIZE] = { 0 };
960
961
0
        if (!str_replace(str, insn->mnemonic,
962
0
             cs_insn_name((csh)handle,
963
0
                    insn->id),
964
0
             tmp->insn.mnemonic)) {
965
          // copy result to mnemonic
966
0
          CS_ASSERT_RET(sizeof(insn->mnemonic) ==
967
0
                  sizeof(str));
968
0
          (void)memcpy(insn->mnemonic, str,
969
0
                 sizeof(insn->mnemonic));
970
0
          insn->mnemonic[sizeof(insn->mnemonic) -
971
0
                   1] = '\0';
972
0
        }
973
974
0
        break;
975
0
      }
976
0
      tmp = tmp->next;
977
0
    }
978
0
  }
979
3.03M
#endif
980
3.03M
}
981
982
// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
983
// this very much depends on instruction alignment requirement of each arch.
984
static uint8_t skipdata_size(cs_struct *handle)
985
0
{
986
0
  switch (handle->arch) {
987
0
  default:
988
    // should never reach
989
0
    return (uint8_t)-1;
990
0
  case CS_ARCH_ARM:
991
    // skip 2 bytes on Thumb mode.
992
0
    if (handle->mode & CS_MODE_THUMB)
993
0
      return 2;
994
    // otherwise, skip 4 bytes
995
0
    return 4;
996
0
  case CS_ARCH_AARCH64:
997
0
  case CS_ARCH_MIPS:
998
0
  case CS_ARCH_PPC:
999
0
  case CS_ARCH_SPARC:
1000
    // skip 4 bytes
1001
0
    return 4;
1002
0
  case CS_ARCH_SYSTEMZ:
1003
    // SystemZ instruction's length can be 2, 4 or 6 bytes,
1004
    // so we just skip 2 bytes
1005
0
    return 2;
1006
0
  case CS_ARCH_X86:
1007
    // X86 has no restriction on instruction alignment
1008
0
    return 1;
1009
0
  case CS_ARCH_XCORE:
1010
    // XCore instruction's length can be 2 or 4 bytes,
1011
    // so we just skip 2 bytes
1012
0
    return 2;
1013
0
  case CS_ARCH_M68K:
1014
    // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
1015
0
    return 2;
1016
0
  case CS_ARCH_TMS320C64X:
1017
    // TMS320C64x alignment is 4.
1018
0
    return 4;
1019
0
  case CS_ARCH_M680X:
1020
    // M680X alignment is 1.
1021
0
    return 1;
1022
0
  case CS_ARCH_EVM:
1023
    // EVM alignment is 1.
1024
0
    return 1;
1025
0
  case CS_ARCH_WASM:
1026
    //WASM alignment is 1
1027
0
    return 1;
1028
0
  case CS_ARCH_MOS65XX:
1029
    // MOS65XX alignment is 1.
1030
0
    return 1;
1031
0
  case CS_ARCH_BPF:
1032
    // both classic and extended BPF have alignment 8.
1033
0
    return 8;
1034
0
  case CS_ARCH_RISCV:
1035
    // special compress mode
1036
0
    if (handle->mode & CS_MODE_RISCV_C)
1037
0
      return 2;
1038
0
    return 4;
1039
0
  case CS_ARCH_SH:
1040
0
    return 2;
1041
0
  case CS_ARCH_TRICORE:
1042
    // TriCore instruction's length can be 2 or 4 bytes,
1043
    // so we just skip 2 bytes
1044
0
    return 2;
1045
0
  case CS_ARCH_ALPHA:
1046
    // Alpha alignment is 4.
1047
0
    return 4;
1048
0
  case CS_ARCH_HPPA:
1049
    // Hppa alignment is 4.
1050
0
    return 4;
1051
0
  case CS_ARCH_LOONGARCH:
1052
    // LoongArch alignment is 4.
1053
0
    return 4;
1054
0
  case CS_ARCH_ARC:
1055
    // ARC instruction's length can be 2, 4, 6 or 8 bytes,
1056
    // therefore, skip 2 bytes
1057
0
    return 2;
1058
0
  }
1059
0
}
1060
1061
CAPSTONE_EXPORT
1062
cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, uintptr_t value)
1063
45.8k
{
1064
45.8k
  struct cs_struct *handle;
1065
45.8k
  cs_opt_mnem *opt;
1066
1067
  // cs_option() can be called with NULL handle just for CS_OPT_MEM
1068
  // This is supposed to be executed before all other APIs (even cs_open())
1069
45.8k
  if (type == CS_OPT_MEM) {
1070
0
    cs_opt_mem *mem = (cs_opt_mem *)value;
1071
1072
0
    cs_mem_malloc = mem->malloc;
1073
0
    cs_mem_calloc = mem->calloc;
1074
0
    cs_mem_realloc = mem->realloc;
1075
0
    cs_mem_free = mem->free;
1076
0
    cs_vsnprintf = mem->vsnprintf;
1077
1078
0
    return CS_ERR_OK;
1079
0
  }
1080
1081
45.8k
  handle = (struct cs_struct *)(uintptr_t)ud;
1082
45.8k
  if (!handle)
1083
0
    return CS_ERR_CSH;
1084
1085
45.8k
  switch (type) {
1086
5.79k
  default:
1087
5.79k
    break;
1088
1089
5.79k
  case CS_OPT_UNSIGNED:
1090
0
    handle->imm_unsigned = (cs_opt_value)value;
1091
0
    return CS_ERR_OK;
1092
1093
40.0k
  case CS_OPT_DETAIL:
1094
40.0k
    handle->detail_opt |= (cs_opt_value)value;
1095
40.0k
    return CS_ERR_OK;
1096
1097
0
  case CS_OPT_SKIPDATA:
1098
0
    handle->skipdata = (value == CS_OPT_ON);
1099
0
    if (handle->skipdata) {
1100
0
      if (handle->skipdata_size == 0) {
1101
        // set the default skipdata size
1102
0
        handle->skipdata_size = skipdata_size(handle);
1103
0
      }
1104
0
    }
1105
0
    return CS_ERR_OK;
1106
1107
0
  case CS_OPT_SKIPDATA_SETUP:
1108
0
    if (value) {
1109
0
      handle->skipdata_setup = *((cs_opt_skipdata *)value);
1110
0
      if (handle->skipdata_setup.mnemonic == NULL) {
1111
0
        handle->skipdata_setup.mnemonic = SKIPDATA_MNEM;
1112
0
      }
1113
0
    }
1114
0
    return CS_ERR_OK;
1115
1116
0
  case CS_OPT_MNEMONIC:
1117
0
    opt = (cs_opt_mnem *)value;
1118
0
    if (opt->id) {
1119
0
      if (opt->mnemonic) {
1120
0
        struct insn_mnem *tmp;
1121
1122
        // add new instruction, or replace existing instruction
1123
        // 1. find if we already had this insn in the linked list
1124
0
        tmp = handle->mnem_list;
1125
0
        while (tmp) {
1126
0
          if (tmp->insn.id == opt->id) {
1127
            // found this instruction, so replace its mnemonic
1128
0
            (void)strncpy(
1129
0
              tmp->insn.mnemonic,
1130
0
              opt->mnemonic,
1131
0
              sizeof(tmp->insn
1132
0
                       .mnemonic) -
1133
0
                1);
1134
0
            tmp->insn.mnemonic
1135
0
              [sizeof(tmp->insn.mnemonic) -
1136
0
               1] = '\0';
1137
0
            break;
1138
0
          }
1139
0
          tmp = tmp->next;
1140
0
        }
1141
1142
        // 2. add this instruction if we have not had it yet
1143
0
        if (!tmp) {
1144
0
          tmp = cs_mem_malloc(sizeof(*tmp));
1145
0
          if (!tmp) {
1146
0
            return CS_ERR_MEM;
1147
0
          }
1148
1149
0
          tmp->insn.id = opt->id;
1150
0
          (void)strncpy(
1151
0
            tmp->insn.mnemonic,
1152
0
            opt->mnemonic,
1153
0
            sizeof(tmp->insn.mnemonic) - 1);
1154
0
          tmp->insn.mnemonic
1155
0
            [sizeof(tmp->insn.mnemonic) - 1] =
1156
0
            '\0';
1157
          // this new instruction is heading the list
1158
0
          tmp->next = handle->mnem_list;
1159
0
          handle->mnem_list = tmp;
1160
0
        }
1161
0
        return CS_ERR_OK;
1162
0
      } else {
1163
0
        struct insn_mnem *prev, *tmp;
1164
1165
        // we want to delete an existing instruction
1166
        // iterate the list to find the instruction to remove it
1167
0
        tmp = handle->mnem_list;
1168
0
        prev = tmp;
1169
0
        while (tmp) {
1170
0
          if (tmp->insn.id == opt->id) {
1171
            // delete this instruction
1172
0
            if (tmp == prev) {
1173
              // head of the list
1174
0
              handle->mnem_list =
1175
0
                tmp->next;
1176
0
            } else {
1177
0
              prev->next = tmp->next;
1178
0
            }
1179
0
            cs_mem_free(tmp);
1180
0
            break;
1181
0
          }
1182
0
          prev = tmp;
1183
0
          tmp = tmp->next;
1184
0
        }
1185
0
      }
1186
0
    }
1187
0
    return CS_ERR_OK;
1188
1189
0
  case CS_OPT_MODE:
1190
    // verify if requested mode is valid
1191
0
    if (value &
1192
0
        arch_configs[handle->arch].arch_disallowed_mode_mask) {
1193
0
      return CS_ERR_OPTION;
1194
0
    }
1195
0
    break;
1196
0
  case CS_OPT_ONLY_OFFSET_BRANCH:
1197
0
    handle->PrintBranchImmAsAddress = value == CS_OPT_ON ? false :
1198
0
                       true;
1199
0
    return CS_ERR_OK;
1200
45.8k
  }
1201
1202
5.79k
  if (!arch_configs[handle->arch].arch_option)
1203
0
    return CS_ERR_ARCH;
1204
1205
5.79k
  return arch_configs[handle->arch].arch_option(handle, type, value);
1206
5.79k
}
1207
1208
// generate @op_str for data instruction of SKIPDATA
1209
#ifndef CAPSTONE_DIET
1210
static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
1211
0
{
1212
0
  char *p = opstr;
1213
0
  int len;
1214
0
  size_t i;
1215
0
  size_t available = sizeof(((cs_insn *)NULL)->op_str);
1216
1217
0
  if (!size) {
1218
0
    opstr[0] = '\0';
1219
0
    return;
1220
0
  }
1221
1222
0
  len = cs_snprintf(p, available, "0x%02x", buffer[0]);
1223
0
  p += len;
1224
0
  available -= len;
1225
1226
0
  for (i = 1; i < size; i++) {
1227
0
    len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
1228
0
    if (len < 0) {
1229
0
      break;
1230
0
    }
1231
0
    if ((size_t)len > available - 1) {
1232
0
      break;
1233
0
    }
1234
0
    p += len;
1235
0
    available -= len;
1236
0
  }
1237
0
}
1238
#endif
1239
1240
// dynamically allocate memory to contain disasm insn
1241
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
1242
CAPSTONE_EXPORT
1243
size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size,
1244
            uint64_t offset, size_t count, cs_insn **insn)
1245
40.0k
{
1246
40.0k
  struct cs_struct *handle;
1247
40.0k
  MCInst mci;
1248
40.0k
  uint16_t insn_size;
1249
40.0k
  size_t c = 0, i;
1250
40.0k
  unsigned int f = 0; // index of the next instruction in the cache
1251
40.0k
  cs_insn *insn_cache; // cache contains disassembled instructions
1252
40.0k
  void *total = NULL;
1253
40.0k
  size_t total_size =
1254
40.0k
    0; // total size of output buffer containing all insns
1255
40.0k
  bool r;
1256
40.0k
  void *tmp;
1257
40.0k
  size_t skipdata_bytes;
1258
40.0k
  uint64_t offset_org; // save all the original info of the buffer
1259
40.0k
  size_t size_org;
1260
40.0k
  const uint8_t *buffer_org;
1261
40.0k
  unsigned int cache_size = INSN_CACHE_SIZE;
1262
40.0k
  size_t next_offset;
1263
1264
40.0k
  handle = (struct cs_struct *)(uintptr_t)ud;
1265
40.0k
  if (!handle) {
1266
    // FIXME: how to handle this case:
1267
    // handle->errnum = CS_ERR_HANDLE;
1268
0
    return 0;
1269
0
  }
1270
1271
40.0k
  handle->errnum = CS_ERR_OK;
1272
1273
40.0k
#ifdef CAPSTONE_USE_SYS_DYN_MEM
1274
40.0k
  if (count > 0 && count <= INSN_CACHE_SIZE)
1275
0
    cache_size = (unsigned int)count;
1276
40.0k
#endif
1277
1278
  // save the original offset for SKIPDATA
1279
40.0k
  buffer_org = buffer;
1280
40.0k
  offset_org = offset;
1281
40.0k
  size_org = size;
1282
1283
40.0k
  total_size = sizeof(cs_insn) * cache_size;
1284
40.0k
  total = cs_mem_calloc(sizeof(cs_insn), cache_size);
1285
40.0k
  if (total == NULL) {
1286
    // insufficient memory
1287
0
    handle->errnum = CS_ERR_MEM;
1288
0
    return 0;
1289
0
  }
1290
1291
40.0k
  insn_cache = total;
1292
1293
3.07M
  while (size > 0) {
1294
3.05M
    MCInst_Init(&mci, handle->arch);
1295
3.05M
    mci.csh = handle;
1296
1297
    // relative branches need to know the address & size of current insn
1298
3.05M
    mci.address = offset;
1299
1300
3.05M
    if (handle->detail_opt) {
1301
      // allocate memory for @detail pointer
1302
3.05M
      insn_cache->detail =
1303
3.05M
        cs_mem_calloc(1, sizeof(cs_detail));
1304
3.05M
      if (!insn_cache->detail) {
1305
0
        insn_cache = (cs_insn *)total;
1306
0
        for (i = 0; i < c; i++, insn_cache++)
1307
0
          cs_mem_free(insn_cache->detail);
1308
1309
0
        cs_mem_free(total);
1310
0
        *insn = NULL;
1311
1312
0
        handle->errnum = CS_ERR_MEM;
1313
0
        return 0;
1314
0
      }
1315
3.05M
    } else {
1316
0
      insn_cache->detail = NULL;
1317
0
    }
1318
1319
    // save all the information for non-detailed mode
1320
3.05M
    mci.flat_insn = insn_cache;
1321
3.05M
    mci.flat_insn->address = offset;
1322
#ifdef CAPSTONE_DIET
1323
    // zero out mnemonic & op_str
1324
    mci.flat_insn->mnemonic[0] = '\0';
1325
    mci.flat_insn->op_str[0] = '\0';
1326
#endif
1327
1328
3.05M
    r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset,
1329
3.05M
           handle->getinsn_info);
1330
3.05M
    if (r) {
1331
3.03M
      SStream ss;
1332
3.03M
      SStream_Init(&ss);
1333
1334
3.03M
      mci.flat_insn->size = insn_size;
1335
1336
      // map internal instruction opcode to public insn ID
1337
1338
3.03M
      handle->insn_id(handle, insn_cache, mci.Opcode);
1339
1340
3.03M
      SStream_opt_unum(&ss, handle->imm_unsigned);
1341
3.03M
      handle->printer(&mci, &ss, handle->printer_info);
1342
3.03M
      fill_insn(handle, insn_cache, &ss, &mci,
1343
3.03M
          handle->post_printer, buffer);
1344
1345
      // adjust for pseudo opcode (X86)
1346
3.03M
      if (handle->arch == CS_ARCH_X86 &&
1347
757k
          insn_cache->id != X86_INS_VCMP)
1348
753k
        insn_cache->id += mci.popcode_adjust;
1349
1350
3.03M
      next_offset = insn_size;
1351
3.03M
    } else {
1352
      // encounter a broken instruction
1353
1354
      // free memory of @detail pointer
1355
25.9k
      if (handle->detail_opt) {
1356
25.9k
        cs_mem_free(insn_cache->detail);
1357
25.9k
      }
1358
1359
      // if there is no request to skip data, or remaining data is too small,
1360
      // then bail out
1361
25.9k
      if (!handle->skipdata || handle->skipdata_size > size)
1362
25.9k
        break;
1363
1364
0
      if (handle->skipdata_setup.callback) {
1365
0
        skipdata_bytes = handle->skipdata_setup.callback(
1366
0
          buffer_org, size_org,
1367
0
          (size_t)(offset - offset_org),
1368
0
          handle->skipdata_setup.user_data);
1369
0
        if (skipdata_bytes > size)
1370
          // remaining data is not enough
1371
0
          break;
1372
1373
0
        if (!skipdata_bytes)
1374
          // user requested not to skip data, so bail out
1375
0
          break;
1376
0
      } else
1377
0
        skipdata_bytes = handle->skipdata_size;
1378
1379
      // we have to skip some amount of data, depending on arch & mode
1380
      // invalid ID for this "data" instruction
1381
0
      insn_cache->id = 0;
1382
0
      insn_cache->address = offset;
1383
0
      insn_cache->size = (uint16_t)MIN(
1384
0
        skipdata_bytes, sizeof(insn_cache->bytes));
1385
0
      memcpy(insn_cache->bytes, buffer,
1386
0
             MIN(skipdata_bytes, sizeof(insn_cache->bytes)));
1387
#ifdef CAPSTONE_DIET
1388
      insn_cache->mnemonic[0] = '\0';
1389
      insn_cache->op_str[0] = '\0';
1390
#else
1391
0
      strncpy(insn_cache->mnemonic,
1392
0
        handle->skipdata_setup.mnemonic,
1393
0
        sizeof(insn_cache->mnemonic) - 1);
1394
0
      skipdata_opstr(insn_cache->op_str, buffer,
1395
0
               skipdata_bytes);
1396
0
#endif
1397
0
      insn_cache->detail = NULL;
1398
1399
0
      next_offset = skipdata_bytes;
1400
0
    }
1401
1402
    // one more instruction entering the cache
1403
3.03M
    f++;
1404
1405
    // one more instruction disassembled
1406
3.03M
    c++;
1407
3.03M
    if (count > 0 && c == count)
1408
      // already got requested number of instructions
1409
0
      break;
1410
1411
3.03M
    if (f == cache_size) {
1412
      // full cache, so expand the cache to contain incoming insns
1413
30.7k
      cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
1414
30.7k
      total_size += (sizeof(cs_insn) * cache_size);
1415
30.7k
      tmp = cs_mem_realloc(total, total_size);
1416
30.7k
      if (tmp == NULL) { // insufficient memory
1417
0
        if (handle->detail_opt) {
1418
0
          insn_cache = (cs_insn *)total;
1419
0
          for (i = 0; i < c; i++, insn_cache++)
1420
0
            cs_mem_free(insn_cache->detail);
1421
0
        }
1422
1423
0
        cs_mem_free(total);
1424
0
        *insn = NULL;
1425
0
        handle->errnum = CS_ERR_MEM;
1426
0
        return 0;
1427
0
      }
1428
1429
30.7k
      total = tmp;
1430
      // continue to fill in the cache after the last instruction
1431
30.7k
      insn_cache = (cs_insn *)((char *)total +
1432
30.7k
             sizeof(cs_insn) * c);
1433
1434
      // reset f back to 0, so we fill in the cache from beginning
1435
30.7k
      f = 0;
1436
30.7k
    } else
1437
3.00M
      insn_cache++;
1438
1439
3.03M
    buffer += next_offset;
1440
3.03M
    size -= next_offset;
1441
3.03M
    offset += next_offset;
1442
3.03M
  }
1443
1444
40.0k
  if (!c) {
1445
    // we did not disassemble any instruction
1446
821
    cs_mem_free(total);
1447
821
    total = NULL;
1448
39.2k
  } else if (f != cache_size) {
1449
    // total did not fully use the last cache, so downsize it
1450
39.2k
    tmp = cs_mem_realloc(total,
1451
39.2k
             total_size - (cache_size - f) *
1452
39.2k
                sizeof(*insn_cache));
1453
39.2k
    if (tmp == NULL) { // insufficient memory
1454
      // free all detail pointers
1455
0
      if (handle->detail_opt) {
1456
0
        insn_cache = (cs_insn *)total;
1457
0
        for (i = 0; i < c; i++, insn_cache++)
1458
0
          cs_mem_free(insn_cache->detail);
1459
0
      }
1460
1461
0
      cs_mem_free(total);
1462
0
      *insn = NULL;
1463
1464
0
      handle->errnum = CS_ERR_MEM;
1465
0
      return 0;
1466
0
    }
1467
1468
39.2k
    total = tmp;
1469
39.2k
  }
1470
1471
40.0k
  *insn = total;
1472
1473
40.0k
  return c;
1474
40.0k
}
1475
1476
CAPSTONE_EXPORT
1477
void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
1478
39.2k
{
1479
39.2k
  size_t i;
1480
1481
  // free all detail pointers
1482
3.07M
  for (i = 0; i < count; i++)
1483
3.03M
    cs_mem_free(insn[i].detail);
1484
1485
  // then free pointer to cs_insn array
1486
39.2k
  cs_mem_free(insn);
1487
39.2k
}
1488
1489
CAPSTONE_EXPORT
1490
cs_insn *CAPSTONE_API cs_malloc(csh ud)
1491
0
{
1492
0
  cs_insn *insn;
1493
0
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1494
1495
0
  insn = cs_mem_malloc(sizeof(cs_insn));
1496
0
  if (!insn) {
1497
    // insufficient memory
1498
0
    handle->errnum = CS_ERR_MEM;
1499
0
    return NULL;
1500
0
  } else {
1501
0
    if (handle->detail_opt) {
1502
      // allocate memory for @detail pointer
1503
0
      insn->detail = cs_mem_malloc(sizeof(cs_detail));
1504
0
      if (insn->detail == NULL) { // insufficient memory
1505
0
        cs_mem_free(insn);
1506
0
        handle->errnum = CS_ERR_MEM;
1507
0
        return NULL;
1508
0
      }
1509
0
    } else
1510
0
      insn->detail = NULL;
1511
0
  }
1512
1513
0
  return insn;
1514
0
}
1515
1516
// iterator for instruction "single-stepping"
1517
CAPSTONE_EXPORT
1518
bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
1519
         uint64_t *address, cs_insn *insn)
1520
0
{
1521
0
  if (*size == 0)
1522
0
    return false;
1523
1524
0
  struct cs_struct *handle;
1525
0
  uint16_t insn_size;
1526
0
  MCInst mci;
1527
0
  bool r;
1528
1529
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1530
0
  if (!handle) {
1531
0
    return false;
1532
0
  }
1533
1534
0
  handle->errnum = CS_ERR_OK;
1535
1536
0
  MCInst_Init(&mci, handle->arch);
1537
0
  mci.csh = handle;
1538
1539
  // relative branches need to know the address & size of current insn
1540
0
  mci.address = *address;
1541
1542
  // save all the information for non-detailed mode
1543
0
  mci.flat_insn = insn;
1544
0
  mci.flat_insn->address = *address;
1545
#ifdef CAPSTONE_DIET
1546
  // zero out mnemonic & op_str
1547
  mci.flat_insn->mnemonic[0] = '\0';
1548
  mci.flat_insn->op_str[0] = '\0';
1549
#endif
1550
1551
0
  r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address,
1552
0
         handle->getinsn_info);
1553
0
  if (r) {
1554
0
    SStream ss;
1555
0
    SStream_Init(&ss);
1556
1557
0
    mci.flat_insn->size = insn_size;
1558
1559
    // map internal instruction opcode to public insn ID
1560
0
    handle->insn_id(handle, insn, mci.Opcode);
1561
1562
0
    SStream_opt_unum(&ss, handle->imm_unsigned);
1563
0
    handle->printer(&mci, &ss, handle->printer_info);
1564
1565
0
    fill_insn(handle, insn, &ss, &mci, handle->post_printer, *code);
1566
1567
    // adjust for pseudo opcode (X86)
1568
0
    if (handle->arch == CS_ARCH_X86)
1569
0
      insn->id += mci.popcode_adjust;
1570
1571
0
    *code += insn_size;
1572
0
    *size -= insn_size;
1573
0
    *address += insn_size;
1574
0
  } else { // encounter a broken instruction
1575
0
    size_t skipdata_bytes;
1576
1577
    // if there is no request to skip data, or remaining data is too small,
1578
    // then bail out
1579
0
    if (!handle->skipdata || handle->skipdata_size > *size)
1580
0
      return false;
1581
1582
0
    if (handle->skipdata_setup.callback) {
1583
0
      skipdata_bytes = handle->skipdata_setup.callback(
1584
0
        *code, *size, 0,
1585
0
        handle->skipdata_setup.user_data);
1586
0
      if (skipdata_bytes > *size)
1587
        // remaining data is not enough
1588
0
        return false;
1589
1590
0
      if (!skipdata_bytes)
1591
        // user requested not to skip data, so bail out
1592
0
        return false;
1593
0
    } else
1594
0
      skipdata_bytes = handle->skipdata_size;
1595
1596
    // we have to skip some amount of data, depending on arch & mode
1597
0
    insn->id = 0; // invalid ID for this "data" instruction
1598
0
    insn->address = *address;
1599
0
    insn->size = (uint16_t)MIN(skipdata_bytes, sizeof(insn->bytes));
1600
0
    memcpy(insn->bytes, *code,
1601
0
           MIN(skipdata_bytes, sizeof(insn->bytes)));
1602
#ifdef CAPSTONE_DIET
1603
    insn->mnemonic[0] = '\0';
1604
    insn->op_str[0] = '\0';
1605
#else
1606
0
    strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1607
0
      sizeof(insn->mnemonic) - 1);
1608
0
    skipdata_opstr(insn->op_str, *code, skipdata_bytes);
1609
0
#endif
1610
1611
0
    *code += skipdata_bytes;
1612
0
    *size -= skipdata_bytes;
1613
0
    *address += skipdata_bytes;
1614
0
  }
1615
1616
0
  return true;
1617
0
}
1618
1619
// return friendly name of register in a string
1620
CAPSTONE_EXPORT
1621
const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
1622
2.99M
{
1623
2.99M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1624
1625
2.99M
  if (!handle || handle->reg_name == NULL) {
1626
0
    return NULL;
1627
0
  }
1628
1629
2.99M
  return handle->reg_name(ud, reg);
1630
2.99M
}
1631
1632
CAPSTONE_EXPORT
1633
const char *CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
1634
3.03M
{
1635
3.03M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1636
1637
3.03M
  if (!handle || handle->insn_name == NULL) {
1638
0
    return NULL;
1639
0
  }
1640
1641
3.03M
  return handle->insn_name(ud, insn);
1642
3.03M
}
1643
1644
CAPSTONE_EXPORT
1645
const char *CAPSTONE_API cs_group_name(csh ud, unsigned int group)
1646
2.63M
{
1647
2.63M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1648
1649
2.63M
  if (!handle || handle->group_name == NULL) {
1650
0
    return NULL;
1651
0
  }
1652
1653
2.63M
  return handle->group_name(ud, group);
1654
2.63M
}
1655
1656
CAPSTONE_EXPORT
1657
bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn,
1658
        unsigned int group_id)
1659
0
{
1660
0
  struct cs_struct *handle;
1661
0
  if (!ud)
1662
0
    return false;
1663
1664
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1665
1666
0
  if (!handle->detail_opt) {
1667
0
    handle->errnum = CS_ERR_DETAIL;
1668
0
    return false;
1669
0
  }
1670
1671
0
  if (!insn->id) {
1672
0
    handle->errnum = CS_ERR_SKIPDATA;
1673
0
    return false;
1674
0
  }
1675
1676
0
  if (!insn->detail) {
1677
0
    handle->errnum = CS_ERR_DETAIL;
1678
0
    return false;
1679
0
  }
1680
1681
0
  return arr_exist8(insn->detail->groups, insn->detail->groups_count,
1682
0
        group_id);
1683
0
}
1684
1685
CAPSTONE_EXPORT
1686
bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
1687
0
{
1688
0
  struct cs_struct *handle;
1689
0
  if (!ud)
1690
0
    return false;
1691
1692
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1693
1694
0
  if (!handle->detail_opt) {
1695
0
    handle->errnum = CS_ERR_DETAIL;
1696
0
    return false;
1697
0
  }
1698
1699
0
  if (!insn->id) {
1700
0
    handle->errnum = CS_ERR_SKIPDATA;
1701
0
    return false;
1702
0
  }
1703
1704
0
  if (!insn->detail) {
1705
0
    handle->errnum = CS_ERR_DETAIL;
1706
0
    return false;
1707
0
  }
1708
1709
0
  return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count,
1710
0
       reg_id);
1711
0
}
1712
1713
CAPSTONE_EXPORT
1714
bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
1715
0
{
1716
0
  struct cs_struct *handle;
1717
0
  if (!ud)
1718
0
    return false;
1719
1720
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1721
1722
0
  if (!handle->detail_opt) {
1723
0
    handle->errnum = CS_ERR_DETAIL;
1724
0
    return false;
1725
0
  }
1726
1727
0
  if (!insn->id) {
1728
0
    handle->errnum = CS_ERR_SKIPDATA;
1729
0
    return false;
1730
0
  }
1731
1732
0
  if (!insn->detail) {
1733
0
    handle->errnum = CS_ERR_DETAIL;
1734
0
    return false;
1735
0
  }
1736
1737
0
  return arr_exist(insn->detail->regs_write,
1738
0
       insn->detail->regs_write_count, reg_id);
1739
0
}
1740
1741
CAPSTONE_EXPORT
1742
int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
1743
0
{
1744
0
  struct cs_struct *handle;
1745
0
  unsigned int count = 0, i;
1746
0
  if (!ud)
1747
0
    return -1;
1748
1749
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1750
1751
0
  if (!handle->detail_opt) {
1752
0
    handle->errnum = CS_ERR_DETAIL;
1753
0
    return -1;
1754
0
  }
1755
1756
0
  if (!insn->id) {
1757
0
    handle->errnum = CS_ERR_SKIPDATA;
1758
0
    return -1;
1759
0
  }
1760
1761
0
  if (!insn->detail) {
1762
0
    handle->errnum = CS_ERR_DETAIL;
1763
0
    return -1;
1764
0
  }
1765
1766
0
  handle->errnum = CS_ERR_OK;
1767
1768
0
  switch (handle->arch) {
1769
0
  default:
1770
0
    handle->errnum = CS_ERR_HANDLE;
1771
0
    return -1;
1772
0
  case CS_ARCH_ARM:
1773
0
    for (i = 0; i < insn->detail->arm.op_count; i++)
1774
0
      if (insn->detail->arm.operands[i].type ==
1775
0
          (arm_op_type)op_type)
1776
0
        count++;
1777
0
    break;
1778
0
  case CS_ARCH_AARCH64:
1779
0
    for (i = 0; i < insn->detail->aarch64.op_count; i++)
1780
0
      if (insn->detail->aarch64.operands[i].type ==
1781
0
          (aarch64_op_type)op_type)
1782
0
        count++;
1783
0
    break;
1784
0
  case CS_ARCH_X86:
1785
0
    for (i = 0; i < insn->detail->x86.op_count; i++)
1786
0
      if (insn->detail->x86.operands[i].type ==
1787
0
          (x86_op_type)op_type)
1788
0
        count++;
1789
0
    break;
1790
0
  case CS_ARCH_MIPS:
1791
0
    for (i = 0; i < insn->detail->mips.op_count; i++)
1792
0
      if (insn->detail->mips.operands[i].type ==
1793
0
          (mips_op_type)op_type)
1794
0
        count++;
1795
0
    break;
1796
0
  case CS_ARCH_PPC:
1797
0
    for (i = 0; i < insn->detail->ppc.op_count; i++)
1798
0
      if (insn->detail->ppc.operands[i].type ==
1799
0
          (ppc_op_type)op_type)
1800
0
        count++;
1801
0
    break;
1802
0
  case CS_ARCH_SPARC:
1803
0
    for (i = 0; i < insn->detail->sparc.op_count; i++)
1804
0
      if (insn->detail->sparc.operands[i].type ==
1805
0
          (sparc_op_type)op_type)
1806
0
        count++;
1807
0
    break;
1808
0
  case CS_ARCH_SYSTEMZ:
1809
0
    for (i = 0; i < insn->detail->systemz.op_count; i++)
1810
0
      if (insn->detail->systemz.operands[i].type ==
1811
0
          (systemz_op_type)op_type)
1812
0
        count++;
1813
0
    break;
1814
0
  case CS_ARCH_XCORE:
1815
0
    for (i = 0; i < insn->detail->xcore.op_count; i++)
1816
0
      if (insn->detail->xcore.operands[i].type ==
1817
0
          (xcore_op_type)op_type)
1818
0
        count++;
1819
0
    break;
1820
0
  case CS_ARCH_M68K:
1821
0
    for (i = 0; i < insn->detail->m68k.op_count; i++)
1822
0
      if (insn->detail->m68k.operands[i].type ==
1823
0
          (m68k_op_type)op_type)
1824
0
        count++;
1825
0
    break;
1826
0
  case CS_ARCH_TMS320C64X:
1827
0
    for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1828
0
      if (insn->detail->tms320c64x.operands[i].type ==
1829
0
          (tms320c64x_op_type)op_type)
1830
0
        count++;
1831
0
    break;
1832
0
  case CS_ARCH_M680X:
1833
0
    for (i = 0; i < insn->detail->m680x.op_count; i++)
1834
0
      if (insn->detail->m680x.operands[i].type ==
1835
0
          (m680x_op_type)op_type)
1836
0
        count++;
1837
0
    break;
1838
0
  case CS_ARCH_EVM:
1839
0
    break;
1840
0
  case CS_ARCH_MOS65XX:
1841
0
    for (i = 0; i < insn->detail->mos65xx.op_count; i++)
1842
0
      if (insn->detail->mos65xx.operands[i].type ==
1843
0
          (mos65xx_op_type)op_type)
1844
0
        count++;
1845
0
    break;
1846
0
  case CS_ARCH_WASM:
1847
0
    for (i = 0; i < insn->detail->wasm.op_count; i++)
1848
0
      if (insn->detail->wasm.operands[i].type ==
1849
0
          (wasm_op_type)op_type)
1850
0
        count++;
1851
0
    break;
1852
0
  case CS_ARCH_BPF:
1853
0
    for (i = 0; i < insn->detail->bpf.op_count; i++)
1854
0
      if (insn->detail->bpf.operands[i].type ==
1855
0
          (bpf_op_type)op_type)
1856
0
        count++;
1857
0
    break;
1858
0
  case CS_ARCH_RISCV:
1859
0
    for (i = 0; i < insn->detail->riscv.op_count; i++)
1860
0
      if (insn->detail->riscv.operands[i].type ==
1861
0
          (riscv_op_type)op_type)
1862
0
        count++;
1863
0
    break;
1864
0
  case CS_ARCH_TRICORE:
1865
0
    for (i = 0; i < insn->detail->tricore.op_count; i++)
1866
0
      if (insn->detail->tricore.operands[i].type ==
1867
0
          (tricore_op_type)op_type)
1868
0
        count++;
1869
0
    break;
1870
0
  case CS_ARCH_ALPHA:
1871
0
    for (i = 0; i < insn->detail->alpha.op_count; i++)
1872
0
      if (insn->detail->alpha.operands[i].type ==
1873
0
          (alpha_op_type)op_type)
1874
0
        count++;
1875
0
    break;
1876
0
  case CS_ARCH_HPPA:
1877
0
    for (i = 0; i < insn->detail->hppa.op_count; i++)
1878
0
      if (insn->detail->hppa.operands[i].type ==
1879
0
          (hppa_op_type)op_type)
1880
0
        count++;
1881
0
    break;
1882
0
  case CS_ARCH_LOONGARCH:
1883
0
    for (i = 0; i < insn->detail->loongarch.op_count; i++)
1884
0
      if (insn->detail->loongarch.operands[i].type ==
1885
0
          (loongarch_op_type)op_type)
1886
0
        count++;
1887
0
    break;
1888
0
  case CS_ARCH_ARC:
1889
0
    for (i = 0; i < insn->detail->arc.op_count; i++)
1890
0
      if (insn->detail->arc.operands[i].type ==
1891
0
          (arc_op_type)op_type)
1892
0
        count++;
1893
0
    break;
1894
0
  }
1895
1896
0
  return count;
1897
0
}
1898
1899
CAPSTONE_EXPORT
1900
int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
1901
           unsigned int post)
1902
0
{
1903
0
  struct cs_struct *handle;
1904
0
  unsigned int count = 0, i;
1905
0
  if (!ud)
1906
0
    return -1;
1907
1908
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1909
1910
0
  if (!handle->detail_opt) {
1911
0
    handle->errnum = CS_ERR_DETAIL;
1912
0
    return -1;
1913
0
  }
1914
1915
0
  if (!insn->id) {
1916
0
    handle->errnum = CS_ERR_SKIPDATA;
1917
0
    return -1;
1918
0
  }
1919
1920
0
  if (!insn->detail) {
1921
0
    handle->errnum = CS_ERR_DETAIL;
1922
0
    return -1;
1923
0
  }
1924
1925
0
  handle->errnum = CS_ERR_OK;
1926
1927
0
  switch (handle->arch) {
1928
0
  default:
1929
0
    handle->errnum = CS_ERR_HANDLE;
1930
0
    return -1;
1931
0
  case CS_ARCH_ARM:
1932
0
    for (i = 0; i < insn->detail->arm.op_count; i++) {
1933
0
      if (insn->detail->arm.operands[i].type ==
1934
0
          (arm_op_type)op_type)
1935
0
        count++;
1936
0
      if (count == post)
1937
0
        return i;
1938
0
    }
1939
0
    break;
1940
0
  case CS_ARCH_AARCH64:
1941
0
    for (i = 0; i < insn->detail->aarch64.op_count; i++) {
1942
0
      if (insn->detail->aarch64.operands[i].type ==
1943
0
          (aarch64_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_X86:
1950
0
    for (i = 0; i < insn->detail->x86.op_count; i++) {
1951
0
      if (insn->detail->x86.operands[i].type ==
1952
0
          (x86_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_MIPS:
1959
0
    for (i = 0; i < insn->detail->mips.op_count; i++) {
1960
0
      if (insn->detail->mips.operands[i].type ==
1961
0
          (mips_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_PPC:
1968
0
    for (i = 0; i < insn->detail->ppc.op_count; i++) {
1969
0
      if (insn->detail->ppc.operands[i].type ==
1970
0
          (ppc_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_SPARC:
1977
0
    for (i = 0; i < insn->detail->sparc.op_count; i++) {
1978
0
      if (insn->detail->sparc.operands[i].type ==
1979
0
          (sparc_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_SYSTEMZ:
1986
0
    for (i = 0; i < insn->detail->systemz.op_count; i++) {
1987
0
      if (insn->detail->systemz.operands[i].type ==
1988
0
          (systemz_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_XCORE:
1995
0
    for (i = 0; i < insn->detail->xcore.op_count; i++) {
1996
0
      if (insn->detail->xcore.operands[i].type ==
1997
0
          (xcore_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_TRICORE:
2004
0
    for (i = 0; i < insn->detail->tricore.op_count; i++) {
2005
0
      if (insn->detail->tricore.operands[i].type ==
2006
0
          (tricore_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_M68K:
2013
0
    for (i = 0; i < insn->detail->m68k.op_count; i++) {
2014
0
      if (insn->detail->m68k.operands[i].type ==
2015
0
          (m68k_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_TMS320C64X:
2022
0
    for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
2023
0
      if (insn->detail->tms320c64x.operands[i].type ==
2024
0
          (tms320c64x_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_M680X:
2031
0
    for (i = 0; i < insn->detail->m680x.op_count; i++) {
2032
0
      if (insn->detail->m680x.operands[i].type ==
2033
0
          (m680x_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_EVM:
2040
#if 0
2041
      for (i = 0; i < insn->detail->evm.op_count; i++) {
2042
        if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
2043
          count++;
2044
        if (count == post)
2045
          return i;
2046
      }
2047
#endif
2048
0
    break;
2049
0
  case CS_ARCH_MOS65XX:
2050
0
    for (i = 0; i < insn->detail->mos65xx.op_count; i++) {
2051
0
      if (insn->detail->mos65xx.operands[i].type ==
2052
0
          (mos65xx_op_type)op_type)
2053
0
        count++;
2054
0
      if (count == post)
2055
0
        return i;
2056
0
    }
2057
0
    break;
2058
0
  case CS_ARCH_WASM:
2059
0
    for (i = 0; i < insn->detail->wasm.op_count; i++) {
2060
0
      if (insn->detail->wasm.operands[i].type ==
2061
0
          (wasm_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_BPF:
2068
0
    for (i = 0; i < insn->detail->bpf.op_count; i++) {
2069
0
      if (insn->detail->bpf.operands[i].type ==
2070
0
          (bpf_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_RISCV:
2077
0
    for (i = 0; i < insn->detail->riscv.op_count; i++) {
2078
0
      if (insn->detail->riscv.operands[i].type ==
2079
0
          (riscv_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_SH:
2086
0
    for (i = 0; i < insn->detail->sh.op_count; i++) {
2087
0
      if (insn->detail->sh.operands[i].type ==
2088
0
          (sh_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_ALPHA:
2095
0
    for (i = 0; i < insn->detail->alpha.op_count; i++) {
2096
0
      if (insn->detail->alpha.operands[i].type ==
2097
0
          (alpha_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_HPPA:
2104
0
    for (i = 0; i < insn->detail->hppa.op_count; i++) {
2105
0
      if (insn->detail->hppa.operands[i].type ==
2106
0
          (hppa_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_LOONGARCH:
2113
0
    for (i = 0; i < insn->detail->loongarch.op_count; i++) {
2114
0
      if (insn->detail->loongarch.operands[i].type ==
2115
0
          (loongarch_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_ARC:
2122
0
    for (i = 0; i < insn->detail->arc.op_count; i++) {
2123
0
      if (insn->detail->arc.operands[i].type ==
2124
0
          (arc_op_type)op_type)
2125
0
        count++;
2126
0
      if (count == post)
2127
0
        return i;
2128
0
    }
2129
0
    break;
2130
0
  }
2131
2132
0
  return -1;
2133
0
}
2134
2135
CAPSTONE_EXPORT
2136
cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
2137
           cs_regs regs_read, uint8_t *regs_read_count,
2138
           cs_regs regs_write,
2139
           uint8_t *regs_write_count)
2140
0
{
2141
0
  struct cs_struct *handle;
2142
2143
0
  if (!ud)
2144
0
    return CS_ERR_CSH;
2145
2146
0
  handle = (struct cs_struct *)(uintptr_t)ud;
2147
2148
#ifdef CAPSTONE_DIET
2149
  // This API does not work in DIET mode
2150
  handle->errnum = CS_ERR_DIET;
2151
  return CS_ERR_DIET;
2152
#else
2153
0
  if (!handle->detail_opt) {
2154
0
    handle->errnum = CS_ERR_DETAIL;
2155
0
    return CS_ERR_DETAIL;
2156
0
  }
2157
2158
0
  if (!insn->id) {
2159
0
    handle->errnum = CS_ERR_SKIPDATA;
2160
0
    return CS_ERR_SKIPDATA;
2161
0
  }
2162
2163
0
  if (!insn->detail) {
2164
0
    handle->errnum = CS_ERR_DETAIL;
2165
0
    return CS_ERR_DETAIL;
2166
0
  }
2167
2168
0
  if (handle->reg_access) {
2169
0
    handle->reg_access(insn, regs_read, regs_read_count, regs_write,
2170
0
           regs_write_count);
2171
0
  } else {
2172
    // this arch is unsupported yet
2173
0
    handle->errnum = CS_ERR_ARCH;
2174
0
    return CS_ERR_ARCH;
2175
0
  }
2176
2177
0
  return CS_ERR_OK;
2178
0
#endif
2179
0
}