blob: e710f4676225c6144a3a794551ed61c384d04385 [file] [log] [blame]
Igor Laevsky444afc82017-11-30 15:07:38 +00001//===- InjectorIRStrategyTest.cpp - Tests for injector strategy -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Igor Laevsky444afc82017-11-30 15:07:38 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ADT/StringRef.h"
10#include "llvm/AsmParser/Parser.h"
11#include "llvm/AsmParser/SlotMapping.h"
12#include "llvm/FuzzMutate/IRMutator.h"
13#include "llvm/FuzzMutate/Operations.h"
14#include "llvm/IR/Instructions.h"
15#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
17#include "llvm/IR/Verifier.h"
18#include "llvm/Support/SourceMgr.h"
19
20#include "gtest/gtest.h"
21
22using namespace llvm;
23
24static constexpr int Seed = 5;
25
26namespace {
27
28std::unique_ptr<IRMutator> createInjectorMutator() {
29 std::vector<TypeGetter> Types{
30 Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty,
31 Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy};
32
33 std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
34 Strategies.push_back(
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000035 std::make_unique<InjectorIRStrategy>(
Igor Laevsky444afc82017-11-30 15:07:38 +000036 InjectorIRStrategy::getDefaultOps()));
37
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000038 return std::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
Igor Laevsky444afc82017-11-30 15:07:38 +000039}
40
41std::unique_ptr<IRMutator> createDeleterMutator() {
42 std::vector<TypeGetter> Types{
43 Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty,
44 Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy};
45
46 std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000047 Strategies.push_back(std::make_unique<InstDeleterIRStrategy>());
Igor Laevsky444afc82017-11-30 15:07:38 +000048
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000049 return std::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
Igor Laevsky444afc82017-11-30 15:07:38 +000050}
51
52std::unique_ptr<Module> parseAssembly(
53 const char *Assembly, LLVMContext &Context) {
54
55 SMDiagnostic Error;
56 std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
57
58 std::string ErrMsg;
59 raw_string_ostream OS(ErrMsg);
60 Error.print("", OS);
61
62 assert(M && !verifyModule(*M, &errs()));
63 return M;
64}
65
Igor Laevsky7a43f262018-01-25 09:22:18 +000066void IterateOnSource(StringRef Source, IRMutator &Mutator) {
67 LLVMContext Ctx;
68
69 for (int i = 0; i < 10; ++i) {
70 auto M = parseAssembly(Source.data(), Ctx);
71 ASSERT_TRUE(M && !verifyModule(*M, &errs()));
72
73 Mutator.mutateModule(*M, Seed, Source.size(), Source.size() + 100);
74 EXPECT_TRUE(!verifyModule(*M, &errs()));
75 }
76}
77
Igor Laevsky444afc82017-11-30 15:07:38 +000078TEST(InjectorIRStrategyTest, EmptyModule) {
79 // Test that we can inject into empty module
80
81 LLVMContext Ctx;
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000082 auto M = std::make_unique<Module>("M", Ctx);
Igor Laevsky444afc82017-11-30 15:07:38 +000083 ASSERT_TRUE(M && !verifyModule(*M, &errs()));
84
85 auto Mutator = createInjectorMutator();
86 ASSERT_TRUE(Mutator);
87
88 Mutator->mutateModule(*M, Seed, 1, 1);
89 EXPECT_TRUE(!verifyModule(*M, &errs()));
90}
91
92TEST(InstDeleterIRStrategyTest, EmptyFunction) {
93 // Test that we don't crash even if we can't remove from one of the functions.
94
Igor Laevsky444afc82017-11-30 15:07:38 +000095 StringRef Source = ""
96 "define <8 x i32> @func1() {\n"
97 "ret <8 x i32> undef\n"
98 "}\n"
99 "\n"
100 "define i32 @func2() {\n"
101 "%A9 = alloca i32\n"
102 "%L6 = load i32, i32* %A9\n"
103 "ret i32 %L6\n"
104 "}\n";
105
106 auto Mutator = createDeleterMutator();
107 ASSERT_TRUE(Mutator);
108
Igor Laevsky7a43f262018-01-25 09:22:18 +0000109 IterateOnSource(Source, *Mutator);
110}
Igor Laevsky444afc82017-11-30 15:07:38 +0000111
Igor Laevsky7a43f262018-01-25 09:22:18 +0000112TEST(InstDeleterIRStrategyTest, PhiNodes) {
113 // Test that inst deleter works correctly with the phi nodes.
114
115 LLVMContext Ctx;
116 StringRef Source = "\n\
117 define i32 @earlyreturncrash(i32 %x) {\n\
118 entry:\n\
119 switch i32 %x, label %sw.epilog [\n\
120 i32 1, label %sw.bb1\n\
121 ]\n\
122 \n\
123 sw.bb1:\n\
124 br label %sw.epilog\n\
125 \n\
126 sw.epilog:\n\
127 %a.0 = phi i32 [ 7, %entry ], [ 9, %sw.bb1 ]\n\
128 %b.0 = phi i32 [ 10, %entry ], [ 4, %sw.bb1 ]\n\
129 ret i32 %a.0\n\
130 }";
131
132 auto Mutator = createDeleterMutator();
133 ASSERT_TRUE(Mutator);
134
135 IterateOnSource(Source, *Mutator);
Igor Laevsky444afc82017-11-30 15:07:38 +0000136}
137
138}