OperationsTest.cpp 13 KB

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