Rizin
unix-like reverse engineering framework and cli tools
fuzz_disasm.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <capstone/capstone.h>

Go to the source code of this file.

Classes

struct  platform
 

Functions

const char * cs_fuzz_arch (uint8_t arch)
 
int LLVMFuzzerTestOneInput (const uint8_t *Data, size_t Size)
 

Variables

static FILE * outfile = NULL
 
struct platform platforms []
 

Function Documentation

◆ cs_fuzz_arch()

const char* cs_fuzz_arch ( uint8_t  arch)

Definition at line 217 of file fuzz_disasm.c.

217  {
218  return platforms[arch % sizeof(platforms)/sizeof(platforms[0])].cstoolname;
219 }
cs_arch arch
Definition: cstool.c:13
struct platform platforms[]
Definition: fuzz_disasm.c:25
const char * cstoolname
Definition: fuzz_disasm.c:20

References arch, platform::cstoolname, and platforms.

Referenced by main().

◆ LLVMFuzzerTestOneInput()

int LLVMFuzzerTestOneInput ( const uint8_t Data,
size_t  Size 
)

Definition at line 221 of file fuzz_disasm.c.

221  {
222  csh handle;
223  cs_insn *all_insn;
224  cs_detail *detail;
225  cs_err err;
226 
227  if (Size < 1) {
228  // 1 byte for arch choice
229  return 0;
230  } else if (Size > 0x1000) {
231  //limit input to 4kb
232  Size = 0x1000;
233  }
234  if (outfile == NULL) {
235  // we compute the output
236  outfile = fopen("/dev/null", "w");
237  if (outfile == NULL) {
238  return 0;
239  }
240  }
241 
242  int platforms_len = sizeof(platforms)/sizeof(platforms[0]);
243  int i = (int)Data[0] % platforms_len;
244 
246  if (err) {
247  return 0;
248  }
250 
251  uint64_t address = 0x1000;
252  size_t count = cs_disasm(handle, Data+1, Size-1, address, 0, &all_insn);
253 
254  if (count) {
255  size_t j;
256  int n;
257 
258  for (j = 0; j < count; j++) {
259  cs_insn *i = &(all_insn[j]);
260  fprintf(outfile, "0x%"PRIx64":\t%s\t\t%s // insn-ID: %u, insn-mnem: %s\n",
261  i->address, i->mnemonic, i->op_str,
262  i->id, cs_insn_name(handle, i->id));
263 
264  detail = i->detail;
265 
266  if (detail->regs_read_count > 0) {
267  fprintf(outfile, "\tImplicit registers read: ");
268  for (n = 0; n < detail->regs_read_count; n++) {
269  fprintf(outfile, "%s ", cs_reg_name(handle, detail->regs_read[n]));
270  }
271  }
272 
273  if (detail->regs_write_count > 0) {
274  fprintf(outfile, "\tImplicit registers modified: ");
275  for (n = 0; n < detail->regs_write_count; n++) {
276  fprintf(outfile, "%s ", cs_reg_name(handle, detail->regs_write[n]));
277  }
278  }
279 
280  if (detail->groups_count > 0) {
281  fprintf(outfile, "\tThis instruction belongs to groups: ");
282  for (n = 0; n < detail->groups_count; n++) {
283  fprintf(outfile, "%s ", cs_group_name(handle, detail->groups[n]));
284  }
285  }
286  }
287  fprintf(outfile, "0x%"PRIx64":\n", all_insn[j-1].address + all_insn[j-1].size);
288  cs_free(all_insn, count);
289  }
290 
291  cs_close(&handle);
292 
293  return 0;
294 }
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
static mcore_handle handle
Definition: asm_mcore.c:8
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:798
CAPSTONE_EXPORT const char *CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Definition: cs.c:1178
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT const char *CAPSTONE_API cs_insn_name(csh ud, unsigned int insn)
Definition: cs.c:1166
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:646
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
static FILE * outfile
Definition: fuzz_disasm.c:23
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
int n
Definition: mipsasm.c:19
static int
Definition: sfsocketcall.h:114
unsigned long uint64_t
Definition: sftypes.h:28
#define PRIx64
Definition: sysdefs.h:94

Variable Documentation

◆ outfile

FILE* outfile = NULL
static

Definition at line 23 of file fuzz_disasm.c.

Referenced by LLVMFuzzerTestOneInput().

◆ platforms

struct platform platforms[]

Definition at line 23 of file fuzz_disasm.c.

Referenced by cs_fuzz_arch(), and LLVMFuzzerTestOneInput().