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
LLVMFuzzerInit()9 extern "C" void LLVMFuzzerInit() {
10 LLVMInitializeAllTargetInfos();
11 LLVMInitializeAllTargetMCs();
12 LLVMInitializeAllDisassemblers();
13 }
14
15
LLVMFuzzerReturnOneInput(const uint8_t * Data,size_t Size,char * AssemblyText)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 }
42