1 //===- InjectorIRStrategyTest.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/ADT/StringRef.h"
11 #include "llvm/AsmParser/Parser.h"
12 #include "llvm/AsmParser/SlotMapping.h"
13 #include "llvm/FuzzMutate/IRMutator.h"
14 #include "llvm/FuzzMutate/Operations.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/Verifier.h"
19 #include "llvm/Support/SourceMgr.h"
20
21 #include "gtest/gtest.h"
22
23 using namespace llvm;
24
25 static constexpr int Seed = 5;
26
27 namespace {
28
createInjectorMutator()29 std::unique_ptr<IRMutator> createInjectorMutator() {
30 std::vector<TypeGetter> Types{
31 Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty,
32 Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy};
33
34 std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
35 Strategies.push_back(
36 llvm::make_unique<InjectorIRStrategy>(
37 InjectorIRStrategy::getDefaultOps()));
38
39 return llvm::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
40 }
41
createDeleterMutator()42 std::unique_ptr<IRMutator> createDeleterMutator() {
43 std::vector<TypeGetter> Types{
44 Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty,
45 Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy};
46
47 std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
48 Strategies.push_back(llvm::make_unique<InstDeleterIRStrategy>());
49
50 return llvm::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
51 }
52
parseAssembly(const char * Assembly,LLVMContext & Context)53 std::unique_ptr<Module> parseAssembly(
54 const char *Assembly, LLVMContext &Context) {
55
56 SMDiagnostic Error;
57 std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
58
59 std::string ErrMsg;
60 raw_string_ostream OS(ErrMsg);
61 Error.print("", OS);
62
63 assert(M && !verifyModule(*M, &errs()));
64 return M;
65 }
66
IterateOnSource(StringRef Source,IRMutator & Mutator)67 void IterateOnSource(StringRef Source, IRMutator &Mutator) {
68 LLVMContext Ctx;
69
70 for (int i = 0; i < 10; ++i) {
71 auto M = parseAssembly(Source.data(), Ctx);
72 ASSERT_TRUE(M && !verifyModule(*M, &errs()));
73
74 Mutator.mutateModule(*M, Seed, Source.size(), Source.size() + 100);
75 EXPECT_TRUE(!verifyModule(*M, &errs()));
76 }
77 }
78
TEST(InjectorIRStrategyTest,EmptyModule)79 TEST(InjectorIRStrategyTest, EmptyModule) {
80 // Test that we can inject into empty module
81
82 LLVMContext Ctx;
83 auto M = llvm::make_unique<Module>("M", Ctx);
84 ASSERT_TRUE(M && !verifyModule(*M, &errs()));
85
86 auto Mutator = createInjectorMutator();
87 ASSERT_TRUE(Mutator);
88
89 Mutator->mutateModule(*M, Seed, 1, 1);
90 EXPECT_TRUE(!verifyModule(*M, &errs()));
91 }
92
TEST(InstDeleterIRStrategyTest,EmptyFunction)93 TEST(InstDeleterIRStrategyTest, EmptyFunction) {
94 // Test that we don't crash even if we can't remove from one of the functions.
95
96 StringRef Source = ""
97 "define <8 x i32> @func1() {\n"
98 "ret <8 x i32> undef\n"
99 "}\n"
100 "\n"
101 "define i32 @func2() {\n"
102 "%A9 = alloca i32\n"
103 "%L6 = load i32, i32* %A9\n"
104 "ret i32 %L6\n"
105 "}\n";
106
107 auto Mutator = createDeleterMutator();
108 ASSERT_TRUE(Mutator);
109
110 IterateOnSource(Source, *Mutator);
111 }
112
TEST(InstDeleterIRStrategyTest,PhiNodes)113 TEST(InstDeleterIRStrategyTest, PhiNodes) {
114 // Test that inst deleter works correctly with the phi nodes.
115
116 LLVMContext Ctx;
117 StringRef Source = "\n\
118 define i32 @earlyreturncrash(i32 %x) {\n\
119 entry:\n\
120 switch i32 %x, label %sw.epilog [\n\
121 i32 1, label %sw.bb1\n\
122 ]\n\
123 \n\
124 sw.bb1:\n\
125 br label %sw.epilog\n\
126 \n\
127 sw.epilog:\n\
128 %a.0 = phi i32 [ 7, %entry ], [ 9, %sw.bb1 ]\n\
129 %b.0 = phi i32 [ 10, %entry ], [ 4, %sw.bb1 ]\n\
130 ret i32 %a.0\n\
131 }";
132
133 auto Mutator = createDeleterMutator();
134 ASSERT_TRUE(Mutator);
135
136 IterateOnSource(Source, *Mutator);
137 }
138
139 }
140