[Sanitize] Don't emit function attribute sanitize_address/thread/memory if the function is blacklisted.

llvm-svn: 176550
This commit is contained in:
Alexey Samsonov 2013-03-06 10:54:18 +00:00
parent 5d14fc061f
commit a1c0a2a2c6
2 changed files with 43 additions and 21 deletions

View File

@ -619,16 +619,20 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
else if (LangOpts.getStackProtector() == LangOptions::SSPReq) else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
F->addFnAttr(llvm::Attribute::StackProtectReq); F->addFnAttr(llvm::Attribute::StackProtectReq);
// When AddressSanitizer is enabled, set SanitizeAddress attribute // Add sanitizer attributes if function is not blacklisted.
// unless __attribute__((no_sanitize_address)) is used. if (!SanitizerBlacklist.isIn(*F)) {
if (SanOpts.Address && !D->hasAttr<NoSanitizeAddressAttr>()) // When AddressSanitizer is enabled, set SanitizeAddress attribute
F->addFnAttr(llvm::Attribute::SanitizeAddress); // unless __attribute__((no_sanitize_address)) is used.
// Same for ThreadSanitizer and __attribute__((no_sanitize_thread)) if (SanOpts.Address && !D->hasAttr<NoSanitizeAddressAttr>())
if (SanOpts.Thread && !D->hasAttr<NoSanitizeThreadAttr>()) F->addFnAttr(llvm::Attribute::SanitizeAddress);
F->addFnAttr(llvm::Attribute::SanitizeThread); // Same for ThreadSanitizer and __attribute__((no_sanitize_thread))
// Same for MemorySanitizer and __attribute__((no_sanitize_memory)) if (SanOpts.Thread && !D->hasAttr<NoSanitizeThreadAttr>()) {
if (SanOpts.Memory && !D->hasAttr<NoSanitizeMemoryAttr>()) F->addFnAttr(llvm::Attribute::SanitizeThread);
F->addFnAttr(llvm::Attribute::SanitizeMemory); }
// Same for MemorySanitizer and __attribute__((no_sanitize_memory))
if (SanOpts.Memory && !D->hasAttr<NoSanitizeMemoryAttr>())
F->addFnAttr(llvm::Attribute::SanitizeMemory);
}
unsigned alignment = D->getMaxAlignment() / Context.getCharWidth(); unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
if (alignment) if (alignment)

View File

@ -1,9 +1,11 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck -check-prefix=WITHOUT %s // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s | FileCheck -check-prefix=WITHOUT %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s -fsanitize=address | FileCheck -check-prefix=ASAN %s // RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s -fsanitize=address | FileCheck -check-prefix=ASAN %s
// RUN: echo "src:%s" > %t // RUN: echo "src:%s" > %t.file.blacklist
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s -fsanitize=address -fsanitize-blacklist=%t | FileCheck -check-prefix=BL %s // RUN: echo "fun:*BlacklistedFunction*" > %t.func.blacklist
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s -fsanitize=address -fsanitize-blacklist=%t.file.blacklist | FileCheck -check-prefix=BLFILE %s
// RUN: %clang_cc1 -triple x86_64-apple-darwin -emit-llvm -o - %s -fsanitize=address -fsanitize-blacklist=%t.func.blacklist | FileCheck -check-prefix=BLFUNC %s
// FIXME: %t is like "src:x:\path\to\clang\test\CodeGen\address-safety-attr.cpp" // FIXME: %t.file.blacklist is like "src:x:\path\to\clang\test\CodeGen\address-safety-attr.cpp"
// REQUIRES: shell // REQUIRES: shell
// The sanitize_address attribute should be attached to functions // The sanitize_address attribute should be attached to functions
@ -11,31 +13,42 @@
// is present. // is present.
// WITHOUT: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]] // WITHOUT: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
// BL: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]] // BLFILE: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
// BLFUNC: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
// ASAN: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]] // ASAN: NoAddressSafety1{{.*}}) [[NOATTR:#[0-9]+]]
__attribute__((no_sanitize_address)) __attribute__((no_sanitize_address))
int NoAddressSafety1(int *a) { return *a; } int NoAddressSafety1(int *a) { return *a; }
// WITHOUT: NoAddressSafety2{{.*}}) [[NOATTR]] // WITHOUT: NoAddressSafety2{{.*}}) [[NOATTR]]
// BL: NoAddressSafety2{{.*}}) [[NOATTR]] // BLFILE: NoAddressSafety2{{.*}}) [[NOATTR]]
// BLFUNC: NoAddressSafety2{{.*}}) [[NOATTR]]
// ASAN: NoAddressSafety2{{.*}}) [[NOATTR]] // ASAN: NoAddressSafety2{{.*}}) [[NOATTR]]
__attribute__((no_sanitize_address)) __attribute__((no_sanitize_address))
int NoAddressSafety2(int *a); int NoAddressSafety2(int *a);
int NoAddressSafety2(int *a) { return *a; } int NoAddressSafety2(int *a) { return *a; }
// WITHOUT: AddressSafetyOk{{.*}}) [[NOATTR]] // WITHOUT: AddressSafetyOk{{.*}}) [[NOATTR]]
// BL: AddressSafetyOk{{.*}}) [[NOATTR]] // BLFILE: AddressSafetyOk{{.*}}) [[NOATTR]]
// BLFUNC: AddressSafetyOk{{.*}}) [[WITH:#[0-9]+]]
// ASAN: AddressSafetyOk{{.*}}) [[WITH:#[0-9]+]] // ASAN: AddressSafetyOk{{.*}}) [[WITH:#[0-9]+]]
int AddressSafetyOk(int *a) { return *a; } int AddressSafetyOk(int *a) { return *a; }
// WITHOUT: BlacklistedFunction{{.*}}) [[NOATTR]]
// BLFILE: BlacklistedFunction{{.*}}) [[NOATTR]]
// BLFUNC: BlacklistedFunction{{.*}}) [[NOATTR]]
// ASAN: BlacklistedFunction{{.*}}) [[WITH]]
int BlacklistedFunction(int *a) { return *a; }
// WITHOUT: TemplateAddressSafetyOk{{.*}}) [[NOATTR]] // WITHOUT: TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
// BL: TemplateAddressSafetyOk{{.*}}) [[NOATTR]] // BLFILE: TemplateAddressSafetyOk{{.*}}) [[NOATTR]]
// BLFUNC: TemplateAddressSafetyOk{{.*}}) [[WITH]]
// ASAN: TemplateAddressSafetyOk{{.*}}) [[WITH]] // ASAN: TemplateAddressSafetyOk{{.*}}) [[WITH]]
template<int i> template<int i>
int TemplateAddressSafetyOk() { return i; } int TemplateAddressSafetyOk() { return i; }
// WITHOUT: TemplateNoAddressSafety{{.*}}) [[NOATTR]] // WITHOUT: TemplateNoAddressSafety{{.*}}) [[NOATTR]]
// BL: TemplateNoAddressSafety{{.*}}) [[NOATTR]] // BLFILE: TemplateNoAddressSafety{{.*}}) [[NOATTR]]
// BLFUNC: TemplateNoAddressSafety{{.*}}) [[NOATTR]]
// ASAN: TemplateNoAddressSafety{{.*}}) [[NOATTR]] // ASAN: TemplateNoAddressSafety{{.*}}) [[NOATTR]]
template<int i> template<int i>
__attribute__((no_sanitize_address)) __attribute__((no_sanitize_address))
@ -48,14 +61,19 @@ int force_instance = TemplateAddressSafetyOk<42>()
int global1 = 0; int global1 = 0;
int global2 = *(int*)((char*)&global1+1); int global2 = *(int*)((char*)&global1+1);
// WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR_NO_TF:#[0-9]+]] // WITHOUT: @__cxx_global_var_init{{.*}}[[NOATTR_NO_TF:#[0-9]+]]
// BL: @__cxx_global_var_init{{.*}}[[NOATTR_NO_TF:#[0-9]+]] // BLFILE: @__cxx_global_var_init{{.*}}[[NOATTR_NO_TF:#[0-9]+]]
// BLFUNC: @__cxx_global_var_init{{.*}}[[WITH_NO_TF:#[0-9]+]]
// ASAN: @__cxx_global_var_init{{.*}}[[WITH_NO_TF:#[0-9]+]] // ASAN: @__cxx_global_var_init{{.*}}[[WITH_NO_TF:#[0-9]+]]
// WITHOUT: attributes [[NOATTR]] = { nounwind{{.*}} } // WITHOUT: attributes [[NOATTR]] = { nounwind{{.*}} }
// WITHOUT: attributes [[NOATTR_NO_TF]] = { nounwind } // WITHOUT: attributes [[NOATTR_NO_TF]] = { nounwind }
// BL: attributes [[NOATTR]] = { nounwind{{.*}} } // BLFILE: attributes [[NOATTR]] = { nounwind{{.*}} }
// BL: attributes [[NOATTR_NO_TF]] = { nounwind } // BLFILE: attributes [[NOATTR_NO_TF]] = { nounwind }
// BLFUNC: attributes [[NOATTR]] = { nounwind{{.*}} }
// BLFUNC: attributes [[WITH]] = { nounwind sanitize_address{{.*}} }
// BLFUNC: attributes [[WITH_NO_TF]] = { nounwind sanitize_address }
// ASAN: attributes [[NOATTR]] = { nounwind{{.*}} } // ASAN: attributes [[NOATTR]] = { nounwind{{.*}} }
// ASAN: attributes [[WITH]] = { nounwind sanitize_address{{.*}} } // ASAN: attributes [[WITH]] = { nounwind sanitize_address{{.*}} }