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.
|
2015-03-04 06:15:32 +08:00
|
|
|
enum SanitizeOrdinal : uint64_t {
|
2014-11-14 10:59:20 +08:00
|
|
|
#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).
|
2015-03-04 06:15:32 +08:00
|
|
|
enum SanitizeKind : uint64_t {
|
|
|
|
#define SANITIZER(NAME, ID) ID = 1ULL << SO_##ID,
|
2014-11-14 10:59:20 +08:00
|
|
|
#define SANITIZER_GROUP(NAME, ID, ALIAS) \
|
2015-03-04 06:15:32 +08:00
|
|
|
ID = ALIAS, ID##Group = 1ULL << SO_##ID##Group,
|
2014-11-14 10:59:20 +08:00
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
NeedsUbsanRt = Undefined | Integer,
|
|
|
|
NotAllowedWithTrap = Vptr,
|
2014-11-21 20:19:01 +08:00
|
|
|
RequiresPIE = Memory | DataFlow,
|
2015-01-06 09:02:48 +08:00
|
|
|
NeedsUnwindTables = Address | Thread | Memory | DataFlow,
|
2015-03-20 08:06:52 +08:00
|
|
|
SupportsCoverage = Address | Memory | Leak | Undefined | Integer | DataFlow,
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
RecoverableByDefault = Undefined | Integer,
|
|
|
|
Unrecoverable = Address | Unreachable | Return,
|
2015-02-21 04:30:56 +08:00
|
|
|
LegacyFsanitizeRecoverMask = Undefined | Integer,
|
2015-04-02 08:23:30 +08:00
|
|
|
NeedsLTO = CFI,
|
2014-11-14 10:59:20 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if set of \p Sanitizers contain at least one sanitizer from
|
|
|
|
/// \p Kinds.
|
2015-03-04 06:15:32 +08:00
|
|
|
static bool hasOneOf(const clang::SanitizerSet &Sanitizers, uint64_t Kinds) {
|
2014-11-14 10:59:20 +08:00
|
|
|
#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.
|
2015-03-04 06:15:32 +08:00
|
|
|
static void addAllOf(clang::SanitizerSet &Sanitizers, uint64_t Kinds) {
|
2014-11-14 10:59:20 +08:00
|
|
|
#define SANITIZER(NAME, ID) \
|
|
|
|
if (Kinds & ID) \
|
|
|
|
Sanitizers.set(clang::SanitizerKind::ID, true);
|
|
|
|
#include "clang/Basic/Sanitizers.def"
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:15:32 +08:00
|
|
|
static uint64_t toSanitizeKind(clang::SanitizerKind K) {
|
2014-11-14 10:59:20 +08:00
|
|
|
#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.
|
2015-03-04 06:15:32 +08:00
|
|
|
static uint64_t parseValue(const char *Value);
|
2014-11-14 10:59:20 +08:00
|
|
|
|
|
|
|
/// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
|
|
|
|
/// invalid components. Returns OR of members of \c SanitizeKind enumeration.
|
2015-03-04 06:15:32 +08:00
|
|
|
static uint64_t parseArgValues(const Driver &D, const llvm::opt::Arg *A,
|
2014-11-14 10:59:20 +08:00
|
|
|
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,
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t Mask);
|
2014-11-14 10:59:20 +08:00
|
|
|
|
|
|
|
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".
|
2015-03-04 06:15:32 +08:00
|
|
|
static std::string describeSanitizeArg(const llvm::opt::Arg *A, uint64_t Mask);
|
2014-11-14 10:59:20 +08:00
|
|
|
|
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.
|
2015-03-04 06:15:32 +08:00
|
|
|
static uint64_t expandGroups(uint64_t Kinds);
|
2014-11-14 10:59:20 +08:00
|
|
|
|
2015-03-04 06:15:32 +08:00
|
|
|
static uint64_t getToolchainUnsupportedKinds(const ToolChain &TC) {
|
2014-11-17 04:53:53 +08:00
|
|
|
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;
|
2015-01-22 15:21:22 +08:00
|
|
|
bool IsMIPS64 = TC.getTriple().getArch() == llvm::Triple::mips64 ||
|
|
|
|
TC.getTriple().getArch() == llvm::Triple::mips64el;
|
2014-11-17 04:53:53 +08:00
|
|
|
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t Unsupported = 0;
|
2015-01-22 15:21:22 +08:00
|
|
|
if (!(IsLinux && (IsX86_64 || IsMIPS64))) {
|
2014-11-17 04:53:53 +08:00
|
|
|
Unsupported |= Memory | DataFlow;
|
|
|
|
}
|
2015-02-23 17:32:35 +08:00
|
|
|
if (!((IsLinux || IsFreeBSD) && (IsX86_64 || IsMIPS64))) {
|
2014-11-17 04:53:53 +08:00
|
|
|
Unsupported |= Thread;
|
|
|
|
}
|
|
|
|
if (!(IsLinux && (IsX86 || IsX86_64))) {
|
|
|
|
Unsupported |= Function;
|
|
|
|
}
|
|
|
|
return Unsupported;
|
|
|
|
}
|
2014-11-14 10:59:20 +08:00
|
|
|
|
2015-03-21 02:45:06 +08:00
|
|
|
static bool getDefaultBlacklist(const Driver &D, uint64_t Kinds,
|
|
|
|
std::string &BLPath) {
|
|
|
|
const char *BlacklistFile = nullptr;
|
|
|
|
if (Kinds & SanitizeKind::Address)
|
|
|
|
BlacklistFile = "asan_blacklist.txt";
|
|
|
|
else if (Kinds & SanitizeKind::Memory)
|
|
|
|
BlacklistFile = "msan_blacklist.txt";
|
|
|
|
else if (Kinds & SanitizeKind::Thread)
|
|
|
|
BlacklistFile = "tsan_blacklist.txt";
|
|
|
|
else if (Kinds & SanitizeKind::DataFlow)
|
|
|
|
BlacklistFile = "dfsan_abilist.txt";
|
|
|
|
|
|
|
|
if (BlacklistFile) {
|
|
|
|
clang::SmallString<64> Path(D.ResourceDir);
|
|
|
|
llvm::sys::path::append(Path, BlacklistFile);
|
|
|
|
BLPath = Path.str();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:59:20 +08:00
|
|
|
bool SanitizerArgs::needsUbsanRt() const {
|
2015-04-02 06:42:25 +08:00
|
|
|
return !UbsanTrapOnError && hasOneOf(Sanitizers, NeedsUbsanRt) &&
|
2015-04-28 08:56:36 +08:00
|
|
|
!Sanitizers.has(SanitizerKind::Address) &&
|
|
|
|
!Sanitizers.has(SanitizerKind::Memory) &&
|
|
|
|
!Sanitizers.has(SanitizerKind::Thread);
|
2014-11-14 10:59:20 +08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-02-21 04:30:56 +08:00
|
|
|
bool SanitizerArgs::needsLTO() const {
|
2015-03-14 10:42:25 +08:00
|
|
|
return hasOneOf(Sanitizers, NeedsLTO);
|
2015-02-21 04:30:56 +08:00
|
|
|
}
|
|
|
|
|
2013-08-08 19:32:17 +08:00
|
|
|
void SanitizerArgs::clear() {
|
2014-11-14 10:59:20 +08:00
|
|
|
Sanitizers.clear();
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
RecoverableSanitizers.clear();
|
2015-02-05 01:40:08 +08:00
|
|
|
BlacklistFiles.clear();
|
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();
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t AllRemove = 0; // During the loop below, the accumulated set of
|
2013-11-02 02:16:25 +08:00
|
|
|
// sanitizers disabled by the current sanitizer
|
|
|
|
// argument or any argument after it.
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t DiagnosedKinds = 0; // All Kinds we have diagnosed up to now.
|
2013-11-02 02:16:25 +08:00
|
|
|
// Used to deduplicate diagnostics.
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t Kinds = 0;
|
|
|
|
uint64_t NotSupported = getToolchainUnsupportedKinds(TC);
|
Improve our handling of rtti/sanitize=vptr/sanitize=undefined
This patch removes the huge blob of code that is dealing with
rtti/exceptions/sanitizers and replaces it with:
A ToolChain function which, for a given set of Args, figures out if rtti
should be:
- enabled
- disabled implicitly
- disabled explicitly
A change in the way SanitizerArgs figures out what sanitizers to enable
(or if it should error out, or warn);
And a check for exceptions/rtti interaction inside addExceptionArgs.
The RTTIMode algorithm is:
- If -mkernel, -fapple-kext, or -fno-rtti are passed, rtti was disabled explicitly;
- If -frtti was passed or we're not targetting the PS4, rtti is enabled;
- If -fexceptions or -fcxx-exceptions was passed and we're targetting
the PS4, rtti was enabled implicitly;
- If we're targetting the PS4, rtti is disabled implicitly;
- Otherwise, rtti is enabled;
Since the only flag needed to pass to -cc1 is -fno-rtti if we want to
disable it, there's no problem in saying rtti is enabled if we're
compiling C code, so we don't look at the input file type.
addExceptionArgs now looks at the RTTIMode and warns that rtti is being
enabled implicitly if targetting the PS4 and exceptions are on. It also
errors out if, targetting the PS4, -fno-rtti was passed, and exceptions
were turned on.
SanitizerArgs now errors out if rtti was disabled explicitly and the vptr
sanitizer was enabled implicitly, but just turns off vptr if rtti is
disabled but -fsanitize=undefined was passed.
Also fixed tests, removed duplicate name from addExceptionArgs comment,
and added one or two surrounding lines when running clang-format.
This changes test/Driver/fsanitize.c to make it not expect a warning when
passed -fsanitize=undefined -fno-rtti, but expect vptr to not be on.
Removed all users and definition of SanitizerArgs::sanitizesVptr().
Reviewers: samsonov
Subscribers: llvm-commits, samsonov, rsmith
Differential Revision: http://reviews.llvm.org/D7525
llvm-svn: 229801
2015-02-19 09:04:49 +08:00
|
|
|
ToolChain::RTTIMode RTTIMode = TC.getRTTIMode();
|
|
|
|
|
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();
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t Add = parseArgValues(D, Arg, true);
|
2014-12-19 10:35:16 +08:00
|
|
|
|
|
|
|
// 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.
|
2015-03-04 06:15:32 +08:00
|
|
|
if (uint64_t KindsToDiagnose = Add & NotSupported & ~DiagnosedKinds) {
|
2014-12-19 10:35:16 +08:00
|
|
|
// 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;
|
|
|
|
|
Improve our handling of rtti/sanitize=vptr/sanitize=undefined
This patch removes the huge blob of code that is dealing with
rtti/exceptions/sanitizers and replaces it with:
A ToolChain function which, for a given set of Args, figures out if rtti
should be:
- enabled
- disabled implicitly
- disabled explicitly
A change in the way SanitizerArgs figures out what sanitizers to enable
(or if it should error out, or warn);
And a check for exceptions/rtti interaction inside addExceptionArgs.
The RTTIMode algorithm is:
- If -mkernel, -fapple-kext, or -fno-rtti are passed, rtti was disabled explicitly;
- If -frtti was passed or we're not targetting the PS4, rtti is enabled;
- If -fexceptions or -fcxx-exceptions was passed and we're targetting
the PS4, rtti was enabled implicitly;
- If we're targetting the PS4, rtti is disabled implicitly;
- Otherwise, rtti is enabled;
Since the only flag needed to pass to -cc1 is -fno-rtti if we want to
disable it, there's no problem in saying rtti is enabled if we're
compiling C code, so we don't look at the input file type.
addExceptionArgs now looks at the RTTIMode and warns that rtti is being
enabled implicitly if targetting the PS4 and exceptions are on. It also
errors out if, targetting the PS4, -fno-rtti was passed, and exceptions
were turned on.
SanitizerArgs now errors out if rtti was disabled explicitly and the vptr
sanitizer was enabled implicitly, but just turns off vptr if rtti is
disabled but -fsanitize=undefined was passed.
Also fixed tests, removed duplicate name from addExceptionArgs comment,
and added one or two surrounding lines when running clang-format.
This changes test/Driver/fsanitize.c to make it not expect a warning when
passed -fsanitize=undefined -fno-rtti, but expect vptr to not be on.
Removed all users and definition of SanitizerArgs::sanitizesVptr().
Reviewers: samsonov
Subscribers: llvm-commits, samsonov, rsmith
Differential Revision: http://reviews.llvm.org/D7525
llvm-svn: 229801
2015-02-19 09:04:49 +08:00
|
|
|
// Test for -fno-rtti + explicit -fsanitizer=vptr before expanding groups
|
|
|
|
// so we don't error out if -fno-rtti and -fsanitize=undefined were
|
|
|
|
// passed.
|
|
|
|
if (Add & SanitizeKind::Vptr &&
|
|
|
|
(RTTIMode == ToolChain::RM_DisabledImplicitly ||
|
|
|
|
RTTIMode == ToolChain::RM_DisabledExplicitly)) {
|
|
|
|
if (RTTIMode == ToolChain::RM_DisabledImplicitly)
|
|
|
|
// Warn about not having rtti enabled if the vptr sanitizer is
|
|
|
|
// explicitly enabled
|
|
|
|
D.Diag(diag::warn_drv_disabling_vptr_no_rtti_default);
|
|
|
|
else {
|
|
|
|
const llvm::opt::Arg *NoRTTIArg = TC.getRTTIArg();
|
|
|
|
assert(NoRTTIArg &&
|
|
|
|
"RTTI disabled explicitly but we have no argument!");
|
|
|
|
D.Diag(diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< "-fsanitize=vptr" << NoRTTIArg->getAsString(Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take out the Vptr sanitizer from the enabled sanitizers
|
|
|
|
AllRemove |= SanitizeKind::Vptr;
|
|
|
|
}
|
|
|
|
|
2014-12-19 10:35:16 +08:00
|
|
|
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();
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t Remove = parseArgValues(D, Arg, true);
|
2014-12-19 10:35:16 +08:00
|
|
|
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
|
|
|
|
Improve our handling of rtti/sanitize=vptr/sanitize=undefined
This patch removes the huge blob of code that is dealing with
rtti/exceptions/sanitizers and replaces it with:
A ToolChain function which, for a given set of Args, figures out if rtti
should be:
- enabled
- disabled implicitly
- disabled explicitly
A change in the way SanitizerArgs figures out what sanitizers to enable
(or if it should error out, or warn);
And a check for exceptions/rtti interaction inside addExceptionArgs.
The RTTIMode algorithm is:
- If -mkernel, -fapple-kext, or -fno-rtti are passed, rtti was disabled explicitly;
- If -frtti was passed or we're not targetting the PS4, rtti is enabled;
- If -fexceptions or -fcxx-exceptions was passed and we're targetting
the PS4, rtti was enabled implicitly;
- If we're targetting the PS4, rtti is disabled implicitly;
- Otherwise, rtti is enabled;
Since the only flag needed to pass to -cc1 is -fno-rtti if we want to
disable it, there's no problem in saying rtti is enabled if we're
compiling C code, so we don't look at the input file type.
addExceptionArgs now looks at the RTTIMode and warns that rtti is being
enabled implicitly if targetting the PS4 and exceptions are on. It also
errors out if, targetting the PS4, -fno-rtti was passed, and exceptions
were turned on.
SanitizerArgs now errors out if rtti was disabled explicitly and the vptr
sanitizer was enabled implicitly, but just turns off vptr if rtti is
disabled but -fsanitize=undefined was passed.
Also fixed tests, removed duplicate name from addExceptionArgs comment,
and added one or two surrounding lines when running clang-format.
This changes test/Driver/fsanitize.c to make it not expect a warning when
passed -fsanitize=undefined -fno-rtti, but expect vptr to not be on.
Removed all users and definition of SanitizerArgs::sanitizesVptr().
Reviewers: samsonov
Subscribers: llvm-commits, samsonov, rsmith
Differential Revision: http://reviews.llvm.org/D7525
llvm-svn: 229801
2015-02-19 09:04:49 +08:00
|
|
|
// We disable the vptr sanitizer if it was enabled by group expansion but RTTI
|
|
|
|
// is disabled.
|
2015-03-21 02:45:06 +08:00
|
|
|
if ((Kinds & SanitizeKind::Vptr) &&
|
Improve our handling of rtti/sanitize=vptr/sanitize=undefined
This patch removes the huge blob of code that is dealing with
rtti/exceptions/sanitizers and replaces it with:
A ToolChain function which, for a given set of Args, figures out if rtti
should be:
- enabled
- disabled implicitly
- disabled explicitly
A change in the way SanitizerArgs figures out what sanitizers to enable
(or if it should error out, or warn);
And a check for exceptions/rtti interaction inside addExceptionArgs.
The RTTIMode algorithm is:
- If -mkernel, -fapple-kext, or -fno-rtti are passed, rtti was disabled explicitly;
- If -frtti was passed or we're not targetting the PS4, rtti is enabled;
- If -fexceptions or -fcxx-exceptions was passed and we're targetting
the PS4, rtti was enabled implicitly;
- If we're targetting the PS4, rtti is disabled implicitly;
- Otherwise, rtti is enabled;
Since the only flag needed to pass to -cc1 is -fno-rtti if we want to
disable it, there's no problem in saying rtti is enabled if we're
compiling C code, so we don't look at the input file type.
addExceptionArgs now looks at the RTTIMode and warns that rtti is being
enabled implicitly if targetting the PS4 and exceptions are on. It also
errors out if, targetting the PS4, -fno-rtti was passed, and exceptions
were turned on.
SanitizerArgs now errors out if rtti was disabled explicitly and the vptr
sanitizer was enabled implicitly, but just turns off vptr if rtti is
disabled but -fsanitize=undefined was passed.
Also fixed tests, removed duplicate name from addExceptionArgs comment,
and added one or two surrounding lines when running clang-format.
This changes test/Driver/fsanitize.c to make it not expect a warning when
passed -fsanitize=undefined -fno-rtti, but expect vptr to not be on.
Removed all users and definition of SanitizerArgs::sanitizesVptr().
Reviewers: samsonov
Subscribers: llvm-commits, samsonov, rsmith
Differential Revision: http://reviews.llvm.org/D7525
llvm-svn: 229801
2015-02-19 09:04:49 +08:00
|
|
|
(RTTIMode == ToolChain::RM_DisabledImplicitly ||
|
|
|
|
RTTIMode == ToolChain::RM_DisabledExplicitly)) {
|
|
|
|
Kinds &= ~SanitizeKind::Vptr;
|
|
|
|
}
|
|
|
|
|
2015-03-21 02:45:06 +08:00
|
|
|
// Warn about undefined sanitizer options that require runtime support.
|
|
|
|
UbsanTrapOnError =
|
|
|
|
Args.hasFlag(options::OPT_fsanitize_undefined_trap_on_error,
|
|
|
|
options::OPT_fno_sanitize_undefined_trap_on_error, false);
|
|
|
|
if (UbsanTrapOnError && (Kinds & SanitizeKind::NotAllowedWithTrap)) {
|
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< lastArgumentForMask(D, Args, NotAllowedWithTrap)
|
|
|
|
<< "-fsanitize-undefined-trap-on-error";
|
|
|
|
Kinds &= ~SanitizeKind::NotAllowedWithTrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warn about incompatible groups of sanitizers.
|
|
|
|
std::pair<uint64_t, uint64_t> IncompatibleGroups[] = {
|
|
|
|
std::make_pair(SanitizeKind::Address, SanitizeKind::Thread),
|
|
|
|
std::make_pair(SanitizeKind::Address, SanitizeKind::Memory),
|
|
|
|
std::make_pair(SanitizeKind::Thread, SanitizeKind::Memory),
|
|
|
|
std::make_pair(SanitizeKind::Leak, SanitizeKind::Thread),
|
2015-04-28 08:56:36 +08:00
|
|
|
std::make_pair(SanitizeKind::Leak, SanitizeKind::Memory)};
|
2015-03-21 02:45:06 +08:00
|
|
|
for (auto G : IncompatibleGroups) {
|
|
|
|
uint64_t Group = G.first;
|
|
|
|
if (Kinds & Group) {
|
|
|
|
if (uint64_t Incompatible = Kinds & G.second) {
|
|
|
|
D.Diag(clang::diag::err_drv_argument_not_allowed_with)
|
|
|
|
<< lastArgumentForMask(D, Args, Group)
|
|
|
|
<< lastArgumentForMask(D, Args, Incompatible);
|
|
|
|
Kinds &= ~Incompatible;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// FIXME: Currently -fsanitize=leak is silently ignored in the presence of
|
|
|
|
// -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?
|
|
|
|
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
// Parse -f(no-)?sanitize-recover flags.
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t RecoverableKinds = RecoverableByDefault;
|
|
|
|
uint64_t DiagnosedUnrecoverableKinds = 0;
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
for (const auto *Arg : Args) {
|
2015-03-12 07:34:25 +08:00
|
|
|
const char *DeprecatedReplacement = nullptr;
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
if (Arg->getOption().matches(options::OPT_fsanitize_recover)) {
|
2015-03-12 07:34:25 +08:00
|
|
|
DeprecatedReplacement = "-fsanitize-recover=undefined,integer";
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
RecoverableKinds |= expandGroups(LegacyFsanitizeRecoverMask);
|
|
|
|
Arg->claim();
|
|
|
|
} else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover)) {
|
2015-03-12 07:34:25 +08:00
|
|
|
DeprecatedReplacement = "-fno-sanitize-recover=undefined,integer";
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
RecoverableKinds &= ~expandGroups(LegacyFsanitizeRecoverMask);
|
|
|
|
Arg->claim();
|
|
|
|
} else if (Arg->getOption().matches(options::OPT_fsanitize_recover_EQ)) {
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t Add = parseArgValues(D, Arg, true);
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
// Report error if user explicitly tries to recover from unrecoverable
|
|
|
|
// sanitizer.
|
2015-03-04 06:15:32 +08:00
|
|
|
if (uint64_t KindsToDiagnose =
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
Add & Unrecoverable & ~DiagnosedUnrecoverableKinds) {
|
|
|
|
SanitizerSet SetToDiagnose;
|
|
|
|
addAllOf(SetToDiagnose, KindsToDiagnose);
|
|
|
|
D.Diag(diag::err_drv_unsupported_option_argument)
|
|
|
|
<< Arg->getOption().getName() << toString(SetToDiagnose);
|
|
|
|
DiagnosedUnrecoverableKinds |= KindsToDiagnose;
|
|
|
|
}
|
|
|
|
RecoverableKinds |= expandGroups(Add);
|
|
|
|
Arg->claim();
|
|
|
|
} else if (Arg->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) {
|
|
|
|
RecoverableKinds &= ~expandGroups(parseArgValues(D, Arg, true));
|
|
|
|
Arg->claim();
|
|
|
|
}
|
2015-03-12 07:34:25 +08:00
|
|
|
if (DeprecatedReplacement) {
|
|
|
|
D.Diag(diag::warn_drv_deprecated_arg) << Arg->getAsString(Args)
|
|
|
|
<< DeprecatedReplacement;
|
|
|
|
}
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
}
|
|
|
|
RecoverableKinds &= Kinds;
|
|
|
|
RecoverableKinds &= ~Unrecoverable;
|
2013-08-08 18:11:02 +08:00
|
|
|
|
2015-02-05 01:40:08 +08:00
|
|
|
// Setup blacklist files.
|
|
|
|
// Add default blacklist from resource directory.
|
|
|
|
{
|
|
|
|
std::string BLPath;
|
2015-03-21 02:45:06 +08:00
|
|
|
if (getDefaultBlacklist(D, Kinds, BLPath) && llvm::sys::fs::exists(BLPath))
|
2015-02-05 01:40:08 +08:00
|
|
|
BlacklistFiles.push_back(BLPath);
|
|
|
|
}
|
2013-08-08 18:11:02 +08:00
|
|
|
// Parse -f(no-)sanitize-blacklist options.
|
2015-02-05 01:40:08 +08:00
|
|
|
for (const auto *Arg : Args) {
|
|
|
|
if (Arg->getOption().matches(options::OPT_fsanitize_blacklist)) {
|
|
|
|
Arg->claim();
|
|
|
|
std::string BLPath = Arg->getValue();
|
|
|
|
if (llvm::sys::fs::exists(BLPath))
|
|
|
|
BlacklistFiles.push_back(BLPath);
|
|
|
|
else
|
2014-11-14 10:59:20 +08:00
|
|
|
D.Diag(clang::diag::err_drv_no_such_file) << BLPath;
|
2015-02-05 01:40:08 +08:00
|
|
|
} else if (Arg->getOption().matches(options::OPT_fno_sanitize_blacklist)) {
|
|
|
|
Arg->claim();
|
|
|
|
BlacklistFiles.clear();
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
2015-02-05 01:40:08 +08:00
|
|
|
}
|
|
|
|
// Validate blacklists format.
|
|
|
|
{
|
|
|
|
std::string BLError;
|
|
|
|
std::unique_ptr<llvm::SpecialCaseList> SCL(
|
|
|
|
llvm::SpecialCaseList::create(BlacklistFiles, BLError));
|
|
|
|
if (!SCL.get())
|
|
|
|
D.Diag(clang::diag::err_drv_malformed_sanitizer_blacklist) << BLError;
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
|
|
|
|
2014-03-20 22:58:36 +08:00
|
|
|
// Parse -f[no-]sanitize-memory-track-origins[=level] options.
|
2015-03-21 02:45:06 +08:00
|
|
|
if (Kinds & SanitizeKind::Memory) {
|
2014-03-20 22:58:36 +08:00
|
|
|
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)) {
|
2015-02-26 23:59:30 +08:00
|
|
|
MsanTrackOrigins = 2;
|
2014-03-20 22:58:36 +08:00
|
|
|
} 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) {
|
2015-04-14 04:03:03 +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.
|
2015-03-21 02:45:06 +08:00
|
|
|
if (Kinds & SanitizeKind::SupportsCoverage) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-21 02:45:06 +08:00
|
|
|
if (Kinds & SanitizeKind::Address) {
|
2014-04-01 21:31:10 +08:00
|
|
|
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();
|
2015-03-21 02:45:06 +08:00
|
|
|
|
|
|
|
// Finally, initialize the set of available and recoverable sanitizers.
|
|
|
|
addAllOf(Sanitizers, Kinds);
|
|
|
|
addAllOf(RecoverableSanitizers, RecoverableKinds);
|
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)));
|
|
|
|
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
if (!RecoverableSanitizers.empty())
|
|
|
|
CmdArgs.push_back(Args.MakeArgString("-fsanitize-recover=" +
|
|
|
|
toString(RecoverableSanitizers)));
|
2014-11-17 04:53:53 +08:00
|
|
|
|
|
|
|
if (UbsanTrapOnError)
|
|
|
|
CmdArgs.push_back("-fsanitize-undefined-trap-on-error");
|
|
|
|
|
2015-02-05 01:40:08 +08:00
|
|
|
for (const auto &BLPath : BlacklistFiles) {
|
2013-08-08 18:11:02 +08:00
|
|
|
SmallString<64> BlacklistOpt("-fsanitize-blacklist=");
|
2015-02-05 01:40:08 +08:00
|
|
|
BlacklistOpt += BLPath;
|
2013-08-08 18:11:02 +08:00
|
|
|
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)));
|
2015-05-08 02:31:29 +08:00
|
|
|
if (SanitizeCoverage) {
|
|
|
|
int CoverageType = std::min(SanitizeCoverage, 3);
|
|
|
|
CmdArgs.push_back(Args.MakeArgString("-fsanitize-coverage-type=" +
|
|
|
|
llvm::utostr(CoverageType)));
|
|
|
|
if (SanitizeCoverage == 4)
|
|
|
|
CmdArgs.push_back(
|
|
|
|
Args.MakeArgString("-fsanitize-coverage-indirect-calls"));
|
|
|
|
}
|
2015-02-17 23:09:33 +08:00
|
|
|
// MSan: Workaround for PR16386.
|
|
|
|
// ASan: This is mainly to help LSan with cases such as
|
|
|
|
// https://code.google.com/p/address-sanitizer/issues/detail?id=373
|
|
|
|
// We can't make this conditional on -fsanitize=leak, as that flag shouldn't
|
|
|
|
// affect compilation.
|
|
|
|
if (Sanitizers.has(SanitizerKind::Memory) ||
|
|
|
|
Sanitizers.has(SanitizerKind::Address))
|
2013-08-08 18:11:02 +08:00
|
|
|
CmdArgs.push_back(Args.MakeArgString("-fno-assume-sane-operator-new"));
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t parseValue(const char *Value) {
|
|
|
|
uint64_t ParsedKind = llvm::StringSwitch<SanitizeKind>(Value)
|
2013-08-08 18:11:02 +08:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t expandGroups(uint64_t 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;
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t parseArgValues(const Driver &D, const llvm::opt::Arg *A,
|
2014-11-14 10:59:20 +08:00
|
|
|
bool DiagnoseErrors) {
|
2014-12-19 10:35:16 +08:00
|
|
|
assert((A->getOption().matches(options::OPT_fsanitize_EQ) ||
|
Reimplement -fsanitize-recover family of flags.
Introduce the following -fsanitize-recover flags:
- -fsanitize-recover=<list>: Enable recovery for selected checks or
group of checks. It is forbidden to explicitly list unrecoverable
sanitizers here (that is, "address", "unreachable", "return").
- -fno-sanitize-recover=<list>: Disable recovery for selected checks or
group of checks.
- -f(no-)?sanitize-recover is now a synonym for
-f(no-)?sanitize-recover=undefined,integer and will soon be deprecated.
These flags are parsed left to right, and mask of "recoverable"
sanitizer is updated accordingly, much like what we do for -fsanitize= flags.
-fsanitize= and -fsanitize-recover= flag families are independent.
CodeGen change: If there is a single UBSan handler function, responsible
for implementing multiple checks, which have different recoverable setting,
then we emit two handler calls instead of one:
the first one for the set of "unrecoverable" checks, another one - for
set of "recoverable" checks. If all checks implemented by a handler have the
same recoverability setting, then the generated code will be the same.
llvm-svn: 225719
2015-01-13 06:39:12 +08:00
|
|
|
A->getOption().matches(options::OPT_fno_sanitize_EQ) ||
|
|
|
|
A->getOption().matches(options::OPT_fsanitize_recover_EQ) ||
|
|
|
|
A->getOption().matches(options::OPT_fno_sanitize_recover_EQ)) &&
|
2014-12-19 10:35:16 +08:00
|
|
|
"Invalid argument in parseArgValues!");
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t Kinds = 0;
|
|
|
|
for (int i = 0, n = A->getNumValues(); i != n; ++i) {
|
|
|
|
const char *Value = A->getValue(i);
|
|
|
|
uint64_t Kind;
|
2014-12-20 02:41:43 +08:00
|
|
|
// 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,
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t 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)) {
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t AddKinds = expandGroups(parseArgValues(D, Arg, false));
|
2014-12-19 10:35:16 +08:00
|
|
|
if (AddKinds & Mask)
|
|
|
|
return describeSanitizeArg(Arg, Mask);
|
|
|
|
} else if (Arg->getOption().matches(options::OPT_fno_sanitize_EQ)) {
|
2015-03-04 06:15:32 +08:00
|
|
|
uint64_t RemoveKinds = expandGroups(parseArgValues(D, Arg, false));
|
2014-12-19 10:35:16 +08:00
|
|
|
Mask &= ~RemoveKinds;
|
|
|
|
}
|
2013-08-08 18:11:02 +08:00
|
|
|
}
|
|
|
|
llvm_unreachable("arg list didn't provide expected value");
|
|
|
|
}
|
|
|
|
|
2015-03-04 06:15:32 +08:00
|
|
|
std::string describeSanitizeArg(const llvm::opt::Arg *A, uint64_t Mask) {
|
2014-11-14 10:59:20 +08:00
|
|
|
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;
|
2015-03-04 06:15:32 +08:00
|
|
|
for (int i = 0, n = A->getNumValues(); i != n; ++i) {
|
|
|
|
if (expandGroups(parseValue(A->getValue(i))) & Mask) {
|
2013-11-02 02:16:25 +08:00
|
|
|
if (!Sanitizers.empty())
|
|
|
|
Sanitizers += ",";
|
2015-03-04 06:15:32 +08:00
|
|
|
Sanitizers += A->getValue(i);
|
2013-11-02 02:16:25 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|