Imported Upstream version 5.18.0.167

Former-commit-id: 289509151e0fee68a1b591a20c9f109c3c789d3a
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-10-20 08:25:10 +00:00
parent e19d552987
commit b084638f15
28489 changed files with 184 additions and 3866856 deletions

View File

@ -1,13 +0,0 @@
set(LLVM_LINK_COMPONENTS
AsmParser
Core
FuzzMutate
Support
)
add_llvm_unittest(FuzzMutateTests
OperationsTest.cpp
ReservoirSamplerTest.cpp
StrategiesTest.cpp
RandomIRBuilderTest.cpp
)

View File

@ -1,405 +0,0 @@
//===- OperationsTest.cpp - Tests for fuzzer operations -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/FuzzMutate/Operations.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/FuzzMutate/OpDescriptor.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/SourceMgr.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <iostream>
// Define some pretty printers to help with debugging failures.
namespace llvm {
void PrintTo(Type *T, ::std::ostream *OS) {
raw_os_ostream ROS(*OS);
T->print(ROS);
}
void PrintTo(BasicBlock *BB, ::std::ostream *OS) {
raw_os_ostream ROS(*OS);
ROS << BB << " (" << BB->getName() << ")";
}
void PrintTo(Value *V, ::std::ostream *OS) {
raw_os_ostream ROS(*OS);
ROS << V << " (";
V->print(ROS);
ROS << ")";
}
void PrintTo(Constant *C, ::std::ostream *OS) { PrintTo(cast<Value>(C), OS); }
} // namespace llvm
using namespace llvm;
using testing::AllOf;
using testing::AnyOf;
using testing::ElementsAre;
using testing::Eq;
using testing::Ge;
using testing::Each;
using testing::Truly;
using testing::NotNull;
using testing::PrintToString;
using testing::SizeIs;
namespace {
std::unique_ptr<Module> parseAssembly(
const char *Assembly, LLVMContext &Context) {
SMDiagnostic Error;
std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
std::string ErrMsg;
raw_string_ostream OS(ErrMsg);
Error.print("", OS);
assert(M && !verifyModule(*M, &errs()));
return M;
}
MATCHER_P(TypesMatch, V, "has type " + PrintToString(V->getType())) {
return arg->getType() == V->getType();
}
MATCHER_P(HasType, T, "") { return arg->getType() == T; }
TEST(OperationsTest, SourcePreds) {
using namespace llvm::fuzzerop;
LLVMContext Ctx;
Constant *i1 = ConstantInt::getFalse(Ctx);
Constant *i8 = ConstantInt::get(Type::getInt8Ty(Ctx), 3);
Constant *i16 = ConstantInt::get(Type::getInt16Ty(Ctx), 1 << 15);
Constant *i32 = ConstantInt::get(Type::getInt32Ty(Ctx), 0);
Constant *i64 = ConstantInt::get(Type::getInt64Ty(Ctx),
std::numeric_limits<uint64_t>::max());
Constant *f16 = ConstantFP::getInfinity(Type::getHalfTy(Ctx));
Constant *f32 = ConstantFP::get(Type::getFloatTy(Ctx), 0.0);
Constant *f64 = ConstantFP::get(Type::getDoubleTy(Ctx), 123.45);
Constant *s =
ConstantStruct::get(StructType::create(Ctx, "OpaqueStruct"));
Constant *a =
ConstantArray::get(ArrayType::get(i32->getType(), 2), {i32, i32});
Constant *v8i8 = ConstantVector::getSplat(8, i8);
Constant *v4f16 = ConstantVector::getSplat(4, f16);
Constant *p0i32 =
ConstantPointerNull::get(PointerType::get(i32->getType(), 0));
auto OnlyI32 = onlyType(i32->getType());
EXPECT_TRUE(OnlyI32.matches({}, i32));
EXPECT_FALSE(OnlyI32.matches({}, i64));
EXPECT_FALSE(OnlyI32.matches({}, p0i32));
EXPECT_FALSE(OnlyI32.matches({}, a));
EXPECT_THAT(OnlyI32.generate({}, {}),
AllOf(SizeIs(Ge(1u)), Each(TypesMatch(i32))));
auto AnyType = anyType();
EXPECT_TRUE(AnyType.matches({}, i1));
EXPECT_TRUE(AnyType.matches({}, f64));
EXPECT_TRUE(AnyType.matches({}, s));
EXPECT_TRUE(AnyType.matches({}, v8i8));
EXPECT_TRUE(AnyType.matches({}, p0i32));
EXPECT_THAT(
AnyType.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
Each(AnyOf(TypesMatch(i32), TypesMatch(f16), TypesMatch(v8i8))));
auto AnyInt = anyIntType();
EXPECT_TRUE(AnyInt.matches({}, i1));
EXPECT_TRUE(AnyInt.matches({}, i64));
EXPECT_FALSE(AnyInt.matches({}, f32));
EXPECT_FALSE(AnyInt.matches({}, v4f16));
EXPECT_THAT(
AnyInt.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
AllOf(SizeIs(Ge(1u)), Each(TypesMatch(i32))));
auto AnyFP = anyFloatType();
EXPECT_TRUE(AnyFP.matches({}, f16));
EXPECT_TRUE(AnyFP.matches({}, f32));
EXPECT_FALSE(AnyFP.matches({}, i16));
EXPECT_FALSE(AnyFP.matches({}, p0i32));
EXPECT_FALSE(AnyFP.matches({}, v4f16));
EXPECT_THAT(
AnyFP.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
AllOf(SizeIs(Ge(1u)), Each(TypesMatch(f16))));
auto AnyPtr = anyPtrType();
EXPECT_TRUE(AnyPtr.matches({}, p0i32));
EXPECT_FALSE(AnyPtr.matches({}, i8));
EXPECT_FALSE(AnyPtr.matches({}, a));
EXPECT_FALSE(AnyPtr.matches({}, v8i8));
auto isPointer = [](Value *V) { return V->getType()->isPointerTy(); };
EXPECT_THAT(
AnyPtr.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
AllOf(SizeIs(Ge(3u)), Each(Truly(isPointer))));
auto AnyVec = anyVectorType();
EXPECT_TRUE(AnyVec.matches({}, v8i8));
EXPECT_TRUE(AnyVec.matches({}, v4f16));
EXPECT_FALSE(AnyVec.matches({}, i8));
EXPECT_FALSE(AnyVec.matches({}, a));
EXPECT_FALSE(AnyVec.matches({}, s));
EXPECT_THAT(AnyVec.generate({}, {v8i8->getType()}),
ElementsAre(TypesMatch(v8i8)));
auto First = matchFirstType();
EXPECT_TRUE(First.matches({i8}, i8));
EXPECT_TRUE(First.matches({s, a}, s));
EXPECT_FALSE(First.matches({f16}, f32));
EXPECT_FALSE(First.matches({v4f16, f64}, f64));
EXPECT_THAT(First.generate({i8}, {}), Each(TypesMatch(i8)));
EXPECT_THAT(First.generate({f16}, {i8->getType()}),
Each(TypesMatch(f16)));
EXPECT_THAT(First.generate({v8i8, i32}, {}), Each(TypesMatch(v8i8)));
}
TEST(OperationsTest, SplitBlock) {
LLVMContext Ctx;
Module M("M", Ctx);
Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
/*isVarArg=*/false),
GlobalValue::ExternalLinkage, "f", &M);
auto SBOp = fuzzerop::splitBlockDescriptor(1);
// Create a block with only a return and split it on the return.
auto *BB = BasicBlock::Create(Ctx, "BB", F);
auto *RI = ReturnInst::Create(Ctx, BB);
SBOp.BuilderFunc({UndefValue::get(Type::getInt1Ty(Ctx))}, RI);
// We should end up with an unconditional branch from BB to BB1, and the
// return ends up in BB1.
auto *UncondBr = cast<BranchInst>(BB->getTerminator());
ASSERT_TRUE(UncondBr->isUnconditional());
auto *BB1 = UncondBr->getSuccessor(0);
ASSERT_THAT(RI->getParent(), Eq(BB1));
// Now add an instruction to BB1 and split on that.
auto *AI = new AllocaInst(Type::getInt8Ty(Ctx), 0, "a", RI);
Value *Cond = ConstantInt::getFalse(Ctx);
SBOp.BuilderFunc({Cond}, AI);
// We should end up with a loop back on BB1 and the instruction we split on
// moves to BB2.
auto *CondBr = cast<BranchInst>(BB1->getTerminator());
EXPECT_THAT(CondBr->getCondition(), Eq(Cond));
ASSERT_THAT(CondBr->getNumSuccessors(), Eq(2u));
ASSERT_THAT(CondBr->getSuccessor(0), Eq(BB1));
auto *BB2 = CondBr->getSuccessor(1);
EXPECT_THAT(AI->getParent(), Eq(BB2));
EXPECT_THAT(RI->getParent(), Eq(BB2));
EXPECT_FALSE(verifyModule(M, &errs()));
}
TEST(OperationsTest, SplitEHBlock) {
// Check that we will not try to branch back to the landingpad block using
// regular branch instruction
LLVMContext Ctx;
const char *SourceCode =
"declare i32* @f()"
"declare i32 @personality_function()"
"define i32* @test() personality i32 ()* @personality_function {\n"
"entry:\n"
" %val = invoke i32* @f()\n"
" to label %normal unwind label %exceptional\n"
"normal:\n"
" ret i32* %val\n"
"exceptional:\n"
" %landing_pad4 = landingpad token cleanup\n"
" ret i32* undef\n"
"}";
auto M = parseAssembly(SourceCode, Ctx);
// Get the landingpad block
BasicBlock &BB = *std::next(M->getFunction("test")->begin(), 2);
fuzzerop::OpDescriptor Descr = fuzzerop::splitBlockDescriptor(1);
Descr.BuilderFunc({ConstantInt::getTrue(Ctx)},&*BB.getFirstInsertionPt());
ASSERT_TRUE(!verifyModule(*M, &errs()));
}
TEST(OperationsTest, SplitBlockWithPhis) {
LLVMContext Ctx;
Type *Int8Ty = Type::getInt8Ty(Ctx);
Module M("M", Ctx);
Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
/*isVarArg=*/false),
GlobalValue::ExternalLinkage, "f", &M);
auto SBOp = fuzzerop::splitBlockDescriptor(1);
// Create 3 blocks with an if-then branch.
auto *BB1 = BasicBlock::Create(Ctx, "BB1", F);
auto *BB2 = BasicBlock::Create(Ctx, "BB2", F);
auto *BB3 = BasicBlock::Create(Ctx, "BB3", F);
BranchInst::Create(BB2, BB3, ConstantInt::getFalse(Ctx), BB1);
BranchInst::Create(BB3, BB2);
// Set up phi nodes selecting values for the incoming edges.
auto *PHI1 = PHINode::Create(Int8Ty, /*NumReservedValues=*/2, "p1", BB3);
PHI1->addIncoming(ConstantInt::get(Int8Ty, 0), BB1);
PHI1->addIncoming(ConstantInt::get(Int8Ty, 1), BB2);
auto *PHI2 = PHINode::Create(Int8Ty, /*NumReservedValues=*/2, "p2", BB3);
PHI2->addIncoming(ConstantInt::get(Int8Ty, 1), BB1);
PHI2->addIncoming(ConstantInt::get(Int8Ty, 0), BB2);
auto *RI = ReturnInst::Create(Ctx, BB3);
// Now we split the block with PHI nodes, making sure they're all updated.
Value *Cond = ConstantInt::getFalse(Ctx);
SBOp.BuilderFunc({Cond}, RI);
// Make sure the PHIs are updated with a value for the third incoming edge.
EXPECT_THAT(PHI1->getNumIncomingValues(), Eq(3u));
EXPECT_THAT(PHI2->getNumIncomingValues(), Eq(3u));
EXPECT_FALSE(verifyModule(M, &errs()));
}
TEST(OperationsTest, GEP) {
LLVMContext Ctx;
Type *Int8PtrTy = Type::getInt8PtrTy(Ctx);
Type *Int32Ty = Type::getInt32Ty(Ctx);
Module M("M", Ctx);
Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
/*isVarArg=*/false),
GlobalValue::ExternalLinkage, "f", &M);
auto *BB = BasicBlock::Create(Ctx, "BB", F);
auto *RI = ReturnInst::Create(Ctx, BB);
auto GEPOp = fuzzerop::gepDescriptor(1);
EXPECT_TRUE(GEPOp.SourcePreds[0].matches({}, UndefValue::get(Int8PtrTy)));
EXPECT_TRUE(GEPOp.SourcePreds[1].matches({UndefValue::get(Int8PtrTy)},
ConstantInt::get(Int32Ty, 0)));
GEPOp.BuilderFunc({UndefValue::get(Int8PtrTy), ConstantInt::get(Int32Ty, 0)},
RI);
EXPECT_FALSE(verifyModule(M, &errs()));
}
TEST(OperationsTest, GEPPointerOperand) {
// Check that we only pick sized pointers for the GEP instructions
LLVMContext Ctx;
const char *SourceCode =
"declare void @f()\n"
"define void @test() {\n"
" %v = bitcast void ()* @f to i64 (i8 addrspace(4)*)*\n"
" %a = alloca i64, i32 10\n"
" ret void\n"
"}";
auto M = parseAssembly(SourceCode, Ctx);
fuzzerop::OpDescriptor Descr = fuzzerop::gepDescriptor(1);
// Get first basic block of the test function
Function &F = *M->getFunction("test");
BasicBlock &BB = *F.begin();
// Don't match %v
ASSERT_FALSE(Descr.SourcePreds[0].matches({}, &*BB.begin()));
// Match %a
ASSERT_TRUE(Descr.SourcePreds[0].matches({}, &*std::next(BB.begin())));
}
TEST(OperationsTest, ExtractAndInsertValue) {
LLVMContext Ctx;
Type *Int8PtrTy = Type::getInt8PtrTy(Ctx);
Type *Int32Ty = Type::getInt32Ty(Ctx);
Type *Int64Ty = Type::getInt64Ty(Ctx);
Type *StructTy = StructType::create(Ctx, {Int8PtrTy, Int32Ty});
Type *OpaqueTy = StructType::create(Ctx, "OpaqueStruct");
Type *ZeroSizedArrayTy = ArrayType::get(Int64Ty, 0);
Type *ArrayTy = ArrayType::get(Int64Ty, 4);
Type *VectorTy = VectorType::get(Int32Ty, 2);
auto EVOp = fuzzerop::extractValueDescriptor(1);
auto IVOp = fuzzerop::insertValueDescriptor(1);
// Sanity check the source preds.
Constant *SVal = UndefValue::get(StructTy);
Constant *OVal = UndefValue::get(OpaqueTy);
Constant *AVal = UndefValue::get(ArrayTy);
Constant *ZAVal = UndefValue::get(ZeroSizedArrayTy);
Constant *VVal = UndefValue::get(VectorTy);
EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, SVal));
EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, OVal));
EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, AVal));
EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, VVal));
EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, SVal));
EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, OVal));
EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, AVal));
EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, VVal));
// Don't consider zero sized arrays as viable sources
EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, ZAVal));
EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, ZAVal));
// Make sure we're range checking appropriately.
EXPECT_TRUE(
EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 0)));
EXPECT_TRUE(
EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 1)));
EXPECT_FALSE(
EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 2)));
EXPECT_FALSE(
EVOp.SourcePreds[1].matches({OVal}, ConstantInt::get(Int32Ty, 0)));
EXPECT_FALSE(
EVOp.SourcePreds[1].matches({OVal}, ConstantInt::get(Int32Ty, 65536)));
EXPECT_TRUE(
EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 0)));
EXPECT_TRUE(
EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 3)));
EXPECT_FALSE(
EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 4)));
EXPECT_THAT(
EVOp.SourcePreds[1].generate({SVal}, {}),
ElementsAre(ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 1)));
// InsertValue should accept any type in the struct, but only in positions
// where it makes sense.
EXPECT_TRUE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int8PtrTy)));
EXPECT_TRUE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int32Ty)));
EXPECT_FALSE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int64Ty)));
EXPECT_FALSE(IVOp.SourcePreds[2].matches({SVal, UndefValue::get(Int32Ty)},
ConstantInt::get(Int32Ty, 0)));
EXPECT_TRUE(IVOp.SourcePreds[2].matches({SVal, UndefValue::get(Int32Ty)},
ConstantInt::get(Int32Ty, 1)));
EXPECT_THAT(IVOp.SourcePreds[1].generate({SVal}, {}),
Each(AnyOf(HasType(Int32Ty), HasType(Int8PtrTy))));
EXPECT_THAT(
IVOp.SourcePreds[2].generate({SVal, ConstantInt::get(Int32Ty, 0)}, {}),
ElementsAre(ConstantInt::get(Int32Ty, 1)));
}
}

View File

@ -1,269 +0,0 @@
//===- RandomIRBuilderTest.cpp - Tests for injector strategy --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/FuzzMutate/RandomIRBuilder.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/AsmParser/SlotMapping.h"
#include "llvm/FuzzMutate/IRMutator.h"
#include "llvm/FuzzMutate/OpDescriptor.h"
#include "llvm/FuzzMutate/Operations.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
static constexpr int Seed = 5;
namespace {
std::unique_ptr<Module> parseAssembly(
const char *Assembly, LLVMContext &Context) {
SMDiagnostic Error;
std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
std::string ErrMsg;
raw_string_ostream OS(ErrMsg);
Error.print("", OS);
assert(M && !verifyModule(*M, &errs()));
return M;
}
TEST(RandomIRBuilderTest, ShuffleVectorIncorrectOperands) {
// Test that we don't create load instruction as a source for the shuffle
// vector operation.
LLVMContext Ctx;
const char *Source =
"define <2 x i32> @test(<2 x i1> %cond, <2 x i32> %a) {\n"
" %A = alloca <2 x i32>\n"
" %I = insertelement <2 x i32> %a, i32 1, i32 1\n"
" ret <2 x i32> undef\n"
"}";
auto M = parseAssembly(Source, Ctx);
fuzzerop::OpDescriptor Descr = fuzzerop::shuffleVectorDescriptor(1);
// Empty known types since we ShuffleVector descriptor doesn't care about them
RandomIRBuilder IB(Seed, {});
// Get first basic block of the first function
Function &F = *M->begin();
BasicBlock &BB = *F.begin();
SmallVector<Instruction *, 32> Insts;
for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I)
Insts.push_back(&*I);
// Pick first and second sources
SmallVector<Value *, 2> Srcs;
ASSERT_TRUE(Descr.SourcePreds[0].matches(Srcs, Insts[1]));
Srcs.push_back(Insts[1]);
ASSERT_TRUE(Descr.SourcePreds[1].matches(Srcs, Insts[1]));
Srcs.push_back(Insts[1]);
// Create new source. Check that it always matches with the descriptor.
// Run some iterations to account for random decisions.
for (int i = 0; i < 10; ++i) {
Value *LastSrc = IB.newSource(BB, Insts, Srcs, Descr.SourcePreds[2]);
ASSERT_TRUE(Descr.SourcePreds[2].matches(Srcs, LastSrc));
}
}
TEST(RandomIRBuilderTest, InsertValueIndexes) {
// Check that we will generate correct indexes for the insertvalue operation
LLVMContext Ctx;
const char *Source =
"%T = type {i8, i32, i64}\n"
"define void @test() {\n"
" %A = alloca %T\n"
" %L = load %T, %T* %A"
" ret void\n"
"}";
auto M = parseAssembly(Source, Ctx);
fuzzerop::OpDescriptor IVDescr = fuzzerop::insertValueDescriptor(1);
std::vector<Type *> Types =
{Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), Type::getInt64Ty(Ctx)};
RandomIRBuilder IB(Seed, Types);
// Get first basic block of the first function
Function &F = *M->begin();
BasicBlock &BB = *F.begin();
// Pick first source
Instruction *Src = &*std::next(BB.begin());
SmallVector<Value *, 2> Srcs(2);
ASSERT_TRUE(IVDescr.SourcePreds[0].matches({}, Src));
Srcs[0] = Src;
// Generate constants for each of the types and check that we pick correct
// index for the given type
for (auto *T: Types) {
// Loop to account for possible random decisions
for (int i = 0; i < 10; ++i) {
// Create value we want to insert. Only it's type matters.
Srcs[1] = ConstantInt::get(T, 5);
// Try to pick correct index
Value *Src = IB.findOrCreateSource(
BB, &*BB.begin(), Srcs, IVDescr.SourcePreds[2]);
ASSERT_TRUE(IVDescr.SourcePreds[2].matches(Srcs, Src));
}
}
}
TEST(RandomIRBuilderTest, ShuffleVectorSink) {
// Check that we will never use shuffle vector mask as a sink form the
// unrelated operation.
LLVMContext Ctx;
const char *SourceCode =
"define void @test(<4 x i32> %a) {\n"
" %S1 = shufflevector <4 x i32> %a, <4 x i32> %a, <4 x i32> undef\n"
" %S2 = shufflevector <4 x i32> %a, <4 x i32> %a, <4 x i32> undef\n"
" ret void\n"
"}";
auto M = parseAssembly(SourceCode, Ctx);
fuzzerop::OpDescriptor IVDescr = fuzzerop::insertValueDescriptor(1);
RandomIRBuilder IB(Seed, {});
// Get first basic block of the first function
Function &F = *M->begin();
BasicBlock &BB = *F.begin();
// Source is %S1
Instruction *Source = &*BB.begin();
// Sink is %S2
SmallVector<Instruction *, 1> Sinks = {&*std::next(BB.begin())};
// Loop to account for random decisions
for (int i = 0; i < 10; ++i) {
// Try to connect S1 to S2. We should always create new sink.
IB.connectToSink(BB, Sinks, Source);
ASSERT_TRUE(!verifyModule(*M, &errs()));
}
}
TEST(RandomIRBuilderTest, InsertValueArray) {
// Check that we can generate insertvalue for the vector operations
LLVMContext Ctx;
const char *SourceCode =
"define void @test() {\n"
" %A = alloca [8 x i32]\n"
" %L = load [8 x i32], [8 x i32]* %A"
" ret void\n"
"}";
auto M = parseAssembly(SourceCode, Ctx);
fuzzerop::OpDescriptor Descr = fuzzerop::insertValueDescriptor(1);
std::vector<Type *> Types =
{Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), Type::getInt64Ty(Ctx)};
RandomIRBuilder IB(Seed, Types);
// Get first basic block of the first function
Function &F = *M->begin();
BasicBlock &BB = *F.begin();
// Pick first source
Instruction *Source = &*std::next(BB.begin());
ASSERT_TRUE(Descr.SourcePreds[0].matches({}, Source));
SmallVector<Value *, 2> Srcs(2);
// Check that we can always pick the last two operands.
for (int i = 0; i < 10; ++i) {
Srcs[0] = Source;
Srcs[1] = IB.findOrCreateSource(BB, {Source}, Srcs, Descr.SourcePreds[1]);
IB.findOrCreateSource(BB, {}, Srcs, Descr.SourcePreds[2]);
}
}
TEST(RandomIRBuilderTest, Invokes) {
// Check that we never generate load or store after invoke instruction
LLVMContext Ctx;
const char *SourceCode =
"declare i32* @f()"
"declare i32 @personality_function()"
"define i32* @test() personality i32 ()* @personality_function {\n"
"entry:\n"
" %val = invoke i32* @f()\n"
" to label %normal unwind label %exceptional\n"
"normal:\n"
" ret i32* %val\n"
"exceptional:\n"
" %landing_pad4 = landingpad token cleanup\n"
" ret i32* undef\n"
"}";
auto M = parseAssembly(SourceCode, Ctx);
std::vector<Type *> Types = {Type::getInt8Ty(Ctx)};
RandomIRBuilder IB(Seed, Types);
// Get first basic block of the test function
Function &F = *M->getFunction("test");
BasicBlock &BB = *F.begin();
Instruction *Invoke = &*BB.begin();
// Find source but never insert new load after invoke
for (int i = 0; i < 10; ++i) {
(void)IB.findOrCreateSource(BB, {Invoke}, {}, fuzzerop::anyIntType());
ASSERT_TRUE(!verifyModule(*M, &errs()));
}
}
TEST(RandomIRBuilderTest, FirstClassTypes) {
// Check that we never insert new source as a load from non first class
// or unsized type.
LLVMContext Ctx;
const char *SourceCode = "%Opaque = type opaque\n"
"define void @test(i8* %ptr) {\n"
"entry:\n"
" %tmp = bitcast i8* %ptr to i32* (i32*)*\n"
" %tmp1 = bitcast i8* %ptr to %Opaque*\n"
" ret void\n"
"}";
auto M = parseAssembly(SourceCode, Ctx);
std::vector<Type *> Types = {Type::getInt8Ty(Ctx)};
RandomIRBuilder IB(Seed, Types);
Function &F = *M->getFunction("test");
BasicBlock &BB = *F.begin();
// Non first class type
Instruction *FuncPtr = &*BB.begin();
// Unsized type
Instruction *OpaquePtr = &*std::next(BB.begin());
for (int i = 0; i < 10; ++i) {
Value *V = IB.findOrCreateSource(BB, {FuncPtr, OpaquePtr});
ASSERT_FALSE(isa<LoadInst>(V));
}
}
}

View File

@ -1,69 +0,0 @@
//===- ReservoirSampler.cpp - Tests for the ReservoirSampler --------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/FuzzMutate/Random.h"
#include "gtest/gtest.h"
#include <random>
using namespace llvm;
TEST(ReservoirSamplerTest, OneItem) {
std::mt19937 Rand;
auto Sampler = makeSampler(Rand, 7, 1);
ASSERT_FALSE(Sampler.isEmpty());
ASSERT_EQ(7, Sampler.getSelection());
}
TEST(ReservoirSamplerTest, NoWeight) {
std::mt19937 Rand;
auto Sampler = makeSampler(Rand, 7, 0);
ASSERT_TRUE(Sampler.isEmpty());
}
TEST(ReservoirSamplerTest, Uniform) {
std::mt19937 Rand;
// Run three chi-squared tests to check that the distribution is reasonably
// uniform.
std::vector<int> Items = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int Failures = 0;
for (int Run = 0; Run < 3; ++Run) {
std::vector<int> Counts(Items.size(), 0);
// We need $np_s > 5$ at minimum, but we're better off going a couple of
// orders of magnitude larger.
int N = Items.size() * 5 * 100;
for (int I = 0; I < N; ++I) {
auto Sampler = makeSampler(Rand, Items);
Counts[Sampler.getSelection()] += 1;
}
// Knuth. TAOCP Vol. 2, 3.3.1 (8):
// $V = \frac{1}{n} \sum_{s=1}^{k} \left(\frac{Y_s^2}{p_s}\right) - n$
double Ps = 1.0 / Items.size();
double Sum = 0.0;
for (int Ys : Counts)
Sum += Ys * Ys / Ps;
double V = (Sum / N) - N;
assert(Items.size() == 10 && "Our chi-squared values assume 10 items");
// Since we have 10 items, there are 9 degrees of freedom and the table of
// chi-squared values is as follows:
//
// | p=1% | 5% | 25% | 50% | 75% | 95% | 99% |
// v=9 | 2.088 | 3.325 | 5.899 | 8.343 | 11.39 | 16.92 | 21.67 |
//
// Check that we're in the likely range of results.
//if (V < 2.088 || V > 21.67)
if (V < 2.088 || V > 21.67)
++Failures;
}
EXPECT_LT(Failures, 3) << "Non-uniform distribution?";
}

View File

@ -1,110 +0,0 @@
//===- InjectorIRStrategyTest.cpp - Tests for injector strategy -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringRef.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/AsmParser/SlotMapping.h"
#include "llvm/FuzzMutate/IRMutator.h"
#include "llvm/FuzzMutate/Operations.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
using namespace llvm;
static constexpr int Seed = 5;
namespace {
std::unique_ptr<IRMutator> createInjectorMutator() {
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(
llvm::make_unique<InjectorIRStrategy>(
InjectorIRStrategy::getDefaultOps()));
return llvm::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
}
std::unique_ptr<IRMutator> createDeleterMutator() {
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(llvm::make_unique<InstDeleterIRStrategy>());
return llvm::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
}
std::unique_ptr<Module> parseAssembly(
const char *Assembly, LLVMContext &Context) {
SMDiagnostic Error;
std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
std::string ErrMsg;
raw_string_ostream OS(ErrMsg);
Error.print("", OS);
assert(M && !verifyModule(*M, &errs()));
return M;
}
TEST(InjectorIRStrategyTest, EmptyModule) {
// Test that we can inject into empty module
LLVMContext Ctx;
auto M = llvm::make_unique<Module>("M", Ctx);
ASSERT_TRUE(M && !verifyModule(*M, &errs()));
auto Mutator = createInjectorMutator();
ASSERT_TRUE(Mutator);
Mutator->mutateModule(*M, Seed, 1, 1);
EXPECT_TRUE(!verifyModule(*M, &errs()));
}
TEST(InstDeleterIRStrategyTest, EmptyFunction) {
// Test that we don't crash even if we can't remove from one of the functions.
LLVMContext Ctx;
StringRef Source = ""
"define <8 x i32> @func1() {\n"
"ret <8 x i32> undef\n"
"}\n"
"\n"
"define i32 @func2() {\n"
"%A9 = alloca i32\n"
"%L6 = load i32, i32* %A9\n"
"ret i32 %L6\n"
"}\n";
auto Mutator = createDeleterMutator();
ASSERT_TRUE(Mutator);
// We need to choose 'func1' in order for the crash to appear.
// Loop 10 times and assume we are lucky.
for (int i = 0; i < 10; ++i) {
auto M = parseAssembly(Source.data(), Ctx);
ASSERT_TRUE(M && !verifyModule(*M, &errs()));
Mutator->mutateModule(*M, Seed, Source.size(), Source.size() + 100);
EXPECT_TRUE(!verifyModule(*M, &errs()));
}
}
}