Coverage Report

Created: 2026-08-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/suite/fuzz/fuzz_disasm.c
Line
Count
Source
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
108k
{
20
108k
  csh handle;
21
108k
  cs_insn *all_insn;
22
108k
  cs_detail *detail;
23
108k
  cs_err err;
24
108k
  unsigned int i;
25
26
108k
  if (Size < 1) {
27
    // 1 byte for arch choice
28
0
    return 0;
29
108k
  } else if (Size > 0x1000) {
30
    //limit input to 4kb
31
0
    Size = 0x1000;
32
0
  }
33
34
108k
  if (outfile == NULL) {
35
    // we compute the output
36
2
    outfile = fopen("/dev/null", "w");
37
2
    if (outfile == NULL) {
38
0
      return 0;
39
0
    }
40
2
  }
41
42
108k
  i = get_platform_entry((uint8_t)Data[0]);
43
44
108k
  err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
45
108k
  if (err) {
46
8
    return 0;
47
8
  }
48
49
108k
  cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
50
108k
  if (Data[0] & 0x80) {
51
    //hack
52
26.1k
    cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
53
26.1k
  }
54
55
108k
  uint64_t address = 0x1000;
56
108k
  size_t count =
57
108k
    cs_disasm(handle, Data + 1, Size - 1, address, 0, &all_insn);
58
59
108k
  if (count) {
60
106k
    size_t j;
61
106k
    unsigned int n;
62
63
8.03M
    for (j = 0; j < count; j++) {
64
7.92M
      cs_insn *insn = &(all_insn[j]);
65
7.92M
      fprintf(outfile,
66
7.92M
        "0x%" PRIx64
67
7.92M
        ":\t%s\t\t%s // insn-ID: %u, insn-mnem: %s\n",
68
7.92M
        insn->address, insn->mnemonic, insn->op_str,
69
7.92M
        insn->id, cs_insn_name(handle, insn->id));
70
71
7.92M
      detail = insn->detail;
72
73
7.92M
      if (detail->regs_read_count > 0) {
74
1.87M
        fprintf(outfile, "\tImplicit registers read: ");
75
4.81M
        for (n = 0; n < detail->regs_read_count; n++) {
76
2.93M
          fprintf(outfile, "%s ",
77
2.93M
            cs_reg_name(
78
2.93M
              handle,
79
2.93M
              detail->regs_read[n]));
80
2.93M
        }
81
1.87M
      }
82
83
7.92M
      if (detail->regs_write_count > 0) {
84
3.43M
        fprintf(outfile,
85
3.43M
          "\tImplicit registers modified: ");
86
7.58M
        for (n = 0; n < detail->regs_write_count; n++) {
87
4.14M
          fprintf(outfile, "%s ",
88
4.14M
            cs_reg_name(
89
4.14M
              handle,
90
4.14M
              detail->regs_write[n]));
91
4.14M
        }
92
3.43M
      }
93
94
7.92M
      if (detail->groups_count > 0) {
95
4.52M
        fprintf(outfile,
96
4.52M
          "\tThis instruction belongs to groups: ");
97
11.1M
        for (n = 0; n < detail->groups_count; n++) {
98
6.60M
          fprintf(outfile, "%s ",
99
6.60M
            cs_group_name(
100
6.60M
              handle,
101
6.60M
              detail->groups[n]));
102
6.60M
        }
103
4.52M
      }
104
7.92M
    }
105
106
106k
    fprintf(outfile, "0x%" PRIx64 ":\n",
107
106k
      all_insn[j - 1].address + all_insn[j - 1].size);
108
106k
    cs_free(all_insn, count);
109
106k
  }
110
111
108k
  cs_close(&handle);
112
113
108k
  return 0;
114
108k
}