Coverage Report

Created: 2025-10-10 06:20

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
26.4k
{
20
26.4k
  csh handle;
21
26.4k
  cs_insn *all_insn;
22
26.4k
  cs_detail *detail;
23
26.4k
  cs_err err;
24
26.4k
  unsigned int i;
25
26
26.4k
  if (Size < 1) {
27
    // 1 byte for arch choice
28
0
    return 0;
29
26.4k
  } else if (Size > 0x1000) {
30
    //limit input to 4kb
31
0
    Size = 0x1000;
32
0
  }
33
34
26.4k
  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
26.4k
  i = get_platform_entry((uint8_t)Data[0]);
43
44
26.4k
  err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
45
26.4k
  if (err) {
46
6
    return 0;
47
6
  }
48
49
26.4k
  cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
50
26.4k
  if (Data[0] & 0x80) {
51
    //hack
52
6.49k
    cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
53
6.49k
  }
54
55
26.4k
  uint64_t address = 0x1000;
56
26.4k
  size_t count =
57
26.4k
    cs_disasm(handle, Data + 1, Size - 1, address, 0, &all_insn);
58
59
26.4k
  if (count) {
60
25.7k
    size_t j;
61
25.7k
    unsigned int n;
62
63
1.15M
    for (j = 0; j < count; j++) {
64
1.12M
      cs_insn *insn = &(all_insn[j]);
65
1.12M
      fprintf(outfile,
66
1.12M
        "0x%" PRIx64
67
1.12M
        ":\t%s\t\t%s // insn-ID: %u, insn-mnem: %s\n",
68
1.12M
        insn->address, insn->mnemonic, insn->op_str,
69
1.12M
        insn->id, cs_insn_name(handle, insn->id));
70
71
1.12M
      detail = insn->detail;
72
73
1.12M
      if (detail->regs_read_count > 0) {
74
250k
        fprintf(outfile, "\tImplicit registers read: ");
75
630k
        for (n = 0; n < detail->regs_read_count; n++) {
76
379k
          fprintf(outfile, "%s ",
77
379k
            cs_reg_name(
78
379k
              handle,
79
379k
              detail->regs_read[n]));
80
379k
        }
81
250k
      }
82
83
1.12M
      if (detail->regs_write_count > 0) {
84
486k
        fprintf(outfile,
85
486k
          "\tImplicit registers modified: ");
86
1.05M
        for (n = 0; n < detail->regs_write_count; n++) {
87
572k
          fprintf(outfile, "%s ",
88
572k
            cs_reg_name(
89
572k
              handle,
90
572k
              detail->regs_write[n]));
91
572k
        }
92
486k
      }
93
94
1.12M
      if (detail->groups_count > 0) {
95
726k
        fprintf(outfile,
96
726k
          "\tThis instruction belongs to groups: ");
97
1.70M
        for (n = 0; n < detail->groups_count; n++) {
98
979k
          fprintf(outfile, "%s ",
99
979k
            cs_group_name(
100
979k
              handle,
101
979k
              detail->groups[n]));
102
979k
        }
103
726k
      }
104
1.12M
    }
105
106
25.7k
    fprintf(outfile, "0x%" PRIx64 ":\n",
107
25.7k
      all_insn[j - 1].address + all_insn[j - 1].size);
108
25.7k
    cs_free(all_insn, count);
109
25.7k
  }
110
111
26.4k
  cs_close(&handle);
112
113
26.4k
  return 0;
114
26.4k
}