r212499 - [Sanitizer] Reduce the usage of sanitizer blacklist in CodeGenModule
Alexey Samsonov
vonosmas at gmail.com
Tue Jul 8 18:21:59 PDT 2014
On Tue, Jul 8, 2014 at 5:45 PM, Richard Smith <richard at metafoo.co.uk> wrote:
> On Mon, Jul 7, 2014 at 4:34 PM, Alexey Samsonov <vonosmas at gmail.com>
> wrote:
>
>> Author: samsonov
>> Date: Mon Jul 7 18:34:34 2014
>> New Revision: 212499
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=212499&view=rev
>> Log:
>> [Sanitizer] Reduce the usage of sanitizer blacklist in CodeGenModule
>>
>> Get rid of cached CodeGenModule::SanOpts, which was used to turn off
>> sanitizer codegen options if current LLVM Module is blacklisted, and use
>> plain LangOpts.Sanitize instead.
>>
>> 1) Some codegen decisions (turning TBAA or writable strings on/off)
>> shouldn't depend on the contents of blacklist.
>>
>> 2) llvm.asan.globals should *always* be created, even if the module
>> is blacklisted - soon Clang's CodeGen where we read sanitizer
>> blacklist files, so we should properly report which globals are
>> blacklisted to the backend.
>>
>> Modified:
>> cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
>> cfe/trunk/lib/CodeGen/CodeGenModule.cpp
>> cfe/trunk/lib/CodeGen/CodeGenModule.h
>> cfe/trunk/test/CodeGen/asan-globals.cpp
>>
>> Modified: cfe/trunk/lib/CodeGen/CGDeclCXX.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDeclCXX.cpp?rev=212499&r1=212498&r2=212499&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/lib/CodeGen/CGDeclCXX.cpp (original)
>> +++ cfe/trunk/lib/CodeGen/CGDeclCXX.cpp Mon Jul 7 18:34:34 2014
>> @@ -245,12 +245,14 @@ CreateGlobalInitOrDestructFunction(CodeG
>> if (!CGM.getLangOpts().Exceptions)
>> Fn->setDoesNotThrow();
>>
>> - if (CGM.getSanOpts().Address)
>> - Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
>> - if (CGM.getSanOpts().Thread)
>> - Fn->addFnAttr(llvm::Attribute::SanitizeThread);
>> - if (CGM.getSanOpts().Memory)
>> - Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
>> + if (!CGM.getSanitizerBlacklist().isIn(*Fn)) {
>> + if (CGM.getLangOpts().Sanitize.Address)
>> + Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
>> + if (CGM.getLangOpts().Sanitize.Thread)
>> + Fn->addFnAttr(llvm::Attribute::SanitizeThread);
>> + if (CGM.getLangOpts().Sanitize.Memory)
>> + Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
>> + }
>>
>> return Fn;
>> }
>>
>> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=212499&r1=212498&r2=212499&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
>> +++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Mon Jul 7 18:34:34 2014
>> @@ -89,9 +89,7 @@ CodeGenModule::CodeGenModule(ASTContext
>> GenericBlockLiteralType(nullptr), LifetimeStartFn(nullptr),
>> LifetimeEndFn(nullptr),
>> SanitizerBlacklist(
>> -
>> llvm::SpecialCaseList::createOrDie(CGO.SanitizerBlacklistFile)),
>> - SanOpts(SanitizerBlacklist->isIn(M) ? SanitizerOptions::Disabled
>> - : LangOpts.Sanitize) {
>> +
>> llvm::SpecialCaseList::createOrDie(CGO.SanitizerBlacklistFile)) {
>>
>> // Initialize the type cache.
>> llvm::LLVMContext &LLVMContext = M.getContext();
>> @@ -122,7 +120,7 @@ CodeGenModule::CodeGenModule(ASTContext
>> createCUDARuntime();
>>
>> // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even
>> at O0.
>> - if (SanOpts.Thread ||
>> + if (LangOpts.Sanitize.Thread ||
>> (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel >
>> 0))
>> TBAA = new CodeGenTBAA(Context, VMContext, CodeGenOpts,
>> getLangOpts(),
>> getCXXABI().getMangleContext());
>> @@ -735,14 +733,13 @@ void CodeGenModule::SetLLVMFunctionAttri
>> if (!SanitizerBlacklist->isIn(*F)) {
>> // When AddressSanitizer is enabled, set SanitizeAddress attribute
>> // unless __attribute__((no_sanitize_address)) is used.
>> - if (SanOpts.Address && !D->hasAttr<NoSanitizeAddressAttr>())
>> + if (LangOpts.Sanitize.Address &&
>> !D->hasAttr<NoSanitizeAddressAttr>())
>> B.addAttribute(llvm::Attribute::SanitizeAddress);
>>
>
> Should this be set if the module is blacklisted? (Likewise for tsan and
> msan attributes.)
>
Currently SanitizerBlacklist::isIn(llvm::Function *F) returns true if
function or the module it is located in
is blacklisted. So, we won't set sanitize_address if module is blacklisted,
as expected.
>
> // Same for ThreadSanitizer and __attribute__((no_sanitize_thread))
>> - if (SanOpts.Thread && !D->hasAttr<NoSanitizeThreadAttr>()) {
>> + if (LangOpts.Sanitize.Thread && !D->hasAttr<NoSanitizeThreadAttr>())
>> B.addAttribute(llvm::Attribute::SanitizeThread);
>> - }
>> // Same for MemorySanitizer and __attribute__((no_sanitize_memory))
>> - if (SanOpts.Memory && !D->hasAttr<NoSanitizeMemoryAttr>())
>> + if (LangOpts.Sanitize.Memory && !D->hasAttr<NoSanitizeMemoryAttr>())
>> B.addAttribute(llvm::Attribute::SanitizeMemory);
>> }
>>
>> @@ -1966,7 +1963,7 @@ void CodeGenModule::EmitGlobalVarDefinit
>>
>> void CodeGenModule::reportGlobalToASan(llvm::GlobalVariable *GV,
>> SourceLocation Loc, bool
>> IsDynInit) {
>> - if (!SanOpts.Address)
>> + if (!LangOpts.Sanitize.Address)
>> return;
>> IsDynInit &= !SanitizerBlacklist->isIn(*GV, "init");
>> bool IsBlacklisted = SanitizerBlacklist->isIn(*GV);
>> @@ -2796,7 +2793,7 @@ CodeGenModule::GetAddrOfConstantStringFr
>> // Mangle the string literal if the ABI allows for it. However, we
>> cannot
>> // do this if we are compiling with ASan or -fwritable-strings
>> because they
>> // rely on strings having normal linkage.
>> - if (!LangOpts.WritableStrings && !SanOpts.Address &&
>> + if (!LangOpts.WritableStrings && !LangOpts.Sanitize.Address &&
>> getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) {
>> llvm::raw_svector_ostream Out(MangledNameBuffer);
>> getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
>>
>> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=212499&r1=212498&r2=212499&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
>> +++ cfe/trunk/lib/CodeGen/CodeGenModule.h Mon Jul 7 18:34:34 2014
>> @@ -475,8 +475,6 @@ class CodeGenModule : public CodeGenType
>>
>> std::unique_ptr<llvm::SpecialCaseList> SanitizerBlacklist;
>>
>> - const SanitizerOptions &SanOpts;
>> -
>> /// @}
>> public:
>> CodeGenModule(ASTContext &C, const CodeGenOptions &CodeGenOpts,
>> @@ -1014,7 +1012,10 @@ public:
>> return *SanitizerBlacklist;
>> }
>>
>> - const SanitizerOptions &getSanOpts() const { return SanOpts; }
>> + const SanitizerOptions &getSanOpts() const {
>> + return SanitizerBlacklist->isIn(TheModule) ?
>> SanitizerOptions::Disabled
>> + : LangOpts.Sanitize;
>> + }
>>
>> void reportGlobalToASan(llvm::GlobalVariable *GV, SourceLocation Loc,
>> bool IsDynInit = false);
>>
>> Modified: cfe/trunk/test/CodeGen/asan-globals.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/asan-globals.cpp?rev=212499&r1=212498&r2=212499&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/test/CodeGen/asan-globals.cpp (original)
>> +++ cfe/trunk/test/CodeGen/asan-globals.cpp Mon Jul 7 18:34:34 2014
>> @@ -1,5 +1,7 @@
>> // RUN: echo "global:*blacklisted_global*" > %t.blacklist
>> // RUN: %clang_cc1 -fsanitize=address -fsanitize-blacklist=%t.blacklist
>> -emit-llvm -o - %s | FileCheck %s
>> +// RUN: echo "src:%s" > %t.blacklist-src
>> +// RUN: %clang_cc1 -fsanitize=address
>> -fsanitize-blacklist=%t.blacklist-src -emit-llvm -o - %s | FileCheck %s
>> --check-prefix=BLACKLIST-SRC
>> // REQUIRES: shell
>>
>> int global;
>> @@ -21,3 +23,10 @@ void func() {
>> // CHECK: ![[BLACKLISTED_GLOBAL]] = metadata !{{{.*}}, null, i1 false,
>> i1 true}
>> // CHECK: ![[STATIC_VAR]] = metadata !{{{.*}} [[STATIC_LOC]], i1 false,
>> i1 false}
>> // CHECK: ![[LITERAL]] = metadata !{{{.*}} [[LITERAL_LOC]], i1 false, i1
>> false}
>> +
>> +// BLACKLIST-SRC: !llvm.asan.globals = !{![[GLOBAL:[0-9]+]],
>> ![[DYN_INIT_GLOBAL:[0-9]+]], ![[BLACKLISTED_GLOBAL:[0-9]+]],
>> ![[STATIC_VAR:[0-9]+]], ![[LITERAL:[0-9]+]]}
>> +// BLACKLIST-SRC: ![[GLOBAL]] = metadata !{{{.*}} null, i1 false, i1
>> true}
>> +// BLACKLIST-SRC: ![[DYN_INIT_GLOBAL]] = metadata !{{{.*}} null, i1
>> true, i1 true}
>> +// BLACKLIST-SRC: ![[BLACKLISTED_GLOBAL]] = metadata !{{{.*}}, null, i1
>> false, i1 true}
>> +// BLACKLIST-SRC: ![[STATIC_VAR]] = metadata !{{{.*}} null, i1 false, i1
>> true}
>> +// BLACKLIST-SRC: ![[LITERAL]] = metadata !{{{.*}} null, i1 false, i1
>> true}
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>>
>
>
--
Alexey Samsonov
vonosmas at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140708/ef73ffb7/attachment.html>
More information about the cfe-commits
mailing list