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>
31using namespace clang;
32using namespace clang::driver;
33
34using ::testing::Contains;
35using ::testing::StrEq;
36
37namespace {
38
39static constexpr const char *ClangBinary = "clang";
40static constexpr const char *InputFile = "/sources/foo.c";
41
42std::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
49class SanitizerArgsTest : public ::testing::Test {
50protected:
51 const Command &emulateSingleCompilation(std::vector<std::string> ExtraArgs,
52 std::vector<std::string> ExtraFiles) {
53 assert(!DriverInstance && "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 DriverInstance.emplace(args: ClangBinary, args: "x86_64-unknown-linux-gnu", args&: Diags,
60 args: "clang LLVM compiler", args: prepareFS(ExtraFiles));
61
62 std::vector<const char *> Args = {ClangBinary};
63 for (const auto &A : ExtraArgs)
64 Args.push_back(x: A.c_str());
65 Args.push_back(x: "-c");
66 Args.push_back(x: InputFile);
67
68 CompilationJob.reset(p: DriverInstance->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 = CompilationJob->getJobs().getJobs();
75 assert(Commands.size() == 1);
76 return *Commands.front();
77 }
78
79private:
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(Path: ClangBinary, ModificationTime: time_t(), Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: ""));
85 FS->addFile(Path: InputFile, ModificationTime: time_t(), Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: ""));
86 for (llvm::StringRef F : ExtraFiles)
87 FS->addFile(Path: F, ModificationTime: time_t(), Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: ""));
88 return FS;
89 }
90
91 std::optional<Driver> DriverInstance;
92 std::unique_ptr<driver::Compilation> CompilationJob;
93};
94
95TEST_F(SanitizerArgsTest, Ignorelists) {
96 const std::string ResourceDir = "/opt/llvm/lib/resources";
97 const std::string UserIgnorelist = "/source/my_ignorelist.txt";
98 const std::string ASanIgnorelist =
99 concatPaths(Components: {ResourceDir, "share", "asan_ignorelist.txt"});
100
101 auto &Command = emulateSingleCompilation(
102 /*ExtraArgs=*/{"-fsanitize=address", "-resource-dir", ResourceDir,
103 std::string("-fsanitize-ignorelist=") + UserIgnorelist},
104 /*ExtraFiles=*/{ASanIgnorelist, UserIgnorelist});
105
106 // System ignorelists are added based on resource-dir.
107 EXPECT_THAT(Command.getArguments(),
108 Contains(StrEq(std::string("-fsanitize-system-ignorelist=") +
109 ASanIgnorelist)));
110 // User ignorelists should also be added.
111 EXPECT_THAT(
112 Command.getArguments(),
113 Contains(StrEq(std::string("-fsanitize-ignorelist=") + UserIgnorelist)));
114}
115
116TEST_F(SanitizerArgsTest, XRayLists) {
117 const std::string XRayAllowlist = "/source/xray_allowlist.txt";
118 const std::string XRayIgnorelist = "/source/xray_ignorelist.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=" + XRayAllowlist,
126 "-fxray-never-instrument=" + XRayIgnorelist,
127 "-fxray-attr-list=" + XRayAttrList,
128 },
129 /*ExtraFiles=*/{XRayAllowlist, XRayIgnorelist, XRayAttrList});
130
131 // Ignorelists 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=" + XRayAllowlist)));
135 EXPECT_THAT(Command.getArguments(),
136 Contains(StrEq("-fxray-never-instrument=" + XRayIgnorelist)));
137 EXPECT_THAT(Command.getArguments(),
138 Contains(StrEq("-fxray-attr-list=" + XRayAttrList)));
139}
140
141} // namespace
142

source code of clang/unittests/Driver/SanitizerArgsTest.cpp