2017-11-10 20:19:08 +08:00
|
|
|
//===--- llvm-opt-fuzzer.cpp - Fuzzer for instruction selection ----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-10 20:19:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Tool to fuzz optimization passes using libFuzzer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2018-04-12 02:49:37 +08:00
|
|
|
#include "llvm/CodeGen/CommandFlags.inc"
|
2017-11-10 20:19:08 +08:00
|
|
|
#include "llvm/FuzzMutate/FuzzerCLI.h"
|
|
|
|
#include "llvm/FuzzMutate/IRMutator.h"
|
|
|
|
#include "llvm/IR/Verifier.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-11-10 20:19:08 +08:00
|
|
|
#include "llvm/Passes/PassBuilder.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
TargetTripleStr("mtriple", cl::desc("Override target triple for module"));
|
|
|
|
|
|
|
|
// Passes to run for this fuzzer instance. Expects new pass manager syntax.
|
|
|
|
static cl::opt<std::string> PassPipeline(
|
|
|
|
"passes",
|
|
|
|
cl::desc("A textual description of the pass pipeline for testing"));
|
|
|
|
|
|
|
|
static std::unique_ptr<IRMutator> Mutator;
|
|
|
|
static std::unique_ptr<TargetMachine> TM;
|
|
|
|
|
|
|
|
std::unique_ptr<IRMutator> createOptMutator() {
|
|
|
|
std::vector<TypeGetter> Types{
|
|
|
|
Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty,
|
|
|
|
Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy};
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
|
|
|
|
Strategies.push_back(
|
2019-08-15 23:54:37 +08:00
|
|
|
std::make_unique<InjectorIRStrategy>(
|
2017-11-10 20:19:08 +08:00
|
|
|
InjectorIRStrategy::getDefaultOps()));
|
|
|
|
Strategies.push_back(
|
2019-08-15 23:54:37 +08:00
|
|
|
std::make_unique<InstDeleterIRStrategy>());
|
2017-11-10 20:19:08 +08:00
|
|
|
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
|
2017-11-10 20:19:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" LLVM_ATTRIBUTE_USED size_t LLVMFuzzerCustomMutator(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize, unsigned int Seed) {
|
|
|
|
|
|
|
|
assert(Mutator &&
|
|
|
|
"IR mutator should have been created during fuzzer initialization");
|
|
|
|
|
|
|
|
LLVMContext Context;
|
2018-02-05 19:05:47 +08:00
|
|
|
auto M = parseAndVerify(Data, Size, Context);
|
|
|
|
if (!M) {
|
2017-11-10 20:19:08 +08:00
|
|
|
errs() << "error: mutator input module is broken!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mutator->mutateModule(*M, Seed, Size, MaxSize);
|
|
|
|
|
|
|
|
if (verifyModule(*M, &errs())) {
|
|
|
|
errs() << "mutation result doesn't pass verification\n";
|
2018-02-05 20:47:40 +08:00
|
|
|
#ifndef NDEBUG
|
2017-11-10 20:19:08 +08:00
|
|
|
M->dump();
|
2018-02-05 20:47:40 +08:00
|
|
|
#endif
|
2018-02-05 19:05:47 +08:00
|
|
|
// Avoid adding incorrect test cases to the corpus.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Buf;
|
|
|
|
{
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-02-15 03:11:32 +08:00
|
|
|
WriteBitcodeToFile(*M, OS);
|
2018-02-05 19:05:47 +08:00
|
|
|
}
|
|
|
|
if (Buf.size() > MaxSize)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// There are some invariants which are not checked by the verifier in favor
|
|
|
|
// of having them checked by the parser. They may be considered as bugs in the
|
|
|
|
// verifier and should be fixed there. However until all of those are covered
|
|
|
|
// we want to check for them explicitly. Otherwise we will add incorrect input
|
|
|
|
// to the corpus and this is going to confuse the fuzzer which will start
|
|
|
|
// exploration of the bitcode reader error handling code.
|
|
|
|
auto NewM = parseAndVerify(
|
|
|
|
reinterpret_cast<const uint8_t*>(Buf.data()), Buf.size(), Context);
|
|
|
|
if (!NewM) {
|
|
|
|
errs() << "mutator failed to re-read the module\n";
|
2018-02-05 20:47:40 +08:00
|
|
|
#ifndef NDEBUG
|
2018-02-05 19:05:47 +08:00
|
|
|
M->dump();
|
2018-02-05 20:47:40 +08:00
|
|
|
#endif
|
2018-02-05 19:05:47 +08:00
|
|
|
return 0;
|
2017-11-10 20:19:08 +08:00
|
|
|
}
|
|
|
|
|
2018-02-05 19:05:47 +08:00
|
|
|
memcpy(Data, Buf.data(), Buf.size());
|
|
|
|
return Buf.size();
|
2017-11-10 20:19:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
|
|
|
|
assert(TM && "Should have been created during fuzzer initialization");
|
|
|
|
|
|
|
|
if (Size <= 1)
|
|
|
|
// We get bogus data given an empty corpus - ignore it.
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Parse module
|
|
|
|
//
|
|
|
|
|
|
|
|
LLVMContext Context;
|
2018-02-05 19:05:47 +08:00
|
|
|
auto M = parseAndVerify(Data, Size, Context);
|
|
|
|
if (!M) {
|
2017-11-10 20:19:08 +08:00
|
|
|
errs() << "error: input module is broken!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up target dependant options
|
|
|
|
//
|
|
|
|
|
|
|
|
M->setTargetTriple(TM->getTargetTriple().normalize());
|
|
|
|
M->setDataLayout(TM->createDataLayout());
|
|
|
|
setFunctionAttributes(TM->getTargetCPU(), TM->getTargetFeatureString(), *M);
|
|
|
|
|
|
|
|
// Create pass pipeline
|
|
|
|
//
|
|
|
|
|
|
|
|
PassBuilder PB(TM.get());
|
|
|
|
|
|
|
|
LoopAnalysisManager LAM;
|
|
|
|
FunctionAnalysisManager FAM;
|
|
|
|
CGSCCAnalysisManager CGAM;
|
|
|
|
ModulePassManager MPM;
|
|
|
|
ModuleAnalysisManager MAM;
|
|
|
|
|
|
|
|
FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
|
|
|
|
PB.registerModuleAnalyses(MAM);
|
|
|
|
PB.registerCGSCCAnalyses(CGAM);
|
|
|
|
PB.registerFunctionAnalyses(FAM);
|
|
|
|
PB.registerLoopAnalyses(LAM);
|
|
|
|
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
|
|
|
|
|
2018-10-17 18:36:23 +08:00
|
|
|
auto Err = PB.parsePassPipeline(MPM, PassPipeline, false, false);
|
|
|
|
assert(!Err && "Should have been checked during fuzzer initialization");
|
|
|
|
// Only fail with assert above, otherwise ignore the parsing error.
|
|
|
|
consumeError(std::move(Err));
|
2017-11-10 20:19:08 +08:00
|
|
|
|
|
|
|
// Run passes which we need to test
|
|
|
|
//
|
|
|
|
|
|
|
|
MPM.run(*M, MAM);
|
|
|
|
|
|
|
|
// Check that passes resulted in a correct code
|
|
|
|
if (verifyModule(*M, &errs())) {
|
|
|
|
errs() << "Transformation resulted in an invalid module\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleLLVMFatalError(void *, const std::string &Message, bool) {
|
|
|
|
// TODO: Would it be better to call into the fuzzer internals directly?
|
|
|
|
dbgs() << "LLVM ERROR: " << Message << "\n"
|
|
|
|
<< "Aborting to trigger fuzzer exit handling.\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(
|
|
|
|
int *argc, char ***argv) {
|
|
|
|
EnableDebugBuffering = true;
|
|
|
|
|
|
|
|
// Make sure we print the summary and the current unit when LLVM errors out.
|
|
|
|
install_fatal_error_handler(handleLLVMFatalError, nullptr);
|
|
|
|
|
|
|
|
// Initialize llvm
|
|
|
|
//
|
|
|
|
|
|
|
|
InitializeAllTargets();
|
|
|
|
InitializeAllTargetMCs();
|
|
|
|
|
|
|
|
PassRegistry &Registry = *PassRegistry::getPassRegistry();
|
|
|
|
initializeCore(Registry);
|
|
|
|
initializeCoroutines(Registry);
|
|
|
|
initializeScalarOpts(Registry);
|
|
|
|
initializeObjCARCOpts(Registry);
|
|
|
|
initializeVectorization(Registry);
|
|
|
|
initializeIPO(Registry);
|
|
|
|
initializeAnalysis(Registry);
|
|
|
|
initializeTransformUtils(Registry);
|
|
|
|
initializeInstCombine(Registry);
|
2018-04-24 08:05:21 +08:00
|
|
|
initializeAggressiveInstCombine(Registry);
|
2017-11-10 20:19:08 +08:00
|
|
|
initializeInstrumentation(Registry);
|
|
|
|
initializeTarget(Registry);
|
|
|
|
|
|
|
|
// Parse input options
|
|
|
|
//
|
|
|
|
|
|
|
|
handleExecNameEncodedOptimizerOpts(*argv[0]);
|
|
|
|
parseFuzzerCLOpts(*argc, *argv);
|
|
|
|
|
|
|
|
// Create TargetMachine
|
|
|
|
//
|
|
|
|
|
|
|
|
if (TargetTripleStr.empty()) {
|
|
|
|
errs() << *argv[0] << ": -mtriple must be specified\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
Triple TargetTriple = Triple(Triple::normalize(TargetTripleStr));
|
|
|
|
|
|
|
|
std::string Error;
|
|
|
|
const Target *TheTarget =
|
|
|
|
TargetRegistry::lookupTarget(MArch, TargetTriple, Error);
|
|
|
|
if (!TheTarget) {
|
|
|
|
errs() << *argv[0] << ": " << Error;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
|
|
|
|
TM.reset(TheTarget->createTargetMachine(
|
|
|
|
TargetTriple.getTriple(), getCPUStr(), getFeaturesStr(),
|
|
|
|
Options, getRelocModel(), getCodeModel(), CodeGenOpt::Default));
|
|
|
|
assert(TM && "Could not allocate target machine!");
|
|
|
|
|
|
|
|
// Check that pass pipeline is specified and correct
|
|
|
|
//
|
|
|
|
|
|
|
|
if (PassPipeline.empty()) {
|
|
|
|
errs() << *argv[0] << ": at least one pass should be specified\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
PassBuilder PB(TM.get());
|
|
|
|
ModulePassManager MPM;
|
2018-10-17 18:36:23 +08:00
|
|
|
if (auto Err = PB.parsePassPipeline(MPM, PassPipeline, false, false)) {
|
|
|
|
errs() << *argv[0] << ": " << toString(std::move(Err)) << "\n";
|
2017-11-10 20:19:08 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create mutator
|
|
|
|
//
|
|
|
|
|
|
|
|
Mutator = createOptMutator();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|