Rizin
unix-like reverse engineering framework and cli tools
fuzz_llvm.cpp
Go to the documentation of this file.
1 #include "llvm-c/Disassembler.h"
2 #include "llvm-c/Target.h"
3 #include "llvm/MC/SubtargetFeature.h"
4 #include "llvm/Support/CommandLine.h"
5 #include "llvm/Support/raw_ostream.h"
6 
7 using namespace llvm;
8 
9 extern "C" void LLVMFuzzerInit() {
10  LLVMInitializeAllTargetInfos();
11  LLVMInitializeAllTargetMCs();
12  LLVMInitializeAllDisassemblers();
13 }
14 
15 
16 extern "C" int LLVMFuzzerReturnOneInput(const uint8_t *Data, size_t Size, char * AssemblyText) {
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 LLVMFuzzerInit()
Definition: fuzz_llvm.cpp:9
int LLVMFuzzerReturnOneInput(const uint8_t *Data, size_t Size, char *AssemblyText)
Definition: fuzz_llvm.cpp:16
void * p
Definition: libc.cpp:67
assert(limit<=UINT32_MAX/2)
unsigned char uint8_t
Definition: sftypes.h:31