From: Douglas Katzman Date: Fri, 14 Oct 2016 19:55:09 +0000 (+0000) Subject: Implement no_sanitize_address for global vars X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a669162b45a961ea66f7f4d7e1a12814ef77d918;p=clang Implement no_sanitize_address for global vars git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284272 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/Attr.td b/include/clang/Basic/Attr.td index 1fea69ff0e..2855207b4b 100644 --- a/include/clang/Basic/Attr.td +++ b/include/clang/Basic/Attr.td @@ -1705,7 +1705,8 @@ def X86ForceAlignArgPointer : InheritableAttr, TargetSpecificAttr { def NoSanitize : InheritableAttr { let Spellings = [GNU<"no_sanitize">, CXX11<"clang", "no_sanitize">]; let Args = [VariadicStringArgument<"Sanitizers">]; - let Subjects = SubjectList<[Function, ObjCMethod], ErrorDiag>; + let Subjects = SubjectList<[Function, ObjCMethod, GlobalVar], ErrorDiag, + "ExpectedFunctionMethodOrGlobalVar">; let Documentation = [NoSanitizeDocs]; let AdditionalMembers = [{ SanitizerMask getMask() const { @@ -1727,7 +1728,8 @@ def NoSanitizeSpecific : InheritableAttr { GCC<"no_sanitize_address">, GCC<"no_sanitize_thread">, GNU<"no_sanitize_memory">]; - let Subjects = SubjectList<[Function], ErrorDiag>; + let Subjects = SubjectList<[Function, GlobalVar], ErrorDiag, + "ExpectedFunctionGlobalVarMethodOrProperty">; let Documentation = [NoSanitizeAddressDocs, NoSanitizeThreadDocs, NoSanitizeMemoryDocs]; let ASTNode = 0; diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 872311f602..b92ca3d34b 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -2577,6 +2577,7 @@ def warn_attribute_wrong_decl_type : Warning< "|functions, methods and blocks" "|functions, methods, and classes" "|functions, methods, and parameters" + "|functions, methods, and global variables" "|classes" "|enums" "|variables" diff --git a/include/clang/Sema/AttributeList.h b/include/clang/Sema/AttributeList.h index ff04f26496..96653ef8ea 100644 --- a/include/clang/Sema/AttributeList.h +++ b/include/clang/Sema/AttributeList.h @@ -891,6 +891,7 @@ enum AttributeDeclKind { ExpectedFunctionMethodOrBlock, ExpectedFunctionMethodOrClass, ExpectedFunctionMethodOrParameter, + ExpectedFunctionMethodOrGlobalVar, ExpectedClass, ExpectedEnum, ExpectedVariable, diff --git a/lib/CodeGen/SanitizerMetadata.cpp b/lib/CodeGen/SanitizerMetadata.cpp index 2a338bac4b..9848e3e452 100644 --- a/lib/CodeGen/SanitizerMetadata.cpp +++ b/lib/CodeGen/SanitizerMetadata.cpp @@ -63,7 +63,13 @@ void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV, std::string QualName; llvm::raw_string_ostream OS(QualName); D.printQualifiedName(OS); - reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit); + + bool IsBlacklisted = false; + for (auto Attr : D.specific_attrs()) + if (Attr->getMask() & SanitizerKind::Address) + IsBlacklisted = true; + reportGlobalToASan(GV, D.getLocation(), OS.str(), D.getType(), IsDynInit, + IsBlacklisted); } void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) { diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp index 5a78a31657..15f3139e1a 100644 --- a/lib/Sema/SemaDeclAttr.cpp +++ b/lib/Sema/SemaDeclAttr.cpp @@ -5313,9 +5313,15 @@ static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) S.Diag(Attr.getLoc(), diag::ext_cxx14_attr) << Attr.getName(); - D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str, - Replacement, - Attr.getAttributeSpellingListIndex())); + D->addAttr(::new (S.Context) + DeprecatedAttr(Attr.getRange(), S.Context, Str, Replacement, + Attr.getAttributeSpellingListIndex())); +} + +static bool isGlobalVar(const Decl *D) { + if (const auto *S = dyn_cast(D)) + return S->hasGlobalStorage(); + return false; } static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { @@ -5333,7 +5339,9 @@ static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; - + else if (isGlobalVar(D) && SanitizerName != "address") + S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) + << Attr.getName() << ExpectedFunctionOrMethod; Sanitizers.push_back(SanitizerName); } @@ -5346,12 +5354,14 @@ static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, const AttributeList &Attr) { StringRef AttrName = Attr.getName()->getName(); normalizeName(AttrName); - StringRef SanitizerName = - llvm::StringSwitch(AttrName) - .Case("no_address_safety_analysis", "address") - .Case("no_sanitize_address", "address") - .Case("no_sanitize_thread", "thread") - .Case("no_sanitize_memory", "memory"); + StringRef SanitizerName = llvm::StringSwitch(AttrName) + .Case("no_address_safety_analysis", "address") + .Case("no_sanitize_address", "address") + .Case("no_sanitize_thread", "thread") + .Case("no_sanitize_memory", "memory"); + if (isGlobalVar(D) && SanitizerName != "address") + S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) + << Attr.getName() << ExpectedFunction; D->addAttr(::new (S.Context) NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1, Attr.getAttributeSpellingListIndex())); diff --git a/test/CodeGen/asan-globals.cpp b/test/CodeGen/asan-globals.cpp index 20c1fa702a..b6a323f032 100644 --- a/test/CodeGen/asan-globals.cpp +++ b/test/CodeGen/asan-globals.cpp @@ -7,6 +7,7 @@ int global; int dyn_init_global = global; +int __attribute__((no_sanitize("address"))) attributed_global; int blacklisted_global; void func() { @@ -14,24 +15,26 @@ void func() { const char *literal = "Hello, world!"; } -// CHECK: !llvm.asan.globals = !{![[EXTRA_GLOBAL:[0-9]+]], ![[GLOBAL:[0-9]+]], ![[DYN_INIT_GLOBAL:[0-9]+]], ![[BLACKLISTED_GLOBAL:[0-9]+]], ![[STATIC_VAR:[0-9]+]], ![[LITERAL:[0-9]+]]} +// CHECK: !llvm.asan.globals = !{![[EXTRA_GLOBAL:[0-9]+]], ![[GLOBAL:[0-9]+]], ![[DYN_INIT_GLOBAL:[0-9]+]], ![[ATTR_GLOBAL:[0-9]+]], ![[BLACKLISTED_GLOBAL:[0-9]+]], ![[STATIC_VAR:[0-9]+]], ![[LITERAL:[0-9]+]]} // CHECK: ![[EXTRA_GLOBAL]] = !{{{.*}} ![[EXTRA_GLOBAL_LOC:[0-9]+]], !"extra_global", i1 false, i1 false} // CHECK: ![[EXTRA_GLOBAL_LOC]] = !{!"{{.*}}extra-source.cpp", i32 1, i32 5} // CHECK: ![[GLOBAL]] = !{{{.*}} ![[GLOBAL_LOC:[0-9]+]], !"global", i1 false, i1 false} // CHECK: ![[GLOBAL_LOC]] = !{!"{{.*}}asan-globals.cpp", i32 8, i32 5} // CHECK: ![[DYN_INIT_GLOBAL]] = !{{{.*}} ![[DYN_INIT_LOC:[0-9]+]], !"dyn_init_global", i1 true, i1 false} // CHECK: ![[DYN_INIT_LOC]] = !{!"{{.*}}asan-globals.cpp", i32 9, i32 5} +// CHECK: ![[ATTR_GLOBAL]] = !{{{.*}}, null, null, i1 false, i1 true} // CHECK: ![[BLACKLISTED_GLOBAL]] = !{{{.*}}, null, null, i1 false, i1 true} // CHECK: ![[STATIC_VAR]] = !{{{.*}} ![[STATIC_LOC:[0-9]+]], !"static_var", i1 false, i1 false} -// CHECK: ![[STATIC_LOC]] = !{!"{{.*}}asan-globals.cpp", i32 13, i32 14} +// CHECK: ![[STATIC_LOC]] = !{!"{{.*}}asan-globals.cpp", i32 14, i32 14} // CHECK: ![[LITERAL]] = !{{{.*}} ![[LITERAL_LOC:[0-9]+]], !"", i1 false, i1 false} -// CHECK: ![[LITERAL_LOC]] = !{!"{{.*}}asan-globals.cpp", i32 14, i32 25} +// CHECK: ![[LITERAL_LOC]] = !{!"{{.*}}asan-globals.cpp", i32 15, i32 25} -// BLACKLIST-SRC: !llvm.asan.globals = !{![[EXTRA_GLOBAL:[0-9]+]], ![[GLOBAL:[0-9]+]], ![[DYN_INIT_GLOBAL:[0-9]+]], ![[BLACKLISTED_GLOBAL:[0-9]+]], ![[STATIC_VAR:[0-9]+]], ![[LITERAL:[0-9]+]]} +// BLACKLIST-SRC: !llvm.asan.globals = !{![[EXTRA_GLOBAL:[0-9]+]], ![[GLOBAL:[0-9]+]], ![[DYN_INIT_GLOBAL:[0-9]+]], ![[ATTR_GLOBAL:[0-9]+]], ![[BLACKLISTED_GLOBAL:[0-9]+]], ![[STATIC_VAR:[0-9]+]], ![[LITERAL:[0-9]+]]} // BLACKLIST-SRC: ![[EXTRA_GLOBAL]] = !{{{.*}} ![[EXTRA_GLOBAL_LOC:[0-9]+]], !"extra_global", i1 false, i1 false} // BLACKLIST-SRC: ![[EXTRA_GLOBAL_LOC]] = !{!"{{.*}}extra-source.cpp", i32 1, i32 5} // BLACKLIST-SRC: ![[GLOBAL]] = !{{{.*}} null, null, i1 false, i1 true} // BLACKLIST-SRC: ![[DYN_INIT_GLOBAL]] = !{{{.*}} null, null, i1 true, i1 true} +// BLACKLIST-SRC: ![[ATTR_GLOBAL]] = !{{{.*}}, null, null, i1 false, i1 true} // BLACKLIST-SRC: ![[BLACKLISTED_GLOBAL]] = !{{{.*}}, null, null, i1 false, i1 true} // BLACKLIST-SRC: ![[STATIC_VAR]] = !{{{.*}} null, null, i1 false, i1 true} // BLACKLIST-SRC: ![[LITERAL]] = !{{{.*}} null, null, i1 false, i1 true} diff --git a/test/SemaCXX/attr-no-sanitize-address.cpp b/test/SemaCXX/attr-no-sanitize-address.cpp index 9499742ac5..127129c948 100644 --- a/test/SemaCXX/attr-no-sanitize-address.cpp +++ b/test/SemaCXX/attr-no-sanitize-address.cpp @@ -21,9 +21,6 @@ int noanal_testfn(int y) { return x; } -int noanal_test_var NO_SANITIZE_ADDRESS; // \ - // expected-error {{'no_sanitize_address' attribute only applies to functions}} - class NoanalFoo { private: int test_field NO_SANITIZE_ADDRESS; // \ diff --git a/test/SemaCXX/attr-no-sanitize.cpp b/test/SemaCXX/attr-no-sanitize.cpp index 741f760628..965def6f02 100644 --- a/test/SemaCXX/attr-no-sanitize.cpp +++ b/test/SemaCXX/attr-no-sanitize.cpp @@ -2,8 +2,6 @@ // RUN: not %clang_cc1 -std=c++11 -ast-dump %s | FileCheck --check-prefix=DUMP %s // RUN: not %clang_cc1 -std=c++11 -ast-print %s | FileCheck --check-prefix=PRINT %s -int v1 __attribute__((no_sanitize("address"))); // expected-error{{'no_sanitize' attribute only applies to functions and methods}} - int f1() __attribute__((no_sanitize)); // expected-error{{'no_sanitize' attribute takes at least 1 argument}} int f2() __attribute__((no_sanitize(1))); // expected-error{{'no_sanitize' attribute requires a string}}