From 59e987ce53b475ea607d8cdfef1af7d8f2323e07 Mon Sep 17 00:00:00 2001 From: Igor Laevsky Date: Thu, 30 Nov 2017 15:26:48 +0000 Subject: [PATCH] [FuzzMutate] Pick correct index for the insertvalue instruction Differential Revision: https://reviews.llvm.org/D40395 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319440 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/FuzzMutate/Operations.cpp | 6 +-- unittests/FuzzMutate/RandomIRBuilderTest.cpp | 46 ++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/FuzzMutate/Operations.cpp b/lib/FuzzMutate/Operations.cpp index 083d9aa039e..8bc902edc9c 100644 --- a/lib/FuzzMutate/Operations.cpp +++ b/lib/FuzzMutate/Operations.cpp @@ -240,9 +240,9 @@ static SourcePred validInsertValueIndex() { auto Pred = [](ArrayRef Cur, const Value *V) { auto *CTy = cast(Cur[0]->getType()); if (auto *CI = dyn_cast(V)) - if (CI->getBitWidth() == 32) - if (CTy->getTypeAtIndex(CI->getZExtValue()) == V->getType()) - return true; + if (CI->getBitWidth() == 32 && + CTy->getTypeAtIndex(CI->getZExtValue()) == Cur[1]->getType()) + return true; return false; }; auto Make = [](ArrayRef Cur, ArrayRef Ts) { diff --git a/unittests/FuzzMutate/RandomIRBuilderTest.cpp b/unittests/FuzzMutate/RandomIRBuilderTest.cpp index f8a50a117ba..55b75cd2653 100644 --- a/unittests/FuzzMutate/RandomIRBuilderTest.cpp +++ b/unittests/FuzzMutate/RandomIRBuilderTest.cpp @@ -84,4 +84,50 @@ TEST(RandomIRBuilderTest, ShuffleVectorIncorrectOperands) { } } +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 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 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)); + } + } +} + } -- 2.50.1