Coverage Report

Created: 2023-12-08 06:05

/src/capstonenext/cs.c
Line
Count
Source (jump to first uncovered line)
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_AARCH64) || 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
48.6k
#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
48.6k
#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_AARCH64
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 | CS_MODE_BOOKE),
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_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
#if defined(CAPSTONE_HAS_AARCH64) || defined(CAPSTONE_HAS_ARM64)
266
  | (1 << CS_ARCH_AARCH64)
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
  return kern_os_malloc(num * size); // malloc bzeroes the buffer
348
}
349
350
cs_malloc_t cs_mem_malloc = kern_os_malloc;
351
cs_calloc_t cs_mem_calloc = cs_kern_os_calloc;
352
cs_realloc_t cs_mem_realloc = kern_os_realloc;
353
cs_free_t cs_mem_free = kern_os_free;
354
cs_vsnprintf_t cs_vsnprintf = vsnprintf;
355
#endif  // !defined(CAPSTONE_HAS_OSXKERNEL) && !defined(_KERNEL_MODE)
356
#else
357
// User-defined
358
cs_malloc_t cs_mem_malloc = NULL;
359
cs_calloc_t cs_mem_calloc = NULL;
360
cs_realloc_t cs_mem_realloc = NULL;
361
cs_free_t cs_mem_free = NULL;
362
cs_vsnprintf_t cs_vsnprintf = NULL;
363
364
#endif  // defined(CAPSTONE_USE_SYS_DYN_MEM)
365
366
CAPSTONE_EXPORT
367
unsigned int CAPSTONE_API cs_version(int *major, int *minor)
368
0
{
369
0
  if (major != NULL && minor != NULL) {
370
0
    *major = CS_API_MAJOR;
371
0
    *minor = CS_API_MINOR;
372
0
  }
373
374
0
  return (CS_API_MAJOR << 8) + CS_API_MINOR;
375
0
}
376
377
CAPSTONE_EXPORT
378
bool CAPSTONE_API cs_support(int query)
379
0
{
380
0
  if (query == CS_ARCH_ALL)
381
0
    return all_arch ==
382
0
            ((1 << CS_ARCH_ARM)  | (1 << CS_ARCH_AARCH64)    |
383
0
            (1 << CS_ARCH_MIPS)  | (1 << CS_ARCH_X86)        |
384
0
            (1 << CS_ARCH_PPC)   | (1 << CS_ARCH_SPARC)      |
385
0
            (1 << CS_ARCH_SYSZ)  | (1 << CS_ARCH_XCORE)      |
386
0
            (1 << CS_ARCH_M68K)  | (1 << CS_ARCH_TMS320C64X) |
387
0
            (1 << CS_ARCH_M680X) | (1 << CS_ARCH_EVM)        |
388
0
            (1 << CS_ARCH_RISCV) | (1 << CS_ARCH_MOS65XX)    |
389
0
            (1 << CS_ARCH_WASM)  | (1 << CS_ARCH_BPF)        |
390
0
            (1 << CS_ARCH_SH)    | (1 << CS_ARCH_TRICORE));
391
392
0
  if ((unsigned int)query < CS_ARCH_MAX)
393
0
    return all_arch & (1 << query);
394
395
0
  if (query == CS_SUPPORT_DIET) {
396
#ifdef CAPSTONE_DIET
397
    return true;
398
#else
399
0
    return false;
400
0
#endif
401
0
  }
402
403
0
  if (query == CS_SUPPORT_X86_REDUCE) {
404
#if defined(CAPSTONE_HAS_X86) && defined(CAPSTONE_X86_REDUCE)
405
    return true;
406
#else
407
0
    return false;
408
0
#endif
409
0
  }
410
411
  // unsupported query
412
0
  return false;
413
0
}
414
415
CAPSTONE_EXPORT
416
cs_err CAPSTONE_API cs_errno(csh handle)
417
0
{
418
0
  struct cs_struct *ud;
419
0
  if (!handle)
420
0
    return CS_ERR_CSH;
421
422
0
  ud = (struct cs_struct *)(uintptr_t)handle;
423
424
0
  return ud->errnum;
425
0
}
426
427
CAPSTONE_EXPORT
428
const char * CAPSTONE_API cs_strerror(cs_err code)
429
0
{
430
0
  switch(code) {
431
0
    default:
432
0
      return "Unknown error code";
433
0
    case CS_ERR_OK:
434
0
      return "OK (CS_ERR_OK)";
435
0
    case CS_ERR_MEM:
436
0
      return "Out of memory (CS_ERR_MEM)";
437
0
    case CS_ERR_ARCH:
438
0
      return "Invalid/unsupported architecture(CS_ERR_ARCH)";
439
0
    case CS_ERR_HANDLE:
440
0
      return "Invalid handle (CS_ERR_HANDLE)";
441
0
    case CS_ERR_CSH:
442
0
      return "Invalid csh (CS_ERR_CSH)";
443
0
    case CS_ERR_MODE:
444
0
      return "Invalid mode (CS_ERR_MODE)";
445
0
    case CS_ERR_OPTION:
446
0
      return "Invalid option (CS_ERR_OPTION)";
447
0
    case CS_ERR_DETAIL:
448
0
      return "Details are unavailable (CS_ERR_DETAIL)";
449
0
    case CS_ERR_MEMSETUP:
450
0
      return "Dynamic memory management uninitialized (CS_ERR_MEMSETUP)";
451
0
    case CS_ERR_VERSION:
452
0
      return "Different API version between core & binding (CS_ERR_VERSION)";
453
0
    case CS_ERR_DIET:
454
0
      return "Information irrelevant in diet engine (CS_ERR_DIET)";
455
0
    case CS_ERR_SKIPDATA:
456
0
      return "Information irrelevant for 'data' instruction in SKIPDATA mode (CS_ERR_SKIPDATA)";
457
0
    case CS_ERR_X86_ATT:
458
0
      return "AT&T syntax is unavailable (CS_ERR_X86_ATT)";
459
0
    case CS_ERR_X86_INTEL:
460
0
      return "INTEL syntax is unavailable (CS_ERR_X86_INTEL)";
461
0
    case CS_ERR_X86_MASM:
462
0
      return "MASM syntax is unavailable (CS_ERR_X86_MASM)";
463
0
  }
464
0
}
465
466
CAPSTONE_EXPORT
467
cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
468
48.6k
{
469
48.6k
  cs_err err;
470
48.6k
  struct cs_struct *ud;
471
48.6k
  if (!cs_mem_malloc || !cs_mem_calloc || !cs_mem_realloc || !cs_mem_free || !cs_vsnprintf)
472
    // Error: before cs_open(), dynamic memory management must be initialized
473
    // with cs_option(CS_OPT_MEM)
474
0
    return CS_ERR_MEMSETUP;
475
476
48.6k
  if (arch < CS_ARCH_MAX && arch_configs[arch].arch_init) {
477
    // verify if requested mode is valid
478
48.6k
    if (mode & arch_configs[arch].arch_disallowed_mode_mask) {
479
5
      *handle = 0;
480
5
      return CS_ERR_MODE;
481
5
    }
482
483
48.6k
    ud = cs_mem_calloc(1, sizeof(*ud));
484
48.6k
    if (!ud) {
485
      // memory insufficient
486
0
      return CS_ERR_MEM;
487
0
    }
488
489
48.6k
    ud->errnum = CS_ERR_OK;
490
48.6k
    ud->arch = arch;
491
48.6k
    ud->mode = mode;
492
    // by default, do not break instruction into details
493
48.6k
    ud->detail_opt = CS_OPT_OFF;
494
495
    // default skipdata setup
496
48.6k
    ud->skipdata_setup.mnemonic = SKIPDATA_MNEM;
497
498
48.6k
    err = arch_configs[ud->arch].arch_init(ud);
499
48.6k
    if (err) {
500
0
      cs_mem_free(ud);
501
0
      *handle = 0;
502
0
      return err;
503
0
    }
504
505
48.6k
    *handle = (uintptr_t)ud;
506
507
48.6k
    return CS_ERR_OK;
508
48.6k
  } else {
509
0
    *handle = 0;
510
0
    return CS_ERR_ARCH;
511
0
  }
512
48.6k
}
513
514
CAPSTONE_EXPORT
515
cs_err CAPSTONE_API cs_close(csh *handle)
516
48.6k
{
517
48.6k
  struct cs_struct *ud;
518
48.6k
  struct insn_mnem *next, *tmp;
519
520
48.6k
  if (*handle == 0)
521
    // invalid handle
522
0
    return CS_ERR_CSH;
523
524
48.6k
  ud = (struct cs_struct *)(*handle);
525
526
48.6k
  if (ud->printer_info)
527
46.2k
    cs_mem_free(ud->printer_info);
528
529
  // free the linked list of customized mnemonic
530
48.6k
  tmp = ud->mnem_list;
531
48.6k
  while(tmp) {
532
0
    next = tmp->next;
533
0
    cs_mem_free(tmp);
534
0
    tmp = next;
535
0
  }
536
537
48.6k
  cs_mem_free(ud->insn_cache);
538
539
48.6k
  memset(ud, 0, sizeof(*ud));
540
48.6k
  cs_mem_free(ud);
541
542
  // invalidate this handle by ZERO out its value.
543
  // this is to make sure it is unusable after cs_close()
544
48.6k
  *handle = 0;
545
546
48.6k
  return CS_ERR_OK;
547
48.6k
}
548
549
// replace str1 in target with str2; target starts with str1
550
// output is put into result (which is array of char with size CS_MNEMONIC_SIZE)
551
// return 0 on success, -1 on failure
552
static int str_replace(char *result, char *target, const char *str1, char *str2)
553
0
{
554
  // only perform replacement if the output fits into result
555
0
  if (strlen(target) - strlen(str1) + strlen(str2) < CS_MNEMONIC_SIZE - 1)  {
556
    // copy str2 to begining of result
557
0
    strcpy(result, str2);
558
    // skip str1 - already replaced by str2
559
0
    strcat(result, target + strlen(str1));
560
561
0
    return 0;
562
0
  } else
563
0
    return -1;
564
0
}
565
566
/// The asm string sometimes has a leading space or tab.
567
/// Here we remove it.
568
3.29M
static void fixup_asm_string(char *asm_str) {
569
3.29M
  if (!asm_str) {
570
0
    return;
571
0
  }
572
3.29M
  int i = 0;
573
3.29M
  int k = 0;
574
3.29M
  bool text_reached = (asm_str[0] != ' ' && asm_str[0] != '\t');
575
60.2M
  while (asm_str[i]) {
576
56.9M
    if (!text_reached && (asm_str[i] == ' ' || asm_str[i] == '\t')) {
577
2.33k
      ++i;
578
2.33k
      text_reached = true;
579
2.33k
      continue;
580
2.33k
    }
581
56.9M
    asm_str[k] = asm_str[i];
582
56.9M
    ++k, ++i;
583
56.9M
  }
584
3.29M
  asm_str[k] = '\0';
585
3.29M
}
586
587
// fill insn with mnemonic & operands info
588
static void fill_insn(struct cs_struct *handle, cs_insn *insn, char *buffer, MCInst *mci,
589
    PostPrinter_t postprinter, const uint8_t *code)
590
3.29M
{
591
3.29M
#ifndef CAPSTONE_DIET
592
3.29M
  char *sp, *mnem;
593
3.29M
#endif
594
3.29M
  fixup_asm_string(buffer);
595
3.29M
  uint16_t copy_size = MIN(sizeof(insn->bytes), insn->size);
596
597
  // fill the instruction bytes.
598
  // we might skip some redundant bytes in front in the case of X86
599
3.29M
  memcpy(insn->bytes, code + insn->size - copy_size, copy_size);
600
3.29M
  insn->op_str[0] = '\0';
601
3.29M
  insn->size = copy_size;
602
603
  // alias instruction might have ID saved in OpcodePub
604
3.29M
  if (MCInst_getOpcodePub(mci))
605
106k
    insn->id = MCInst_getOpcodePub(mci);
606
607
  // post printer handles some corner cases (hacky)
608
3.29M
  if (postprinter)
609
170k
    postprinter((csh)handle, insn, buffer, mci);
610
611
3.29M
#ifndef CAPSTONE_DIET
612
3.29M
  mnem = insn->mnemonic;
613
  // memset(mnem, 0, CS_MNEMONIC_SIZE);
614
18.2M
  for (sp = buffer; *sp; sp++) {
615
18.0M
    if (*sp == ' '|| *sp == '\t')
616
3.11M
      break;
617
14.9M
    if (*sp == '|')  // lock|rep prefix for x86
618
79.4k
      *sp = ' ';
619
    // copy to @mnemonic
620
14.9M
    *mnem = *sp;
621
14.9M
    mnem++;
622
14.9M
  }
623
624
3.29M
  *mnem = '\0';
625
626
  // we might have customized mnemonic
627
3.29M
  if (handle->mnem_list) {
628
0
    struct insn_mnem *tmp = handle->mnem_list;
629
0
    while(tmp) {
630
0
      if (tmp->insn.id == insn->id) {
631
0
        char str[CS_MNEMONIC_SIZE];
632
633
0
        if (!str_replace(str, insn->mnemonic, cs_insn_name((csh)handle, insn->id), tmp->insn.mnemonic)) {
634
          // copy result to mnemonic
635
0
          (void)strncpy(insn->mnemonic, str, sizeof(insn->mnemonic) - 1);
636
0
          insn->mnemonic[sizeof(insn->mnemonic) - 1] = '\0';
637
0
        }
638
639
0
        break;
640
0
      }
641
0
      tmp = tmp->next;
642
0
    }
643
0
  }
644
645
  // copy @op_str
646
3.29M
  if (*sp) {
647
    // find the next non-space char
648
3.11M
    sp++;
649
3.11M
    for (; ((*sp == ' ') || (*sp == '\t')); sp++);
650
3.11M
    strncpy(insn->op_str, sp, sizeof(insn->op_str) - 1);
651
3.11M
    insn->op_str[sizeof(insn->op_str) - 1] = '\0';
652
3.11M
  } else
653
173k
    insn->op_str[0] = '\0';
654
655
3.29M
#endif
656
3.29M
}
657
658
// how many bytes will we skip when encountering data (CS_OPT_SKIPDATA)?
659
// this very much depends on instruction alignment requirement of each arch.
660
static uint8_t skipdata_size(cs_struct *handle)
661
0
{
662
0
  switch(handle->arch) {
663
0
    default:
664
      // should never reach
665
0
      return (uint8_t)-1;
666
0
    case CS_ARCH_ARM:
667
      // skip 2 bytes on Thumb mode.
668
0
      if (handle->mode & CS_MODE_THUMB)
669
0
        return 2;
670
      // otherwise, skip 4 bytes
671
0
      return 4;
672
0
    case CS_ARCH_AARCH64:
673
0
    case CS_ARCH_MIPS:
674
0
    case CS_ARCH_PPC:
675
0
    case CS_ARCH_SPARC:
676
      // skip 4 bytes
677
0
      return 4;
678
0
    case CS_ARCH_SYSZ:
679
      // SystemZ instruction's length can be 2, 4 or 6 bytes,
680
      // so we just skip 2 bytes
681
0
      return 2;
682
0
    case CS_ARCH_X86:
683
      // X86 has no restriction on instruction alignment
684
0
      return 1;
685
0
    case CS_ARCH_XCORE:
686
      // XCore instruction's length can be 2 or 4 bytes,
687
      // so we just skip 2 bytes
688
0
      return 2;
689
0
    case CS_ARCH_M68K:
690
      // M68K has 2 bytes instruction alignment but contain multibyte instruction so we skip 2 bytes
691
0
      return 2;
692
0
    case CS_ARCH_TMS320C64X:
693
      // TMS320C64x alignment is 4.
694
0
      return 4;
695
0
    case CS_ARCH_M680X:
696
      // M680X alignment is 1.
697
0
      return 1;
698
0
    case CS_ARCH_EVM:
699
      // EVM alignment is 1.
700
0
      return 1;
701
0
    case CS_ARCH_WASM:
702
      //WASM alignment is 1
703
0
      return 1;
704
0
    case CS_ARCH_MOS65XX:
705
      // MOS65XX alignment is 1.
706
0
      return 1;
707
0
    case CS_ARCH_BPF:
708
      // both classic and extended BPF have alignment 8.
709
0
      return 8;
710
0
    case CS_ARCH_RISCV:
711
      // special compress mode
712
0
      if (handle->mode & CS_MODE_RISCVC)
713
0
        return 2;
714
0
      return 4;
715
0
    case CS_ARCH_SH:
716
0
      return 2;
717
0
    case CS_ARCH_TRICORE:
718
      // TriCore instruction's length can be 2 or 4 bytes,
719
      // so we just skip 2 bytes
720
0
      return 2;
721
0
  }
722
0
}
723
724
CAPSTONE_EXPORT
725
cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
726
60.0k
{
727
60.0k
  struct cs_struct *handle;
728
60.0k
  cs_opt_mnem *opt;
729
730
  // cs_option() can be called with NULL handle just for CS_OPT_MEM
731
  // This is supposed to be executed before all other APIs (even cs_open())
732
60.0k
  if (type == CS_OPT_MEM) {
733
0
    cs_opt_mem *mem = (cs_opt_mem *)value;
734
735
0
    cs_mem_malloc = mem->malloc;
736
0
    cs_mem_calloc = mem->calloc;
737
0
    cs_mem_realloc = mem->realloc;
738
0
    cs_mem_free = mem->free;
739
0
    cs_vsnprintf = mem->vsnprintf;
740
741
0
    return CS_ERR_OK;
742
0
  }
743
744
60.0k
  handle = (struct cs_struct *)(uintptr_t)ud;
745
60.0k
  if (!handle)
746
0
    return CS_ERR_CSH;
747
748
60.0k
  switch(type) {
749
11.3k
    default:
750
11.3k
      break;
751
752
11.3k
    case CS_OPT_UNSIGNED:
753
0
      handle->imm_unsigned = (cs_opt_value)value;
754
0
      return CS_ERR_OK;
755
756
48.6k
    case CS_OPT_DETAIL:
757
48.6k
      handle->detail_opt |= (cs_opt_value)value;
758
48.6k
      return CS_ERR_OK;
759
760
0
    case CS_OPT_SKIPDATA:
761
0
      handle->skipdata = (value == CS_OPT_ON);
762
0
      if (handle->skipdata) {
763
0
        if (handle->skipdata_size == 0) {
764
          // set the default skipdata size
765
0
          handle->skipdata_size = skipdata_size(handle);
766
0
        }
767
0
      }
768
0
      return CS_ERR_OK;
769
770
0
    case CS_OPT_SKIPDATA_SETUP:
771
0
      if (value) {
772
0
        handle->skipdata_setup = *((cs_opt_skipdata *)value);
773
0
        if (handle->skipdata_setup.mnemonic == NULL) {
774
0
          handle->skipdata_setup.mnemonic = SKIPDATA_MNEM;
775
0
        }
776
0
      }
777
0
      return CS_ERR_OK;
778
779
0
    case CS_OPT_MNEMONIC:
780
0
      opt = (cs_opt_mnem *)value;
781
0
      if (opt->id) {
782
0
        if (opt->mnemonic) {
783
0
          struct insn_mnem *tmp;
784
785
          // add new instruction, or replace existing instruction
786
          // 1. find if we already had this insn in the linked list
787
0
          tmp = handle->mnem_list;
788
0
          while(tmp) {
789
0
            if (tmp->insn.id == opt->id) {
790
              // found this instruction, so replace its mnemonic
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
0
              break;
794
0
            }
795
0
            tmp = tmp->next;
796
0
          }
797
798
          // 2. add this instruction if we have not had it yet
799
0
          if (!tmp) {
800
0
            tmp = cs_mem_malloc(sizeof(*tmp));
801
0
            tmp->insn.id = opt->id;
802
0
            (void)strncpy(tmp->insn.mnemonic, opt->mnemonic, sizeof(tmp->insn.mnemonic) - 1);
803
0
            tmp->insn.mnemonic[sizeof(tmp->insn.mnemonic) - 1] = '\0';
804
            // this new instruction is heading the list
805
0
            tmp->next = handle->mnem_list;
806
0
            handle->mnem_list = tmp;
807
0
          }
808
0
          return CS_ERR_OK;
809
0
        } else {
810
0
          struct insn_mnem *prev, *tmp;
811
812
          // we want to delete an existing instruction
813
          // iterate the list to find the instruction to remove it
814
0
          tmp = handle->mnem_list;
815
0
          prev = tmp;
816
0
          while(tmp) {
817
0
            if (tmp->insn.id == opt->id) {
818
              // delete this instruction
819
0
              if (tmp == prev) {
820
                // head of the list
821
0
                handle->mnem_list = tmp->next;
822
0
              } else {
823
0
                prev->next = tmp->next;
824
0
              }
825
0
              cs_mem_free(tmp);
826
0
              break;
827
0
            }
828
0
            prev = tmp;
829
0
            tmp = tmp->next;
830
0
          }
831
0
        }
832
0
      }
833
0
      return CS_ERR_OK;
834
835
0
    case CS_OPT_MODE:
836
      // verify if requested mode is valid
837
0
      if (value & arch_configs[handle->arch].arch_disallowed_mode_mask) {
838
0
        return CS_ERR_OPTION;
839
0
      }
840
0
      break;
841
0
    case CS_OPT_NO_BRANCH_OFFSET:
842
0
      if (handle->PrintBranchImmNotAsAddress)
843
0
        return CS_ERR_OK;
844
0
      break;
845
60.0k
  }
846
847
11.3k
  return arch_configs[handle->arch].arch_option(handle, type, value);
848
60.0k
}
849
850
// generate @op_str for data instruction of SKIPDATA
851
#ifndef CAPSTONE_DIET
852
static void skipdata_opstr(char *opstr, const uint8_t *buffer, size_t size)
853
0
{
854
0
  char *p = opstr;
855
0
  int len;
856
0
  size_t i;
857
0
  size_t available = sizeof(((cs_insn*)NULL)->op_str);
858
859
0
  if (!size) {
860
0
    opstr[0] = '\0';
861
0
    return;
862
0
  }
863
864
0
  len = cs_snprintf(p, available, "0x%02x", buffer[0]);
865
0
  p+= len;
866
0
  available -= len;
867
868
0
  for(i = 1; i < size; i++) {
869
0
    len = cs_snprintf(p, available, ", 0x%02x", buffer[i]);
870
0
    if (len < 0) {
871
0
      break;
872
0
    }
873
0
    if ((size_t)len > available - 1) {
874
0
      break;
875
0
    }
876
0
    p+= len;
877
0
    available -= len;
878
0
  }
879
0
}
880
#endif
881
882
// dynamicly allocate memory to contain disasm insn
883
// NOTE: caller must free() the allocated memory itself to avoid memory leaking
884
CAPSTONE_EXPORT
885
size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
886
48.6k
{
887
48.6k
  struct cs_struct *handle;
888
48.6k
  MCInst mci;
889
48.6k
  uint16_t insn_size;
890
48.6k
  size_t c = 0, i;
891
48.6k
  unsigned int f = 0; // index of the next instruction in the cache
892
48.6k
  cs_insn *insn_cache;  // cache contains disassembled instructions
893
48.6k
  void *total = NULL;
894
48.6k
  size_t total_size = 0;  // total size of output buffer containing all insns
895
48.6k
  bool r;
896
48.6k
  void *tmp;
897
48.6k
  size_t skipdata_bytes;
898
48.6k
  uint64_t offset_org; // save all the original info of the buffer
899
48.6k
  size_t size_org;
900
48.6k
  const uint8_t *buffer_org;
901
48.6k
  unsigned int cache_size = INSN_CACHE_SIZE;
902
48.6k
  size_t next_offset;
903
904
48.6k
  handle = (struct cs_struct *)(uintptr_t)ud;
905
48.6k
  if (!handle) {
906
    // FIXME: how to handle this case:
907
    // handle->errnum = CS_ERR_HANDLE;
908
0
    return 0;
909
0
  }
910
911
48.6k
  handle->errnum = CS_ERR_OK;
912
913
48.6k
#ifdef CAPSTONE_USE_SYS_DYN_MEM
914
48.6k
  if (count > 0 && count <= INSN_CACHE_SIZE)
915
0
    cache_size = (unsigned int) count;
916
48.6k
#endif
917
918
  // save the original offset for SKIPDATA
919
48.6k
  buffer_org = buffer;
920
48.6k
  offset_org = offset;
921
48.6k
  size_org = size;
922
923
48.6k
  total_size = sizeof(cs_insn) * cache_size;
924
48.6k
  total = cs_mem_calloc(sizeof(cs_insn), cache_size);
925
48.6k
  if (total == NULL) {
926
    // insufficient memory
927
0
    handle->errnum = CS_ERR_MEM;
928
0
    return 0;
929
0
  }
930
931
48.6k
  insn_cache = total;
932
933
3.34M
  while (size > 0) {
934
3.32M
    MCInst_Init(&mci);
935
3.32M
    mci.csh = handle;
936
937
    // relative branches need to know the address & size of current insn
938
3.32M
    mci.address = offset;
939
940
3.32M
    if (handle->detail_opt) {
941
      // allocate memory for @detail pointer
942
3.32M
      insn_cache->detail = cs_mem_malloc(sizeof(cs_detail));
943
3.32M
    } else {
944
0
      insn_cache->detail = NULL;
945
0
    }
946
947
    // save all the information for non-detailed mode
948
3.32M
    mci.flat_insn = insn_cache;
949
3.32M
    mci.flat_insn->address = offset;
950
#ifdef CAPSTONE_DIET
951
    // zero out mnemonic & op_str
952
    mci.flat_insn->mnemonic[0] = '\0';
953
    mci.flat_insn->op_str[0] = '\0';
954
#endif
955
956
3.32M
    r = handle->disasm(ud, buffer, size, &mci, &insn_size, offset, handle->getinsn_info);
957
3.32M
    if (r) {
958
3.29M
      SStream ss;
959
3.29M
      SStream_Init(&ss);
960
961
3.29M
      mci.flat_insn->size = insn_size;
962
963
      // map internal instruction opcode to public insn ID
964
965
3.29M
      handle->insn_id(handle, insn_cache, mci.Opcode);
966
967
3.29M
      handle->printer(&mci, &ss, handle->printer_info);
968
3.29M
      fill_insn(handle, insn_cache, ss.buffer, &mci, handle->post_printer, buffer);
969
970
      // adjust for pseudo opcode (X86)
971
3.29M
      if (handle->arch == CS_ARCH_X86)
972
957k
        insn_cache->id += mci.popcode_adjust;
973
974
3.29M
      next_offset = insn_size;
975
3.29M
    } else {
976
      // encounter a broken instruction
977
978
      // free memory of @detail pointer
979
30.1k
      if (handle->detail_opt) {
980
30.1k
        cs_mem_free(insn_cache->detail);
981
30.1k
      }
982
983
      // if there is no request to skip data, or remaining data is too small,
984
      // then bail out
985
30.1k
      if (!handle->skipdata || handle->skipdata_size > size)
986
30.1k
        break;
987
988
0
      if (handle->skipdata_setup.callback) {
989
0
        skipdata_bytes = handle->skipdata_setup.callback(buffer_org, size_org,
990
0
            (size_t)(offset - offset_org), handle->skipdata_setup.user_data);
991
0
        if (skipdata_bytes > size)
992
          // remaining data is not enough
993
0
          break;
994
995
0
        if (!skipdata_bytes)
996
          // user requested not to skip data, so bail out
997
0
          break;
998
0
      } else
999
0
        skipdata_bytes = handle->skipdata_size;
1000
1001
      // we have to skip some amount of data, depending on arch & mode
1002
0
      insn_cache->id = 0; // invalid ID for this "data" instruction
1003
0
      insn_cache->address = offset;
1004
0
      insn_cache->size = (uint16_t)skipdata_bytes;
1005
0
      memcpy(insn_cache->bytes, buffer, skipdata_bytes);
1006
#ifdef CAPSTONE_DIET
1007
      insn_cache->mnemonic[0] = '\0';
1008
      insn_cache->op_str[0] = '\0';
1009
#else
1010
0
      strncpy(insn_cache->mnemonic, handle->skipdata_setup.mnemonic,
1011
0
          sizeof(insn_cache->mnemonic) - 1);
1012
0
      skipdata_opstr(insn_cache->op_str, buffer, skipdata_bytes);
1013
0
#endif
1014
0
      insn_cache->detail = NULL;
1015
1016
0
      next_offset = skipdata_bytes;
1017
0
    }
1018
1019
    // one more instruction entering the cache
1020
3.29M
    f++;
1021
1022
    // one more instruction disassembled
1023
3.29M
    c++;
1024
3.29M
    if (count > 0 && c == count)
1025
      // already got requested number of instructions
1026
0
      break;
1027
1028
3.29M
    if (f == cache_size) {
1029
      // full cache, so expand the cache to contain incoming insns
1030
31.7k
      cache_size = cache_size * 8 / 5; // * 1.6 ~ golden ratio
1031
31.7k
      total_size += (sizeof(cs_insn) * cache_size);
1032
31.7k
      tmp = cs_mem_realloc(total, total_size);
1033
31.7k
      if (tmp == NULL) { // insufficient memory
1034
0
        if (handle->detail_opt) {
1035
0
          insn_cache = (cs_insn *)total;
1036
0
          for (i = 0; i < c; i++, insn_cache++)
1037
0
            cs_mem_free(insn_cache->detail);
1038
0
        }
1039
1040
0
        cs_mem_free(total);
1041
0
        *insn = NULL;
1042
0
        handle->errnum = CS_ERR_MEM;
1043
0
        return 0;
1044
0
      }
1045
1046
31.7k
      total = tmp;
1047
      // continue to fill in the cache after the last instruction
1048
31.7k
      insn_cache = (cs_insn *)((char *)total + sizeof(cs_insn) * c);
1049
1050
      // reset f back to 0, so we fill in the cache from begining
1051
31.7k
      f = 0;
1052
31.7k
    } else
1053
3.25M
      insn_cache++;
1054
1055
3.29M
    buffer += next_offset;
1056
3.29M
    size -= next_offset;
1057
3.29M
    offset += next_offset;
1058
3.29M
  }
1059
1060
48.6k
  if (!c) {
1061
    // we did not disassemble any instruction
1062
843
    cs_mem_free(total);
1063
843
    total = NULL;
1064
47.8k
  } else if (f != cache_size) {
1065
    // total did not fully use the last cache, so downsize it
1066
47.8k
    tmp = cs_mem_realloc(total, total_size - (cache_size - f) * sizeof(*insn_cache));
1067
47.8k
    if (tmp == NULL) { // insufficient memory
1068
      // free all detail pointers
1069
0
      if (handle->detail_opt) {
1070
0
        insn_cache = (cs_insn *)total;
1071
0
        for (i = 0; i < c; i++, insn_cache++)
1072
0
          cs_mem_free(insn_cache->detail);
1073
0
      }
1074
1075
0
      cs_mem_free(total);
1076
0
      *insn = NULL;
1077
1078
0
      handle->errnum = CS_ERR_MEM;
1079
0
      return 0;
1080
0
    }
1081
1082
47.8k
    total = tmp;
1083
47.8k
  }
1084
1085
48.6k
  *insn = total;
1086
1087
48.6k
  return c;
1088
48.6k
}
1089
1090
CAPSTONE_EXPORT
1091
void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
1092
47.8k
{
1093
47.8k
  size_t i;
1094
1095
  // free all detail pointers
1096
3.33M
  for (i = 0; i < count; i++)
1097
3.29M
    cs_mem_free(insn[i].detail);
1098
1099
  // then free pointer to cs_insn array
1100
47.8k
  cs_mem_free(insn);
1101
47.8k
}
1102
1103
CAPSTONE_EXPORT
1104
cs_insn * CAPSTONE_API cs_malloc(csh ud)
1105
0
{
1106
0
  cs_insn *insn;
1107
0
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1108
1109
0
  insn = cs_mem_malloc(sizeof(cs_insn));
1110
0
  if (!insn) {
1111
    // insufficient memory
1112
0
    handle->errnum = CS_ERR_MEM;
1113
0
    return NULL;
1114
0
  } else {
1115
0
    if (handle->detail_opt) {
1116
      // allocate memory for @detail pointer
1117
0
      insn->detail = cs_mem_malloc(sizeof(cs_detail));
1118
0
      if (insn->detail == NULL) { // insufficient memory
1119
0
        cs_mem_free(insn);
1120
0
        handle->errnum = CS_ERR_MEM;
1121
0
        return NULL;
1122
0
      }
1123
0
    } else
1124
0
      insn->detail = NULL;
1125
0
  }
1126
1127
0
  return insn;
1128
0
}
1129
1130
// iterator for instruction "single-stepping"
1131
CAPSTONE_EXPORT
1132
bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size,
1133
    uint64_t *address, cs_insn *insn)
1134
0
{
1135
0
  struct cs_struct *handle;
1136
0
  uint16_t insn_size;
1137
0
  MCInst mci;
1138
0
  bool r;
1139
1140
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1141
0
  if (!handle) {
1142
0
    return false;
1143
0
  }
1144
1145
0
  handle->errnum = CS_ERR_OK;
1146
1147
0
  MCInst_Init(&mci);
1148
0
  mci.csh = handle;
1149
1150
  // relative branches need to know the address & size of current insn
1151
0
  mci.address = *address;
1152
1153
  // save all the information for non-detailed mode
1154
0
  mci.flat_insn = insn;
1155
0
  mci.flat_insn->address = *address;
1156
#ifdef CAPSTONE_DIET
1157
  // zero out mnemonic & op_str
1158
  mci.flat_insn->mnemonic[0] = '\0';
1159
  mci.flat_insn->op_str[0] = '\0';
1160
#endif
1161
1162
0
  r = handle->disasm(ud, *code, *size, &mci, &insn_size, *address, handle->getinsn_info);
1163
0
  if (r) {
1164
0
    SStream ss;
1165
0
    SStream_Init(&ss);
1166
1167
0
    mci.flat_insn->size = insn_size;
1168
1169
    // map internal instruction opcode to public insn ID
1170
0
    handle->insn_id(handle, insn, mci.Opcode);
1171
1172
0
    handle->printer(&mci, &ss, handle->printer_info);
1173
1174
0
    fill_insn(handle, insn, ss.buffer, &mci, handle->post_printer, *code);
1175
1176
    // adjust for pseudo opcode (X86)
1177
0
    if (handle->arch == CS_ARCH_X86)
1178
0
      insn->id += mci.popcode_adjust;
1179
1180
0
    *code += insn_size;
1181
0
    *size -= insn_size;
1182
0
    *address += insn_size;
1183
0
  } else {   // encounter a broken instruction
1184
0
    size_t skipdata_bytes;
1185
1186
    // if there is no request to skip data, or remaining data is too small,
1187
    // then bail out
1188
0
    if (!handle->skipdata || handle->skipdata_size > *size)
1189
0
      return false;
1190
1191
0
    if (handle->skipdata_setup.callback) {
1192
0
      skipdata_bytes = handle->skipdata_setup.callback(*code, *size,
1193
0
          0, handle->skipdata_setup.user_data);
1194
0
      if (skipdata_bytes > *size)
1195
        // remaining data is not enough
1196
0
        return false;
1197
1198
0
      if (!skipdata_bytes)
1199
        // user requested not to skip data, so bail out
1200
0
        return false;
1201
0
    } else
1202
0
      skipdata_bytes = handle->skipdata_size;
1203
1204
    // we have to skip some amount of data, depending on arch & mode
1205
0
    insn->id = 0; // invalid ID for this "data" instruction
1206
0
    insn->address = *address;
1207
0
    insn->size = (uint16_t)skipdata_bytes;
1208
#ifdef CAPSTONE_DIET
1209
    insn->mnemonic[0] = '\0';
1210
    insn->op_str[0] = '\0';
1211
#else
1212
0
    memcpy(insn->bytes, *code, skipdata_bytes);
1213
0
    strncpy(insn->mnemonic, handle->skipdata_setup.mnemonic,
1214
0
        sizeof(insn->mnemonic) - 1);
1215
0
    skipdata_opstr(insn->op_str, *code, skipdata_bytes);
1216
0
#endif
1217
1218
0
    *code += skipdata_bytes;
1219
0
    *size -= skipdata_bytes;
1220
0
    *address += skipdata_bytes;
1221
0
  }
1222
1223
0
  return true;
1224
0
}
1225
1226
// return friendly name of register in a string
1227
CAPSTONE_EXPORT
1228
const char * CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
1229
3.19M
{
1230
3.19M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1231
1232
3.19M
  if (!handle || handle->reg_name == NULL) {
1233
0
    return NULL;
1234
0
  }
1235
1236
3.19M
  return handle->reg_name(ud, reg);
1237
3.19M
}
1238
1239
CAPSTONE_EXPORT
1240
const char * CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
1241
3.29M
{
1242
3.29M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1243
1244
3.29M
  if (!handle || handle->insn_name == NULL) {
1245
0
    return NULL;
1246
0
  }
1247
1248
3.29M
  return handle->insn_name(ud, insn);
1249
3.29M
}
1250
1251
CAPSTONE_EXPORT
1252
const char * CAPSTONE_API cs_group_name(csh ud, unsigned int group)
1253
2.47M
{
1254
2.47M
  struct cs_struct *handle = (struct cs_struct *)(uintptr_t)ud;
1255
1256
2.47M
  if (!handle || handle->group_name == NULL) {
1257
0
    return NULL;
1258
0
  }
1259
1260
2.47M
  return handle->group_name(ud, group);
1261
2.47M
}
1262
1263
CAPSTONE_EXPORT
1264
bool CAPSTONE_API cs_insn_group(csh ud, const cs_insn *insn, unsigned int group_id)
1265
0
{
1266
0
  struct cs_struct *handle;
1267
0
  if (!ud)
1268
0
    return false;
1269
1270
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1271
1272
0
  if (!handle->detail_opt) {
1273
0
    handle->errnum = CS_ERR_DETAIL;
1274
0
    return false;
1275
0
  }
1276
1277
0
  if (!insn->id) {
1278
0
    handle->errnum = CS_ERR_SKIPDATA;
1279
0
    return false;
1280
0
  }
1281
1282
0
  if (!insn->detail) {
1283
0
    handle->errnum = CS_ERR_DETAIL;
1284
0
    return false;
1285
0
  }
1286
1287
0
  return arr_exist8(insn->detail->groups, insn->detail->groups_count, group_id);
1288
0
}
1289
1290
CAPSTONE_EXPORT
1291
bool CAPSTONE_API cs_reg_read(csh ud, const cs_insn *insn, unsigned int reg_id)
1292
0
{
1293
0
  struct cs_struct *handle;
1294
0
  if (!ud)
1295
0
    return false;
1296
1297
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1298
1299
0
  if (!handle->detail_opt) {
1300
0
    handle->errnum = CS_ERR_DETAIL;
1301
0
    return false;
1302
0
  }
1303
1304
0
  if (!insn->id) {
1305
0
    handle->errnum = CS_ERR_SKIPDATA;
1306
0
    return false;
1307
0
  }
1308
1309
0
  if (!insn->detail) {
1310
0
    handle->errnum = CS_ERR_DETAIL;
1311
0
    return false;
1312
0
  }
1313
1314
0
  return arr_exist(insn->detail->regs_read, insn->detail->regs_read_count, reg_id);
1315
0
}
1316
1317
CAPSTONE_EXPORT
1318
bool CAPSTONE_API cs_reg_write(csh ud, const cs_insn *insn, unsigned int reg_id)
1319
0
{
1320
0
  struct cs_struct *handle;
1321
0
  if (!ud)
1322
0
    return false;
1323
1324
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1325
1326
0
  if (!handle->detail_opt) {
1327
0
    handle->errnum = CS_ERR_DETAIL;
1328
0
    return false;
1329
0
  }
1330
1331
0
  if (!insn->id) {
1332
0
    handle->errnum = CS_ERR_SKIPDATA;
1333
0
    return false;
1334
0
  }
1335
1336
0
  if (!insn->detail) {
1337
0
    handle->errnum = CS_ERR_DETAIL;
1338
0
    return false;
1339
0
  }
1340
1341
0
  return arr_exist(insn->detail->regs_write, insn->detail->regs_write_count, reg_id);
1342
0
}
1343
1344
CAPSTONE_EXPORT
1345
int CAPSTONE_API cs_op_count(csh ud, const cs_insn *insn, unsigned int op_type)
1346
0
{
1347
0
  struct cs_struct *handle;
1348
0
  unsigned int count = 0, i;
1349
0
  if (!ud)
1350
0
    return -1;
1351
1352
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1353
1354
0
  if (!handle->detail_opt) {
1355
0
    handle->errnum = CS_ERR_DETAIL;
1356
0
    return -1;
1357
0
  }
1358
1359
0
  if (!insn->id) {
1360
0
    handle->errnum = CS_ERR_SKIPDATA;
1361
0
    return -1;
1362
0
  }
1363
1364
0
  if (!insn->detail) {
1365
0
    handle->errnum = CS_ERR_DETAIL;
1366
0
    return -1;
1367
0
  }
1368
1369
0
  handle->errnum = CS_ERR_OK;
1370
1371
0
  switch (handle->arch) {
1372
0
    default:
1373
0
      handle->errnum = CS_ERR_HANDLE;
1374
0
      return -1;
1375
0
    case CS_ARCH_ARM:
1376
0
      for (i = 0; i < insn->detail->arm.op_count; i++)
1377
0
        if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
1378
0
          count++;
1379
0
      break;
1380
0
    case CS_ARCH_AARCH64:
1381
0
      for (i = 0; i < insn->detail->aarch64.op_count; i++)
1382
0
        if (insn->detail->aarch64.operands[i].type == (aarch64_op_type)op_type)
1383
0
          count++;
1384
0
      break;
1385
0
    case CS_ARCH_X86:
1386
0
      for (i = 0; i < insn->detail->x86.op_count; i++)
1387
0
        if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
1388
0
          count++;
1389
0
      break;
1390
0
    case CS_ARCH_MIPS:
1391
0
      for (i = 0; i < insn->detail->mips.op_count; i++)
1392
0
        if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
1393
0
          count++;
1394
0
      break;
1395
0
    case CS_ARCH_PPC:
1396
0
      for (i = 0; i < insn->detail->ppc.op_count; i++)
1397
0
        if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
1398
0
          count++;
1399
0
      break;
1400
0
    case CS_ARCH_SPARC:
1401
0
      for (i = 0; i < insn->detail->sparc.op_count; i++)
1402
0
        if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1403
0
          count++;
1404
0
      break;
1405
0
    case CS_ARCH_SYSZ:
1406
0
      for (i = 0; i < insn->detail->sysz.op_count; i++)
1407
0
        if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1408
0
          count++;
1409
0
      break;
1410
0
    case CS_ARCH_XCORE:
1411
0
      for (i = 0; i < insn->detail->xcore.op_count; i++)
1412
0
        if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1413
0
          count++;
1414
0
      break;
1415
0
    case CS_ARCH_M68K:
1416
0
      for (i = 0; i < insn->detail->m68k.op_count; i++)
1417
0
        if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1418
0
          count++;
1419
0
      break;
1420
0
    case CS_ARCH_TMS320C64X:
1421
0
      for (i = 0; i < insn->detail->tms320c64x.op_count; i++)
1422
0
        if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1423
0
          count++;
1424
0
      break;
1425
0
    case CS_ARCH_M680X:
1426
0
      for (i = 0; i < insn->detail->m680x.op_count; i++)
1427
0
        if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1428
0
          count++;
1429
0
      break;
1430
0
    case CS_ARCH_EVM:
1431
0
      break;
1432
0
    case CS_ARCH_MOS65XX:
1433
0
      for (i = 0; i < insn->detail->mos65xx.op_count; i++)
1434
0
        if (insn->detail->mos65xx.operands[i].type == (mos65xx_op_type)op_type)
1435
0
          count++;
1436
0
      break;
1437
0
    case CS_ARCH_WASM:
1438
0
      for (i = 0; i < insn->detail->wasm.op_count; i++)
1439
0
        if (insn->detail->wasm.operands[i].type == (wasm_op_type)op_type)
1440
0
          count++;
1441
0
      break;
1442
0
    case CS_ARCH_BPF:
1443
0
      for (i = 0; i < insn->detail->bpf.op_count; i++)
1444
0
        if (insn->detail->bpf.operands[i].type == (bpf_op_type)op_type)
1445
0
          count++;
1446
0
      break;
1447
0
    case CS_ARCH_RISCV:
1448
0
      for (i = 0; i < insn->detail->riscv.op_count; i++)
1449
0
        if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type)
1450
0
          count++;
1451
0
      break;
1452
0
    case CS_ARCH_TRICORE:
1453
0
      for (i = 0; i < insn->detail->tricore.op_count; i++)
1454
0
        if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
1455
0
          count++;
1456
0
      break;
1457
0
  }
1458
1459
0
  return count;
1460
0
}
1461
1462
CAPSTONE_EXPORT
1463
int CAPSTONE_API cs_op_index(csh ud, const cs_insn *insn, unsigned int op_type,
1464
    unsigned int post)
1465
0
{
1466
0
  struct cs_struct *handle;
1467
0
  unsigned int count = 0, i;
1468
0
  if (!ud)
1469
0
    return -1;
1470
1471
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1472
1473
0
  if (!handle->detail_opt) {
1474
0
    handle->errnum = CS_ERR_DETAIL;
1475
0
    return -1;
1476
0
  }
1477
1478
0
  if (!insn->id) {
1479
0
    handle->errnum = CS_ERR_SKIPDATA;
1480
0
    return -1;
1481
0
  }
1482
1483
0
  if (!insn->detail) {
1484
0
    handle->errnum = CS_ERR_DETAIL;
1485
0
    return -1;
1486
0
  }
1487
1488
0
  handle->errnum = CS_ERR_OK;
1489
1490
0
  switch (handle->arch) {
1491
0
    default:
1492
0
      handle->errnum = CS_ERR_HANDLE;
1493
0
      return -1;
1494
0
    case CS_ARCH_ARM:
1495
0
      for (i = 0; i < insn->detail->arm.op_count; i++) {
1496
0
        if (insn->detail->arm.operands[i].type == (arm_op_type)op_type)
1497
0
          count++;
1498
0
        if (count == post)
1499
0
          return i;
1500
0
      }
1501
0
      break;
1502
0
    case CS_ARCH_AARCH64:
1503
0
      for (i = 0; i < insn->detail->aarch64.op_count; i++) {
1504
0
        if (insn->detail->aarch64.operands[i].type == (aarch64_op_type)op_type)
1505
0
          count++;
1506
0
        if (count == post)
1507
0
          return i;
1508
0
      }
1509
0
      break;
1510
0
    case CS_ARCH_X86:
1511
0
      for (i = 0; i < insn->detail->x86.op_count; i++) {
1512
0
        if (insn->detail->x86.operands[i].type == (x86_op_type)op_type)
1513
0
          count++;
1514
0
        if (count == post)
1515
0
          return i;
1516
0
      }
1517
0
      break;
1518
0
    case CS_ARCH_MIPS:
1519
0
      for (i = 0; i < insn->detail->mips.op_count; i++) {
1520
0
        if (insn->detail->mips.operands[i].type == (mips_op_type)op_type)
1521
0
          count++;
1522
0
        if (count == post)
1523
0
          return i;
1524
0
      }
1525
0
      break;
1526
0
    case CS_ARCH_PPC:
1527
0
      for (i = 0; i < insn->detail->ppc.op_count; i++) {
1528
0
        if (insn->detail->ppc.operands[i].type == (ppc_op_type)op_type)
1529
0
          count++;
1530
0
        if (count == post)
1531
0
          return i;
1532
0
      }
1533
0
      break;
1534
0
    case CS_ARCH_SPARC:
1535
0
      for (i = 0; i < insn->detail->sparc.op_count; i++) {
1536
0
        if (insn->detail->sparc.operands[i].type == (sparc_op_type)op_type)
1537
0
          count++;
1538
0
        if (count == post)
1539
0
          return i;
1540
0
      }
1541
0
      break;
1542
0
    case CS_ARCH_SYSZ:
1543
0
      for (i = 0; i < insn->detail->sysz.op_count; i++) {
1544
0
        if (insn->detail->sysz.operands[i].type == (sysz_op_type)op_type)
1545
0
          count++;
1546
0
        if (count == post)
1547
0
          return i;
1548
0
      }
1549
0
      break;
1550
0
    case CS_ARCH_XCORE:
1551
0
      for (i = 0; i < insn->detail->xcore.op_count; i++) {
1552
0
        if (insn->detail->xcore.operands[i].type == (xcore_op_type)op_type)
1553
0
          count++;
1554
0
        if (count == post)
1555
0
          return i;
1556
0
      }
1557
0
      break;
1558
0
    case CS_ARCH_TRICORE:
1559
0
      for (i = 0; i < insn->detail->tricore.op_count; i++) {
1560
0
        if (insn->detail->tricore.operands[i].type == (tricore_op_type)op_type)
1561
0
          count++;
1562
0
        if (count == post)
1563
0
          return i;
1564
0
      }
1565
0
      break;
1566
0
    case CS_ARCH_M68K:
1567
0
      for (i = 0; i < insn->detail->m68k.op_count; i++) {
1568
0
        if (insn->detail->m68k.operands[i].type == (m68k_op_type)op_type)
1569
0
          count++;
1570
0
        if (count == post)
1571
0
          return i;
1572
0
      }
1573
0
      break;
1574
0
    case CS_ARCH_TMS320C64X:
1575
0
      for (i = 0; i < insn->detail->tms320c64x.op_count; i++) {
1576
0
        if (insn->detail->tms320c64x.operands[i].type == (tms320c64x_op_type)op_type)
1577
0
          count++;
1578
0
        if (count == post)
1579
0
          return i;
1580
0
      }
1581
0
      break;
1582
0
    case CS_ARCH_M680X:
1583
0
      for (i = 0; i < insn->detail->m680x.op_count; i++) {
1584
0
        if (insn->detail->m680x.operands[i].type == (m680x_op_type)op_type)
1585
0
          count++;
1586
0
        if (count == post)
1587
0
          return i;
1588
0
      }
1589
0
      break;
1590
0
    case CS_ARCH_EVM:
1591
#if 0
1592
      for (i = 0; i < insn->detail->evm.op_count; i++) {
1593
        if (insn->detail->evm.operands[i].type == (evm_op_type)op_type)
1594
          count++;
1595
        if (count == post)
1596
          return i;
1597
      }
1598
#endif
1599
0
      break;
1600
0
    case CS_ARCH_MOS65XX:
1601
0
      for (i = 0; i < insn->detail->mos65xx.op_count; i++) {
1602
0
        if (insn->detail->mos65xx.operands[i].type == (mos65xx_op_type)op_type)
1603
0
          count++;
1604
0
        if (count == post)
1605
0
          return i;
1606
0
      }
1607
0
      break;
1608
0
    case CS_ARCH_WASM:
1609
0
      for (i = 0; i < insn->detail->wasm.op_count; i++) {
1610
0
        if (insn->detail->wasm.operands[i].type == (wasm_op_type)op_type)
1611
0
          count++;
1612
0
        if (count == post)
1613
0
          return i;
1614
0
      }
1615
0
      break;
1616
0
    case CS_ARCH_BPF:
1617
0
      for (i = 0; i < insn->detail->bpf.op_count; i++) {
1618
0
        if (insn->detail->bpf.operands[i].type == (bpf_op_type)op_type)
1619
0
          count++;
1620
0
        if (count == post)
1621
0
          return i;
1622
0
      }
1623
0
      break;
1624
0
    case CS_ARCH_RISCV:
1625
0
      for (i = 0; i < insn->detail->riscv.op_count; i++) {
1626
0
        if (insn->detail->riscv.operands[i].type == (riscv_op_type)op_type)
1627
0
          count++;
1628
0
        if (count == post)
1629
0
          return i;
1630
0
      }
1631
0
      break;
1632
0
    case CS_ARCH_SH:
1633
0
      for (i = 0; i < insn->detail->sh.op_count; i++) {
1634
0
        if (insn->detail->sh.operands[i].type == (sh_op_type)op_type)
1635
0
          count++;
1636
0
        if (count == post)
1637
0
          return i;
1638
0
      }
1639
0
      break;
1640
0
  }
1641
1642
0
  return -1;
1643
0
}
1644
1645
CAPSTONE_EXPORT
1646
cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn,
1647
    cs_regs regs_read, uint8_t *regs_read_count,
1648
    cs_regs regs_write, uint8_t *regs_write_count)
1649
0
{
1650
0
  struct cs_struct *handle;
1651
1652
0
  if (!ud)
1653
0
    return -1;
1654
1655
0
  handle = (struct cs_struct *)(uintptr_t)ud;
1656
1657
#ifdef CAPSTONE_DIET
1658
  // This API does not work in DIET mode
1659
  handle->errnum = CS_ERR_DIET;
1660
  return CS_ERR_DIET;
1661
#else
1662
0
  if (!handle->detail_opt) {
1663
0
    handle->errnum = CS_ERR_DETAIL;
1664
0
    return CS_ERR_DETAIL;
1665
0
  }
1666
1667
0
  if (!insn->id) {
1668
0
    handle->errnum = CS_ERR_SKIPDATA;
1669
0
    return CS_ERR_SKIPDATA;
1670
0
  }
1671
1672
0
  if (!insn->detail) {
1673
0
    handle->errnum = CS_ERR_DETAIL;
1674
0
    return CS_ERR_DETAIL;
1675
0
  }
1676
1677
0
  if (handle->reg_access) {
1678
0
    handle->reg_access(insn, regs_read, regs_read_count, regs_write, regs_write_count);
1679
0
  } else {
1680
    // this arch is unsupported yet
1681
0
    handle->errnum = CS_ERR_ARCH;
1682
0
    return CS_ERR_ARCH;
1683
0
  }
1684
1685
0
  return CS_ERR_OK;
1686
0
#endif
1687
0
}