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

Go to the source code of this file.

Classes

struct  platform
 

Functions

void LLVMFuzzerInit ()
 
int LLVMFuzzerReturnOneInput (const uint8_t *Data, size_t Size, char *AssemblyText)
 
int LLVMFuzzerTestOneInput (const uint8_t *Data, size_t Size)
 

Variables

FILE * outfile = NULL
 
struct platform platforms []
 

Function Documentation

◆ LLVMFuzzerInit()

void LLVMFuzzerInit ( )

Definition at line 9 of file fuzz_llvm.cpp.

9  {
10  LLVMInitializeAllTargetInfos();
11  LLVMInitializeAllTargetMCs();
12  LLVMInitializeAllDisassemblers();
13 }

Referenced by LLVMFuzzerTestOneInput().

◆ LLVMFuzzerReturnOneInput()

int LLVMFuzzerReturnOneInput ( const uint8_t Data,
size_t  Size,
char *  AssemblyText 
)

Definition at line 16 of file fuzz_llvm.cpp.

16  {
17  LLVMDisasmContextRef Ctx;
18  std::vector<uint8_t> DataCopy(Data, Data + Size);
19  uint8_t *p = DataCopy.data();
20  int r = 1;
21 
22  switch(Data[0]) {
23  case 0:
24  Ctx = LLVMCreateDisasmCPUFeatures("i386", "", "", nullptr, 0, nullptr, nullptr);
25  if (LLVMSetDisasmOptions(Ctx, LLVMDisassembler_Option_AsmPrinterVariant) == 0) {
26  abort();
27  }
28  break;
29  //TODO other cases
30  default:
31  return 1;
32  }
33  assert(Ctx);
34 
35  if (LLVMDisasmInstruction(Ctx, p+1, Size-1, 0, AssemblyText, 80) > 0) {
36  r = 0;
37  }
38  LLVMDisasmDispose(Ctx);
39 
40  return r;
41 }
#define r
Definition: crypto_rc6.c:12
void * p
Definition: libc.cpp:67
assert(limit<=UINT32_MAX/2)
unsigned char uint8_t
Definition: sftypes.h:31

References assert(), p, and r.

Referenced by LLVMFuzzerTestOneInput().

◆ LLVMFuzzerTestOneInput()

int LLVMFuzzerTestOneInput ( const uint8_t Data,
size_t  Size 
)

Definition at line 180 of file fuzz_diff.c.

180  {
181  csh handle;
182  cs_insn *insn;
183  cs_err err;
184  const uint8_t **Datap = &Data;
185  size_t * Sizep = &Size;
186  uint64_t address = 0x1000;
187  char LLVMAssemblyText[80];
188  char CapstoneAssemblyText[80];
189 
190  if (Size < 1) {
191  // 1 byte for arch choice
192  return 0;
193  } else if (Size > 0x1000) {
194  //limit input to 4kb
195  Size = 0x1000;
196  }
197  if (outfile == NULL) {
198  // we compute the output
199  outfile = fopen("/dev/null", "w");
200  if (outfile == NULL) {
201  return 0;
202  }
203  LLVMFuzzerInit();
204  }
205 
206  if (Data[0] >= sizeof(platforms)/sizeof(platforms[0])) {
207  return 0;
208  }
209 
210  if (LLVMFuzzerReturnOneInput(Data, Size, LLVMAssemblyText) == 1) {
211  return 0;
212  }
213 
214  err = cs_open(platforms[Data[0]].arch, platforms[Data[0]].mode, &handle);
215  if (err) {
216  return 0;
217  }
218 
219  insn = cs_malloc(handle);
220  Data++;
221  Size--;
222  assert(insn);
223  if (cs_disasm_iter(handle, Datap, Sizep, &address, insn)) {
224  snprintf(CapstoneAssemblyText, 80, "\t%s\t%s", insn->mnemonic, insn->op_str);
225  if (strcmp(CapstoneAssemblyText, LLVMAssemblyText) != 0) {
226  printf("capstone %s != llvm %s", CapstoneAssemblyText, LLVMAssemblyText);
227  abort();
228  }
229  } else {
230  printf("capstone failed with llvm %s", LLVMAssemblyText);
231  abort();
232  }
233  cs_free(insn, 1);
234  cs_close(&handle);
235 
236  return 0;
237 }
static bool err
Definition: armass.c:435
static mcore_handle handle
Definition: asm_mcore.c:8
size_t csh
Definition: capstone.h:71
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_insn *CAPSTONE_API cs_malloc(csh ud)
Definition: cs.c:1030
CAPSTONE_EXPORT bool CAPSTONE_API cs_disasm_iter(csh ud, const uint8_t **code, size_t *size, uint64_t *address, cs_insn *insn)
Definition: cs.c:1058
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
cs_arch arch
Definition: cstool.c:13
void LLVMFuzzerInit()
Definition: fuzz_llvm.cpp:9
int LLVMFuzzerReturnOneInput(const uint8_t *Data, size_t Size, char *AssemblyText)
Definition: fuzz_llvm.cpp:16
struct platform platforms[]
Definition: fuzz_diff.c:18
FILE * outfile
Definition: fuzz_diff.c:16
const char int mode
Definition: ioapi.h:137
snprintf
Definition: kernel.h:364
unsigned long uint64_t
Definition: sftypes.h:28

Variable Documentation

◆ outfile

FILE* outfile = NULL

◆ platforms

struct platform platforms[]