r361274 - Do not use the incorrect attribute spelling list index when translating a no_sanitize_foo attribute into a no_sanitize("foo") attribute.

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Tue May 21 10:24:49 PDT 2019


Author: aaronballman
Date: Tue May 21 10:24:49 2019
New Revision: 361274

URL: http://llvm.org/viewvc/llvm-project?rev=361274&view=rev
Log:
Do not use the incorrect attribute spelling list index when translating a no_sanitize_foo attribute into a no_sanitize("foo") attribute.

This fixes a crash when AST pretty printing declarations marked with no_sanitize_memory.

Added:
    cfe/trunk/test/AST/ast-print-no-sanitize.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=361274&r1=361273&r2=361274&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Tue May 21 10:24:49 2019
@@ -6329,9 +6329,21 @@ static void handleNoSanitizeSpecificAttr
   if (isGlobalVar(D) && SanitizerName != "address")
     S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
         << AL << ExpectedFunction;
-  D->addAttr(::new (S.Context)
-                 NoSanitizeAttr(AL.getRange(), S.Context, &SanitizerName, 1,
-                                AL.getAttributeSpellingListIndex()));
+
+  // FIXME: Rather than create a NoSanitizeSpecificAttr, this creates a
+  // NoSanitizeAttr object; but we need to calculate the correct spelling list
+  // index rather than incorrectly assume the index for NoSanitizeSpecificAttr
+  // has the same spellings as the index for NoSanitizeAttr. We don't have a
+  // general way to "translate" between the two, so this hack attempts to work
+  // around the issue with hard-coded indicies. This is critical for calling
+  // getSpelling() or prettyPrint() on the resulting semantic attribute object
+  // without failing assertions.
+  unsigned TranslatedSpellingIndex = 0;
+  if (AL.isC2xAttribute() || AL.isCXX11Attribute())
+    TranslatedSpellingIndex = 1;
+
+  D->addAttr(::new (S.Context) NoSanitizeAttr(
+      AL.getRange(), S.Context, &SanitizerName, 1, TranslatedSpellingIndex));
 }
 
 static void handleInternalLinkageAttr(Sema &S, Decl *D, const ParsedAttr &AL) {

Added: cfe/trunk/test/AST/ast-print-no-sanitize.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/AST/ast-print-no-sanitize.cpp?rev=361274&view=auto
==============================================================================
--- cfe/trunk/test/AST/ast-print-no-sanitize.cpp (added)
+++ cfe/trunk/test/AST/ast-print-no-sanitize.cpp Tue May 21 10:24:49 2019
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -std=c++11 -ast-print %s -o - | FileCheck %s
+
+void should_not_crash_1() __attribute__((no_sanitize_memory));
+[[clang::no_sanitize_memory]] void should_not_crash_2();
+
+// CHECK: void should_not_crash_1() __attribute__((no_sanitize("memory")));
+// CHECK: void should_not_crash_2() {{\[\[}}clang::no_sanitize("memory"){{\]\]}};




More information about the cfe-commits mailing list