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