OperationsTest.cpp 14 KB

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