blob: 55b75cd265316944a4693ca439c7a814276bd597 [file] [log] [blame]
Igor Laevskyfaacdf82017-11-30 15:24:41 +00001//===- RandomIRBuilderTest.cpp - Tests for injector strategy --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/FuzzMutate/RandomIRBuilder.h"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/AsmParser/Parser.h"
13#include "llvm/AsmParser/SlotMapping.h"
14#include "llvm/FuzzMutate/IRMutator.h"
15#include "llvm/FuzzMutate/OpDescriptor.h"
16#include "llvm/FuzzMutate/Operations.h"
17#include "llvm/IR/Constants.h"
18#include "llvm/IR/Instructions.h"
19#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/Verifier.h"
22#include "llvm/Support/SourceMgr.h"
23
24#include "gtest/gtest.h"
25
26using namespace llvm;
27
28static constexpr int Seed = 5;
29
30namespace {
31
32std::unique_ptr<Module> parseAssembly(
33 const char *Assembly, LLVMContext &Context) {
34
35 SMDiagnostic Error;
36 std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
37
38 std::string ErrMsg;
39 raw_string_ostream OS(ErrMsg);
40 Error.print("", OS);
41
42 assert(M && !verifyModule(*M, &errs()));
43 return M;
44}
45
46TEST(RandomIRBuilderTest, ShuffleVectorIncorrectOperands) {
47 // Test that we don't create load instruction as a source for the shuffle
48 // vector operation.
49
50 LLVMContext Ctx;
51 const char *Source =
52 "define <2 x i32> @test(<2 x i1> %cond, <2 x i32> %a) {\n"
53 " %A = alloca <2 x i32>\n"
54 " %I = insertelement <2 x i32> %a, i32 1, i32 1\n"
55 " ret <2 x i32> undef\n"
56 "}";
57 auto M = parseAssembly(Source, Ctx);
58
59 fuzzerop::OpDescriptor Descr = fuzzerop::shuffleVectorDescriptor(1);
60
61 // Empty known types since we ShuffleVector descriptor doesn't care about them
62 RandomIRBuilder IB(Seed, {});
63
64 // Get first basic block of the first function
65 Function &F = *M->begin();
66 BasicBlock &BB = *F.begin();
67
68 SmallVector<Instruction *, 32> Insts;
69 for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I)
70 Insts.push_back(&*I);
71
72 // Pick first and second sources
73 SmallVector<Value *, 2> Srcs;
74 ASSERT_TRUE(Descr.SourcePreds[0].matches(Srcs, Insts[1]));
75 Srcs.push_back(Insts[1]);
76 ASSERT_TRUE(Descr.SourcePreds[1].matches(Srcs, Insts[1]));
77 Srcs.push_back(Insts[1]);
78
79 // Create new source. Check that it always matches with the descriptor.
80 // Run some iterations to account for random decisions.
81 for (int i = 0; i < 10; ++i) {
82 Value *LastSrc = IB.newSource(BB, Insts, Srcs, Descr.SourcePreds[2]);
83 ASSERT_TRUE(Descr.SourcePreds[2].matches(Srcs, LastSrc));
84 }
85}
86
Igor Laevsky48147d02017-11-30 15:26:48 +000087TEST(RandomIRBuilderTest, InsertValueIndexes) {
88 // Check that we will generate correct indexes for the insertvalue operation
89
90 LLVMContext Ctx;
91 const char *Source =
92 "%T = type {i8, i32, i64}\n"
93 "define void @test() {\n"
94 " %A = alloca %T\n"
95 " %L = load %T, %T* %A"
96 " ret void\n"
97 "}";
98 auto M = parseAssembly(Source, Ctx);
99
100 fuzzerop::OpDescriptor IVDescr = fuzzerop::insertValueDescriptor(1);
101
102 std::vector<Type *> Types =
103 {Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), Type::getInt64Ty(Ctx)};
104 RandomIRBuilder IB(Seed, Types);
105
106 // Get first basic block of the first function
107 Function &F = *M->begin();
108 BasicBlock &BB = *F.begin();
109
110 // Pick first source
111 Instruction *Src = &*std::next(BB.begin());
112
113 SmallVector<Value *, 2> Srcs(2);
114 ASSERT_TRUE(IVDescr.SourcePreds[0].matches({}, Src));
115 Srcs[0] = Src;
116
117 // Generate constants for each of the types and check that we pick correct
118 // index for the given type
119 for (auto *T: Types) {
120 // Loop to account for possible random decisions
121 for (int i = 0; i < 10; ++i) {
122 // Create value we want to insert. Only it's type matters.
123 Srcs[1] = ConstantInt::get(T, 5);
124
125 // Try to pick correct index
126 Value *Src = IB.findOrCreateSource(
127 BB, &*BB.begin(), Srcs, IVDescr.SourcePreds[2]);
128 ASSERT_TRUE(IVDescr.SourcePreds[2].matches(Srcs, Src));
129 }
130 }
131}
132
Igor Laevskyfaacdf82017-11-30 15:24:41 +0000133}