StrategiesTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //===- InjectorIRStrategyTest.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/ADT/StringRef.h"
  9. #include "llvm/AsmParser/Parser.h"
  10. #include "llvm/AsmParser/SlotMapping.h"
  11. #include "llvm/FuzzMutate/IRMutator.h"
  12. #include "llvm/FuzzMutate/Operations.h"
  13. #include "llvm/IR/Instructions.h"
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "llvm/IR/Module.h"
  16. #include "llvm/IR/Verifier.h"
  17. #include "llvm/Support/SourceMgr.h"
  18. #include "gtest/gtest.h"
  19. using namespace llvm;
  20. static constexpr int Seed = 5;
  21. namespace {
  22. std::unique_ptr<IRMutator> createInjectorMutator() {
  23. std::vector<TypeGetter> Types{
  24. Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty,
  25. Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy};
  26. std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
  27. Strategies.push_back(
  28. std::make_unique<InjectorIRStrategy>(
  29. InjectorIRStrategy::getDefaultOps()));
  30. return std::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
  31. }
  32. std::unique_ptr<IRMutator> createDeleterMutator() {
  33. std::vector<TypeGetter> Types{
  34. Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty,
  35. Type::getInt64Ty, Type::getFloatTy, Type::getDoubleTy};
  36. std::vector<std::unique_ptr<IRMutationStrategy>> Strategies;
  37. Strategies.push_back(std::make_unique<InstDeleterIRStrategy>());
  38. return std::make_unique<IRMutator>(std::move(Types), std::move(Strategies));
  39. }
  40. std::unique_ptr<Module> parseAssembly(
  41. const char *Assembly, LLVMContext &Context) {
  42. SMDiagnostic Error;
  43. std::unique_ptr<Module> M = parseAssemblyString(Assembly, Error, Context);
  44. std::string ErrMsg;
  45. raw_string_ostream OS(ErrMsg);
  46. Error.print("", OS);
  47. assert(M && !verifyModule(*M, &errs()));
  48. return M;
  49. }
  50. void IterateOnSource(StringRef Source, IRMutator &Mutator) {
  51. LLVMContext Ctx;
  52. for (int i = 0; i < 10; ++i) {
  53. auto M = parseAssembly(Source.data(), Ctx);
  54. ASSERT_TRUE(M && !verifyModule(*M, &errs()));
  55. Mutator.mutateModule(*M, Seed, Source.size(), Source.size() + 100);
  56. EXPECT_TRUE(!verifyModule(*M, &errs()));
  57. }
  58. }
  59. TEST(InjectorIRStrategyTest, EmptyModule) {
  60. // Test that we can inject into empty module
  61. LLVMContext Ctx;
  62. auto M = std::make_unique<Module>("M", Ctx);
  63. ASSERT_TRUE(M && !verifyModule(*M, &errs()));
  64. auto Mutator = createInjectorMutator();
  65. ASSERT_TRUE(Mutator);
  66. Mutator->mutateModule(*M, Seed, 1, 1);
  67. EXPECT_TRUE(!verifyModule(*M, &errs()));
  68. }
  69. TEST(InstDeleterIRStrategyTest, EmptyFunction) {
  70. // Test that we don't crash even if we can't remove from one of the functions.
  71. StringRef Source = ""
  72. "define <8 x i32> @func1() {\n"
  73. "ret <8 x i32> undef\n"
  74. "}\n"
  75. "\n"
  76. "define i32 @func2() {\n"
  77. "%A9 = alloca i32\n"
  78. "%L6 = load i32, i32* %A9\n"
  79. "ret i32 %L6\n"
  80. "}\n";
  81. auto Mutator = createDeleterMutator();
  82. ASSERT_TRUE(Mutator);
  83. IterateOnSource(Source, *Mutator);
  84. }
  85. TEST(InstDeleterIRStrategyTest, PhiNodes) {
  86. // Test that inst deleter works correctly with the phi nodes.
  87. LLVMContext Ctx;
  88. StringRef Source = "\n\
  89. define i32 @earlyreturncrash(i32 %x) {\n\
  90. entry:\n\
  91. switch i32 %x, label %sw.epilog [\n\
  92. i32 1, label %sw.bb1\n\
  93. ]\n\
  94. \n\
  95. sw.bb1:\n\
  96. br label %sw.epilog\n\
  97. \n\
  98. sw.epilog:\n\
  99. %a.0 = phi i32 [ 7, %entry ], [ 9, %sw.bb1 ]\n\
  100. %b.0 = phi i32 [ 10, %entry ], [ 4, %sw.bb1 ]\n\
  101. ret i32 %a.0\n\
  102. }";
  103. auto Mutator = createDeleterMutator();
  104. ASSERT_TRUE(Mutator);
  105. IterateOnSource(Source, *Mutator);
  106. }
  107. }