Coverage Report

Created: 2026-09-03 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonev5/cs.c
Line
Count
Source
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3
#if defined (WIN32) || defined (WIN64) || defined (_WIN32) || defined (_WIN64)
4
#pragma warning(disable:4996)     // disable MSVC's warning on strcpy()
5
#pragma warning(disable:28719)    // disable MSVC's warning on strcpy()
6
#endif
7
#if defined(CAPSTONE_HAS_OSXKERNEL)
8
#include <Availability.h>
9
#include <libkern/libkern.h>
10
#else
11
#include <stddef.h>
12
#include <stdio.h>
13
#include <stdlib.h>
14
#endif
15
16
#include <string.h>
17
#include <capstone/capstone.h>
18
19
#include "utils.h"
20
#include "MCRegisterInfo.h"
21
22
#if defined(_KERNEL_MODE)
23
#include "windows\winkernel_mm.h"
24
#endif
25
26
// Issue #681: Windows kernel does not support formatting float point
27
#if defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
28
#if defined(CAPSTONE_HAS_ARM) || defined(CAPSTONE_HAS_ARM64) || 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 __FILE__ "("CAPSTONE_STR(__LINE__)") : warning message : "
32
33
#pragma message(CAPSTONE_MSVC_WRANING_PREFIX "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.")
34
35
#undef CAPSTONE_MSVC_WRANING_PREFIX
36
#undef CAPSTONE_STR
37
#undef CAPSTONE_STR_INTERNAL
38
#endif
39
#endif  // defined(_KERNEL_MODE) && !defined(CAPSTONE_DIET)
40
41
#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(CAPSTONE_DIET) && !defined(_KERNEL_MODE)
42
37.2k
#define INSN_CACHE_SIZE 32
43
#else
44
// reduce stack variable size for kernel/firmware
45
#define INSN_CACHE_SIZE 8
46
#endif
47
48
// default SKIPDATA mnemonic
49
#ifndef CAPSTONE_DIET
50
37.2k
#define SKIPDATA_MNEM ".byte"
51
#else // No printing is available in diet mode
52
#define SKIPDATA_MNEM NULL
53
#endif
54
55
#include "arch/AArch64/AArch64Module.h"
56
#include "arch/ARM/ARMModule.h"
57
#include "arch/EVM/EVMModule.h"
58
#include "arch/WASM/WASMModule.h"
59
#include "arch/M680X/M680XModule.h"
60
#include "arch/M68K/M68KModule.h"
61
#include "arch/Mips/MipsModule.h"
62
#include "arch/PowerPC/PPCModule.h"
63
#include "arch/Sparc/SparcModule.h"
64
#include "arch/SystemZ/SystemZModule.h"
65
#include "arch/TMS320C64x/TMS320C64xModule.h"
66
#include "arch/X86/X86Module.h"
67
#include "arch/XCore/XCoreModule.h"
68
#include "arch/RISCV/RISCVModule.h"
69
#include "arch/MOS65XX/MOS65XXModule.h"
70
#include "arch/BPF/BPFModule.h"
71
#include "arch/SH/SHModule.h"
72
#include "arch/TriCore/TriCoreModule.h"
73
74
static const struct {
75
  // constructor initialization
76
  cs_err (*arch_init)(cs_struct *);
77
  // support cs_option()
78
  cs_err (*arch_option)(cs_struct *, cs_opt_type, size_t value);
79
  // bitmask for finding disallowed modes for an arch:
80
  // to be called in cs_open()/cs_option()
81
  cs_mode arch_disallowed_mode_mask;
82
} arch_configs[MAX_ARCH] = {
83
#ifdef CAPSTONE_HAS_ARM
84
  {
85
    ARM_global_init,
86
    ARM_option,
87
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_V8 | CS_MODE_MCLASS
88
        | CS_MODE_THUMB | CS_MODE_BIG_ENDIAN)
89
  },
90
#else
91
  { NULL, NULL, 0 },
92
#endif
93
#ifdef CAPSTONE_HAS_ARM64
94
  {
95
    AArch64_global_init,
96
    AArch64_option,
97
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_BIG_ENDIAN),
98
  },
99
#else
100
  { NULL, NULL, 0 },
101
#endif
102
#ifdef CAPSTONE_HAS_MIPS
103
  {
104
    Mips_global_init,
105
    Mips_option,
106
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_MICRO
107
        | CS_MODE_MIPS32R6 | CS_MODE_BIG_ENDIAN | CS_MODE_MIPS2 | CS_MODE_MIPS3),
108
  },
109
#else
110
  { NULL, NULL, 0 },
111
#endif
112
#ifdef CAPSTONE_HAS_X86
113
  {
114
    X86_global_init,
115
    X86_option,
116
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_16),
117
  },
118
#else
119
  { NULL, NULL, 0 },
120
#endif
121
#ifdef CAPSTONE_HAS_POWERPC
122
  {
123
    PPC_global_init,
124
    PPC_option,
125
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_BIG_ENDIAN
126
        | CS_MODE_QPX | CS_MODE_PS),
127
  },
128
#else
129
  { NULL, NULL, 0 },
130
#endif
131
#ifdef CAPSTONE_HAS_SPARC
132
  {
133
    Sparc_global_init,
134
    Sparc_option,
135
    ~(CS_MODE_BIG_ENDIAN | CS_MODE_V9),
136
  },
137
#else
138
  { NULL, NULL, 0 },
139
#endif
140
#ifdef CAPSTONE_HAS_SYSZ
141
  {
142
    SystemZ_global_init,
143
    SystemZ_option,
144
    ~(CS_MODE_BIG_ENDIAN),
145
  },
146
#else
147
  { NULL, NULL, 0 },
148
#endif
149
#ifdef CAPSTONE_HAS_XCORE
150
  {
151
    XCore_global_init,
152
    XCore_option,
153
    ~(CS_MODE_BIG_ENDIAN),
154
  },
155
#else
156
  { NULL, NULL, 0 },
157
#endif
158
#ifdef CAPSTONE_HAS_M68K
159
  {
160
    M68K_global_init,
161
    M68K_option,
162
    ~(CS_MODE_BIG_ENDIAN | CS_MODE_M68K_000 | CS_MODE_M68K_010 | CS_MODE_M68K_020
163
        | CS_MODE_M68K_030 | CS_MODE_M68K_040 | CS_MODE_M68K_060),
164
  },
165
#else
166
  { NULL, NULL, 0 },
167
#endif
168
#ifdef CAPSTONE_HAS_TMS320C64X
169
  {
170
    TMS320C64x_global_init,
171
    TMS320C64x_option,
172
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BIG_ENDIAN),
173
  },
174
#else
175
  { NULL, NULL, 0 },
176
#endif
177
#ifdef CAPSTONE_HAS_M680X
178
  {
179
    M680X_global_init,
180
    M680X_option,
181
    ~(CS_MODE_M680X_6301 | CS_MODE_M680X_6309 | CS_MODE_M680X_6800
182
        | CS_MODE_M680X_6801 | CS_MODE_M680X_6805 | CS_MODE_M680X_6808
183
        | CS_MODE_M680X_6809 | CS_MODE_M680X_6811 | CS_MODE_M680X_CPU12
184
        | CS_MODE_M680X_HCS08),
185
  },
186
#else
187
  { NULL, NULL, 0 },
188
#endif
189
#ifdef CAPSTONE_HAS_EVM
190
  {
191
    EVM_global_init,
192
    EVM_option,
193
    0,
194
  },
195
#else
196
  { NULL, NULL, 0 },
197
#endif
198
#ifdef CAPSTONE_HAS_MOS65XX
199
  {
200
    MOS65XX_global_init,
201
    MOS65XX_option,
202
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_MOS65XX_6502 | CS_MODE_MOS65XX_65C02
203
        | CS_MODE_MOS65XX_W65C02 | CS_MODE_MOS65XX_65816_LONG_MX),
204
  },
205
#else
206
  { NULL, NULL, 0 },
207
#endif
208
#ifdef CAPSTONE_HAS_WASM
209
  {
210
    WASM_global_init,
211
    WASM_option,
212
    0,
213
  },
214
#else
215
  { NULL, NULL, 0 },
216
#endif
217
#ifdef CAPSTONE_HAS_BPF
218
  {
219
    BPF_global_init,
220
    BPF_option,
221
    ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_BPF_CLASSIC | CS_MODE_BPF_EXTENDED
222
        | CS_MODE_BIG_ENDIAN),
223
  },
224
#else
225
  { NULL, NULL, 0 },
226
#endif
227
#ifdef CAPSTONE_HAS_RISCV
228
  {
229
    RISCV_global_init,
230
    RISCV_option,
231
    ~(CS_MODE_RISCV32 | CS_MODE_RISCV64 | CS_MODE_RISCVC),
232
  },
233
#else
234
  { NULL, NULL, 0 },
235
#endif
236
#ifdef CAPSTONE_HAS_SH
237
  {
238
    SH_global_init,
239
    SH_option,
240
    ~(CS_MODE_SH2 | CS_MODE_SH2A | CS_MODE_SH3 |
241
      CS_MODE_SH4 | CS_MODE_SH4A |
242
      CS_MODE_SHFPU | CS_MODE_SHDSP|CS_MODE_BIG_ENDIAN),
243
  },
244
#else
245
  { NULL, NULL, 0 },
246
#endif
247
#ifdef CAPSTONE_HAS_TRICORE
248
  {
249
    TRICORE_global_init,
250
    TRICORE_option,
251
    ~(CS_MODE_TRICORE_110 | CS_MODE_TRICORE_120 | CS_MODE_TRICORE_130
252
    | CS_MODE_TRICORE_131 | CS_MODE_TRICORE_160 | CS_MODE_TRICORE_161
253
    | CS_MODE_TRICORE_162 | CS_MODE_LITTLE_ENDIAN),
254
  },
255
#else
256
  { NULL, NULL, 0 },
257
#endif
258
};
259
260
// bitmask of enabled architectures
261
static const uint32_t all_arch = 0
262
#ifdef CAPSTONE_HAS_ARM
263
  | (1 << CS_ARCH_ARM)
264
#endif
265
#ifdef CAPSTONE_HAS_ARM64
266
  | (1 << CS_ARCH_ARM64)
267
#endif
268
#ifdef CAPSTONE_HAS_MIPS
269
  | (1 << CS_ARCH_MIPS)
270
#endif
271
#ifdef CAPSTONE_HAS_X86
272
  | (1 << CS_ARCH_X86)
273
#endif
274
#ifdef CAPSTONE_HAS_POWERPC
275
  | (1 << CS_ARCH_PPC)
276
#endif
277
#ifdef CAPSTONE_HAS_SPARC
278
  | (1 << CS_ARCH_SPARC)
279
#endif
280
#ifdef CAPSTONE_HAS_SYSZ
281
  | (1 << CS_ARCH_SYSZ)
282
#endif
283
#ifdef CAPSTONE_HAS_XCORE
284
  | (1 << CS_ARCH_XCORE)
285
#endif
286
#ifdef CAPSTONE_HAS_M68K
287
  | (1 << CS_ARCH_M68K)
288
#endif
289
#ifdef CAPSTONE_HAS_TMS320C64X
290
  | (1 << CS_ARCH_TMS320C64X)
291
#endif
292
#ifdef CAPSTONE_HAS_M680X
293
  | (1 << CS_ARCH_M680X)
294
#endif
295
#ifdef CAPSTONE_HAS_EVM
296
  | (1 << CS_ARCH_EVM)
297
#endif
298
#ifdef CAPSTONE_HAS_MOS65XX
299
  | (1 << CS_ARCH_MOS65XX)
300
#endif
301
#ifdef CAPSTONE_HAS_WASM
302
  | (1 << CS_ARCH_WASM)
303
#endif
304
#ifdef CAPSTONE_HAS_BPF
305
  | (1 << CS_ARCH_BPF)
306
#endif
307
#ifdef CAPSTONE_HAS_RISCV
308
  | (1 << CS_ARCH_RISCV)
309
#endif
310
#ifdef CAPSTONE_HAS_SH
311
  | (1 << CS_ARCH_SH)
312
#endif
313
#ifdef CAPSTONE_HAS_TRICORE
314
  | (1 << CS_ARCH_TRICORE)
315
#endif
316
;
317
318
319
#if defined(CAPSTONE_USE_SYS_DYN_MEM)
320
#if !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
321
// default
322
cs_malloc_t cs_mem_malloc = malloc;
323
cs_calloc_t cs_mem_calloc = calloc;
324
cs_realloc_t cs_mem_realloc = realloc;
325
cs_free_t cs_mem_free = free;
326
#if defined(_WIN32_WCE)
327
cs_vsnprintf_t cs_vsnprintf = _vsnprintf;
328
#else
329
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
330
#endif  // defined(_WIN32_WCE)
331
332
#elif defined(_KERNEL_MODE)
333
// Windows driver
334
cs_malloc_t cs_mem_malloc = cs_winkernel_malloc;
335
cs_calloc_t cs_mem_calloc = cs_winkernel_calloc;
336
cs_realloc_t cs_mem_realloc = cs_winkernel_realloc;
337
cs_free_t cs_mem_free = cs_winkernel_free;
338
cs_vsnprintf_t cs_vsnprintf = cs_winkernel_vsnprintf;
339
#else
340
// OSX kernel
341
extern void* kern_os_malloc(size_t size);
342
extern void kern_os_free(void* addr);
343
extern void* kern_os_realloc(void* addr, size_t nsize);
344
345
static void* cs_kern_os_calloc(size_t num, size_t size)
346
{
347
  size_t alloc = num * size;
348
  if (num && size != alloc / num) {
349
    return NULL; // overflow check
350
  }
351
  return kern_os_malloc(alloc); // malloc bzeroes the buffer
352
}
353
354
cs_malloc_t cs_mem_malloc = kern_os_malloc;
355
cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
356
cs_realloc_t cs_mem_realloc = kern_os_realloc;
357
cs_free_t cs_mem_free = kern_os_free;
358
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
359
#endif  // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
360
#else
361
// User-defined
362
cs_malloc_t cs_mem_malloc = NULL;
363
cs_calloc_t cs_mem_calloc = NULL;
364
cs_realloc_t cs_mem_realloc = NULL;
365
cs_free_t cs_mem_free = NULL;
366
cs_vsnprintf_t cs_vsnprintf = NULL;
367
368
#endif  // defined(CAPSTONE_USE_SYS_DYN_MEM)
369
370
CAPSTONE_EXPORT
371
unsigned int CAPSTONE_API cs_version(int *major, int *minor)
372
0
{
373
0
  if (major != NULL && minor != NULL) {
374
0
    *major = CS_API_MAJOR;
375
0
    *minor = CS_API_MINOR;
376
0
  }
377
378
0
  return (CS_API_MAJOR << 8) + CS_API_MINOR;
379
0
}
380
381
CAPSTONE_EXPORT
382
bool CAPSTONE_API cs_support(int query)
383
0
{
384
0
  if (query == CS_ARCH_ALL)
385
0
    return all_arch == ((1 << CS_ARCH_ARM)   | (1 << CS_ARCH_ARM64)      |
386
0
            (1 << CS_ARCH_MIPS)  | (1 << CS_ARCH_X86)        |
387
0
            (1 << CS_ARCH_PPC)   | (1 << CS_ARCH_SPARC)      |
388
0
            (1 << CS_ARCH_SYSZ)  | (1 << CS_ARCH_XCORE)      |
389
0
            (1 << CS_ARCH_M68K)  | (1 << CS_ARCH_TMS320C64X) |
390
0
            (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM)        |
391
0
            (1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX)    |
392
0
            (1 << CS_ARCH_WASM)  | (1 << CS_ARCH_BPF)        |
393
0
            (1 << CS_ARCH_SH)    | (1 << CS_ARCH_TRICORE));
394
395
0
  if ((unsigned int)query < CS_ARCH_MAX)
396
0
    return all_arch & (1 << query);
397
398
0
  if (query == CS_SUPPORT_DIET) {
399
#ifdef CAPSTONE_DIET
400
    return true;
401
#else
402
0
    return false;
403
0
#endif
404
0
  }
405
406
0
  if (query == CS_SUPPORT_X86_REDUCE) {
407
#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
408
    return true;
409
#else
410
0
    return false;
411
0
#endif
412
0
  }
413
414
  // unsupported query
415
0
  return false;
416
0
}
417
418
CAPSTONE_EXPORT
419
cs_err CAPSTONE_API cs_errno(csh handle)
420
0
{
421
0
  struct cs_struct *ud;
422
0
  if (!handle)
423
0
    return CS_ERR_CSH;
424
425
0
  ud = (struct cs_struct *)(uintptr_t)handle;
426
427
0
  return ud->errnum;
428
0
}
429
430
CAPSTONE_EXPORT
431
const char * CAPSTONE_API cs_strerror(cs_err code)
432
0
{
433
0
  switch(code) {
434
0
    default:
435
0
      return "Unknown error code";
436
0
    case CS_ERR_OK:
437
0
      return "OK (CS_ERR_OK)";
438
0
    case CS_ERR_MEM:
439
0
      return "Out of memory (CS_ERR_MEM)";
440
0
    case CS_ERR_ARCH:
441
0
      return "Invalid/unsupported architecture(CS_ERR_ARCH)";
442
0
    case CS_ERR_HANDLE:
443
0
      return "Invalid handle (CS_ERR_HANDLE)";
444
0
    case CS_ERR_CSH:
445
0
      return "Invalid csh (CS_ERR_CSH)";
446
0
    case CS_ERR_MODE:
447
0
      return "Invalid mode (CS_ERR_MODE)";
448
0
    case CS_ERR_OPTION:
449
0
      return "Invalid option (CS_ERR_OPTION)";
450
0
    case CS_ERR_DETAIL:
451
0
      return "Details are unavailable (CS_ERR_DETAIL)";
452
0
    case CS_ERR_MEMSETUP:
453
0
      return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
454
0
    case CS_ERR_VERSION:
455
0
      return "Different API version between core & binding (CS_ERR_VERSION)";
456
0
    case CS_ERR_DIET:
457
0
      return "Information irrelevant in diet engine (CS_ERR_DIET)";
458
0
    case CS_ERR_SKIPDATA:
459
0
      return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
460
0
    case CS_ERR_X86_ATT:
461
0
      return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
462
0
    case CS_ERR_X86_INTEL:
463
0
      return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
464
0
    case CS_ERR_X86_MASM:
465
0
      return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
466
0
  }
467
0
}
468
469
CAPSTONE_EXPORT
470
cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
471
37.2k
{
472
37.2k
  cs_err err;
473
37.2k
  struct cs_struct *ud;
474
37.2k
  if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
475
    // Error: before cs_open(), dynamic memory management must be initialized
476
    // with cs_option(CS_OPT_MEM)
477
0
    return CS_ERR_MEMSETUP;
478
479
37.2k
  if (arch < CS_ARCH_MAX && arch_configs[arch].arch_init) {
480
    // verify if requested mode is valid
481
37.2k
    if (mode & arch_configs[arch].arch_disallowed_mode_mask) {
482
2
      *handle = 0;
483
2
      return CS_ERR_MODE;
484
2
    }
485
486
37.2k
    ud = cs_mem_calloc(1, sizeof(*ud));
487
37.2k
    if (!ud) {
488
      // memory insufficient
489
0
      return CS_ERR_MEM;
490
0
    }
491
492
37.2k
    ud->errnum = CS_ERR_OK;
493
37.2k
    ud->arch = arch;
494
37.2k
    ud->mode = mode;
495
    // by default, do not break instruction into details
496
37.2k
    ud->detail = CS_OPT_OFF;
497
498
    // default skipdata setup
499
37.2k
    ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
500
501
37.2k
    err = arch_configs[ud->arch].arch_init(ud);
502
37.2k
    if (err) {
503
0
      cs_mem_free(ud);
504
0
      *handle = 0;
505
0
      return err;
506
0
    }
507
508
37.2k
    *handle = (uintptr_t)ud;
509
510
37.2k
    return CS_ERR_OK;
511
37.2k
  } else {
512
0
    *handle = 0;
513
0
    return CS_ERR_ARCH;
514
0
  }
515
37.2k
}
516
517
CAPSTONE_EXPORT
518
cs_err CAPSTONE_API cs_close(csh *handle)
519
37.2k
{
520
37.2k
  struct cs_struct *ud;
521
37.2k
  struct insn_mnem *next, *tmp;
522
523
37.2k
  if (*handle == 0)
524
    // invalid handle
525
0
    return CS_ERR_CSH;
526
527
37.2k
  ud = (struct cs_struct *)(*handle);
528
529
37.2k
  if (ud->printer_info)
530
35.4k
    cs_mem_free(ud->printer_info);
531
532
  // free the linked list of customized mnemonic
533
37.2k
  tmp = ud->mnem_list;
534
37.2k
  while(tmp) {
535
0
    next = tmp->next;
536
0
    cs_mem_free(tmp);
537
0
    tmp = next;
538
0
  }
539
540
37.2k
  cs_mem_free(ud->insn_cache);
541
37.2k
  cs_mem_free(ud->x86_insn_lut);
542
37.2k
  cs_mem_free(ud->x86_insn_reg_lut);
543
544
37.2k
  memset(ud, 0, sizeof(*ud));
545
37.2k
  cs_mem_free(ud);
546
547
  // invalidate this handle by ZERO out its value.
548
  // this is to make sure it is unusable after cs_close()
549
37.2k
  *handle = 0;
550
551
37.2k
  return CS_ERR_OK;
552
37.2k
}
553
554
// replace str1 in target with str2; target starts with str1
555
// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
556
// return 0 on success, -1 on failure
557
static int str_replace(char *result, char *target, const char *str1, char *str2)
558
0
{
559
  // only perform replacement if the output fits into result
560
0
  if (strlen(target) - strlen(str1) + strlen(str2) < CS_MNEMONIC_SIZE - 1)  {
561
    // copy str2 to begining of result
562
0
    strcpy(result, str2);
563
    // skip str1 - already replaced by str2
564
0
    strcat(result, target + strlen(str1));
565
566
0
    return 0;
567
0
  } else
568
0
    return -1;
569
0
}
570
571
// fill insn with mnemonic & operands info
572
static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
573
    PostPrinter_t postprinter, const uint8_t *code)
574
2.42M
{
575
2.42M
#ifndef CAPSTONE_DIET
576
2.42M
  char *sp, *mnem;
577
2.42M
#endif
578
2.42M
  uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
579
580
  // fill the instruction bytes.
581
  // we might skip some redundant bytes in front in the case of X86
582
2.42M
  memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
583
2.42M
  insn->op_str[0] = '\0';
584
  // mnemonic is filled below, after the post printer runs: post printers
585
  // must derive any checks from insn_asm, never from insn->mnemonic
586
2.42M
  insn->mnemonic[0] = '\0';
587
2.42M
  insn->size = copy_size;
588
589
  // alias instruction might have ID saved in OpcodePub
590
2.42M
  if (MCInst_getOpcodePub(mci))
591
162k
    insn->id = MCInst_getOpcodePub(mci);
592
593
  // post printer handles some corner cases (hacky)
594
2.42M
  if (postprinter)
595
1.78M
    postprinter((csh)handle, insn, buffer, mci);
596
597
2.42M
#ifndef CAPSTONE_DIET
598
2.42M
  mnem = insn->mnemonic;
599
  // memset(mnem, 0, CS_MNEMONIC_SIZE);
600
13.8M
  for (sp = buffer; *sp; sp++) {
601
13.7M
    if (*sp == ' '|| *sp == '\t')
602
2.30M
      break;
603
11.4M
    if (*sp == '|')  // lock|rep prefix for x86
604
70.6k
      *sp = ' ';
605
    // copy to @mnemonic
606
11.4M
    *mnem = *sp;
607
11.4M
    mnem++;
608
11.4M
  }
609
610
2.42M
  *mnem = '\0';
611
612
  // we might have customized mnemonic
613
2.42M
  if (handle->mnem_list) {
614
0
    struct insn_mnem *tmp = handle->mnem_list;
615
0
    while(tmp) {
616
0
      if (tmp->insn.id == insn->id) {
617
0
        char str[CS_MNEMONIC_SIZE];
618
619
0
        if (!str_replace(str, insn->mnemonic, cs_insn_name((csh)handle, insn->id), tmp->insn.mnemonic)) {
620
          // copy result to mnemonic
621
0
          (void)strncpy(insn->mnemonic, str, sizeof(insn->mnemonic) - 1);
622
0
          insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
623
0
        }
624
625
0
        break;
626
0
      }
627
0
      tmp = tmp->next;
628
0
    }
629
0
  }
630
631
  // copy @op_str
632
2.42M
  if (*sp) {
633
    // find the next non-space char
634
2.30M
    sp++;
635
2.30M
    for (; ((*sp == ' ') || (*sp == '\t')); sp++);
636
2.30M
    strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
637
2.30M
    insn->op_str[sizeof(insn->op_str) - 1] = '\0';
638
2.30M
  } else
639
123k
    insn->op_str[0] = '\0';
640
641
2.42M
#endif
642
2.42M
}
643
644
// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
645
// this very much depends on instruction alignment requirement of each arch.
646
static uint8_t skipdata_size(cs_struct *handle)
647
0
{
648
0
  switch(handle->arch) {
649
0
    default:
650
      // should never reach
651
0
      return (uint8_t)-1;
652
0
    case CS_ARCH_ARM:
653
      // skip 2 bytes on Thumb mode.
654
0
      if (handle->mode & CS_MODE_THUMB)
655
0
        return 2;
656
      // otherwise, skip 4 bytes
657
0
      return 4;
658
0
    case CS_ARCH_ARM64:
659
0
    case CS_ARCH_MIPS:
660
0
    case CS_ARCH_PPC:
661
0
    case CS_ARCH_SPARC:
662
      // skip 4 bytes
663
0
      return 4;
664
0
    case CS_ARCH_SYSZ:
665
      // SystemZ instruction's length can be 2, 4 or 6 bytes,
666
      // so we just skip 2 bytes
667
0
      return 2;
668
0
    case CS_ARCH_X86:
669
      // X86 has no restriction on instruction alignment
670
0
      return 1;
671
0
    case CS_ARCH_XCORE:
672
      // XCore instruction's length can be 2 or 4 bytes,
673
      // so we just skip 2 bytes
674
0
      return 2;
675
0
    case CS_ARCH_M68K:
676
      // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
677
0
      return 2;
678
0
    case CS_ARCH_TMS320C64X:
679
      // TMS320C64x alignment is 4.
680
0
      return 4;
681
0
    case CS_ARCH_M680X:
682
      // M680X alignment is 1.
683
0
      return 1;
684
0
    case CS_ARCH_EVM:
685
      // EVM alignment is 1.
686
0
      return 1;
687
0
    case CS_ARCH_WASM:
688
      //WASM alignment is 1
689
0
      return 1;
690
0
    case CS_ARCH_MOS65XX:
691
      // MOS65XX alignment is 1.
692
0
      return 1;
693
0
    case CS_ARCH_BPF:
694
      // both classic and extended BPF have alignment 8.
695
0
      return 8;
696
0
    case CS_ARCH_RISCV:
697
      // special compress mode
698
0
      if (handle->mode & CS_MODE_RISCVC)
699
0
        return 2;
700
0
      return 4;
701
0
    case CS_ARCH_SH:
702
0
      return 2;
703
0
    case CS_ARCH_TRICORE:
704
      // TriCore instruction's length can be 2 or 4 bytes,
705
      // so we just skip 2 bytes
706
0
      return 2;
707
0
  }
708
0
}
709
710
CAPSTONE_EXPORT
711
cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
712
47.8k
{
713
47.8k
  struct cs_struct *handle;
714
47.8k
  cs_opt_mnem *opt;
715
716
  // cs_option() can be called with NULL handle just for CS_OPT_MEM
717
  // This is supposed to be executed before all other APIs (even cs_open())
718
47.8k
  if (type == CS_OPT_MEM) {
719
0
    cs_opt_mem *mem = (cs_opt_mem *)value;
720
721
0
    cs_mem_malloc = mem->malloc;
722
0
    cs_mem_calloc = mem->calloc;
723
0
    cs_mem_realloc = mem->realloc;
724
0
    cs_mem_free = mem->free;
725
0
    cs_vsnprintf = mem->vsnprintf;
726
727
0
    return CS_ERR_OK;
728
0
  }
729
730
47.8k
  handle = (struct cs_struct *)(uintptr_t)ud;
731
47.8k
  if (!handle)
732
0
    return CS_ERR_CSH;
733
734
47.8k
  switch(type) {
735
10.5k
    default:
736
10.5k
      break;
737
738
10.5k
    case CS_OPT_UNSIGNED:
739
0
      handle->imm_unsigned = (cs_opt_value)value;
740
0
      return CS_ERR_OK;
741
742
37.2k
    case CS_OPT_DETAIL:
743
37.2k
      handle->detail = (cs_opt_value)value;
744
37.2k
      return CS_ERR_OK;
745
746
0
    case CS_OPT_SKIPDATA:
747
0
      handle->skipdata = (value == CS_OPT_ON);
748
0
      if (handle->skipdata) {
749
0
        if (handle->skipdata_size == 0) {
750
          // set the default skipdata size
751
0
          handle->skipdata_size = skipdata_size(handle);
752
0
        }
753
0
      }
754
0
      return CS_ERR_OK;
755
756
0
    case CS_OPT_SKIPDATA_SETUP:
757
0
      if (value) {
758
0
        handle->skipdata_setup = *((cs_opt_skipdata *)value);
759
0
        if (handle->skipdata_setup.mnemonic == NULL) {
760
0
          handle->skipdata_setup.mnemonic = SKIPDATA_MNEM;
761
0
        }
762
0
      }
763
0
      return CS_ERR_OK;
764
765
0
    case CS_OPT_MNEMONIC:
766
0
      opt = (cs_opt_mnem *)value;
767
0
      if (opt->id) {
768
0
        if (opt->mnemonic) {
769
0
          struct insn_mnem *tmp;
770
771
          // add new instruction, or replace existing instruction
772
          // 1. find if we already had this insn in the linked list
773
0
          tmp = handle->mnem_list;
774
0
          while(tmp) {
775
0
            if (tmp->insn.id == opt->id) {
776
              // found this instruction, so replace its mnemonic
777
0
              (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
778
0
              tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
779
0
              break;
780
0
            }
781
0
            tmp = tmp->next;
782
0
          }
783
784
          // 2. add this instruction if we have not had it yet
785
0
          if (!tmp) {
786
0
            tmp = cs_mem_malloc(sizeof(*tmp));
787
0
            if (!tmp) {
788
0
              return CS_ERR_MEM;
789
0
            }
790
0
            tmp->insn.id = opt->id;
791
0
            (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
792
0
            tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
793
            // this new instruction is heading the list
794
0
            tmp->next = handle->mnem_list;
795
0
            handle->mnem_list = tmp;
796
0
          }
797
0
          return CS_ERR_OK;
798
0
        } else {
799
0
          struct insn_mnem *prev, *tmp;
800
801
          // we want to delete an existing instruction
802
          // iterate the list to find the instruction to remove it
803
0
          tmp = handle->mnem_list;
804
0
          prev = tmp;
805
0
          while(tmp) {
806
0
            if (tmp->insn.id == opt->id) {
807
              // delete this instruction
808
0
              if (tmp == prev) {
809
                // head of the list
810
0
                handle->mnem_list = tmp->next;
811
0
              } else {
812
0
                prev->next = tmp->next;
813
0
              }
814
0
              cs_mem_free(tmp);
815
0
              break;
816
0
            }
817
0
            prev = tmp;
818
0
            tmp = tmp->next;
819
0
          }
820
0
        }
821
0
      }
822
0
      return CS_ERR_OK;
823
824
0
    case CS_OPT_MODE:
825
      // verify if requested mode is valid
826
0
      if (value & arch_configs[handle->arch].arch_disallowed_mode_mask) {
827
0
        return CS_ERR_OPTION;
828
0
      }
829
0
      break;
830
47.8k
  }
831
832
10.5k
  return arch_configs[handle->arch].arch_option(handle, type, value);
833
47.8k
}
834
835
// generate @op_str for data instruction of SKIPDATA
836
#ifndef CAPSTONE_DIET
837
static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
838
0
{
839
0
  char *p = opstr;
840
0
  int len;
841
0
  size_t i;
842
0
  size_t available = sizeof(((cs_insn*)NULL)->op_str);
843
844
0
  if (!size) {
845
0
    opstr[0] = '\0';
846
0
    return;
847
0
  }
848
849
0
  len = cs_snprintf(p, available, "0x%02x", buffer[0]);
850
0
  if (len < 0 || (size_t)len > available - 1) {
851
0
    opstr[0] = '\0';
852
0
    return;
853
0
  }
854
0
  p+= len;
855
0
  available -= len;
856
857
0
  for(i = 1; i < size; i++) {
858
0
    len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
859
0
    if (len < 0) {
860
0
      break;
861
0
    }
862
0
    if ((size_t)len > available - 1) {
863
0
      break;
864
0
    }
865
0
    p+= len;
866
0
    available -= len;
867
0
  }
868
0
}
869
#endif
870
871
// dynamicly allocate memory to contain disasm insn
872
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
873
CAPSTONE_EXPORT
874
size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
875
37.2k
{
876
37.2k
  struct cs_struct *handle;
877
37.2k
  MCInst mci;
878
37.2k
  uint16_t insn_size;
879
37.2k
  size_t c = 0, i;
880
37.2k
  unsigned int f = 0; // index of the next instruction in the cache
881
37.2k
  cs_insn *insn_cache;  // cache contains disassembled instructions
882
37.2k
  void *total = NULL;
883
37.2k
  size_t total_size = 0;  // total size of output buffer containing all insns
884
37.2k
  bool r;
885
37.2k
  void *tmp;
886
37.2k
  size_t skipdata_bytes;
887
37.2k
  uint64_t offset_org; // save all the original info of the buffer
888
37.2k
  size_t size_org;
889
37.2k
  const uint8_t *buffer_org;
890
37.2k
  unsigned int cache_size = INSN_CACHE_SIZE;
891
37.2k
  size_t next_offset;
892
893
37.2k
  handle = (struct cs_struct *)(uintptr_t)ud;
894
37.2k
  if (!handle) {
895
    // FIXME: how to handle this case:
896
    // handle->errnum = CS_ERR_HANDLE;
897
0
    return 0;
898
0
  }
899
900
37.2k
  handle->errnum = CS_ERR_OK;
901
902
  // reset IT block of ARM structure
903
37.2k
  if (handle->arch == CS_ARCH_ARM)
904
6.93k
    handle->ITBlock.size = 0;
905
906
37.2k
#ifdef CAPSTONE_USE_SYS_DYN_MEM
907
37.2k
  if (count > 0 && count <= INSN_CACHE_SIZE)
908
0
    cache_size = (unsigned int) count;
909
37.2k
#endif
910
911
  // save the original offset for SKIPDATA
912
37.2k
  buffer_org = buffer;
913
37.2k
  offset_org = offset;
914
37.2k
  size_org = size;
915
916
37.2k
  total_size = sizeof(cs_insn) * cache_size;
917
37.2k
  total = cs_mem_calloc(sizeof(cs_insn), cache_size);
918
37.2k
  if (total == NULL) {
919
    // insufficient memory
920
0
    handle->errnum = CS_ERR_MEM;
921
0
    return 0;
922
0
  }
923
924
37.2k
  insn_cache = total;
925
926
2.46M
  while (size > 0) {
927
2.44M
    MCInst_Init(&mci);
928
2.44M
    mci.csh = handle;
929
930
    // relative branches need to know the address & size of current insn
931
2.44M
    mci.address = offset;
932
933
2.44M
    if (handle->detail) {
934
      // allocate memory for @detail pointer
935
2.44M
      insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
936
2.44M
      if (insn_cache->detail == NULL) {
937
        // insufficient memory
938
0
        handle->errnum = CS_ERR_MEM;
939
0
        break;
940
0
      }
941
2.44M
    } else {
942
0
      insn_cache->detail = NULL;
943
0
    }
944
945
    // save all the information for non-detailed mode
946
2.44M
    mci.flat_insn = insn_cache;
947
2.44M
    mci.flat_insn->address = offset;
948
#ifdef CAPSTONE_DIET
949
    // zero out mnemonic & op_str
950
    mci.flat_insn->mnemonic[0] = '\0';
951
    mci.flat_insn->op_str[0] = '\0';
952
#endif
953
954
2.44M
    r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
955
2.44M
    if (r) {
956
2.42M
      SStream ss;
957
2.42M
      SStream_Init(&ss);
958
959
2.42M
      mci.flat_insn->size = insn_size;
960
961
      // map internal instruction opcode to public insn ID
962
963
2.42M
      handle->insn_id(handle, insn_cache, mci.Opcode);
964
965
2.42M
      handle->printer(&mci, &ss, handle->printer_info);
966
2.42M
      fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
967
968
      // adjust for pseudo opcode (X86)
969
2.42M
      if (handle->arch == CS_ARCH_X86 && insn_cache->id != X86_INS_VCMP)
970
591k
        insn_cache->id += mci.popcode_adjust;
971
972
2.42M
      next_offset = insn_size;
973
2.42M
    } else {
974
      // encounter a broken instruction
975
976
      // free memory of @detail pointer
977
19.5k
      if (handle->detail) {
978
19.5k
        cs_mem_free(insn_cache->detail);
979
19.5k
      }
980
981
      // if there is no request to skip data, or remaining data is too small,
982
      // then bail out
983
19.5k
      if (!handle->skipdata || handle->skipdata_size > size)
984
19.5k
        break;
985
986
0
      if (handle->skipdata_setup.callback) {
987
0
        skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
988
0
            (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
989
0
        if (skipdata_bytes > size)
990
          // remaining data is not enough
991
0
          break;
992
993
0
        if (!skipdata_bytes)
994
          // user requested not to skip data, so bail out
995
0
          break;
996
0
      } else
997
0
        skipdata_bytes = handle->skipdata_size;
998
999
      // we have to skip some amount of data, depending on arch & mode
1000
      // invalid ID for this "data" instruction
1001
0
      insn_cache->id = 0;
1002
0
      insn_cache->address = offset;
1003
0
      insn_cache->size = (uint16_t)MIN(
1004
0
        skipdata_bytes, sizeof(insn_cache->bytes));
1005
0
      memcpy(insn_cache->bytes, buffer,
1006
0
             MIN(skipdata_bytes, sizeof(insn_cache->bytes)));
1007
#ifdef CAPSTONE_DIET
1008
      insn_cache->mnemonic[0] = '\0';
1009
      insn_cache->op_str[0] = '\0';
1010
#else
1011
0
      strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
1012
0
          sizeof(insn_cache->mnemonic) - 1);
1013
0
      skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
1014
0
#endif
1015
0
      insn_cache->detail = NULL;
1016
1017
0
      next_offset = skipdata_bytes;
1018
0
    }
1019
1020
    // one more instruction entering the cache
1021
2.42M
    f++;
1022
1023
    // one more instruction disassembled
1024
2.42M
    c++;
1025
2.42M
    if (count > 0 && c == count)
1026
      // already got requested number of instructions
1027
0
      break;
1028
1029
2.42M
    if (f == cache_size) {
1030
      // full cache, so expand the cache to contain incoming insns
1031
24.8k
      cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
1032
24.8k
      total_size += (sizeof(cs_insn) * cache_size);
1033
24.8k
      tmp = cs_mem_realloc(total, total_size);
1034
24.8k
      if (tmp == NULL) { // insufficient memory
1035
0
        if (handle->detail) {
1036
0
          insn_cache = (cs_insn *)total;
1037
0
          for (i = 0; i < c; i++, insn_cache++)
1038
0
            cs_mem_free(insn_cache->detail);
1039
0
        }
1040
1041
0
        cs_mem_free(total);
1042
0
        *insn = NULL;
1043
0
        handle->errnum = CS_ERR_MEM;
1044
0
        return 0;
1045
0
      }
1046
1047
24.8k
      total = tmp;
1048
      // continue to fill in the cache after the last instruction
1049
24.8k
      insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
1050
1051
      // reset f back to 0, so we fill in the cache from begining
1052
24.8k
      f = 0;
1053
24.8k
    } else
1054
2.40M
      insn_cache++;
1055
1056
2.42M
    buffer += next_offset;
1057
2.42M
    size -= next_offset;
1058
2.42M
    offset += next_offset;
1059
2.42M
  }
1060
1061
37.2k
  if (!c) {
1062
    // we did not disassemble any instruction
1063
802
    cs_mem_free(total);
1064
802
    total = NULL;
1065
36.4k
  } else if (f != cache_size) {
1066
    // total did not fully use the last cache, so downsize it
1067
36.4k
    tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
1068
36.4k
    if (tmp == NULL) { // insufficient memory
1069
      // free all detail pointers
1070
0
      if (handle->detail) {
1071
0
        insn_cache = (cs_insn *)total;
1072
0
        for (i = 0; i < c; i++, insn_cache++)
1073
0
          cs_mem_free(insn_cache->detail);
1074
0
      }
1075
1076
0
      cs_mem_free(total);
1077
0
      *insn = NULL;
1078
1079
0
      handle->errnum = CS_ERR_MEM;
1080
0
      return 0;
1081
0
    }
1082
1083
36.4k
    total = tmp;
1084
36.4k
  }
1085
1086
37.2k
  *insn = total;
1087
1088
37.2k
  return c;
1089
37.2k
}
1090
1091
CAPSTONE_EXPORT
1092
void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
1093
36.4k
{
1094
36.4k
  size_t i;
1095
1096
  // free all detail pointers
1097
2.46M
  for (i = 0; i < count; i++)
1098
2.42M
    cs_mem_free(insn[i].detail);
1099
1100
  // then free pointer to cs_insn array
1101
36.4k
  cs_mem_free(insn);
1102
36.4k
}
1103
1104
CAPSTONE_EXPORT
1105
cs_insn * CAPSTONE_API cs_malloc(csh ud)
1106
0
{
1107
0
  cs_insn *insn;
1108
0
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1109
1110
0
  insn = cs_mem_malloc(sizeof(cs_insn));
1111
0
  if (!insn) {
1112
    // insufficient memory
1113
0
    handle->errnum = CS_ERR_MEM;
1114
0
    return NULL;
1115
0
  } else {
1116
0
    if (handle->detail) {
1117
      // allocate memory for @detail pointer
1118
0
      insn->detail = cs_mem_malloc(sizeof(cs_detail));
1119
0
      if (insn->detail == NULL) { // insufficient memory
1120
0
        cs_mem_free(insn);
1121
0
        handle->errnum = CS_ERR_MEM;
1122
0
        return NULL;
1123
0
      }
1124
0
    } else
1125
0
      insn->detail = NULL;
1126
0
  }
1127
1128
0
  return insn;
1129
0
}
1130
1131
// iterator for instruction "single-stepping"
1132
CAPSTONE_EXPORT
1133
bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
1134
    uint64_t *address, cs_insn *insn)
1135
0
{
1136
0
  if (*size == 0)
1137
0
    return false;
1138
1139
0
  struct cs_struct *handle;
1140
0
  uint16_t insn_size;
1141
0
  MCInst mci;
1142
0
  bool r;
1143
1144
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1145
0
  if (!handle) {
1146
0
    return false;
1147
0
  }
1148
1149
0
  handle->errnum = CS_ERR_OK;
1150
1151
0
  MCInst_Init(&mci);
1152
0
  mci.csh = handle;
1153
1154
  // relative branches need to know the address & size of current insn
1155
0
  mci.address = *address;
1156
1157
  // save all the information for non-detailed mode
1158
0
  mci.flat_insn = insn;
1159
0
  mci.flat_insn->address = *address;
1160
#ifdef CAPSTONE_DIET
1161
  // zero out mnemonic & op_str
1162
  mci.flat_insn->mnemonic[0] = '\0';
1163
  mci.flat_insn->op_str[0] = '\0';
1164
#endif
1165
1166
0
  r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
1167
0
  if (r) {
1168
0
    SStream ss;
1169
0
    SStream_Init(&ss);
1170
1171
0
    mci.flat_insn->size = insn_size;
1172
1173
    // map internal instruction opcode to public insn ID
1174
0
    handle->insn_id(handle, insn, mci.Opcode);
1175
1176
0
    handle->printer(&mci, &ss, handle->printer_info);
1177
1178
0
    fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
1179
1180
    // adjust for pseudo opcode (X86)
1181
0
    if (handle->arch == CS_ARCH_X86)
1182
0
      insn->id += mci.popcode_adjust;
1183
1184
0
    *code += insn_size;
1185
0
    *size -= insn_size;
1186
0
    *address += insn_size;
1187
0
  } else {   // encounter a broken instruction
1188
0
    size_t skipdata_bytes;
1189
1190
    // if there is no request to skip data, or remaining data is too small,
1191
    // then bail out
1192
0
    if (!handle->skipdata || handle->skipdata_size > *size)
1193
0
      return false;
1194
1195
0
    if (handle->skipdata_setup.callback) {
1196
0
      skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
1197
0
          0, handle->skipdata_setup.user_data);
1198
0
      if (skipdata_bytes > *size)
1199
        // remaining data is not enough
1200
0
        return false;
1201
1202
0
      if (!skipdata_bytes)
1203
        // user requested not to skip data, so bail out
1204
0
        return false;
1205
0
    } else
1206
0
      skipdata_bytes = handle->skipdata_size;
1207
1208
    // we have to skip some amount of data, depending on arch & mode
1209
0
    insn->id = 0; // invalid ID for this "data" instruction
1210
0
    insn->address = *address;
1211
0
    insn->size = (uint16_t)MIN(skipdata_bytes, sizeof(insn->bytes));
1212
0
    memcpy(insn->bytes, *code,
1213
0
           MIN(skipdata_bytes, sizeof(insn->bytes)));
1214
#ifdef CAPSTONE_DIET
1215
    insn->mnemonic[0] = '\0';
1216
    insn->op_str[0] = '\0';
1217
#else
1218
0
    strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1219
0
        sizeof(insn->mnemonic) - 1);
1220
0
    skipdata_opstr(insn->op_str, *code, skipdata_bytes);
1221
0
#endif
1222
1223
0
    *code += skipdata_bytes;
1224
0
    *size -= skipdata_bytes;
1225
0
    *address += skipdata_bytes;
1226
0
  }
1227
1228
0
  return true;
1229
0
}
1230
1231
// return friendly name of register in a string
1232
CAPSTONE_EXPORT
1233
const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
1234
1.82M
{
1235
1.82M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1236
1237
1.82M
  if (!handle || handle->reg_name == NULL) {
1238
0
    return NULL;
1239
0
  }
1240
1241
1.82M
  return handle->reg_name(ud, reg);
1242
1.82M
}
1243
1244
CAPSTONE_EXPORT
1245
const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
1246
2.42M
{
1247
2.42M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1248
1249
2.42M
  if (!handle || handle->insn_name == NULL) {
1250
0
    return NULL;
1251
0
  }
1252
1253
2.42M
  return handle->insn_name(ud, insn);
1254
2.42M
}
1255
1256
CAPSTONE_EXPORT
1257
const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
1258
1.99M
{
1259
1.99M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1260
1261
1.99M
  if (!handle || handle->group_name == NULL) {
1262
0
    return NULL;
1263
0
  }
1264
1265
1.99M
  return handle->group_name(ud, group);
1266
1.99M
}
1267
1268
CAPSTONE_EXPORT
1269
bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
1270
0
{
1271
0
  struct cs_struct *handle;
1272
0
  if (!ud)
1273
0
    return false;
1274
1275
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1276
1277
0
  if (!handle->detail) {
1278
0
    handle->errnum = CS_ERR_DETAIL;
1279
0
    return false;
1280
0
  }
1281
1282
0
  if (!insn->id) {
1283
0
    handle->errnum = CS_ERR_SKIPDATA;
1284
0
    return false;
1285
0
  }
1286
1287
0
  if (!insn->detail) {
1288
0
    handle->errnum = CS_ERR_DETAIL;
1289
0
    return false;
1290
0
  }
1291
1292
0
  return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
1293
0
}
1294
1295
CAPSTONE_EXPORT
1296
bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
1297
0
{
1298
0
  struct cs_struct *handle;
1299
0
  if (!ud)
1300
0
    return false;
1301
1302
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1303
1304
0
  if (!handle->detail) {
1305
0
    handle->errnum = CS_ERR_DETAIL;
1306
0
    return false;
1307
0
  }
1308
1309
0
  if (!insn->id) {
1310
0
    handle->errnum = CS_ERR_SKIPDATA;
1311
0
    return false;
1312
0
  }
1313
1314
0
  if (!insn->detail) {
1315
0
    handle->errnum = CS_ERR_DETAIL;
1316
0
    return false;
1317
0
  }
1318
1319
0
  return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
1320
0
}
1321
1322
CAPSTONE_EXPORT
1323
bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
1324
991k
{
1325
991k
  struct cs_struct *handle;
1326
991k
  if (!ud)
1327
0
    return false;
1328
1329
991k
  handle = (struct cs_struct *)(uintptr_t)ud;
1330
1331
991k
  if (!handle->detail) {
1332
0
    handle->errnum = CS_ERR_DETAIL;
1333
0
    return false;
1334
0
  }
1335
1336
991k
  if (!insn->id) {
1337
0
    handle->errnum = CS_ERR_SKIPDATA;
1338
0
    return false;
1339
0
  }
1340
1341
991k
  if (!insn->detail) {
1342
0
    handle->errnum = CS_ERR_DETAIL;
1343
0
    return false;
1344
0
  }
1345
1346
991k
  return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
1347
991k
}
1348
1349
CAPSTONE_EXPORT
1350
int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
1351
0
{
1352
0
  struct cs_struct *handle;
1353
0
  unsigned int count = 0, i;
1354
0
  if (!ud)
1355
0
    return -1;
1356
1357
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1358
1359
0
  if (!handle->detail) {
1360
0
    handle->errnum = CS_ERR_DETAIL;
1361
0
    return -1;
1362
0
  }
1363
1364
0
  if (!insn->id) {
1365
0
    handle->errnum = CS_ERR_SKIPDATA;
1366
0
    return -1;
1367
0
  }
1368
1369
0
  if (!insn->detail) {
1370
0
    handle->errnum = CS_ERR_DETAIL;
1371
0
    return -1;
1372
0
  }
1373
1374
0
  handle->errnum = CS_ERR_OK;
1375
1376
0
  switch (handle->arch) {
1377
0
    default:
1378
0
      handle->errnum = CS_ERR_HANDLE;
1379
0
      return -1;
1380
0
    case CS_ARCH_ARM:
1381
0
      for (i = 0; i < insn->detail->arm.op_count; i++)
1382
0
        if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
1383
0
          count++;
1384
0
      break;
1385
0
    case CS_ARCH_ARM64:
1386
0
      for (i = 0; i < insn->detail->arm64.op_count; i++)
1387
0
        if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
1388
0
          count++;
1389
0
      break;
1390
0
    case CS_ARCH_X86:
1391
0
      for (i = 0; i < insn->detail->x86.op_count; i++)
1392
0
        if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
1393
0
          count++;
1394
0
      break;
1395
0
    case CS_ARCH_MIPS:
1396
0
      for (i = 0; i < insn->detail->mips.op_count; i++)
1397
0
        if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
1398
0
          count++;
1399
0
      break;
1400
0
    case CS_ARCH_PPC:
1401
0
      for (i = 0; i < insn->detail->ppc.op_count; i++)
1402
0
        if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
1403
0
          count++;
1404
0
      break;
1405
0
    case CS_ARCH_SPARC:
1406
0
      for (i = 0; i < insn->detail->sparc.op_count; i++)
1407
0
        if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1408
0
          count++;
1409
0
      break;
1410
0
    case CS_ARCH_SYSZ:
1411
0
      for (i = 0; i < insn->detail->sysz.op_count; i++)
1412
0
        if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1413
0
          count++;
1414
0
      break;
1415
0
    case CS_ARCH_XCORE:
1416
0
      for (i = 0; i < insn->detail->xcore.op_count; i++)
1417
0
        if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1418
0
          count++;
1419
0
      break;
1420
0
    case CS_ARCH_M68K:
1421
0
      for (i = 0; i < insn->detail->m68k.op_count; i++)
1422
0
        if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1423
0
          count++;
1424
0
      break;
1425
0
    case CS_ARCH_TMS320C64X:
1426
0
      for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1427
0
        if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1428
0
          count++;
1429
0
      break;
1430
0
    case CS_ARCH_M680X:
1431
0
      for (i = 0; i < insn->detail->m680x.op_count; i++)
1432
0
        if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1433
0
          count++;
1434
0
      break;
1435
0
    case CS_ARCH_EVM:
1436
0
      break;
1437
0
    case CS_ARCH_MOS65XX:
1438
0
      for (i = 0; i < insn->detail->mos65xx.op_count; i++)
1439
0
        if (insn->detail->mos65xx.operands[i].type == (mos65xx_op_type)op_type)
1440
0
          count++;
1441
0
      break;
1442
0
    case CS_ARCH_WASM:
1443
0
      for (i = 0; i < insn->detail->wasm.op_count; i++)
1444
0
        if (insn->detail->wasm.operands[i].type == (wasm_op_type)op_type)
1445
0
          count++;
1446
0
      break;
1447
0
    case CS_ARCH_BPF:
1448
0
      for (i = 0; i < insn->detail->bpf.op_count; i++)
1449
0
        if (insn->detail->bpf.operands[i].type == (bpf_op_type)op_type)
1450
0
          count++;
1451
0
      break;
1452
0
    case CS_ARCH_RISCV:
1453
0
      for (i = 0; i < insn->detail->riscv.op_count; i++)
1454
0
        if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type)
1455
0
          count++;
1456
0
      break;
1457
0
    case CS_ARCH_TRICORE:
1458
0
      for (i = 0; i < insn->detail->tricore.op_count; i++)
1459
0
        if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
1460
0
          count++;
1461
0
      break;
1462
0
  }
1463
1464
0
  return count;
1465
0
}
1466
1467
CAPSTONE_EXPORT
1468
int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
1469
    unsigned int post)
1470
0
{
1471
0
  struct cs_struct *handle;
1472
0
  unsigned int count = 0, i;
1473
0
  if (!ud)
1474
0
    return -1;
1475
1476
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1477
1478
0
  if (!handle->detail) {
1479
0
    handle->errnum = CS_ERR_DETAIL;
1480
0
    return -1;
1481
0
  }
1482
1483
0
  if (!insn->id) {
1484
0
    handle->errnum = CS_ERR_SKIPDATA;
1485
0
    return -1;
1486
0
  }
1487
1488
0
  if (!insn->detail) {
1489
0
    handle->errnum = CS_ERR_DETAIL;
1490
0
    return -1;
1491
0
  }
1492
1493
0
  handle->errnum = CS_ERR_OK;
1494
1495
0
  switch (handle->arch) {
1496
0
    default:
1497
0
      handle->errnum = CS_ERR_HANDLE;
1498
0
      return -1;
1499
0
    case CS_ARCH_ARM:
1500
0
      for (i = 0; i < insn->detail->arm.op_count; i++) {
1501
0
        if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
1502
0
          count++;
1503
0
        if (count == post)
1504
0
          return i;
1505
0
      }
1506
0
      break;
1507
0
    case CS_ARCH_ARM64:
1508
0
      for (i = 0; i < insn->detail->arm64.op_count; i++) {
1509
0
        if (insn->detail->arm64.operands[i].type == (arm64_op_type)op_type)
1510
0
          count++;
1511
0
        if (count == post)
1512
0
          return i;
1513
0
      }
1514
0
      break;
1515
0
    case CS_ARCH_X86:
1516
0
      for (i = 0; i < insn->detail->x86.op_count; i++) {
1517
0
        if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
1518
0
          count++;
1519
0
        if (count == post)
1520
0
          return i;
1521
0
      }
1522
0
      break;
1523
0
    case CS_ARCH_MIPS:
1524
0
      for (i = 0; i < insn->detail->mips.op_count; i++) {
1525
0
        if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
1526
0
          count++;
1527
0
        if (count == post)
1528
0
          return i;
1529
0
      }
1530
0
      break;
1531
0
    case CS_ARCH_PPC:
1532
0
      for (i = 0; i < insn->detail->ppc.op_count; i++) {
1533
0
        if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
1534
0
          count++;
1535
0
        if (count == post)
1536
0
          return i;
1537
0
      }
1538
0
      break;
1539
0
    case CS_ARCH_SPARC:
1540
0
      for (i = 0; i < insn->detail->sparc.op_count; i++) {
1541
0
        if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1542
0
          count++;
1543
0
        if (count == post)
1544
0
          return i;
1545
0
      }
1546
0
      break;
1547
0
    case CS_ARCH_SYSZ:
1548
0
      for (i = 0; i < insn->detail->sysz.op_count; i++) {
1549
0
        if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1550
0
          count++;
1551
0
        if (count == post)
1552
0
          return i;
1553
0
      }
1554
0
      break;
1555
0
    case CS_ARCH_XCORE:
1556
0
      for (i = 0; i < insn->detail->xcore.op_count; i++) {
1557
0
        if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1558
0
          count++;
1559
0
        if (count == post)
1560
0
          return i;
1561
0
      }
1562
0
      break;
1563
0
    case CS_ARCH_TRICORE:
1564
0
      for (i = 0; i < insn->detail->tricore.op_count; i++) {
1565
0
        if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
1566
0
          count++;
1567
0
        if (count == post)
1568
0
          return i;
1569
0
      }
1570
0
      break;
1571
0
    case CS_ARCH_M68K:
1572
0
      for (i = 0; i < insn->detail->m68k.op_count; i++) {
1573
0
        if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1574
0
          count++;
1575
0
        if (count == post)
1576
0
          return i;
1577
0
      }
1578
0
      break;
1579
0
    case CS_ARCH_TMS320C64X:
1580
0
      for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1581
0
        if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1582
0
          count++;
1583
0
        if (count == post)
1584
0
          return i;
1585
0
      }
1586
0
      break;
1587
0
    case CS_ARCH_M680X:
1588
0
      for (i = 0; i < insn->detail->m680x.op_count; i++) {
1589
0
        if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1590
0
          count++;
1591
0
        if (count == post)
1592
0
          return i;
1593
0
      }
1594
0
      break;
1595
0
    case CS_ARCH_EVM:
1596
#if 0
1597
      for (i = 0; i < insn->detail->evm.op_count; i++) {
1598
        if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1599
          count++;
1600
        if (count == post)
1601
          return i;
1602
      }
1603
#endif
1604
0
      break;
1605
0
    case CS_ARCH_MOS65XX:
1606
0
      for (i = 0; i < insn->detail->mos65xx.op_count; i++) {
1607
0
        if (insn->detail->mos65xx.operands[i].type == (mos65xx_op_type)op_type)
1608
0
          count++;
1609
0
        if (count == post)
1610
0
          return i;
1611
0
      }
1612
0
      break;
1613
0
    case CS_ARCH_WASM:
1614
0
      for (i = 0; i < insn->detail->wasm.op_count; i++) {
1615
0
        if (insn->detail->wasm.operands[i].type == (wasm_op_type)op_type)
1616
0
          count++;
1617
0
        if (count == post)
1618
0
          return i;
1619
0
      }
1620
0
      break;
1621
0
    case CS_ARCH_BPF:
1622
0
      for (i = 0; i < insn->detail->bpf.op_count; i++) {
1623
0
        if (insn->detail->bpf.operands[i].type == (bpf_op_type)op_type)
1624
0
          count++;
1625
0
        if (count == post)
1626
0
          return i;
1627
0
      }
1628
0
      break;
1629
0
    case CS_ARCH_RISCV:
1630
0
      for (i = 0; i < insn->detail->riscv.op_count; i++) {
1631
0
        if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type)
1632
0
          count++;
1633
0
        if (count == post)
1634
0
          return i;
1635
0
      }
1636
0
      break;
1637
0
    case CS_ARCH_SH:
1638
0
      for (i = 0; i < insn->detail->sh.op_count; i++) {
1639
0
        if (insn->detail->sh.operands[i].type == (sh_op_type)op_type)
1640
0
          count++;
1641
0
        if (count == post)
1642
0
          return i;
1643
0
      }
1644
0
      break;
1645
0
  }
1646
1647
0
  return -1;
1648
0
}
1649
1650
CAPSTONE_EXPORT
1651
cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
1652
    cs_regs regs_read, uint8_t *regs_read_count,
1653
    cs_regs regs_write, uint8_t *regs_write_count)
1654
0
{
1655
0
  struct cs_struct *handle;
1656
1657
0
  if (!ud)
1658
0
    return -1;
1659
1660
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1661
1662
#ifdef CAPSTONE_DIET
1663
  // This API does not work in DIET mode
1664
  handle->errnum = CS_ERR_DIET;
1665
  return CS_ERR_DIET;
1666
#else
1667
0
  if (!handle->detail) {
1668
0
    handle->errnum = CS_ERR_DETAIL;
1669
0
    return CS_ERR_DETAIL;
1670
0
  }
1671
1672
0
  if (!insn->id) {
1673
0
    handle->errnum = CS_ERR_SKIPDATA;
1674
0
    return CS_ERR_SKIPDATA;
1675
0
  }
1676
1677
0
  if (!insn->detail) {
1678
0
    handle->errnum = CS_ERR_DETAIL;
1679
0
    return CS_ERR_DETAIL;
1680
0
  }
1681
1682
0
  if (handle->reg_access) {
1683
0
    handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1684
0
  } else {
1685
    // this arch is unsupported yet
1686
0
    handle->errnum = CS_ERR_ARCH;
1687
0
    return CS_ERR_ARCH;
1688
0
  }
1689
1690
0
  return CS_ERR_OK;
1691
0
#endif
1692
0
}