RandomIRBuilderTest.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //===- RandomIRBuilderTest.cpp - Tests for injector strategy --------------===//
  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/RandomIRBuilder.h"
  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/OpDescriptor.h"
  14. #include "llvm/FuzzMutate/Operations.h"
  15. #include "llvm/IR/Constants.h"
  16. #include "llvm/IR/Instructions.h"
  17. #include "llvm/IR/LLVMContext.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/IR/Verifier.h"
  20. #include "llvm/Support/SourceMgr.h"
  21. #include "gtest/gtest.h"
  22. using namespace llvm;
  23. static constexpr int Seed = 5;
  24. namespace {
  25. std::unique_ptr<Module> parseAssembly(
  26. const char *Assembly, LLVMContext &Context) {
  27. SMDiagnostic Error;
  28. std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
  29. std::string ErrMsg;
  30. raw_string_ostream OS(ErrMsg);
  31. Error.print("", OS);
  32. assert(M && !verifyModule(*M, &errs()));
  33. return M;
  34. }
  35. TEST(RandomIRBuilderTest, ShuffleVectorIncorrectOperands) {
  36. // Test that we don't create load instruction as a source for the shuffle
  37. // vector operation.
  38. LLVMContext Ctx;
  39. const char *Source =
  40. "define <2 x i32> @test(<2 x i1> %cond, <2 x i32> %a) {\n"
  41. " %A = alloca <2 x i32>\n"
  42. " %I = insertelement <2 x i32> %a, i32 1, i32 1\n"
  43. " ret <2 x i32> undef\n"
  44. "}";
  45. auto M = parseAssembly(Source, Ctx);
  46. fuzzerop::OpDescriptor Descr = fuzzerop::shuffleVectorDescriptor(1);
  47. // Empty known types since we ShuffleVector descriptor doesn't care about them
  48. RandomIRBuilder IB(Seed, {});
  49. // Get first basic block of the first function
  50. Function &F = *M->begin();
  51. BasicBlock &BB = *F.begin();
  52. SmallVector<Instruction *, 32> Insts;
  53. for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I)
  54. Insts.push_back(&*I);
  55. // Pick first and second sources
  56. SmallVector<Value *, 2> Srcs;
  57. ASSERT_TRUE(Descr.SourcePreds[0].matches(Srcs, Insts[1]));
  58. Srcs.push_back(Insts[1]);
  59. ASSERT_TRUE(Descr.SourcePreds[1].matches(Srcs, Insts[1]));
  60. Srcs.push_back(Insts[1]);
  61. // Create new source. Check that it always matches with the descriptor.
  62. // Run some iterations to account for random decisions.
  63. for (int i = 0; i < 10; ++i) {
  64. Value *LastSrc = IB.newSource(BB, Insts, Srcs, Descr.SourcePreds[2]);
  65. ASSERT_TRUE(Descr.SourcePreds[2].matches(Srcs, LastSrc));
  66. }
  67. }
  68. TEST(RandomIRBuilderTest, InsertValueIndexes) {
  69. // Check that we will generate correct indexes for the insertvalue operation
  70. LLVMContext Ctx;
  71. const char *Source =
  72. "%T = type {i8, i32, i64}\n"
  73. "define void @test() {\n"
  74. " %A = alloca %T\n"
  75. " %L = load %T, %T* %A"
  76. " ret void\n"
  77. "}";
  78. auto M = parseAssembly(Source, Ctx);
  79. fuzzerop::OpDescriptor IVDescr = fuzzerop::insertValueDescriptor(1);
  80. std::vector<Type *> Types =
  81. {Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), Type::getInt64Ty(Ctx)};
  82. RandomIRBuilder IB(Seed, Types);
  83. // Get first basic block of the first function
  84. Function &F = *M->begin();
  85. BasicBlock &BB = *F.begin();
  86. // Pick first source
  87. Instruction *Src = &*std::next(BB.begin());
  88. SmallVector<Value *, 2> Srcs(2);
  89. ASSERT_TRUE(IVDescr.SourcePreds[0].matches({}, Src));
  90. Srcs[0] = Src;
  91. // Generate constants for each of the types and check that we pick correct
  92. // index for the given type
  93. for (auto *T: Types) {
  94. // Loop to account for possible random decisions
  95. for (int i = 0; i < 10; ++i) {
  96. // Create value we want to insert. Only it's type matters.
  97. Srcs[1] = ConstantInt::get(T, 5);
  98. // Try to pick correct index
  99. Value *Src = IB.findOrCreateSource(
  100. BB, &*BB.begin(), Srcs, IVDescr.SourcePreds[2]);
  101. ASSERT_TRUE(IVDescr.SourcePreds[2].matches(Srcs, Src));
  102. }
  103. }
  104. }
  105. TEST(RandomIRBuilderTest, ShuffleVectorSink) {
  106. // Check that we will never use shuffle vector mask as a sink form the
  107. // unrelated operation.
  108. LLVMContext Ctx;
  109. const char *SourceCode =
  110. "define void @test(<4 x i32> %a) {\n"
  111. " %S1 = shufflevector <4 x i32> %a, <4 x i32> %a, <4 x i32> undef\n"
  112. " %S2 = shufflevector <4 x i32> %a, <4 x i32> %a, <4 x i32> undef\n"
  113. " ret void\n"
  114. "}";
  115. auto M = parseAssembly(SourceCode, Ctx);
  116. fuzzerop::OpDescriptor IVDescr = fuzzerop::insertValueDescriptor(1);
  117. RandomIRBuilder IB(Seed, {});
  118. // Get first basic block of the first function
  119. Function &F = *M->begin();
  120. BasicBlock &BB = *F.begin();
  121. // Source is %S1
  122. Instruction *Source = &*BB.begin();
  123. // Sink is %S2
  124. SmallVector<Instruction *, 1> Sinks = {&*std::next(BB.begin())};
  125. // Loop to account for random decisions
  126. for (int i = 0; i < 10; ++i) {
  127. // Try to connect S1 to S2. We should always create new sink.
  128. IB.connectToSink(BB, Sinks, Source);
  129. ASSERT_TRUE(!verifyModule(*M, &errs()));
  130. }
  131. }
  132. TEST(RandomIRBuilderTest, InsertValueArray) {
  133. // Check that we can generate insertvalue for the vector operations
  134. LLVMContext Ctx;
  135. const char *SourceCode =
  136. "define void @test() {\n"
  137. " %A = alloca [8 x i32]\n"
  138. " %L = load [8 x i32], [8 x i32]* %A"
  139. " ret void\n"
  140. "}";
  141. auto M = parseAssembly(SourceCode, Ctx);
  142. fuzzerop::OpDescriptor Descr = fuzzerop::insertValueDescriptor(1);
  143. std::vector<Type *> Types =
  144. {Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), Type::getInt64Ty(Ctx)};
  145. RandomIRBuilder IB(Seed, Types);
  146. // Get first basic block of the first function
  147. Function &F = *M->begin();
  148. BasicBlock &BB = *F.begin();
  149. // Pick first source
  150. Instruction *Source = &*std::next(BB.begin());
  151. ASSERT_TRUE(Descr.SourcePreds[0].matches({}, Source));
  152. SmallVector<Value *, 2> Srcs(2);
  153. // Check that we can always pick the last two operands.
  154. for (int i = 0; i < 10; ++i) {
  155. Srcs[0] = Source;
  156. Srcs[1] = IB.findOrCreateSource(BB, {Source}, Srcs, Descr.SourcePreds[1]);
  157. IB.findOrCreateSource(BB, {}, Srcs, Descr.SourcePreds[2]);
  158. }
  159. }
  160. TEST(RandomIRBuilderTest, Invokes) {
  161. // Check that we never generate load or store after invoke instruction
  162. LLVMContext Ctx;
  163. const char *SourceCode =
  164. "declare i32* @f()"
  165. "declare i32 @personality_function()"
  166. "define i32* @test() personality i32 ()* @personality_function {\n"
  167. "entry:\n"
  168. " %val = invoke i32* @f()\n"
  169. " to label %normal unwind label %exceptional\n"
  170. "normal:\n"
  171. " ret i32* %val\n"
  172. "exceptional:\n"
  173. " %landing_pad4 = landingpad token cleanup\n"
  174. " ret i32* undef\n"
  175. "}";
  176. auto M = parseAssembly(SourceCode, Ctx);
  177. std::vector<Type *> Types = {Type::getInt8Ty(Ctx)};
  178. RandomIRBuilder IB(Seed, Types);
  179. // Get first basic block of the test function
  180. Function &F = *M->getFunction("test");
  181. BasicBlock &BB = *F.begin();
  182. Instruction *Invoke = &*BB.begin();
  183. // Find source but never insert new load after invoke
  184. for (int i = 0; i < 10; ++i) {
  185. (void)IB.findOrCreateSource(BB, {Invoke}, {}, fuzzerop::anyIntType());
  186. ASSERT_TRUE(!verifyModule(*M, &errs()));
  187. }
  188. }
  189. TEST(RandomIRBuilderTest, FirstClassTypes) {
  190. // Check that we never insert new source as a load from non first class
  191. // or unsized type.
  192. LLVMContext Ctx;
  193. const char *SourceCode = "%Opaque = type opaque\n"
  194. "define void @test(i8* %ptr) {\n"
  195. "entry:\n"
  196. " %tmp = bitcast i8* %ptr to i32* (i32*)*\n"
  197. " %tmp1 = bitcast i8* %ptr to %Opaque*\n"
  198. " ret void\n"
  199. "}";
  200. auto M = parseAssembly(SourceCode, Ctx);
  201. std::vector<Type *> Types = {Type::getInt8Ty(Ctx)};
  202. RandomIRBuilder IB(Seed, Types);
  203. Function &F = *M->getFunction("test");
  204. BasicBlock &BB = *F.begin();
  205. // Non first class type
  206. Instruction *FuncPtr = &*BB.begin();
  207. // Unsized type
  208. Instruction *OpaquePtr = &*std::next(BB.begin());
  209. for (int i = 0; i < 10; ++i) {
  210. Value *V = IB.findOrCreateSource(BB, {FuncPtr, OpaquePtr});
  211. ASSERT_FALSE(isa<LoadInst>(V));
  212. }
  213. }
  214. TEST(RandomIRBuilderTest, SwiftError) {
  215. // Check that we never pick swifterror value as a source for operation
  216. // other than load, store and call.
  217. LLVMContext Ctx;
  218. const char *SourceCode = "declare void @use(i8** swifterror %err)"
  219. "define void @test() {\n"
  220. "entry:\n"
  221. " %err = alloca swifterror i8*, align 8\n"
  222. " call void @use(i8** swifterror %err)\n"
  223. " ret void\n"
  224. "}";
  225. auto M = parseAssembly(SourceCode, Ctx);
  226. std::vector<Type *> Types = {Type::getInt8Ty(Ctx)};
  227. RandomIRBuilder IB(Seed, Types);
  228. // Get first basic block of the test function
  229. Function &F = *M->getFunction("test");
  230. BasicBlock &BB = *F.begin();
  231. Instruction *Alloca = &*BB.begin();
  232. fuzzerop::OpDescriptor Descr = fuzzerop::gepDescriptor(1);
  233. for (int i = 0; i < 10; ++i) {
  234. Value *V = IB.findOrCreateSource(BB, {Alloca}, {}, Descr.SourcePreds[0]);
  235. ASSERT_FALSE(isa<AllocaInst>(V));
  236. }
  237. }
  238. }