Coverage Report

Created: 2025-08-28 06:43

/src/capstonenext/suite/fuzz/fuzz_disasm.c
Line
Count
Source (jump to first uncovered line)
1
// the following must precede stdio (woo, thanks msft)
2
#if defined(_MSC_VER) && _MSC_VER < 1900
3
#define _CRT_SECURE_NO_WARNINGS
4
#endif
5
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <inttypes.h>
9
10
#include <capstone/capstone.h>
11
12
#include "platform.h"
13
14
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
15
16
static FILE *outfile = NULL;
17
18
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
19
62.5k
{
20
62.5k
  csh handle;
21
62.5k
  cs_insn *all_insn;
22
62.5k
  cs_detail *detail;
23
62.5k
  cs_err err;
24
62.5k
  unsigned int i;
25
26
62.5k
  if (Size < 1) {
27
    // 1 byte for arch choice
28
0
    return 0;
29
62.5k
  } else if (Size > 0x1000) {
30
    //limit input to 4kb
31
0
    Size = 0x1000;
32
0
  }
33
34
62.5k
  if (outfile == NULL) {
35
    // we compute the output
36
1
    outfile = fopen("/dev/null", "w");
37
1
    if (outfile == NULL) {
38
0
      return 0;
39
0
    }
40
1
  }
41
42
62.5k
  i = get_platform_entry((uint8_t)Data[0]);
43
44
62.5k
  err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
45
62.5k
  if (err) {
46
7
    return 0;
47
7
  }
48
49
62.5k
  cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
50
62.5k
  if (Data[0] & 0x80) {
51
    //hack
52
16.7k
    cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
53
16.7k
  }
54
55
62.5k
  uint64_t address = 0x1000;
56
62.5k
  size_t count =
57
62.5k
    cs_disasm(handle, Data + 1, Size - 1, address, 0, &all_insn);
58
59
62.5k
  if (count) {
60
60.9k
    size_t j;
61
60.9k
    unsigned int n;
62
63
3.20M
    for (j = 0; j < count; j++) {
64
3.14M
      cs_insn *insn = &(all_insn[j]);
65
3.14M
      fprintf(outfile,
66
3.14M
        "0x%" PRIx64
67
3.14M
        ":\t%s\t\t%s // insn-ID: %u, insn-mnem: %s\n",
68
3.14M
        insn->address, insn->mnemonic, insn->op_str,
69
3.14M
        insn->id, cs_insn_name(handle, insn->id));
70
71
3.14M
      detail = insn->detail;
72
73
3.14M
      if (detail->regs_read_count > 0) {
74
743k
        fprintf(outfile, "\tImplicit registers read: ");
75
1.85M
        for (n = 0; n < detail->regs_read_count; n++) {
76
1.11M
          fprintf(outfile, "%s ",
77
1.11M
            cs_reg_name(
78
1.11M
              handle,
79
1.11M
              detail->regs_read[n]));
80
1.11M
        }
81
743k
      }
82
83
3.14M
      if (detail->regs_write_count > 0) {
84
1.43M
        fprintf(outfile,
85
1.43M
          "\tImplicit registers modified: ");
86
3.10M
        for (n = 0; n < detail->regs_write_count; n++) {
87
1.67M
          fprintf(outfile, "%s ",
88
1.67M
            cs_reg_name(
89
1.67M
              handle,
90
1.67M
              detail->regs_write[n]));
91
1.67M
        }
92
1.43M
      }
93
94
3.14M
      if (detail->groups_count > 0) {
95
1.99M
        fprintf(outfile,
96
1.99M
          "\tThis instruction belongs to groups: ");
97
4.69M
        for (n = 0; n < detail->groups_count; n++) {
98
2.70M
          fprintf(outfile, "%s ",
99
2.70M
            cs_group_name(
100
2.70M
              handle,
101
2.70M
              detail->groups[n]));
102
2.70M
        }
103
1.99M
      }
104
3.14M
    }
105
106
60.9k
    fprintf(outfile, "0x%" PRIx64 ":\n",
107
60.9k
      all_insn[j - 1].address + all_insn[j - 1].size);
108
60.9k
    cs_free(all_insn, count);
109
60.9k
  }
110
111
62.5k
  cs_close(&handle);
112
113
62.5k
  return 0;
114
62.5k
}