2017-11-10 12:19:08 +00:00
|
|
|
//===--- llvm-opt-fuzzer.cpp - Fuzzer for instruction selection ----------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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 12:19:08 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Tool to fuzz optimization passes using libFuzzer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Bitcode/BitcodeReader.h"
|
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2020-03-04 00:47:43 +01:00
|
|
|
#include "llvm/CodeGen/CommandFlags.h"
|
2017-11-10 12:19:08 +00:00
|
|
|
#include "llvm/FuzzMutate/FuzzerCLI.h"
|
|
|
|
#include "llvm/FuzzMutate/IRMutator.h"
|
|
|
|
#include "llvm/IR/Verifier.h"
|
2021-10-08 10:48:15 -07:00
|
|
|
#include "llvm/MC/TargetRegistry.h"
|
2017-11-10 12:19:08 +00:00
|
|
|
#include "llvm/Passes/PassBuilder.h"
|
2020-12-09 15:06:50 +03:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2017-11-10 12:19:08 +00:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2020-05-19 17:12:32 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2017-11-10 12:19:08 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2020-03-04 00:47:43 +01:00
|
|
|
static codegen::RegisterCodeGenFlags CGF;
|
|
|
|
|
2017-11-10 12:19:08 +00:00
|
|
|
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;
|
[FuzzMutate] Module size heuristics
IRMutation::mutateModule() currently requires the bitcode size of the module.
To compute the bitcode size, one way is to write the module to a buffer using
BitcodeWriter and calculating the buffer size. This would be fine for a single
mutation, but infeasible for repeated mutations due to the large overhead. It
turns out that the only IR strategy weight calculation method that depends on
the current module size is InstDeleterStrategy, which deletes instructions more
frequently as the module size approaches a given max size. However, there is no
real need for the size to be in bytes of bitcode, so we can use a different
metric. One alternative is to let the size be the number of objects in the
Module, including instructions, basic blocks, globals, and aliases. Although
getting the number of instructions is still O(n), it should have significantly
less overhead than BitcodeWriter. This suggestion would cause a change to the
IRMutator API, since IRMutator::mutateModule() can calculate the Module size
itself.
Reviewed By: Peter
Differential Revision: https://reviews.llvm.org/D149989
2023-05-05 15:43:58 -07:00
|
|
|
Strategies.push_back(std::make_unique<InjectorIRStrategy>(
|
|
|
|
InjectorIRStrategy::getDefaultOps()));
|
|
|
|
Strategies.push_back(std::make_unique<InstDeleterIRStrategy>());
|
2021-01-23 15:28:57 +00:00
|
|
|
Strategies.push_back(std::make_unique<InstModificationIRStrategy>());
|
2017-11-10 12:19:08 +00:00
|
|
|
|
2019-08-15 15:54:37 +00:00
|
|
|
return std::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
|
2017-11-10 12:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" LLVM_ATTRIBUTE_USED size_t LLVMFuzzerCustomMutator(
|
|
|
|
uint8_t *Data, size_t Size, size_t MaxSize, unsigned int Seed) {
|
|
|
|
|
|
|
|
assert(Mutator &&
|
[FuzzMutate] Module size heuristics
IRMutation::mutateModule() currently requires the bitcode size of the module.
To compute the bitcode size, one way is to write the module to a buffer using
BitcodeWriter and calculating the buffer size. This would be fine for a single
mutation, but infeasible for repeated mutations due to the large overhead. It
turns out that the only IR strategy weight calculation method that depends on
the current module size is InstDeleterStrategy, which deletes instructions more
frequently as the module size approaches a given max size. However, there is no
real need for the size to be in bytes of bitcode, so we can use a different
metric. One alternative is to let the size be the number of objects in the
Module, including instructions, basic blocks, globals, and aliases. Although
getting the number of instructions is still O(n), it should have significantly
less overhead than BitcodeWriter. This suggestion would cause a change to the
IRMutator API, since IRMutator::mutateModule() can calculate the Module size
itself.
Reviewed By: Peter
Differential Revision: https://reviews.llvm.org/D149989
2023-05-05 15:43:58 -07:00
|
|
|
"IR mutator should have been created during fuzzer initialization");
|
2017-11-10 12:19:08 +00:00
|
|
|
|
|
|
|
LLVMContext Context;
|
2018-02-05 11:05:47 +00:00
|
|
|
auto M = parseAndVerify(Data, Size, Context);
|
|
|
|
if (!M) {
|
2017-11-10 12:19:08 +00:00
|
|
|
errs() << "error: mutator input module is broken!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
[FuzzMutate] Module size heuristics
IRMutation::mutateModule() currently requires the bitcode size of the module.
To compute the bitcode size, one way is to write the module to a buffer using
BitcodeWriter and calculating the buffer size. This would be fine for a single
mutation, but infeasible for repeated mutations due to the large overhead. It
turns out that the only IR strategy weight calculation method that depends on
the current module size is InstDeleterStrategy, which deletes instructions more
frequently as the module size approaches a given max size. However, there is no
real need for the size to be in bytes of bitcode, so we can use a different
metric. One alternative is to let the size be the number of objects in the
Module, including instructions, basic blocks, globals, and aliases. Although
getting the number of instructions is still O(n), it should have significantly
less overhead than BitcodeWriter. This suggestion would cause a change to the
IRMutator API, since IRMutator::mutateModule() can calculate the Module size
itself.
Reviewed By: Peter
Differential Revision: https://reviews.llvm.org/D149989
2023-05-05 15:43:58 -07:00
|
|
|
Mutator->mutateModule(*M, Seed, MaxSize);
|
2017-11-10 12:19:08 +00:00
|
|
|
|
|
|
|
if (verifyModule(*M, &errs())) {
|
|
|
|
errs() << "mutation result doesn't pass verification\n";
|
2018-02-05 12:47:40 +00:00
|
|
|
#ifndef NDEBUG
|
2017-11-10 12:19:08 +00:00
|
|
|
M->dump();
|
2018-02-05 12:47:40 +00:00
|
|
|
#endif
|
2018-02-05 11:05:47 +00:00
|
|
|
// Avoid adding incorrect test cases to the corpus.
|
|
|
|
return 0;
|
|
|
|
}
|
[FuzzMutate] Module size heuristics
IRMutation::mutateModule() currently requires the bitcode size of the module.
To compute the bitcode size, one way is to write the module to a buffer using
BitcodeWriter and calculating the buffer size. This would be fine for a single
mutation, but infeasible for repeated mutations due to the large overhead. It
turns out that the only IR strategy weight calculation method that depends on
the current module size is InstDeleterStrategy, which deletes instructions more
frequently as the module size approaches a given max size. However, there is no
real need for the size to be in bytes of bitcode, so we can use a different
metric. One alternative is to let the size be the number of objects in the
Module, including instructions, basic blocks, globals, and aliases. Although
getting the number of instructions is still O(n), it should have significantly
less overhead than BitcodeWriter. This suggestion would cause a change to the
IRMutator API, since IRMutator::mutateModule() can calculate the Module size
itself.
Reviewed By: Peter
Differential Revision: https://reviews.llvm.org/D149989
2023-05-05 15:43:58 -07:00
|
|
|
|
2018-02-05 11:05:47 +00:00
|
|
|
std::string Buf;
|
|
|
|
{
|
|
|
|
raw_string_ostream OS(Buf);
|
2018-02-14 19:11:32 +00:00
|
|
|
WriteBitcodeToFile(*M, OS);
|
2018-02-05 11:05:47 +00:00
|
|
|
}
|
|
|
|
if (Buf.size() > MaxSize)
|
|
|
|
return 0;
|
[FuzzMutate] Module size heuristics
IRMutation::mutateModule() currently requires the bitcode size of the module.
To compute the bitcode size, one way is to write the module to a buffer using
BitcodeWriter and calculating the buffer size. This would be fine for a single
mutation, but infeasible for repeated mutations due to the large overhead. It
turns out that the only IR strategy weight calculation method that depends on
the current module size is InstDeleterStrategy, which deletes instructions more
frequently as the module size approaches a given max size. However, there is no
real need for the size to be in bytes of bitcode, so we can use a different
metric. One alternative is to let the size be the number of objects in the
Module, including instructions, basic blocks, globals, and aliases. Although
getting the number of instructions is still O(n), it should have significantly
less overhead than BitcodeWriter. This suggestion would cause a change to the
IRMutator API, since IRMutator::mutateModule() can calculate the Module size
itself.
Reviewed By: Peter
Differential Revision: https://reviews.llvm.org/D149989
2023-05-05 15:43:58 -07:00
|
|
|
|
2018-02-05 11:05:47 +00:00
|
|
|
// 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
|
[FuzzMutate] Module size heuristics
IRMutation::mutateModule() currently requires the bitcode size of the module.
To compute the bitcode size, one way is to write the module to a buffer using
BitcodeWriter and calculating the buffer size. This would be fine for a single
mutation, but infeasible for repeated mutations due to the large overhead. It
turns out that the only IR strategy weight calculation method that depends on
the current module size is InstDeleterStrategy, which deletes instructions more
frequently as the module size approaches a given max size. However, there is no
real need for the size to be in bytes of bitcode, so we can use a different
metric. One alternative is to let the size be the number of objects in the
Module, including instructions, basic blocks, globals, and aliases. Although
getting the number of instructions is still O(n), it should have significantly
less overhead than BitcodeWriter. This suggestion would cause a change to the
IRMutator API, since IRMutator::mutateModule() can calculate the Module size
itself.
Reviewed By: Peter
Differential Revision: https://reviews.llvm.org/D149989
2023-05-05 15:43:58 -07:00
|
|
|
// to the corpus and this is going to confuse the fuzzer which will start
|
2018-02-05 11:05:47 +00:00
|
|
|
// exploration of the bitcode reader error handling code.
|
[FuzzMutate] Module size heuristics
IRMutation::mutateModule() currently requires the bitcode size of the module.
To compute the bitcode size, one way is to write the module to a buffer using
BitcodeWriter and calculating the buffer size. This would be fine for a single
mutation, but infeasible for repeated mutations due to the large overhead. It
turns out that the only IR strategy weight calculation method that depends on
the current module size is InstDeleterStrategy, which deletes instructions more
frequently as the module size approaches a given max size. However, there is no
real need for the size to be in bytes of bitcode, so we can use a different
metric. One alternative is to let the size be the number of objects in the
Module, including instructions, basic blocks, globals, and aliases. Although
getting the number of instructions is still O(n), it should have significantly
less overhead than BitcodeWriter. This suggestion would cause a change to the
IRMutator API, since IRMutator::mutateModule() can calculate the Module size
itself.
Reviewed By: Peter
Differential Revision: https://reviews.llvm.org/D149989
2023-05-05 15:43:58 -07:00
|
|
|
auto NewM = parseAndVerify(reinterpret_cast<const uint8_t *>(Buf.data()),
|
|
|
|
Buf.size(), Context);
|
2018-02-05 11:05:47 +00:00
|
|
|
if (!NewM) {
|
|
|
|
errs() << "mutator failed to re-read the module\n";
|
2018-02-05 12:47:40 +00:00
|
|
|
#ifndef NDEBUG
|
2018-02-05 11:05:47 +00:00
|
|
|
M->dump();
|
2018-02-05 12:47:40 +00:00
|
|
|
#endif
|
2018-02-05 11:05:47 +00:00
|
|
|
return 0;
|
2017-11-10 12:19:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-05 11:05:47 +00:00
|
|
|
memcpy(Data, Buf.data(), Buf.size());
|
|
|
|
return Buf.size();
|
2017-11-10 12:19:08 +00: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 11:05:47 +00:00
|
|
|
auto M = parseAndVerify(Data, Size, Context);
|
|
|
|
if (!M) {
|
2017-11-10 12:19:08 +00:00
|
|
|
errs() << "error: input module is broken!\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up target dependant options
|
|
|
|
//
|
|
|
|
|
|
|
|
M->setTargetTriple(TM->getTargetTriple().normalize());
|
|
|
|
M->setDataLayout(TM->createDataLayout());
|
2020-03-04 00:47:43 +01:00
|
|
|
codegen::setFunctionAttributes(TM->getTargetCPU(),
|
|
|
|
TM->getTargetFeatureString(), *M);
|
2017-11-10 12:19:08 +00:00
|
|
|
|
|
|
|
// Create pass pipeline
|
|
|
|
//
|
|
|
|
|
2021-05-03 16:09:56 -07:00
|
|
|
PassBuilder PB(TM.get());
|
2017-11-10 12:19:08 +00:00
|
|
|
|
|
|
|
LoopAnalysisManager LAM;
|
|
|
|
FunctionAnalysisManager FAM;
|
|
|
|
CGSCCAnalysisManager CGAM;
|
|
|
|
ModulePassManager MPM;
|
|
|
|
ModuleAnalysisManager MAM;
|
|
|
|
|
|
|
|
PB.registerModuleAnalyses(MAM);
|
|
|
|
PB.registerCGSCCAnalyses(CGAM);
|
|
|
|
PB.registerFunctionAnalyses(FAM);
|
|
|
|
PB.registerLoopAnalyses(LAM);
|
|
|
|
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
|
|
|
|
|
2020-10-29 15:43:31 -07:00
|
|
|
auto Err = PB.parsePassPipeline(MPM, PassPipeline);
|
2018-10-17 10:36:23 +00:00
|
|
|
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 12:19:08 +00: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;
|
|
|
|
}
|
|
|
|
|
2021-10-05 10:51:28 +01:00
|
|
|
static void handleLLVMFatalError(void *, const char *Message, bool) {
|
2017-11-10 12:19:08 +00:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
[FuzzMutate] Module size heuristics
IRMutation::mutateModule() currently requires the bitcode size of the module.
To compute the bitcode size, one way is to write the module to a buffer using
BitcodeWriter and calculating the buffer size. This would be fine for a single
mutation, but infeasible for repeated mutations due to the large overhead. It
turns out that the only IR strategy weight calculation method that depends on
the current module size is InstDeleterStrategy, which deletes instructions more
frequently as the module size approaches a given max size. However, there is no
real need for the size to be in bytes of bitcode, so we can use a different
metric. One alternative is to let the size be the number of objects in the
Module, including instructions, basic blocks, globals, and aliases. Although
getting the number of instructions is still O(n), it should have significantly
less overhead than BitcodeWriter. This suggestion would cause a change to the
IRMutator API, since IRMutator::mutateModule() can calculate the Module size
itself.
Reviewed By: Peter
Differential Revision: https://reviews.llvm.org/D149989
2023-05-05 15:43:58 -07:00
|
|
|
extern "C" LLVM_ATTRIBUTE_USED int LLVMFuzzerInitialize(int *argc,
|
|
|
|
char ***argv) {
|
2017-11-10 12:19:08 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
// Parse input options
|
|
|
|
//
|
|
|
|
|
|
|
|
handleExecNameEncodedOptimizerOpts(*argv[0]);
|
|
|
|
parseFuzzerCLOpts(*argc, *argv);
|
|
|
|
|
|
|
|
// Create TargetMachine
|
|
|
|
//
|
|
|
|
|
|
|
|
if (TargetTripleStr.empty()) {
|
|
|
|
errs() << *argv[0] << ": -mtriple must be specified\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2022-11-24 11:23:38 +00:00
|
|
|
ExitOnError ExitOnErr(std::string(*argv[0]) + ": error:");
|
|
|
|
TM = ExitOnErr(codegen::createTargetMachineForTriple(
|
|
|
|
Triple::normalize(TargetTripleStr)));
|
2017-11-10 12:19:08 +00:00
|
|
|
|
|
|
|
// Check that pass pipeline is specified and correct
|
|
|
|
//
|
|
|
|
|
|
|
|
if (PassPipeline.empty()) {
|
|
|
|
errs() << *argv[0] << ": at least one pass should be specified\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-05-03 16:09:56 -07:00
|
|
|
PassBuilder PB(TM.get());
|
2017-11-10 12:19:08 +00:00
|
|
|
ModulePassManager MPM;
|
2020-10-29 15:43:31 -07:00
|
|
|
if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
|
2018-10-17 10:36:23 +00:00
|
|
|
errs() << *argv[0] << ": " << toString(std::move(Err)) << "\n";
|
2017-11-10 12:19:08 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create mutator
|
|
|
|
//
|
|
|
|
|
|
|
|
Mutator = createOptMutator();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|