2013-08-08 18:11:02 +08:00
|
|
|
//===--- SanitizerArgs.cpp - Arguments for sanitizer tools ---------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2013-08-19 17:14:21 +08:00
|
|
|
#include "clang/Driver/SanitizerArgs.h"
|
2013-08-08 18:11:02 +08:00
|
|
|
#include "clang/Driver/Driver.h"
|
|
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
|
|
|
#include "clang/Driver/Options.h"
|
|
|
|
#include "clang/Driver/ToolChain.h"
|
2014-03-20 22:58:36 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-08-08 18:11:02 +08:00
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
2014-07-10 03:40:08 +08:00
|
|
|
#include "llvm/Support/SpecialCaseList.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2013-08-08 18:11:02 +08:00
|
|
|
|
|
|
|
using namespace clang::driver;
|
|
|
|
using namespace llvm::opt;
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
namespace {
|
|
|
|
/// Assign ordinals to possible values of -fsanitize= flag.
|
|
|
|
/// We use the ordinal values as bit positions within \c SanitizeKind.
|
|
|
|
enum SanitizeOrdinal {
|
|
|
|
#define SANITIZER(NAME, ID) SO_##ID,
|
|
|
|
#define SANITIZER_GROUP(NAME, ID, ALIAS) SO_##ID##Group,
|
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
SO_Count
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Represents a set of sanitizer kinds. It is also used to define:
|
|
|
|
/// 1) set of sanitizers each sanitizer group expands into.
|
|
|
|
/// 2) set of sanitizers sharing a specific property (e.g.
|
|
|
|
/// all sanitizers with zero-base shadow).
|
|
|
|
enum SanitizeKind {
|
|
|
|
#define SANITIZER(NAME, ID) ID = 1 << SO_##ID,
|
|
|
|
#define SANITIZER_GROUP(NAME, ID, ALIAS) \
|
|
|
|
ID = ALIAS, ID##Group = 1 << SO_##ID##Group,
|
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
NeedsUbsanRt = Undefined | Integer,
|
|
|
|
NotAllowedWithTrap = Vptr,
|
2014-11-21 20:19:01 +08:00
|
|
|
RequiresPIE = Memory | DataFlow,
|
2014-11-14 10:59:20 +08:00
|
|
|
NeedsUnwindTables = Address | Thread | Memory | DataFlow
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if set of \p Sanitizers contain at least one sanitizer from
|
|
|
|
/// \p Kinds.
|
|
|
|
static bool hasOneOf(const clang::SanitizerSet &Sanitizers, unsigned Kinds) {
|
|
|
|
#define SANITIZER(NAME, ID) \
|
|
|
|
if (Sanitizers.has(clang::SanitizerKind::ID) && (Kinds & ID)) \
|
|
|
|
return true;
|
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds all sanitizers from \p Kinds to \p Sanitizers.
|
|
|
|
static void addAllOf(clang::SanitizerSet &Sanitizers, unsigned Kinds) {
|
|
|
|
#define SANITIZER(NAME, ID) \
|
|
|
|
if (Kinds & ID) \
|
|
|
|
Sanitizers.set(clang::SanitizerKind::ID, true);
|
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned toSanitizeKind(clang::SanitizerKind K) {
|
|
|
|
#define SANITIZER(NAME, ID) \
|
|
|
|
if (K == clang::SanitizerKind::ID) \
|
|
|
|
return ID;
|
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
llvm_unreachable("Invalid SanitizerKind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
|
|
|
|
/// Returns a member of the \c SanitizeKind enumeration, or \c 0
|
|
|
|
/// if \p Value is not known.
|
|
|
|
static unsigned parseValue(const char *Value);
|
|
|
|
|
|
|
|
/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
|
|
|
|
/// invalid components. Returns OR of members of \c SanitizeKind enumeration.
|
|
|
|
static unsigned parseArgValues(const Driver &D, const llvm::opt::Arg *A,
|
|
|
|
bool DiagnoseErrors);
|
|
|
|
|
|
|
|
/// Produce an argument string from ArgList \p Args, which shows how it
|
|
|
|
/// provides some sanitizer kind from \p Mask. For example, the argument list
|
|
|
|
/// "-fsanitize=thread,vptr -fsanitize=address" with mask \c NeedsUbsanRt
|
|
|
|
/// would produce "-fsanitize=vptr".
|
|
|
|
static std::string lastArgumentForMask(const Driver &D,
|
|
|
|
const llvm::opt::ArgList &Args,
|
|
|
|
unsigned Mask);
|
|
|
|
|
|
|
|
static std::string lastArgumentForKind(const Driver &D,
|
|
|
|
const llvm::opt::ArgList &Args,
|
|
|
|
clang::SanitizerKind K) {
|
|
|
|
return lastArgumentForMask(D, Args, toSanitizeKind(K));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Produce an argument string from argument \p A, which shows how it provides
|
|
|
|
/// a value in \p Mask. For instance, the argument
|
|
|
|
/// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
|
|
|
|
/// "-fsanitize=alignment".
|
|
|
|
static std::string describeSanitizeArg(const llvm::opt::Arg *A, unsigned Mask);
|
|
|
|
|
2014-11-17 04:53:53 +08:00
|
|
|
/// Produce a string containing comma-separated names of sanitizers in \p
|
|
|
|
/// Sanitizers set.
|
|
|
|
static std::string toString(const clang::SanitizerSet &Sanitizers);
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
/// For each sanitizer group bit set in \p Kinds, set the bits for sanitizers
|
|
|
|
/// this group enables.
|
|
|
|
static unsigned expandGroups(unsigned Kinds);
|
|
|
|
|
2014-11-17 04:53:53 +08:00
|
|
|
static unsigned getToolchainUnsupportedKinds(const ToolChain &TC) {
|
|
|
|
bool IsFreeBSD = TC.getTriple().getOS() == llvm::Triple::FreeBSD;
|
|
|
|
bool IsLinux = TC.getTriple().getOS() == llvm::Triple::Linux;
|
|
|
|
bool IsX86 = TC.getTriple().getArch() == llvm::Triple::x86;
|
|
|
|
bool IsX86_64 = TC.getTriple().getArch() == llvm::Triple::x86_64;
|
|
|
|
|
|
|
|
unsigned Unsupported = 0;
|
|
|
|
if (!(IsLinux && IsX86_64)) {
|
|
|
|
Unsupported |= Memory | DataFlow;
|
|
|
|
}
|
|
|
|
if (!((IsLinux || IsFreeBSD) && IsX86_64)) {
|
|
|
|
Unsupported |= Thread;
|
|
|
|
}
|
|
|
|
if (!(IsLinux && (IsX86 || IsX86_64))) {
|
|
|
|
Unsupported |= Function;
|
|
|
|
}
|
|
|
|
return Unsupported;
|
|
|
|
}
|
2014-11-14 10:59:20 +08:00
|
|
|
|
|
|
|
bool SanitizerArgs::needsUbsanRt() const {
|
|
|
|
return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt);
|
|
|
|
}
|
|
|
|
|
2014-11-21 20:19:01 +08:00
|
|
|
bool SanitizerArgs::requiresPIE() const {
|
|
|
|
return AsanZeroBaseShadow || hasOneOf(Sanitizers, RequiresPIE);
|
2014-11-14 10:59:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SanitizerArgs::needsUnwindTables() const {
|
|
|
|
return hasOneOf(Sanitizers, NeedsUnwindTables);
|
|
|
|
}
|
|
|
|
|
2013-08-08 19:32:17 +08:00
|
|
|
void SanitizerArgs::clear() {
|
2014-11-14 10:59:20 +08:00
|
|
|
Sanitizers.clear();
|
2014-11-17 04:53:53 +08:00
|
|
|
SanitizeRecover = false;
|
2013-08-08 19:32:17 +08:00
|
|
|
BlacklistFile = "";
|
2014-11-12 06:15:07 +08:00
|
|
|
SanitizeCoverage = 0;
|
2014-03-20 22:58:36 +08:00
|
|
|
MsanTrackOrigins = 0;
|
2014-10-10 01:53:04 +08:00
|
|
|
AsanFieldPadding = 0;
|
2013-11-02 02:16:25 +08:00
|
|
|
AsanZeroBaseShadow = false;
|
2013-08-08 19:32:17 +08:00
|
|
|
UbsanTrapOnError = false;
|
2014-04-01 21:31:10 +08:00
|
|
|
AsanSharedRuntime = false;
|
2014-08-09 06:47:17 +08:00
|
|
|
LinkCXXRuntimes = false;
|
2013-08-08 19:32:17 +08:00
|
|
|
}
|
2013-08-08 18:11:02 +08:00
|
|
|
|
2013-11-02 02:16:25 +08:00
|
|
|
SanitizerArgs::SanitizerArgs(const ToolChain &TC,
|
|
|
|
const llvm::opt::ArgList &Args) {
|
2013-08-08 19:32:17 +08:00
|
|
|
clear();
|
2013-11-02 02:16:25 +08:00
|
|
|
unsigned AllRemove = 0; // During the loop below, the accumulated set of
|
|
|
|
// sanitizers disabled by the current sanitizer
|
|
|
|
// argument or any argument after it.
|
|
|
|
unsigned DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
|
|
|
|
// Used to deduplicate diagnostics.
|
2014-11-17 04:53:53 +08:00
|
|
|
unsigned Kinds = 0;
|
|
|
|
unsigned NotSupported = getToolchainUnsupportedKinds(TC);
|
2013-11-02 02:16:25 +08:00
|
|
|
const Driver &D = TC.getDriver();
|
|
|
|
for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
|
|
|
|
I != E; ++I) {
|
2014-12-19 10:35:16 +08:00
|
|
|
const auto *Arg = *I;
|
|
|
|
if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
|
|
|
|
Arg->claim();
|
|
|
|
unsigned Add = parseArgValues(D, Arg, true);
|
|
|
|
|
|
|
|
// Avoid diagnosing any sanitizer which is disabled later.
|
|
|
|
Add &= ~AllRemove;
|
|
|
|
// At this point we have not expanded groups, so any unsupported
|
|
|
|
// sanitizers in Add are those which have been explicitly enabled.
|
|
|
|
// Diagnose them.
|
|
|
|
if (unsigned KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) {
|
|
|
|
// Only diagnose the new kinds.
|
|
|
|
std::string Desc = describeSanitizeArg(*I, KindsToDiagnose);
|
|
|
|
D.Diag(diag::err_drv_unsupported_opt_for_target)
|
|
|
|
<< Desc << TC.getTriple().str();
|
|
|
|
DiagnosedKinds |= KindsToDiagnose;
|
|
|
|
}
|
|
|
|
Add &= ~NotSupported;
|
|
|
|
|
|
|
|
Add = expandGroups(Add);
|
|
|
|
// Group expansion may have enabled a sanitizer which is disabled later.
|
|
|
|
Add &= ~AllRemove;
|
|
|
|
// Silently discard any unsupported sanitizers implicitly enabled through
|
|
|
|
// group expansion.
|
|
|
|
Add &= ~NotSupported;
|
|
|
|
|
|
|
|
Kinds |= Add;
|
|
|
|
} else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
|
|
|
|
Arg->claim();
|
|
|
|
unsigned Remove = parseArgValues(D, Arg, true);
|
|
|
|
AllRemove |= expandGroups(Remove);
|
2014-11-17 04:53:53 +08:00
|
|
|
}
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
2014-11-17 04:53:53 +08:00
|
|
|
addAllOf(Sanitizers, Kinds);
|
|
|
|
|
|
|
|
SanitizeRecover = Args.hasFlag(options::OPT_fsanitize_recover,
|
|
|
|
options::OPT_fno_sanitize_recover, true);
|
2013-08-08 18:11:02 +08:00
|
|
|
|
|
|
|
UbsanTrapOnError =
|
|
|
|
Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
|
|
|
|
options::OPT_fno_sanitize_undefined_trap_on_error, false);
|
|
|
|
|
|
|
|
// Warn about undefined sanitizer options that require runtime support.
|
2014-11-14 10:59:20 +08:00
|
|
|
if (UbsanTrapOnError && hasOneOf(Sanitizers, NotAllowedWithTrap)) {
|
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< lastArgumentForMask(D, Args, NotAllowedWithTrap)
|
2014-03-20 18:48:29 +08:00
|
|
|
<< "-fsanitize-undefined-trap-on-error";
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
// Check for incompatible sanitizers.
|
|
|
|
bool NeedsAsan = Sanitizers.has(SanitizerKind::Address);
|
|
|
|
bool NeedsTsan = Sanitizers.has(SanitizerKind::Thread);
|
|
|
|
bool NeedsMsan = Sanitizers.has(SanitizerKind::Memory);
|
|
|
|
bool NeedsLsan = Sanitizers.has(SanitizerKind::Leak);
|
2013-08-08 18:11:02 +08:00
|
|
|
if (NeedsAsan && NeedsTsan)
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Address)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Thread);
|
2013-08-08 18:11:02 +08:00
|
|
|
if (NeedsAsan && NeedsMsan)
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Address)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Memory);
|
2013-08-08 18:11:02 +08:00
|
|
|
if (NeedsTsan && NeedsMsan)
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Thread)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Memory);
|
2013-08-08 18:11:02 +08:00
|
|
|
if (NeedsLsan && NeedsTsan)
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Leak)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Thread);
|
2013-08-08 18:11:02 +08:00
|
|
|
if (NeedsLsan && NeedsMsan)
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Leak)
|
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Memory);
|
2013-12-06 00:25:25 +08:00
|
|
|
// FIXME: Currently -fsanitize=leak is silently ignored in the presence of
|
2013-08-08 18:11:02 +08:00
|
|
|
// -fsanitize=address. Perhaps it should print an error, or perhaps
|
|
|
|
// -f(-no)sanitize=leak should change whether leak detection is enabled by
|
|
|
|
// default in ASan?
|
|
|
|
|
|
|
|
// Parse -f(no-)sanitize-blacklist options.
|
|
|
|
if (Arg *BLArg = Args.getLastArg(options::OPT_fsanitize_blacklist,
|
|
|
|
options::OPT_fno_sanitize_blacklist)) {
|
|
|
|
if (BLArg->getOption().matches(options::OPT_fsanitize_blacklist)) {
|
|
|
|
std::string BLPath = BLArg->getValue();
|
2013-08-19 21:59:22 +08:00
|
|
|
if (llvm::sys::fs::exists(BLPath)) {
|
|
|
|
// Validate the blacklist format.
|
|
|
|
std::string BLError;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<llvm::SpecialCaseList> SCL(
|
2013-08-19 21:59:22 +08:00
|
|
|
llvm::SpecialCaseList::create(BLPath, BLError));
|
|
|
|
if (!SCL.get())
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
|
2013-08-19 21:59:22 +08:00
|
|
|
else
|
|
|
|
BlacklistFile = BLPath;
|
|
|
|
} else {
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
|
2013-08-19 21:59:22 +08:00
|
|
|
}
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If no -fsanitize-blacklist option is specified, try to look up for
|
|
|
|
// blacklist in the resource directory.
|
|
|
|
std::string BLPath;
|
2014-11-14 08:46:39 +08:00
|
|
|
if (getDefaultBlacklist(D, BLPath) && llvm::sys::fs::exists(BLPath))
|
2013-08-08 18:11:02 +08:00
|
|
|
BlacklistFile = BLPath;
|
|
|
|
}
|
|
|
|
|
2014-03-20 22:58:36 +08:00
|
|
|
// Parse -f[no-]sanitize-memory-track-origins[=level] options.
|
|
|
|
if (NeedsMsan) {
|
|
|
|
if (Arg *A =
|
|
|
|
Args.getLastArg(options::OPT_fsanitize_memory_track_origins_EQ,
|
|
|
|
options::OPT_fsanitize_memory_track_origins,
|
|
|
|
options::OPT_fno_sanitize_memory_track_origins)) {
|
|
|
|
if (A->getOption().matches(options::OPT_fsanitize_memory_track_origins)) {
|
|
|
|
MsanTrackOrigins = 1;
|
|
|
|
} else if (A->getOption().matches(
|
|
|
|
options::OPT_fno_sanitize_memory_track_origins)) {
|
|
|
|
MsanTrackOrigins = 0;
|
|
|
|
} else {
|
|
|
|
StringRef S = A->getValue();
|
|
|
|
if (S.getAsInteger(0, MsanTrackOrigins) || MsanTrackOrigins < 0 ||
|
|
|
|
MsanTrackOrigins > 2) {
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
|
2014-03-20 22:58:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 05:46:33 +08:00
|
|
|
// Parse -fsanitize-coverage=N. Currently one of asan/msan/lsan is required.
|
|
|
|
if (NeedsAsan || NeedsMsan || NeedsLsan) {
|
2014-11-12 06:15:07 +08:00
|
|
|
if (Arg *A = Args.getLastArg(options::OPT_fsanitize_coverage)) {
|
|
|
|
StringRef S = A->getValue();
|
|
|
|
// Legal values are 0..4.
|
|
|
|
if (S.getAsInteger(0, SanitizeCoverage) || SanitizeCoverage < 0 ||
|
|
|
|
SanitizeCoverage > 4)
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
|
2014-11-12 06:15:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:31:10 +08:00
|
|
|
if (NeedsAsan) {
|
|
|
|
AsanSharedRuntime =
|
2014-06-05 19:14:00 +08:00
|
|
|
Args.hasArg(options::OPT_shared_libasan) ||
|
|
|
|
(TC.getTriple().getEnvironment() == llvm::Triple::Android);
|
2013-11-02 02:16:25 +08:00
|
|
|
AsanZeroBaseShadow =
|
2014-01-16 18:19:31 +08:00
|
|
|
(TC.getTriple().getEnvironment() == llvm::Triple::Android);
|
2014-10-10 01:53:04 +08:00
|
|
|
if (Arg *A =
|
|
|
|
Args.getLastArg(options::OPT_fsanitize_address_field_padding)) {
|
|
|
|
StringRef S = A->getValue();
|
|
|
|
// Legal values are 0 and 1, 2, but in future we may add more levels.
|
|
|
|
if (S.getAsInteger(0, AsanFieldPadding) || AsanFieldPadding < 0 ||
|
|
|
|
AsanFieldPadding > 2) {
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
|
2014-10-10 01:53:04 +08:00
|
|
|
}
|
|
|
|
}
|
2014-10-15 07:15:44 +08:00
|
|
|
|
|
|
|
if (Arg *WindowsDebugRTArg =
|
|
|
|
Args.getLastArg(options::OPT__SLASH_MTd, options::OPT__SLASH_MT,
|
|
|
|
options::OPT__SLASH_MDd, options::OPT__SLASH_MD,
|
|
|
|
options::OPT__SLASH_LDd, options::OPT__SLASH_LD)) {
|
|
|
|
switch (WindowsDebugRTArg->getOption().getID()) {
|
|
|
|
case options::OPT__SLASH_MTd:
|
|
|
|
case options::OPT__SLASH_MDd:
|
|
|
|
case options::OPT__SLASH_LDd:
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
2014-10-15 07:15:44 +08:00
|
|
|
<< WindowsDebugRTArg->getAsString(Args)
|
2014-11-14 10:59:20 +08:00
|
|
|
<< lastArgumentForKind(D, Args, SanitizerKind::Address);
|
|
|
|
D.Diag(clang::diag::note_drv_address_sanitizer_debug_runtime);
|
2014-10-15 07:15:44 +08:00
|
|
|
}
|
|
|
|
}
|
2014-04-01 21:31:10 +08:00
|
|
|
}
|
2014-08-09 06:47:17 +08:00
|
|
|
|
|
|
|
// Parse -link-cxx-sanitizer flag.
|
|
|
|
LinkCXXRuntimes =
|
|
|
|
Args.hasArg(options::OPT_fsanitize_link_cxx_runtime) || D.CCCIsCXX();
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
|
|
|
|
2014-11-17 04:53:53 +08:00
|
|
|
static std::string toString(const clang::SanitizerSet &Sanitizers) {
|
|
|
|
std::string Res;
|
|
|
|
#define SANITIZER(NAME, ID) \
|
|
|
|
if (Sanitizers.has(clang::SanitizerKind::ID)) { \
|
|
|
|
if (!Res.empty()) \
|
|
|
|
Res += ","; \
|
|
|
|
Res += NAME; \
|
|
|
|
}
|
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2013-11-02 02:16:25 +08:00
|
|
|
void SanitizerArgs::addArgs(const llvm::opt::ArgList &Args,
|
2013-08-08 18:11:02 +08:00
|
|
|
llvm::opt::ArgStringList &CmdArgs) const {
|
2014-11-14 10:59:20 +08:00
|
|
|
if (Sanitizers.empty())
|
2013-08-08 18:11:02 +08:00
|
|
|
return;
|
2014-11-17 04:53:53 +08:00
|
|
|
CmdArgs.push_back(Args.MakeArgString("-fsanitize=" + toString(Sanitizers)));
|
|
|
|
|
|
|
|
if (!SanitizeRecover)
|
|
|
|
CmdArgs.push_back("-fno-sanitize-recover");
|
|
|
|
|
|
|
|
if (UbsanTrapOnError)
|
|
|
|
CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
|
|
|
|
|
2013-08-08 18:11:02 +08:00
|
|
|
if (!BlacklistFile.empty()) {
|
|
|
|
SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
|
|
|
|
BlacklistOpt += BlacklistFile;
|
|
|
|
CmdArgs.push_back(Args.MakeArgString(BlacklistOpt));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MsanTrackOrigins)
|
2014-03-20 22:58:36 +08:00
|
|
|
CmdArgs.push_back(Args.MakeArgString("-fsanitize-memory-track-origins=" +
|
|
|
|
llvm::utostr(MsanTrackOrigins)));
|
2014-10-10 01:53:04 +08:00
|
|
|
if (AsanFieldPadding)
|
|
|
|
CmdArgs.push_back(Args.MakeArgString("-fsanitize-address-field-padding=" +
|
|
|
|
llvm::utostr(AsanFieldPadding)));
|
2014-11-12 06:15:07 +08:00
|
|
|
if (SanitizeCoverage)
|
|
|
|
CmdArgs.push_back(Args.MakeArgString("-fsanitize-coverage=" +
|
|
|
|
llvm::utostr(SanitizeCoverage)));
|
2013-08-08 18:11:02 +08:00
|
|
|
// Workaround for PR16386.
|
2014-11-14 10:59:20 +08:00
|
|
|
if (Sanitizers.has(SanitizerKind::Memory))
|
2013-08-08 18:11:02 +08:00
|
|
|
CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
bool SanitizerArgs::getDefaultBlacklist(const Driver &D, std::string &BLPath) {
|
|
|
|
const char *BlacklistFile = nullptr;
|
|
|
|
if (Sanitizers.has(SanitizerKind::Address))
|
|
|
|
BlacklistFile = "asan_blacklist.txt";
|
|
|
|
else if (Sanitizers.has(SanitizerKind::Memory))
|
|
|
|
BlacklistFile = "msan_blacklist.txt";
|
|
|
|
else if (Sanitizers.has(SanitizerKind::Thread))
|
|
|
|
BlacklistFile = "tsan_blacklist.txt";
|
|
|
|
else if (Sanitizers.has(SanitizerKind::DataFlow))
|
|
|
|
BlacklistFile = "dfsan_abilist.txt";
|
|
|
|
|
|
|
|
if (BlacklistFile) {
|
|
|
|
SmallString<64> Path(D.ResourceDir);
|
|
|
|
llvm::sys::path::append(Path, BlacklistFile);
|
|
|
|
BLPath = Path.str();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned parseValue(const char *Value) {
|
2013-08-08 18:11:02 +08:00
|
|
|
unsigned ParsedKind = llvm::StringSwitch<SanitizeKind>(Value)
|
|
|
|
#define SANITIZER(NAME, ID) .Case(NAME, ID)
|
2013-11-02 02:16:25 +08:00
|
|
|
#define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID##Group)
|
2013-08-08 18:11:02 +08:00
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
.Default(SanitizeKind());
|
|
|
|
return ParsedKind;
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
unsigned expandGroups(unsigned Kinds) {
|
2013-11-02 02:16:25 +08:00
|
|
|
#define SANITIZER(NAME, ID)
|
|
|
|
#define SANITIZER_GROUP(NAME, ID, ALIAS) if (Kinds & ID##Group) Kinds |= ID;
|
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
return Kinds;
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
unsigned parseArgValues(const Driver &D, const llvm::opt::Arg *A,
|
|
|
|
bool DiagnoseErrors) {
|
2014-12-19 10:35:16 +08:00
|
|
|
assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
|
|
|
|
A->getOption().matches(options::OPT_fno_sanitize_EQ)) &&
|
|
|
|
"Invalid argument in parseArgValues!");
|
2014-12-20 02:41:43 +08:00
|
|
|
unsigned Kinds = 0;
|
2013-08-08 18:11:02 +08:00
|
|
|
for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
|
2014-12-20 02:41:43 +08:00
|
|
|
const char *Value = A->getValue(I);
|
|
|
|
unsigned Kind;
|
|
|
|
// Special case: don't accept -fsanitize=all.
|
|
|
|
if (A->getOption().matches(options::OPT_fsanitize_EQ) &&
|
|
|
|
0 == strcmp("all", Value))
|
|
|
|
Kind = 0;
|
|
|
|
else
|
|
|
|
Kind = parseValue(Value);
|
|
|
|
|
|
|
|
if (Kind)
|
|
|
|
Kinds |= Kind;
|
2013-08-08 18:11:02 +08:00
|
|
|
else if (DiagnoseErrors)
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_unsupported_option_argument)
|
2014-12-20 02:41:43 +08:00
|
|
|
<< A->getOption().getName() << Value;
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
2014-12-20 02:41:43 +08:00
|
|
|
return Kinds;
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
std::string lastArgumentForMask(const Driver &D, const llvm::opt::ArgList &Args,
|
|
|
|
unsigned Mask) {
|
2013-08-08 18:11:02 +08:00
|
|
|
for (llvm::opt::ArgList::const_reverse_iterator I = Args.rbegin(),
|
|
|
|
E = Args.rend();
|
|
|
|
I != E; ++I) {
|
2014-12-19 10:35:16 +08:00
|
|
|
const auto *Arg = *I;
|
|
|
|
if (Arg->getOption().matches(options::OPT_fsanitize_EQ)) {
|
|
|
|
unsigned AddKinds = expandGroups(parseArgValues(D, Arg, false));
|
|
|
|
if (AddKinds & Mask)
|
|
|
|
return describeSanitizeArg(Arg, Mask);
|
|
|
|
} else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
|
|
|
|
unsigned RemoveKinds = expandGroups(parseArgValues(D, Arg, false));
|
|
|
|
Mask &= ~RemoveKinds;
|
|
|
|
}
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("arg list didn't provide expected value");
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
std::string describeSanitizeArg(const llvm::opt::Arg *A, unsigned Mask) {
|
|
|
|
assert(A->getOption().matches(options::OPT_fsanitize_EQ)
|
|
|
|
&& "Invalid argument in describeSanitizerArg!");
|
2013-08-08 18:11:02 +08:00
|
|
|
|
2013-11-02 02:16:25 +08:00
|
|
|
std::string Sanitizers;
|
|
|
|
for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
|
2014-11-14 10:59:20 +08:00
|
|
|
if (expandGroups(parseValue(A->getValue(I))) & Mask) {
|
2013-11-02 02:16:25 +08:00
|
|
|
if (!Sanitizers.empty())
|
|
|
|
Sanitizers += ",";
|
|
|
|
Sanitizers += A->getValue(I);
|
|
|
|
}
|
|
|
|
}
|
2013-08-08 18:11:02 +08:00
|
|
|
|
2013-11-02 02:16:25 +08:00
|
|
|
assert(!Sanitizers.empty() && "arg didn't provide expected value");
|
|
|
|
return "-fsanitize=" + Sanitizers;
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|