Index: tools/llvm-mc-fuzzer/CMakeLists.txt =================================================================== --- tools/llvm-mc-fuzzer/CMakeLists.txt +++ tools/llvm-mc-fuzzer/CMakeLists.txt @@ -3,11 +3,14 @@ ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/Fuzzer) set(LLVM_LINK_COMPONENTS + AllTargetsAsmPrinters + AllTargetsAsmParsers AllTargetsDescs AllTargetsDisassemblers AllTargetsInfos MC MCDisassembler + MCParser Support ) add_llvm_tool(llvm-mc-fuzzer Index: tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp =================================================================== --- tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp +++ tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp @@ -13,8 +13,26 @@ #include "llvm-c/Disassembler.h" #include "llvm-c/Target.h" #include "llvm/MC/SubtargetFeature.h" +#include "llvm/MC/MCAsmBackend.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCObjectFileInfo.h" +#include "llvm/MC/MCParser/AsmLexer.h" +#include "llvm/MC/MCParser/MCTargetAsmParser.h" +#include "llvm/MC/MCRegisterInfo.h" +#include "llvm/MC/MCSectionMachO.h" +#include "llvm/MC/MCStreamer.h" +#include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/MC/MCTargetOptionsCommandFlags.h" +#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileUtilities.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/TargetRegistry.h" using namespace llvm; @@ -61,6 +79,140 @@ cl::PositionalEatsArgs); static std::vector ModifiedArgv; +class LLVMFuzzerInputBuffer : public MemoryBuffer +{ + public: + LLVMFuzzerInputBuffer(const uint8_t *data_, size_t size_) + : data(reinterpret_cast(data_)), size(size_) { + init(data, data+size, false); + } + + + virtual BufferKind getBufferKind() const { + return MemoryBuffer_Malloc /* it's not disk-backed so I think that's + ** the intent ... though AFAIK it + ** probably came from an mmap or sbrk + */; + } + + private: + const char *data; + size_t size; +}; + +static int AssembleInput(const char *ProgName, const Target *TheTarget, + SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str, + MCAsmInfo &MAI, MCSubtargetInfo &STI, + MCInstrInfo &MCII, MCTargetOptions &MCOptions) { + static const bool NoInitialTextSection = false; + + std::unique_ptr Parser( + createMCAsmParser(SrcMgr, Ctx, Str, MAI)); + + std::unique_ptr TAP( + TheTarget->createMCAsmParser(STI, *Parser, MCII, MCOptions)); + + if (!TAP) { + errs() << ProgName + << ": error: this target '" << TripleName + << "', does not support assembly parsing.\n"; + abort(); + + return 1; + } + + Parser->setTargetParser(*TAP); + + int Res = Parser->Run(NoInitialTextSection); + + return Res; +} + + +int AssembleOneInput(const uint8_t *Data, size_t Size) { + static const bool ShowInst = false; + static const bool asmverbose = false; + static const bool useDwarfDirectory = true; + + if (TripleName.empty()) + TripleName = sys::getDefaultTargetTriple(); + static Triple TheTriple(Triple::normalize(TripleName)); + + SourceMgr SrcMgr; + + std::unique_ptr BufferPtr(new LLVMFuzzerInputBuffer(Data, Size)); + + // Tell SrcMgr about this buffer, which is what the parser will pick up. + SrcMgr.AddNewSourceBuffer(std::move(BufferPtr), SMLoc()); + + static const std::vector NoIncludeDirs; + SrcMgr.setIncludeDirs(NoIncludeDirs); + + static std::string ArchName; + std::string Error; + const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, + Error); + if (!TheTarget) { + errs() << "error: this target '" << TheTriple.normalize() + << "/" << ArchName << "', was not found: '" << Error << "'\n"; + + abort(); + } + + std::unique_ptr MRI(TheTarget->createMCRegInfo(TripleName)); + assert(MRI && "Unable to create target register info!"); + + std::unique_ptr MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); + assert(MAI && "Unable to create target asm info!"); + + + MCObjectFileInfo MOFI; + MCContext Ctx(MAI.get(), MRI.get(), &MOFI, &SrcMgr); + + static const bool use_pic = false; + static const CodeModel::Model CMModel = CodeModel::Default; + MOFI.InitMCObjectFileInfo(TheTriple, use_pic, CMModel, Ctx); + + const unsigned OutputAsmVariant = 0; + std::unique_ptr MCII(TheTarget->createMCInstrInfo()); + MCInstPrinter *IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant, + *MAI, *MCII, *MRI); + if (!IP) { + errs() + << "error: unable to create instruction printer for target triple '" + << TheTriple.normalize() << "' with assembly variant " + << OutputAsmVariant << ".\n"; + + abort(); + } + + const char *ProgName = "llvm-mc-fuzzer"; + std::unique_ptr STI( + TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr)); + MCCodeEmitter *CE = nullptr; + MCAsmBackend *MAB = nullptr; + + MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); + + std::string output_string; + raw_string_ostream out(output_string); + auto FOut = llvm::make_unique(out); + + std::unique_ptr Str; + + // TODO: TheTarget->createNullStreamer(Ctx) is an option but + // not yet supported by all of the backends + Str.reset(TheTarget->createAsmStreamer( + Ctx, std::move(FOut), asmverbose, + useDwarfDirectory, IP, CE, MAB, ShowInst)); + const int Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, + *MCII, MCOptions); + + (void) Res; + + return 0; +} + int DisassembleOneInput(const uint8_t *Data, size_t Size) { char AssemblyText[AssemblyTextBufSize]; @@ -89,7 +241,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { if (Action == AC_Assemble) - errs() << "error: -assemble is not implemented\n"; + return AssembleOneInput(Data, Size); else if (Action == AC_Disassemble) return DisassembleOneInput(Data, Size); @@ -126,6 +278,7 @@ LLVMInitializeAllTargetInfos(); LLVMInitializeAllTargetMCs(); LLVMInitializeAllDisassemblers(); + LLVMInitializeAllAsmParsers(); cl::ParseCommandLineOptions(*argc, OriginalArgv);