Coverage Report

Created: 2026-12-01 07:03

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