RandomIRBuilderTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //===- RandomIRBuilderTest.cpp - Tests for injector strategy --------------===//
  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/RandomIRBuilder.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/AsmParser/Parser.h"
  12. #include "llvm/AsmParser/SlotMapping.h"
  13. #include "llvm/FuzzMutate/IRMutator.h"
  14. #include "llvm/FuzzMutate/OpDescriptor.h"
  15. #include "llvm/FuzzMutate/Operations.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/IR/Verifier.h"
  21. #include "llvm/Support/SourceMgr.h"
  22. #include "gtest/gtest.h"
  23. using namespace llvm;
  24. static constexpr int Seed = 5;
  25. namespace {
  26. std::unique_ptr<Module> parseAssembly(
  27. const char *Assembly, LLVMContext &Context) {
  28. SMDiagnostic Error;
  29. std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
  30. std::string ErrMsg;
  31. raw_string_ostream OS(ErrMsg);
  32. Error.print("", OS);
  33. assert(M && !verifyModule(*M, &errs()));
  34. return M;
  35. }
  36. TEST(RandomIRBuilderTest, ShuffleVectorIncorrectOperands) {
  37. // Test that we don't create load instruction as a source for the shuffle
  38. // vector operation.
  39. LLVMContext Ctx;
  40. const char *Source =
  41. "define <2 x i32> @test(<2 x i1> %cond, <2 x i32> %a) {\n"
  42. " %A = alloca <2 x i32>\n"
  43. " %I = insertelement <2 x i32> %a, i32 1, i32 1\n"
  44. " ret <2 x i32> undef\n"
  45. "}";
  46. auto M = parseAssembly(Source, Ctx);
  47. fuzzerop::OpDescriptor Descr = fuzzerop::shuffleVectorDescriptor(1);
  48. // Empty known types since we ShuffleVector descriptor doesn't care about them
  49. RandomIRBuilder IB(Seed, {});
  50. // Get first basic block of the first function
  51. Function &F = *M->begin();
  52. BasicBlock &BB = *F.begin();
  53. SmallVector<Instruction *, 32> Insts;
  54. for (auto I = BB.getFirstInsertionPt(), E = BB.end(); I != E; ++I)
  55. Insts.push_back(&*I);
  56. // Pick first and second sources
  57. SmallVector<Value *, 2> Srcs;
  58. ASSERT_TRUE(Descr.SourcePreds[0].matches(Srcs, Insts[1]));
  59. Srcs.push_back(Insts[1]);
  60. ASSERT_TRUE(Descr.SourcePreds[1].matches(Srcs, Insts[1]));
  61. Srcs.push_back(Insts[1]);
  62. // Create new source. Check that it always matches with the descriptor.
  63. // Run some iterations to account for random decisions.
  64. for (int i = 0; i < 10; ++i) {
  65. Value *LastSrc = IB.newSource(BB, Insts, Srcs, Descr.SourcePreds[2]);
  66. ASSERT_TRUE(Descr.SourcePreds[2].matches(Srcs, LastSrc));
  67. }
  68. }
  69. TEST(RandomIRBuilderTest, InsertValueIndexes) {
  70. // Check that we will generate correct indexes for the insertvalue operation
  71. LLVMContext Ctx;
  72. const char *Source =
  73. "%T = type {i8, i32, i64}\n"
  74. "define void @test() {\n"
  75. " %A = alloca %T\n"
  76. " %L = load %T, %T* %A"
  77. " ret void\n"
  78. "}";
  79. auto M = parseAssembly(Source, Ctx);
  80. fuzzerop::OpDescriptor IVDescr = fuzzerop::insertValueDescriptor(1);
  81. std::vector<Type *> Types =
  82. {Type::getInt8Ty(Ctx), Type::getInt32Ty(Ctx), Type::getInt64Ty(Ctx)};
  83. RandomIRBuilder IB(Seed, Types);
  84. // Get first basic block of the first function
  85. Function &F = *M->begin();
  86. BasicBlock &BB = *F.begin();
  87. // Pick first source
  88. Instruction *Src = &*std::next(BB.begin());
  89. SmallVector<Value *, 2> Srcs(2);
  90. ASSERT_TRUE(IVDescr.SourcePreds[0].matches({}, Src));
  91. Srcs[0] = Src;
  92. // Generate constants for each of the types and check that we pick correct
  93. // index for the given type
  94. for (auto *T: Types) {
  95. // Loop to account for possible random decisions
  96. for (int i = 0; i < 10; ++i) {
  97. // Create value we want to insert. Only it's type matters.
  98. Srcs[1] = ConstantInt::get(T, 5);
  99. // Try to pick correct index
  100. Value *Src = IB.findOrCreateSource(
  101. BB, &*BB.begin(), Srcs, IVDescr.SourcePreds[2]);
  102. ASSERT_TRUE(IVDescr.SourcePreds[2].matches(Srcs, Src));
  103. }
  104. }
  105. }
  106. }