Coverage Report

Created: 2026-07-16 06:55

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