2019-11-21 18:32:17 +08:00
|
|
|
//===- unittests/Driver/SanitizerArgsTest.cpp -----------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/DiagnosticIDs.h"
|
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Driver.h"
|
|
|
|
#include "clang/Driver/Job.h"
|
|
|
|
#include "clang/Frontend/TextDiagnosticPrinter.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/IntrusiveRefCntPtr.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::driver;
|
|
|
|
|
|
|
|
using ::testing::Contains;
|
|
|
|
using ::testing::StrEq;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static constexpr const char *ClangBinary = "clang";
|
|
|
|
static constexpr const char *InputFile = "/sources/foo.c";
|
|
|
|
|
|
|
|
std::string concatPaths(llvm::ArrayRef<StringRef> Components) {
|
|
|
|
llvm::SmallString<128> P;
|
|
|
|
for (StringRef C : Components)
|
|
|
|
llvm::sys::path::append(P, C);
|
2020-01-30 13:27:46 +08:00
|
|
|
return std::string(P);
|
2019-11-21 18:32:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class SanitizerArgsTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
const Command &emulateSingleCompilation(std::vector<std::string> ExtraArgs,
|
|
|
|
std::vector<std::string> ExtraFiles) {
|
2019-11-21 19:17:55 +08:00
|
|
|
assert(!DriverInstance && "Running twice is not allowed");
|
2019-11-21 18:32:17 +08:00
|
|
|
|
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticOptions> Opts = new DiagnosticOptions;
|
|
|
|
DiagnosticsEngine Diags(
|
|
|
|
new DiagnosticIDs, Opts,
|
|
|
|
new TextDiagnosticPrinter(llvm::errs(), Opts.get()));
|
2019-11-21 19:17:55 +08:00
|
|
|
DriverInstance.emplace(ClangBinary, "x86_64-unknown-linux-gnu", Diags,
|
2020-09-11 17:17:31 +08:00
|
|
|
"clang LLVM compiler", prepareFS(ExtraFiles));
|
2019-11-21 18:32:17 +08:00
|
|
|
|
|
|
|
std::vector<const char *> Args = {ClangBinary};
|
|
|
|
for (const auto &A : ExtraArgs)
|
|
|
|
Args.push_back(A.c_str());
|
|
|
|
Args.push_back("-c");
|
|
|
|
Args.push_back(InputFile);
|
|
|
|
|
2019-11-21 19:17:55 +08:00
|
|
|
CompilationJob.reset(DriverInstance->BuildCompilation(Args));
|
2019-11-21 18:32:17 +08:00
|
|
|
|
|
|
|
if (Diags.hasErrorOccurred())
|
|
|
|
ADD_FAILURE() << "Error occurred while parsing compilation arguments. "
|
|
|
|
"See stderr for details.";
|
|
|
|
|
2019-11-21 19:17:55 +08:00
|
|
|
const auto &Commands = CompilationJob->getJobs().getJobs();
|
2019-11-21 18:32:17 +08:00
|
|
|
assert(Commands.size() == 1);
|
|
|
|
return *Commands.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>
|
|
|
|
prepareFS(llvm::ArrayRef<std::string> ExtraFiles) {
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS =
|
|
|
|
new llvm::vfs::InMemoryFileSystem;
|
|
|
|
FS->addFile(ClangBinary, time_t(), llvm::MemoryBuffer::getMemBuffer(""));
|
|
|
|
FS->addFile(InputFile, time_t(), llvm::MemoryBuffer::getMemBuffer(""));
|
|
|
|
for (llvm::StringRef F : ExtraFiles)
|
|
|
|
FS->addFile(F, time_t(), llvm::MemoryBuffer::getMemBuffer(""));
|
|
|
|
return FS;
|
|
|
|
}
|
|
|
|
|
2019-11-21 19:17:55 +08:00
|
|
|
llvm::Optional<Driver> DriverInstance;
|
|
|
|
std::unique_ptr<driver::Compilation> CompilationJob;
|
2019-11-21 18:32:17 +08:00
|
|
|
};
|
|
|
|
|
2021-05-04 21:50:43 +08:00
|
|
|
TEST_F(SanitizerArgsTest, Ignorelists) {
|
2019-11-21 18:32:17 +08:00
|
|
|
const std::string ResourceDir = "/opt/llvm/lib/resources";
|
2021-05-04 21:50:43 +08:00
|
|
|
const std::string UserIgnorelist = "/source/my_ignorelist.txt";
|
|
|
|
const std::string ASanIgnorelist =
|
|
|
|
concatPaths({ResourceDir, "share", "asan_ignorelist.txt"});
|
2019-11-21 18:32:17 +08:00
|
|
|
|
|
|
|
auto &Command = emulateSingleCompilation(
|
|
|
|
/*ExtraArgs=*/{"-fsanitize=address", "-resource-dir", ResourceDir,
|
2021-05-04 21:50:43 +08:00
|
|
|
std::string("-fsanitize-ignorelist=") + UserIgnorelist},
|
|
|
|
/*ExtraFiles=*/{ASanIgnorelist, UserIgnorelist});
|
2019-11-21 18:32:17 +08:00
|
|
|
|
2021-05-04 21:50:43 +08:00
|
|
|
// System ignorelists are added based on resource-dir.
|
2019-11-21 18:32:17 +08:00
|
|
|
EXPECT_THAT(Command.getArguments(),
|
2021-05-04 21:50:43 +08:00
|
|
|
Contains(StrEq(std::string("-fsanitize-system-ignorelist=") +
|
|
|
|
ASanIgnorelist)));
|
|
|
|
// User ignorelists should also be added.
|
2019-11-21 18:32:17 +08:00
|
|
|
EXPECT_THAT(
|
|
|
|
Command.getArguments(),
|
2021-05-04 21:50:43 +08:00
|
|
|
Contains(StrEq(std::string("-fsanitize-ignorelist=") + UserIgnorelist)));
|
2019-11-21 18:32:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(SanitizerArgsTest, XRayLists) {
|
2021-12-01 03:48:53 +08:00
|
|
|
const std::string XRayAllowlist = "/source/xray_allowlist.txt";
|
2021-05-04 21:50:43 +08:00
|
|
|
const std::string XRayIgnorelist = "/source/xray_ignorelist.txt";
|
2019-11-21 18:32:17 +08:00
|
|
|
const std::string XRayAttrList = "/source/xray_attr_list.txt";
|
|
|
|
|
|
|
|
auto &Command = emulateSingleCompilation(
|
|
|
|
/*ExtraArgs=*/
|
|
|
|
{
|
|
|
|
"-fxray-instrument",
|
2021-12-01 03:48:53 +08:00
|
|
|
"-fxray-always-instrument=" + XRayAllowlist,
|
2021-05-04 21:50:43 +08:00
|
|
|
"-fxray-never-instrument=" + XRayIgnorelist,
|
2019-11-21 18:32:17 +08:00
|
|
|
"-fxray-attr-list=" + XRayAttrList,
|
|
|
|
},
|
2021-12-01 03:48:53 +08:00
|
|
|
/*ExtraFiles=*/{XRayAllowlist, XRayIgnorelist, XRayAttrList});
|
2019-11-21 18:32:17 +08:00
|
|
|
|
2021-05-04 21:50:43 +08:00
|
|
|
// Ignorelists exist in the filesystem, so they should be added to the
|
2019-11-21 18:32:17 +08:00
|
|
|
// compilation command, produced by the driver.
|
|
|
|
EXPECT_THAT(Command.getArguments(),
|
2021-12-01 03:48:53 +08:00
|
|
|
Contains(StrEq("-fxray-always-instrument=" + XRayAllowlist)));
|
2019-11-21 18:32:17 +08:00
|
|
|
EXPECT_THAT(Command.getArguments(),
|
2021-05-04 21:50:43 +08:00
|
|
|
Contains(StrEq("-fxray-never-instrument=" + XRayIgnorelist)));
|
2019-11-21 18:32:17 +08:00
|
|
|
EXPECT_THAT(Command.getArguments(),
|
|
|
|
Contains(StrEq("-fxray-attr-list=" + XRayAttrList)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|