| 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/SmallString.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/Support/Path.h" |
| 22 | #include "llvm/Support/VirtualFileSystem.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/TargetParser/Host.h" |
| 25 | #include "gmock/gmock.h" |
| 26 | #include "gtest/gtest.h" |
| 27 | #include <cstdlib> |
| 28 | #include <memory> |
| 29 | #include <optional> |
| 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(path&: P, a: C); |
| 46 | return std::string(P); |
| 47 | } |
| 48 | |
| 49 | class SanitizerArgsTest : public ::testing::Test { |
| 50 | protected: |
| 51 | const Command &emulateSingleCompilation(std::vector<std::string> , |
| 52 | std::vector<std::string> ) { |
| 53 | assert(!DriverInstance && "Running twice is not allowed" ); |
| 54 | |
| 55 | DiagnosticOptions DiagOpts; |
| 56 | DiagnosticsEngine Diags(new DiagnosticIDs, DiagOpts, |
| 57 | new TextDiagnosticPrinter(llvm::errs(), DiagOpts)); |
| 58 | DriverInstance.emplace(args: ClangBinary, args: "x86_64-unknown-linux-gnu" , args&: Diags, |
| 59 | args: "clang LLVM compiler" , args: prepareFS(ExtraFiles)); |
| 60 | |
| 61 | std::vector<const char *> Args = {ClangBinary}; |
| 62 | for (const auto &A : ExtraArgs) |
| 63 | Args.push_back(x: A.c_str()); |
| 64 | Args.push_back(x: "-c" ); |
| 65 | Args.push_back(x: InputFile); |
| 66 | |
| 67 | CompilationJob.reset(p: DriverInstance->BuildCompilation(Args)); |
| 68 | |
| 69 | if (Diags.hasErrorOccurred()) |
| 70 | ADD_FAILURE() << "Error occurred while parsing compilation arguments. " |
| 71 | "See stderr for details." ; |
| 72 | |
| 73 | const auto &Commands = CompilationJob->getJobs().getJobs(); |
| 74 | assert(Commands.size() == 1); |
| 75 | return *Commands.front(); |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> |
| 80 | prepareFS(llvm::ArrayRef<std::string> ) { |
| 81 | llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS = |
| 82 | new llvm::vfs::InMemoryFileSystem; |
| 83 | FS->addFile(Path: ClangBinary, ModificationTime: time_t(), Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: "" )); |
| 84 | FS->addFile(Path: InputFile, ModificationTime: time_t(), Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: "" )); |
| 85 | for (llvm::StringRef F : ExtraFiles) |
| 86 | FS->addFile(Path: F, ModificationTime: time_t(), Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: "" )); |
| 87 | return FS; |
| 88 | } |
| 89 | |
| 90 | std::optional<Driver> DriverInstance; |
| 91 | std::unique_ptr<driver::Compilation> CompilationJob; |
| 92 | }; |
| 93 | |
| 94 | TEST_F(SanitizerArgsTest, Ignorelists) { |
| 95 | const std::string ResourceDir = "/opt/llvm/lib/resources" ; |
| 96 | const std::string UserIgnorelist = "/source/my_ignorelist.txt" ; |
| 97 | const std::string ASanIgnorelist = |
| 98 | concatPaths(Components: {ResourceDir, "share" , "asan_ignorelist.txt" }); |
| 99 | |
| 100 | auto &Command = emulateSingleCompilation( |
| 101 | /*ExtraArgs=*/{"-fsanitize=address" , "-resource-dir" , ResourceDir, |
| 102 | std::string("-fsanitize-ignorelist=" ) + UserIgnorelist}, |
| 103 | /*ExtraFiles=*/{ASanIgnorelist, UserIgnorelist}); |
| 104 | |
| 105 | // System ignorelists are added based on resource-dir. |
| 106 | EXPECT_THAT(Command.getArguments(), |
| 107 | Contains(StrEq(std::string("-fsanitize-system-ignorelist=" ) + |
| 108 | ASanIgnorelist))); |
| 109 | // User ignorelists should also be added. |
| 110 | EXPECT_THAT( |
| 111 | Command.getArguments(), |
| 112 | Contains(StrEq(std::string("-fsanitize-ignorelist=" ) + UserIgnorelist))); |
| 113 | } |
| 114 | |
| 115 | TEST_F(SanitizerArgsTest, XRayLists) { |
| 116 | const std::string XRayAllowlist = "/source/xray_allowlist.txt" ; |
| 117 | const std::string XRayIgnorelist = "/source/xray_ignorelist.txt" ; |
| 118 | const std::string XRayAttrList = "/source/xray_attr_list.txt" ; |
| 119 | |
| 120 | auto &Command = emulateSingleCompilation( |
| 121 | /*ExtraArgs=*/ |
| 122 | { |
| 123 | "-fxray-instrument" , |
| 124 | "-fxray-always-instrument=" + XRayAllowlist, |
| 125 | "-fxray-never-instrument=" + XRayIgnorelist, |
| 126 | "-fxray-attr-list=" + XRayAttrList, |
| 127 | }, |
| 128 | /*ExtraFiles=*/{XRayAllowlist, XRayIgnorelist, XRayAttrList}); |
| 129 | |
| 130 | // Ignorelists exist in the filesystem, so they should be added to the |
| 131 | // compilation command, produced by the driver. |
| 132 | EXPECT_THAT(Command.getArguments(), |
| 133 | Contains(StrEq("-fxray-always-instrument=" + XRayAllowlist))); |
| 134 | EXPECT_THAT(Command.getArguments(), |
| 135 | Contains(StrEq("-fxray-never-instrument=" + XRayIgnorelist))); |
| 136 | EXPECT_THAT(Command.getArguments(), |
| 137 | Contains(StrEq("-fxray-attr-list=" + XRayAttrList))); |
| 138 | } |
| 139 | |
| 140 | } // namespace |
| 141 | |