Debian Codegraph @

llvm-project/llvm/unittests/FuzzMutate/OperationsTest.cpp

raw
1 //===- OperationsTest.cpp - Tests for fuzzer operations -------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/FuzzMutate/Operations.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/FuzzMutate/OpDescriptor.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/Verifier.h"
16 #include "llvm/Support/SourceMgr.h"
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include <iostream>
20
21 // Define some pretty printers to help with debugging failures.
22 namespace llvm {
23 void PrintTo(Type *T, ::std::ostream *OS) {
24 raw_os_ostream ROS(*OS);
25 T->print(ROS);
26 }
27
28 void PrintTo(BasicBlock *BB, ::std::ostream *OS) {
29 raw_os_ostream ROS(*OS);
30 ROS << BB << " (" << BB->getName() << ")";
31 }
32
33 void PrintTo(Value *V, ::std::ostream *OS) {
34 raw_os_ostream ROS(*OS);
35 ROS << V << " (";
36 V->print(ROS);
37 ROS << ")";
38 }
39 void PrintTo(Constant *C, ::std::ostream *OS) { PrintTo(cast<Value>(C), OS); }
40
41 } // namespace llvm
42
43 using namespace llvm;
44
45 using testing::AllOf;
46 using testing::AnyOf;
47 using testing::Each;
48 using testing::ElementsAre;
49 using testing::Eq;
50 using testing::Ge;
51 using testing::NotNull;
52 using testing::PrintToString;
53 using testing::SizeIs;
54 using testing::Truly;
55
56 namespace {
57 std::unique_ptr<Module> parseAssembly(const char *Assembly,
58 LLVMContext &Context) {
59
60 SMDiagnostic Error;
61 std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
62
63 std::string ErrMsg;
64 raw_string_ostream OS(ErrMsg);
65 Error.print("", OS);
66
67 assert(M && !verifyModule(*M, &errs()));
68 return M;
69 }
70
71 MATCHER_P(TypesMatch, V, "has type " + PrintToString(V->getType())) {
72 return arg->getType() == V->getType();
73 }
74
75 MATCHER_P(HasType, T, "") { return arg->getType() == T; }
76
77 TEST(OperationsTest, SourcePreds) {
78 using namespace llvm::fuzzerop;
79
80 LLVMContext Ctx;
81
82 Constant *i1 = ConstantInt::getFalse(Ctx);
83 Constant *i8 = ConstantInt::get(Type::getInt8Ty(Ctx), 3);
84 Constant *i16 = ConstantInt::get(Type::getInt16Ty(Ctx), 1 << 15);
85 Constant *i32 = ConstantInt::get(Type::getInt32Ty(Ctx), 0);
86 Constant *i64 = ConstantInt::get(Type::getInt64Ty(Ctx),
87 std::numeric_limits<uint64_t>::max());
88 Constant *f16 = ConstantFP::getInfinity(Type::getHalfTy(Ctx));
89 Constant *f32 = ConstantFP::get(Type::getFloatTy(Ctx), 0.0);
90 Constant *f64 = ConstantFP::get(Type::getDoubleTy(Ctx), 123.45);
91 Constant *s = ConstantStruct::get(StructType::create(Ctx, "OpaqueStruct"));
92 Constant *a =
93 ConstantArray::get(ArrayType::get(i32->getType(), 2), {i32, i32});
94 Constant *v8i8 = ConstantVector::getSplat(ElementCount::getFixed(8), i8);
95 Constant *v4f16 = ConstantVector::getSplat(ElementCount::getFixed(4), f16);
96 Constant *p0i32 =
97 ConstantPointerNull::get(PointerType::get(i32->getType(), 0));
98
99 auto OnlyI32 = onlyType(i32->getType());
100 EXPECT_TRUE(OnlyI32.matches({}, i32));
101 EXPECT_FALSE(OnlyI32.matches({}, i64));
102 EXPECT_FALSE(OnlyI32.matches({}, p0i32));
103 EXPECT_FALSE(OnlyI32.matches({}, a));
104
105 EXPECT_THAT(OnlyI32.generate({}, {}),
106 AllOf(SizeIs(Ge(1u)), Each(TypesMatch(i32))));
107
108 auto AnyType = anyType();
109 EXPECT_TRUE(AnyType.matches({}, i1));
110 EXPECT_TRUE(AnyType.matches({}, f64));
111 EXPECT_TRUE(AnyType.matches({}, s));
112 EXPECT_TRUE(AnyType.matches({}, v8i8));
113 EXPECT_TRUE(AnyType.matches({}, p0i32));
114
115 EXPECT_THAT(
116 AnyType.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
117 Each(AnyOf(TypesMatch(i32), TypesMatch(f16), TypesMatch(v8i8))));
118
119 auto AnyInt = anyIntType();
120 EXPECT_TRUE(AnyInt.matches({}, i1));
121 EXPECT_TRUE(AnyInt.matches({}, i64));
122 EXPECT_FALSE(AnyInt.matches({}, f32));
123 EXPECT_FALSE(AnyInt.matches({}, v4f16));
124
125 EXPECT_THAT(
126 AnyInt.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
127 AllOf(SizeIs(Ge(1u)), Each(TypesMatch(i32))));
128
129 auto AnyFP = anyFloatType();
130 EXPECT_TRUE(AnyFP.matches({}, f16));
131 EXPECT_TRUE(AnyFP.matches({}, f32));
132 EXPECT_FALSE(AnyFP.matches({}, i16));
133 EXPECT_FALSE(AnyFP.matches({}, p0i32));
134 EXPECT_FALSE(AnyFP.matches({}, v4f16));
135
136 EXPECT_THAT(
137 AnyFP.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
138 AllOf(SizeIs(Ge(1u)), Each(TypesMatch(f16))));
139
140 auto AnyPtr = anyPtrType();
141 EXPECT_TRUE(AnyPtr.matches({}, p0i32));
142 EXPECT_FALSE(AnyPtr.matches({}, i8));
143 EXPECT_FALSE(AnyPtr.matches({}, a));
144 EXPECT_FALSE(AnyPtr.matches({}, v8i8));
145
146 auto isPointer = [](Value *V) { return V->getType()->isPointerTy(); };
147 EXPECT_THAT(
148 AnyPtr.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
149 AllOf(SizeIs(Ge(3u)), Each(Truly(isPointer))));
150
151 auto AnyVec = anyVectorType();
152 EXPECT_TRUE(AnyVec.matches({}, v8i8));
153 EXPECT_TRUE(AnyVec.matches({}, v4f16));
154 EXPECT_FALSE(AnyVec.matches({}, i8));
155 EXPECT_FALSE(AnyVec.matches({}, a));
156 EXPECT_FALSE(AnyVec.matches({}, s));
157
158 EXPECT_THAT(AnyVec.generate({}, {v8i8->getType()}),
159 ElementsAre(TypesMatch(v8i8)));
160
161 auto First = matchFirstType();
162 EXPECT_TRUE(First.matches({i8}, i8));
163 EXPECT_TRUE(First.matches({s, a}, s));
164 EXPECT_FALSE(First.matches({f16}, f32));
165 EXPECT_FALSE(First.matches({v4f16, f64}, f64));
166
167 EXPECT_THAT(First.generate({i8}, {}), Each(TypesMatch(i8)));
168 EXPECT_THAT(First.generate({f16}, {i8->getType()}), Each(TypesMatch(f16)));
169 EXPECT_THAT(First.generate({v8i8, i32}, {}), Each(TypesMatch(v8i8)));
170 }
171
172 TEST(OperationsTest, SplitBlock) {
173 LLVMContext Ctx;
174
175 Module M("M", Ctx);
176 Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
177 /*isVarArg=*/false),
178 GlobalValue::ExternalLinkage, "f", &M);
179 auto SBOp = fuzzerop::splitBlockDescriptor(1);
180
181 // Create a block with only a return and split it on the return.
182 auto *BB = BasicBlock::Create(Ctx, "BB", F);
183 auto *RI = ReturnInst::Create(Ctx, BB);
184 SBOp.BuilderFunc({UndefValue::get(Type::getInt1Ty(Ctx))}, RI);
185
186 // We should end up with an unconditional branch from BB to BB1, and the
187 // return ends up in BB1.
188 auto *UncondBr = cast<BranchInst>(BB->getTerminator());
189 ASSERT_TRUE(UncondBr->isUnconditional());
190 auto *BB1 = UncondBr->getSuccessor(0);
191 ASSERT_THAT(RI->getParent(), Eq(BB1));
192
193 // Now add an instruction to BB1 and split on that.
194 auto *AI = new AllocaInst(Type::getInt8Ty(Ctx), 0, "a", RI);
195 Value *Cond = ConstantInt::getFalse(Ctx);
196 SBOp.BuilderFunc({Cond}, AI);
197
198 // We should end up with a loop back on BB1 and the instruction we split on
199 // moves to BB2.
200 auto *CondBr = cast<BranchInst>(BB1->getTerminator());
201 EXPECT_THAT(CondBr->getCondition(), Eq(Cond));
202 ASSERT_THAT(CondBr->getNumSuccessors(), Eq(2u));
203 ASSERT_THAT(CondBr->getSuccessor(0), Eq(BB1));
204 auto *BB2 = CondBr->getSuccessor(1);
205 EXPECT_THAT(AI->getParent(), Eq(BB2));
206 EXPECT_THAT(RI->getParent(), Eq(BB2));
207
208 EXPECT_FALSE(verifyModule(M, &errs()));
209 }
210
211 TEST(OperationsTest, SplitEHBlock) {
212 // Check that we will not try to branch back to the landingpad block using
213 // regular branch instruction
214
215 LLVMContext Ctx;
216 const char *SourceCode =
217 "declare ptr @f()"
218 "declare i32 @personality_function()"
219 "define ptr @test() personality ptr @personality_function {\n"
220 "entry:\n"
221 " %val = invoke ptr @f()\n"
222 " to label %normal unwind label %exceptional\n"
223 "normal:\n"
224 " ret ptr %val\n"
225 "exceptional:\n"
226 " %landing_pad4 = landingpad token cleanup\n"
227 " ret ptr undef\n"
228 "}";
229 auto M = parseAssembly(SourceCode, Ctx);
230
231 // Get the landingpad block
232 BasicBlock &BB = *std::next(M->getFunction("test")->begin(), 2);
233
234 fuzzerop::OpDescriptor Descr = fuzzerop::splitBlockDescriptor(1);
235
236 Descr.BuilderFunc({ConstantInt::getTrue(Ctx)}, &*BB.getFirstInsertionPt());
237 ASSERT_TRUE(!verifyModule(*M, &errs()));
238 }
239
240 TEST(OperationsTest, SplitBlockWithPhis) {
241 LLVMContext Ctx;
242
243 Type *Int8Ty = Type::getInt8Ty(Ctx);
244
245 Module M("M", Ctx);
246 Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
247 /*isVarArg=*/false),
248 GlobalValue::ExternalLinkage, "f", &M);
249 auto SBOp = fuzzerop::splitBlockDescriptor(1);
250
251 // Create 3 blocks with an if-then branch.
252 auto *BB1 = BasicBlock::Create(Ctx, "BB1", F);
253 auto *BB2 = BasicBlock::Create(Ctx, "BB2", F);
254 auto *BB3 = BasicBlock::Create(Ctx, "BB3", F);
255 BranchInst::Create(BB2, BB3, ConstantInt::getFalse(Ctx), BB1);
256 BranchInst::Create(BB3, BB2);
257
258 // Set up phi nodes selecting values for the incoming edges.
259 auto *PHI1 = PHINode::Create(Int8Ty, /*NumReservedValues=*/2, "p1", BB3);
260 PHI1->addIncoming(ConstantInt::get(Int8Ty, 0), BB1);
261 PHI1->addIncoming(ConstantInt::get(Int8Ty, 1), BB2);
262 auto *PHI2 = PHINode::Create(Int8Ty, /*NumReservedValues=*/2, "p2", BB3);
263 PHI2->addIncoming(ConstantInt::get(Int8Ty, 1), BB1);
264 PHI2->addIncoming(ConstantInt::get(Int8Ty, 0), BB2);
265 auto *RI = ReturnInst::Create(Ctx, BB3);
266
267 // Now we split the block with PHI nodes, making sure they're all updated.
268 Value *Cond = ConstantInt::getFalse(Ctx);
269 SBOp.BuilderFunc({Cond}, RI);
270
271 // Make sure the PHIs are updated with a value for the third incoming edge.
272 EXPECT_THAT(PHI1->getNumIncomingValues(), Eq(3u));
273 EXPECT_THAT(PHI2->getNumIncomingValues(), Eq(3u));
274 EXPECT_FALSE(verifyModule(M, &errs()));
275 }
276
277 TEST(OperationsTest, GEP) {
278 LLVMContext Ctx;
279
280 Type *Int8PtrTy = Type::getInt8PtrTy(Ctx);
281 Type *Int32Ty = Type::getInt32Ty(Ctx);
282
283 Module M("M", Ctx);
284 Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
285 /*isVarArg=*/false),
286 GlobalValue::ExternalLinkage, "f", &M);
287 auto *BB = BasicBlock::Create(Ctx, "BB", F);
288 auto *RI = ReturnInst::Create(Ctx, BB);
289
290 auto GEPOp = fuzzerop::gepDescriptor(1);
291 EXPECT_TRUE(GEPOp.SourcePreds[0].matches({}, UndefValue::get(Int8PtrTy)));
292 EXPECT_TRUE(GEPOp.SourcePreds[1].matches({UndefValue::get(Int8PtrTy)},
293 ConstantInt::get(Int32Ty, 0)));
294
295 GEPOp.BuilderFunc({UndefValue::get(Int8PtrTy), ConstantInt::get(Int32Ty, 0)},
296 RI);
297 EXPECT_FALSE(verifyModule(M, &errs()));
298 }
299
300 TEST(OperationsTest, GEPPointerOperand) {
301 // Check that we only pick sized pointers for the GEP instructions
302
303 LLVMContext Ctx;
304 const char *SourceCode =
305 "%opaque = type opaque\n"
306 "declare void @f()\n"
307 "define void @test(%opaque %o) {\n"
308 " %a = alloca i64, i32 10\n"
309 " ret void\n"
310 "}";
311 auto M = parseAssembly(SourceCode, Ctx);
312
313 fuzzerop::OpDescriptor Descr = fuzzerop::gepDescriptor(1);
314
315 // Get first basic block of the test function
316 Function &F = *M->getFunction("test");
317 BasicBlock &BB = *F.begin();
318
319 // Don't match %o
320 ASSERT_FALSE(Descr.SourcePreds[0].matches({}, &*F.arg_begin()));
321
322 // Match %a
323 ASSERT_TRUE(Descr.SourcePreds[0].matches({}, &*BB.begin()));
324 }
325
326 TEST(OperationsTest, ExtractAndInsertValue) {
327 LLVMContext Ctx;
328
329 Type *Int8PtrTy = Type::getInt8PtrTy(Ctx);
330 Type *Int32Ty = Type::getInt32Ty(Ctx);
331 Type *Int64Ty = Type::getInt64Ty(Ctx);
332
333 Type *StructTy = StructType::create(Ctx, {Int8PtrTy, Int32Ty});
334 Type *OpaqueTy = StructType::create(Ctx, "OpaqueStruct");
335 Type *ZeroSizedArrayTy = ArrayType::get(Int64Ty, 0);
336 Type *ArrayTy = ArrayType::get(Int64Ty, 4);
337 Type *VectorTy = FixedVectorType::get(Int32Ty, 2);
338
339 auto EVOp = fuzzerop::extractValueDescriptor(1);
340 auto IVOp = fuzzerop::insertValueDescriptor(1);
341
342 // Sanity check the source preds.
343 Constant *SVal = UndefValue::get(StructTy);
344 Constant *OVal = UndefValue::get(OpaqueTy);
345 Constant *AVal = UndefValue::get(ArrayTy);
346 Constant *ZAVal = UndefValue::get(ZeroSizedArrayTy);
347 Constant *VVal = UndefValue::get(VectorTy);
348
349 EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, SVal));
350 EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, OVal));
351 EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, AVal));
352 EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, VVal));
353 EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, SVal));
354 EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, OVal));
355 EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, AVal));
356 EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, VVal));
357
358 // Don't consider zero sized arrays as viable sources
359 EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, ZAVal));
360 EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, ZAVal));
361
362 // Make sure we're range checking appropriately.
363 EXPECT_TRUE(
364 EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 0)));
365 EXPECT_TRUE(
366 EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 1)));
367 EXPECT_FALSE(
368 EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 2)));
369 EXPECT_FALSE(
370 EVOp.SourcePreds[1].matches({OVal}, ConstantInt::get(Int32Ty, 0)));
371 EXPECT_FALSE(
372 EVOp.SourcePreds[1].matches({OVal}, ConstantInt::get(Int32Ty, 65536)));
373 EXPECT_TRUE(
374 EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 0)));
375 EXPECT_TRUE(
376 EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 3)));
377 EXPECT_FALSE(
378 EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 4)));
379
380 EXPECT_THAT(
381 EVOp.SourcePreds[1].generate({SVal}, {}),
382 ElementsAre(ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 1)));
383
384 // InsertValue should accept any type in the struct, but only in positions
385 // where it makes sense.
386 EXPECT_TRUE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int8PtrTy)));
387 EXPECT_TRUE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int32Ty)));
388 EXPECT_FALSE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int64Ty)));
389 EXPECT_FALSE(IVOp.SourcePreds[2].matches({SVal, UndefValue::get(Int32Ty)},
390 ConstantInt::get(Int32Ty, 0)));
391 EXPECT_TRUE(IVOp.SourcePreds[2].matches({SVal, UndefValue::get(Int32Ty)},
392 ConstantInt::get(Int32Ty, 1)));
393
394 EXPECT_THAT(IVOp.SourcePreds[1].generate({SVal}, {}),
395 Each(AnyOf(HasType(Int32Ty), HasType(Int8PtrTy))));
396 EXPECT_THAT(
397 IVOp.SourcePreds[2].generate({SVal, ConstantInt::get(Int32Ty, 0)}, {}),
398 ElementsAre(ConstantInt::get(Int32Ty, 1)));
399 }
400
401 } // namespace