OperationsTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //===- OperationsTest.cpp - Tests for fuzzer operations -------------------===//
  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. #include "llvm/FuzzMutate/Operations.h"
  10. #include "llvm/FuzzMutate/OpDescriptor.h"
  11. #include "llvm/IR/Constants.h"
  12. #include "llvm/IR/Instructions.h"
  13. #include "llvm/IR/Module.h"
  14. #include "llvm/IR/Verifier.h"
  15. #include "gmock/gmock.h"
  16. #include "gtest/gtest.h"
  17. #include <iostream>
  18. // Define some pretty printers to help with debugging failures.
  19. namespace llvm {
  20. void PrintTo(Type *T, ::std::ostream *OS) {
  21. raw_os_ostream ROS(*OS);
  22. T->print(ROS);
  23. }
  24. void PrintTo(BasicBlock *BB, ::std::ostream *OS) {
  25. raw_os_ostream ROS(*OS);
  26. ROS << BB << " (" << BB->getName() << ")";
  27. }
  28. void PrintTo(Value *V, ::std::ostream *OS) {
  29. raw_os_ostream ROS(*OS);
  30. ROS << V << " (";
  31. V->print(ROS);
  32. ROS << ")";
  33. }
  34. void PrintTo(Constant *C, ::std::ostream *OS) { PrintTo(cast<Value>(C), OS); }
  35. } // namespace llvm
  36. using namespace llvm;
  37. using testing::AllOf;
  38. using testing::AnyOf;
  39. using testing::ElementsAre;
  40. using testing::Eq;
  41. using testing::Ge;
  42. using testing::Each;
  43. using testing::Truly;
  44. using testing::NotNull;
  45. using testing::PrintToString;
  46. using testing::SizeIs;
  47. MATCHER_P(TypesMatch, V, "has type " + PrintToString(V->getType())) {
  48. return arg->getType() == V->getType();
  49. }
  50. MATCHER_P(HasType, T, "") { return arg->getType() == T; }
  51. TEST(OperationsTest, SourcePreds) {
  52. using namespace llvm::fuzzerop;
  53. LLVMContext Ctx;
  54. Constant *i1 = ConstantInt::getFalse(Ctx);
  55. Constant *i8 = ConstantInt::get(Type::getInt8Ty(Ctx), 3);
  56. Constant *i16 = ConstantInt::get(Type::getInt16Ty(Ctx), 1 << 15);
  57. Constant *i32 = ConstantInt::get(Type::getInt32Ty(Ctx), 0);
  58. Constant *i64 = ConstantInt::get(Type::getInt64Ty(Ctx),
  59. std::numeric_limits<uint64_t>::max());
  60. Constant *f16 = ConstantFP::getInfinity(Type::getHalfTy(Ctx));
  61. Constant *f32 = ConstantFP::get(Type::getFloatTy(Ctx), 0.0);
  62. Constant *f64 = ConstantFP::get(Type::getDoubleTy(Ctx), 123.45);
  63. Constant *s =
  64. ConstantStruct::get(StructType::create(Ctx, "OpaqueStruct"));
  65. Constant *a =
  66. ConstantArray::get(ArrayType::get(i32->getType(), 2), {i32, i32});
  67. Constant *v8i8 = ConstantVector::getSplat(8, i8);
  68. Constant *v4f16 = ConstantVector::getSplat(4, f16);
  69. Constant *p0i32 =
  70. ConstantPointerNull::get(PointerType::get(i32->getType(), 0));
  71. auto OnlyI32 = onlyType(i32->getType());
  72. EXPECT_TRUE(OnlyI32.matches({}, i32));
  73. EXPECT_FALSE(OnlyI32.matches({}, i64));
  74. EXPECT_FALSE(OnlyI32.matches({}, p0i32));
  75. EXPECT_FALSE(OnlyI32.matches({}, a));
  76. EXPECT_THAT(OnlyI32.generate({}, {}),
  77. AllOf(SizeIs(Ge(1u)), Each(TypesMatch(i32))));
  78. auto AnyType = anyType();
  79. EXPECT_TRUE(AnyType.matches({}, i1));
  80. EXPECT_TRUE(AnyType.matches({}, f64));
  81. EXPECT_TRUE(AnyType.matches({}, s));
  82. EXPECT_TRUE(AnyType.matches({}, v8i8));
  83. EXPECT_TRUE(AnyType.matches({}, p0i32));
  84. EXPECT_THAT(
  85. AnyType.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
  86. Each(AnyOf(TypesMatch(i32), TypesMatch(f16), TypesMatch(v8i8))));
  87. auto AnyInt = anyIntType();
  88. EXPECT_TRUE(AnyInt.matches({}, i1));
  89. EXPECT_TRUE(AnyInt.matches({}, i64));
  90. EXPECT_FALSE(AnyInt.matches({}, f32));
  91. EXPECT_FALSE(AnyInt.matches({}, v4f16));
  92. EXPECT_THAT(
  93. AnyInt.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
  94. AllOf(SizeIs(Ge(1u)), Each(TypesMatch(i32))));
  95. auto AnyFP = anyFloatType();
  96. EXPECT_TRUE(AnyFP.matches({}, f16));
  97. EXPECT_TRUE(AnyFP.matches({}, f32));
  98. EXPECT_FALSE(AnyFP.matches({}, i16));
  99. EXPECT_FALSE(AnyFP.matches({}, p0i32));
  100. EXPECT_FALSE(AnyFP.matches({}, v4f16));
  101. EXPECT_THAT(
  102. AnyFP.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
  103. AllOf(SizeIs(Ge(1u)), Each(TypesMatch(f16))));
  104. auto AnyPtr = anyPtrType();
  105. EXPECT_TRUE(AnyPtr.matches({}, p0i32));
  106. EXPECT_FALSE(AnyPtr.matches({}, i8));
  107. EXPECT_FALSE(AnyPtr.matches({}, a));
  108. EXPECT_FALSE(AnyPtr.matches({}, v8i8));
  109. auto isPointer = [](Value *V) { return V->getType()->isPointerTy(); };
  110. EXPECT_THAT(
  111. AnyPtr.generate({}, {i32->getType(), f16->getType(), v8i8->getType()}),
  112. AllOf(SizeIs(Ge(3u)), Each(Truly(isPointer))));
  113. auto AnyVec = anyVectorType();
  114. EXPECT_TRUE(AnyVec.matches({}, v8i8));
  115. EXPECT_TRUE(AnyVec.matches({}, v4f16));
  116. EXPECT_FALSE(AnyVec.matches({}, i8));
  117. EXPECT_FALSE(AnyVec.matches({}, a));
  118. EXPECT_FALSE(AnyVec.matches({}, s));
  119. EXPECT_THAT(AnyVec.generate({}, {v8i8->getType()}),
  120. ElementsAre(TypesMatch(v8i8)));
  121. auto First = matchFirstType();
  122. EXPECT_TRUE(First.matches({i8}, i8));
  123. EXPECT_TRUE(First.matches({s, a}, s));
  124. EXPECT_FALSE(First.matches({f16}, f32));
  125. EXPECT_FALSE(First.matches({v4f16, f64}, f64));
  126. EXPECT_THAT(First.generate({i8}, {}), Each(TypesMatch(i8)));
  127. EXPECT_THAT(First.generate({f16}, {i8->getType()}),
  128. Each(TypesMatch(f16)));
  129. EXPECT_THAT(First.generate({v8i8, i32}, {}), Each(TypesMatch(v8i8)));
  130. }
  131. TEST(OperationsTest, SplitBlock) {
  132. LLVMContext Ctx;
  133. Module M("M", Ctx);
  134. Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
  135. /*isVarArg=*/false),
  136. GlobalValue::ExternalLinkage, "f", &M);
  137. auto SBOp = fuzzerop::splitBlockDescriptor(1);
  138. // Create a block with only a return and split it on the return.
  139. auto *BB = BasicBlock::Create(Ctx, "BB", F);
  140. auto *RI = ReturnInst::Create(Ctx, BB);
  141. SBOp.BuilderFunc({UndefValue::get(Type::getInt1Ty(Ctx))}, RI);
  142. // We should end up with an unconditional branch from BB to BB1, and the
  143. // return ends up in BB1.
  144. auto *UncondBr = cast<BranchInst>(BB->getTerminator());
  145. ASSERT_TRUE(UncondBr->isUnconditional());
  146. auto *BB1 = UncondBr->getSuccessor(0);
  147. ASSERT_THAT(RI->getParent(), Eq(BB1));
  148. // Now add an instruction to BB1 and split on that.
  149. auto *AI = new AllocaInst(Type::getInt8Ty(Ctx), 0, "a", RI);
  150. Value *Cond = ConstantInt::getFalse(Ctx);
  151. SBOp.BuilderFunc({Cond}, AI);
  152. // We should end up with a loop back on BB1 and the instruction we split on
  153. // moves to BB2.
  154. auto *CondBr = cast<BranchInst>(BB1->getTerminator());
  155. EXPECT_THAT(CondBr->getCondition(), Eq(Cond));
  156. ASSERT_THAT(CondBr->getNumSuccessors(), Eq(2u));
  157. ASSERT_THAT(CondBr->getSuccessor(0), Eq(BB1));
  158. auto *BB2 = CondBr->getSuccessor(1);
  159. EXPECT_THAT(AI->getParent(), Eq(BB2));
  160. EXPECT_THAT(RI->getParent(), Eq(BB2));
  161. EXPECT_FALSE(verifyModule(M, &errs()));
  162. }
  163. TEST(OperationsTest, SplitBlockWithPhis) {
  164. LLVMContext Ctx;
  165. Type *Int8Ty = Type::getInt8Ty(Ctx);
  166. Module M("M", Ctx);
  167. Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
  168. /*isVarArg=*/false),
  169. GlobalValue::ExternalLinkage, "f", &M);
  170. auto SBOp = fuzzerop::splitBlockDescriptor(1);
  171. // Create 3 blocks with an if-then branch.
  172. auto *BB1 = BasicBlock::Create(Ctx, "BB1", F);
  173. auto *BB2 = BasicBlock::Create(Ctx, "BB2", F);
  174. auto *BB3 = BasicBlock::Create(Ctx, "BB3", F);
  175. BranchInst::Create(BB2, BB3, ConstantInt::getFalse(Ctx), BB1);
  176. BranchInst::Create(BB3, BB2);
  177. // Set up phi nodes selecting values for the incoming edges.
  178. auto *PHI1 = PHINode::Create(Int8Ty, /*NumReservedValues=*/2, "p1", BB3);
  179. PHI1->addIncoming(ConstantInt::get(Int8Ty, 0), BB1);
  180. PHI1->addIncoming(ConstantInt::get(Int8Ty, 1), BB2);
  181. auto *PHI2 = PHINode::Create(Int8Ty, /*NumReservedValues=*/2, "p2", BB3);
  182. PHI2->addIncoming(ConstantInt::get(Int8Ty, 1), BB1);
  183. PHI2->addIncoming(ConstantInt::get(Int8Ty, 0), BB2);
  184. auto *RI = ReturnInst::Create(Ctx, BB3);
  185. // Now we split the block with PHI nodes, making sure they're all updated.
  186. Value *Cond = ConstantInt::getFalse(Ctx);
  187. SBOp.BuilderFunc({Cond}, RI);
  188. // Make sure the PHIs are updated with a value for the third incoming edge.
  189. EXPECT_THAT(PHI1->getNumIncomingValues(), Eq(3u));
  190. EXPECT_THAT(PHI2->getNumIncomingValues(), Eq(3u));
  191. EXPECT_FALSE(verifyModule(M, &errs()));
  192. }
  193. TEST(OperationsTest, GEP) {
  194. LLVMContext Ctx;
  195. Type *Int8PtrTy = Type::getInt8PtrTy(Ctx);
  196. Type *Int32Ty = Type::getInt32Ty(Ctx);
  197. Module M("M", Ctx);
  198. Function *F = Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {},
  199. /*isVarArg=*/false),
  200. GlobalValue::ExternalLinkage, "f", &M);
  201. auto *BB = BasicBlock::Create(Ctx, "BB", F);
  202. auto *RI = ReturnInst::Create(Ctx, BB);
  203. auto GEPOp = fuzzerop::gepDescriptor(1);
  204. EXPECT_TRUE(GEPOp.SourcePreds[0].matches({}, UndefValue::get(Int8PtrTy)));
  205. EXPECT_TRUE(GEPOp.SourcePreds[1].matches({UndefValue::get(Int8PtrTy)},
  206. ConstantInt::get(Int32Ty, 0)));
  207. GEPOp.BuilderFunc({UndefValue::get(Int8PtrTy), ConstantInt::get(Int32Ty, 0)},
  208. RI);
  209. EXPECT_FALSE(verifyModule(M, &errs()));
  210. }
  211. TEST(OperationsTest, ExtractAndInsertValue) {
  212. LLVMContext Ctx;
  213. Type *Int8PtrTy = Type::getInt8PtrTy(Ctx);
  214. Type *Int32Ty = Type::getInt32Ty(Ctx);
  215. Type *Int64Ty = Type::getInt64Ty(Ctx);
  216. Type *StructTy = StructType::create(Ctx, {Int8PtrTy, Int32Ty});
  217. Type *OpaqueTy = StructType::create(Ctx, "OpaqueStruct");
  218. Type *ArrayTy = ArrayType::get(Int64Ty, 4);
  219. Type *VectorTy = VectorType::get(Int32Ty, 2);
  220. auto EVOp = fuzzerop::extractValueDescriptor(1);
  221. auto IVOp = fuzzerop::insertValueDescriptor(1);
  222. // Sanity check the source preds.
  223. Constant *SVal = UndefValue::get(StructTy);
  224. Constant *OVal = UndefValue::get(OpaqueTy);
  225. Constant *AVal = UndefValue::get(ArrayTy);
  226. Constant *VVal = UndefValue::get(VectorTy);
  227. EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, SVal));
  228. EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, OVal));
  229. EXPECT_TRUE(EVOp.SourcePreds[0].matches({}, AVal));
  230. EXPECT_FALSE(EVOp.SourcePreds[0].matches({}, VVal));
  231. EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, SVal));
  232. EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, OVal));
  233. EXPECT_TRUE(IVOp.SourcePreds[0].matches({}, AVal));
  234. EXPECT_FALSE(IVOp.SourcePreds[0].matches({}, VVal));
  235. // Make sure we're range checking appropriately.
  236. EXPECT_TRUE(
  237. EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 0)));
  238. EXPECT_TRUE(
  239. EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 1)));
  240. EXPECT_FALSE(
  241. EVOp.SourcePreds[1].matches({SVal}, ConstantInt::get(Int32Ty, 2)));
  242. EXPECT_FALSE(
  243. EVOp.SourcePreds[1].matches({OVal}, ConstantInt::get(Int32Ty, 0)));
  244. EXPECT_FALSE(
  245. EVOp.SourcePreds[1].matches({OVal}, ConstantInt::get(Int32Ty, 65536)));
  246. EXPECT_TRUE(
  247. EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 0)));
  248. EXPECT_TRUE(
  249. EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 3)));
  250. EXPECT_FALSE(
  251. EVOp.SourcePreds[1].matches({AVal}, ConstantInt::get(Int32Ty, 4)));
  252. EXPECT_THAT(
  253. EVOp.SourcePreds[1].generate({SVal}, {}),
  254. ElementsAre(ConstantInt::get(Int32Ty, 0), ConstantInt::get(Int32Ty, 1)));
  255. // InsertValue should accept any type in the struct, but only in positions
  256. // where it makes sense.
  257. EXPECT_TRUE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int8PtrTy)));
  258. EXPECT_TRUE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int32Ty)));
  259. EXPECT_FALSE(IVOp.SourcePreds[1].matches({SVal}, UndefValue::get(Int64Ty)));
  260. EXPECT_FALSE(IVOp.SourcePreds[2].matches({SVal, UndefValue::get(Int32Ty)},
  261. ConstantInt::get(Int32Ty, 0)));
  262. EXPECT_TRUE(IVOp.SourcePreds[2].matches({SVal, UndefValue::get(Int32Ty)},
  263. ConstantInt::get(Int32Ty, 1)));
  264. EXPECT_THAT(IVOp.SourcePreds[1].generate({SVal}, {}),
  265. Each(AnyOf(HasType(Int32Ty), HasType(Int8PtrTy))));
  266. EXPECT_THAT(
  267. IVOp.SourcePreds[2].generate({SVal, ConstantInt::get(Int32Ty, 0)}, {}),
  268. ElementsAre(ConstantInt::get(Int32Ty, 1)));
  269. }