Coverage Report

Created: 2026-02-26 07:11

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