Add -fsanitize=memory.

llvm-svn: 169124
This commit is contained in:
Evgeniy Stepanov 2012-12-03 13:20:43 +00:00
parent dab61684ef
commit aea92e530d
5 changed files with 45 additions and 0 deletions

View File

@ -101,6 +101,8 @@ def err_drv_mg_requires_m_or_mm : Error<
"option '-MG' requires '-M' or '-MM'">;
def err_drv_asan_android_requires_pie : Error<
"AddressSanitizer on Android requires '-pie'">;
def err_drv_sanitizer_requires_pie : Error<
"%select{Thread|Memory}0Sanitizer requires '-pie'">;
def err_drv_unknown_objc_runtime : Error<
"unknown or ill-formed Objective-C runtime '%0'">;

View File

@ -48,6 +48,9 @@ SANITIZER("use-after-scope", UseAfterScope)
SANITIZER_GROUP("address-full", AddressFull,
Address | InitOrder | UseAfterReturn | UseAfterScope)
// MemorySanitizer
SANITIZER("memory", Memory)
// ThreadSanitizer
SANITIZER("thread", Thread)

View File

@ -179,6 +179,11 @@ static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
PM.add(createAddressSanitizerModulePass(LangOpts.SanitizeInitOrder));
}
static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
PassManagerBase &PM) {
PM.add(createMemorySanitizerPass());
}
static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
PassManagerBase &PM) {
PM.add(createThreadSanitizerPass());
@ -227,6 +232,13 @@ void EmitAssemblyHelper::CreatePasses(TargetMachine *TM) {
addAddressSanitizerPasses);
}
if (LangOpts.SanitizeMemory) {
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
addMemorySanitizerPass);
PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
addMemorySanitizerPass);
}
if (LangOpts.SanitizeThread) {
PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
addThreadSanitizerPass);

View File

@ -30,6 +30,7 @@ class SanitizerArgs {
#include "clang/Basic/Sanitizers.def"
NeedsAsanRt = AddressFull,
NeedsTsanRt = Thread,
NeedsMsanRt = Memory,
NeedsUbsanRt = (Undefined & ~Bounds) | Integer
};
unsigned Kind;
@ -41,6 +42,7 @@ class SanitizerArgs {
bool needsAsanRt() const { return Kind & NeedsAsanRt; }
bool needsTsanRt() const { return Kind & NeedsTsanRt; }
bool needsMsanRt() const { return Kind & NeedsMsanRt; }
bool needsUbsanRt() const { return Kind & NeedsUbsanRt; }
bool sanitizesVptr() const { return Kind & Vptr; }

View File

@ -1524,6 +1524,9 @@ static void addAsanRTLinux(const ToolChain &TC, const ArgList &Args,
static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
ArgStringList &CmdArgs) {
if (!Args.hasArg(options::OPT_shared)) {
if (!Args.hasArg(options::OPT_pie))
TC.getDriver().Diag(diag::err_drv_sanitizer_requires_pie) <<
/* Thread */ 0;
// LibTsan is "libclang_rt.tsan-<ArchName>.a" in the Linux library
// resource directory.
SmallString<128> LibTsan(TC.getDriver().ResourceDir);
@ -1537,6 +1540,27 @@ static void addTsanRTLinux(const ToolChain &TC, const ArgList &Args,
}
}
/// If MemorySanitizer is enabled, add appropriate linker flags (Linux).
/// This needs to be called before we add the C run-time (malloc, etc).
static void addMsanRTLinux(const ToolChain &TC, const ArgList &Args,
ArgStringList &CmdArgs) {
if (!Args.hasArg(options::OPT_shared)) {
if (!Args.hasArg(options::OPT_pie))
TC.getDriver().Diag(diag::err_drv_sanitizer_requires_pie) <<
/* Memory */ 1;
// LibMsan is "libclang_rt.msan-<ArchName>.a" in the Linux library
// resource directory.
SmallString<128> LibMsan(TC.getDriver().ResourceDir);
llvm::sys::path::append(LibMsan, "lib", "linux",
(Twine("libclang_rt.msan-") +
TC.getArchName() + ".a"));
CmdArgs.push_back(Args.MakeArgString(LibMsan));
CmdArgs.push_back("-lpthread");
CmdArgs.push_back("-ldl");
CmdArgs.push_back("-export-dynamic");
}
}
/// If UndefinedBehaviorSanitizer is enabled, add appropriate linker flags
/// (Linux).
static void addUbsanRTLinux(const ToolChain &TC, const ArgList &Args,
@ -5443,6 +5467,8 @@ void linuxtools::Link::ConstructJob(Compilation &C, const JobAction &JA,
addAsanRTLinux(getToolChain(), Args, CmdArgs);
if (Sanitize.needsTsanRt())
addTsanRTLinux(getToolChain(), Args, CmdArgs);
if (Sanitize.needsMsanRt())
addMsanRTLinux(getToolChain(), Args, CmdArgs);
if (D.CCCIsCXX &&
!Args.hasArg(options::OPT_nostdlib) &&