Ilya Biryukov | aa981c1 | 2019-11-21 11:32:17 +0100 | [diff] [blame^] | 1 | //===- unittests/Driver/SanitizerArgsTest.cpp -----------------------------===// |
| 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 | |
| 9 | #include "clang/Basic/Diagnostic.h" |
| 10 | #include "clang/Basic/DiagnosticIDs.h" |
| 11 | #include "clang/Basic/DiagnosticOptions.h" |
| 12 | #include "clang/Driver/Compilation.h" |
| 13 | #include "clang/Driver/Driver.h" |
| 14 | #include "clang/Driver/Job.h" |
| 15 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 16 | #include "llvm/ADT/ArrayRef.h" |
| 17 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
| 18 | #include "llvm/ADT/Optional.h" |
| 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/Support/Host.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Support/Path.h" |
| 24 | #include "llvm/Support/VirtualFileSystem.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | #include "gmock/gmock.h" |
| 27 | #include "gtest/gtest.h" |
| 28 | #include <cstdlib> |
| 29 | #include <memory> |
| 30 | #include <string> |
| 31 | using namespace clang; |
| 32 | using namespace clang::driver; |
| 33 | |
| 34 | using ::testing::Contains; |
| 35 | using ::testing::StrEq; |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | static constexpr const char *ClangBinary = "clang"; |
| 40 | static constexpr const char *InputFile = "/sources/foo.c"; |
| 41 | |
| 42 | std::string concatPaths(llvm::ArrayRef<StringRef> Components) { |
| 43 | llvm::SmallString<128> P; |
| 44 | for (StringRef C : Components) |
| 45 | llvm::sys::path::append(P, C); |
| 46 | return P.str().str(); |
| 47 | } |
| 48 | |
| 49 | class SanitizerArgsTest : public ::testing::Test { |
| 50 | protected: |
| 51 | const Command &emulateSingleCompilation(std::vector<std::string> ExtraArgs, |
| 52 | std::vector<std::string> ExtraFiles) { |
| 53 | assert(!Driver && "Running twice is not allowed"); |
| 54 | |
| 55 | llvm::IntrusiveRefCntPtr<DiagnosticOptions> Opts = new DiagnosticOptions; |
| 56 | DiagnosticsEngine Diags( |
| 57 | new DiagnosticIDs, Opts, |
| 58 | new TextDiagnosticPrinter(llvm::errs(), Opts.get())); |
| 59 | Driver.emplace(ClangBinary, "x86_64-unknown-linux-gnu", Diags, |
| 60 | prepareFS(ExtraFiles)); |
| 61 | |
| 62 | std::vector<const char *> Args = {ClangBinary}; |
| 63 | for (const auto &A : ExtraArgs) |
| 64 | Args.push_back(A.c_str()); |
| 65 | Args.push_back("-c"); |
| 66 | Args.push_back(InputFile); |
| 67 | |
| 68 | Compilation.reset(Driver->BuildCompilation(Args)); |
| 69 | |
| 70 | if (Diags.hasErrorOccurred()) |
| 71 | ADD_FAILURE() << "Error occurred while parsing compilation arguments. " |
| 72 | "See stderr for details."; |
| 73 | |
| 74 | const auto &Commands = Compilation->getJobs().getJobs(); |
| 75 | assert(Commands.size() == 1); |
| 76 | return *Commands.front(); |
| 77 | } |
| 78 | |
| 79 | private: |
| 80 | llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> |
| 81 | prepareFS(llvm::ArrayRef<std::string> ExtraFiles) { |
| 82 | llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS = |
| 83 | new llvm::vfs::InMemoryFileSystem; |
| 84 | FS->addFile(ClangBinary, time_t(), llvm::MemoryBuffer::getMemBuffer("")); |
| 85 | FS->addFile(InputFile, time_t(), llvm::MemoryBuffer::getMemBuffer("")); |
| 86 | for (llvm::StringRef F : ExtraFiles) |
| 87 | FS->addFile(F, time_t(), llvm::MemoryBuffer::getMemBuffer("")); |
| 88 | return FS; |
| 89 | } |
| 90 | |
| 91 | llvm::Optional<Driver> Driver; |
| 92 | std::unique_ptr<driver::Compilation> Compilation; |
| 93 | }; |
| 94 | |
| 95 | TEST_F(SanitizerArgsTest, Blacklists) { |
| 96 | const std::string ResourceDir = "/opt/llvm/lib/resources"; |
| 97 | const std::string UserBlacklist = "/source/my_blacklist.txt"; |
| 98 | const std::string ASanBlacklist = |
| 99 | concatPaths({ResourceDir, "share", "asan_blacklist.txt"}); |
| 100 | |
| 101 | auto &Command = emulateSingleCompilation( |
| 102 | /*ExtraArgs=*/{"-fsanitize=address", "-resource-dir", ResourceDir, |
| 103 | std::string("-fsanitize-blacklist=") + UserBlacklist}, |
| 104 | /*ExtraFiles=*/{ASanBlacklist, UserBlacklist}); |
| 105 | |
| 106 | // System blacklists are added based on resource-dir. |
| 107 | EXPECT_THAT(Command.getArguments(), |
| 108 | Contains(StrEq(std::string("-fsanitize-system-blacklist=") + |
| 109 | ASanBlacklist))); |
| 110 | // User blacklists should also be added. |
| 111 | EXPECT_THAT( |
| 112 | Command.getArguments(), |
| 113 | Contains(StrEq(std::string("-fsanitize-blacklist=") + UserBlacklist))); |
| 114 | } |
| 115 | |
| 116 | TEST_F(SanitizerArgsTest, XRayLists) { |
| 117 | const std::string XRayWhitelist = "/source/xray_whitelist.txt"; |
| 118 | const std::string XRayBlacklist = "/source/xray_blacklist.txt"; |
| 119 | const std::string XRayAttrList = "/source/xray_attr_list.txt"; |
| 120 | |
| 121 | auto &Command = emulateSingleCompilation( |
| 122 | /*ExtraArgs=*/ |
| 123 | { |
| 124 | "-fxray-instrument", |
| 125 | "-fxray-always-instrument=" + XRayWhitelist, |
| 126 | "-fxray-never-instrument=" + XRayBlacklist, |
| 127 | "-fxray-attr-list=" + XRayAttrList, |
| 128 | }, |
| 129 | /*ExtraFiles=*/{XRayWhitelist, XRayBlacklist, XRayAttrList}); |
| 130 | |
| 131 | // Blacklists exist in the filesystem, so they should be added to the |
| 132 | // compilation command, produced by the driver. |
| 133 | EXPECT_THAT(Command.getArguments(), |
| 134 | Contains(StrEq("-fxray-always-instrument=" + XRayWhitelist))); |
| 135 | EXPECT_THAT(Command.getArguments(), |
| 136 | Contains(StrEq("-fxray-never-instrument=" + XRayBlacklist))); |
| 137 | EXPECT_THAT(Command.getArguments(), |
| 138 | Contains(StrEq("-fxray-attr-list=" + XRayAttrList))); |
| 139 | } |
| 140 | |
| 141 | } // namespace |