Coverage Report

Created: 2026-04-12 06:30

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