]> granicus.if.org Git - clang/commitdiff
[XRay][llvm+clang] Consolidate attribute list files
authorDean Michael Berris <dberris@google.com>
Mon, 9 Apr 2018 04:02:09 +0000 (04:02 +0000)
committerDean Michael Berris <dberris@google.com>
Mon, 9 Apr 2018 04:02:09 +0000 (04:02 +0000)
Summary:
This change consolidates the always/never lists that may be provided to
clang to externally control which functions should be XRay instrumented
by imbuing attributes. The files follow the same format as defined in
https://clang.llvm.org/docs/SanitizerSpecialCaseList.html for the
sanitizer blacklist.

We also deprecate the existing `-fxray-instrument-always=` and
`-fxray-instrument-never=` flags, in favour of `-fxray-attr-list=`.

This fixes http://llvm.org/PR34721.

Reviewers: echristo, vlad.tsyrklevich, eugenis

Reviewed By: vlad.tsyrklevich

Subscribers: llvm-commits, cfe-commits

Differential Revision: https://reviews.llvm.org/D45357

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@329543 91177308-0d34-0410-b5e6-96231b3b80d8

12 files changed:
include/clang/Basic/LangOptions.h
include/clang/Basic/XRayLists.h
include/clang/Driver/Options.td
include/clang/Driver/XRayArgs.h
lib/AST/ASTContext.cpp
lib/Basic/XRayLists.cpp
lib/Driver/XRayArgs.cpp
lib/Frontend/CompilerInvocation.cpp
test/CodeGen/xray-always-instrument.cpp
test/CodeGen/xray-attr-list.cpp [new file with mode: 0644]
test/CodeGen/xray-imbue-arg1.cpp
test/CodeGen/xray-never-instrument.cpp [new file with mode: 0644]

index 905f60ad552aea0f8468f04e321e2af091b0f303..debf80d1bbfe7026127057b246fe7c541bdd610e 100644 (file)
@@ -148,13 +148,20 @@ public:
   /// \brief Paths to the XRay "always instrument" files specifying which
   /// objects (files, functions, variables) should be imbued with the XRay
   /// "always instrument" attribute.
+  /// WARNING: This is a deprecated field and will go away in the future.
   std::vector<std::string> XRayAlwaysInstrumentFiles;
 
   /// \brief Paths to the XRay "never instrument" files specifying which
   /// objects (files, functions, variables) should be imbued with the XRay
   /// "never instrument" attribute.
+  /// WARNING: This is a deprecated field and will go away in the future.
   std::vector<std::string> XRayNeverInstrumentFiles;
 
+  /// \brief Paths to the XRay attribute list files, specifying which objects
+  /// (files, functions, variables) should be imbued with the appropriate XRay
+  /// attribute(s).
+  std::vector<std::string> XRayAttrListFiles;
+
   clang::ObjCRuntime ObjCRuntime;
 
   std::string ObjCConstantStringClass;
index 8cfea70e280a9eb60d8fb973ea62cf9948a57b85..244b1d533b7ca6fbbba06835752926322d8fd405 100644 (file)
@@ -26,12 +26,13 @@ namespace clang {
 class XRayFunctionFilter {
   std::unique_ptr<llvm::SpecialCaseList> AlwaysInstrument;
   std::unique_ptr<llvm::SpecialCaseList> NeverInstrument;
+  std::unique_ptr<llvm::SpecialCaseList> AttrList;
   SourceManager &SM;
 
 public:
   XRayFunctionFilter(ArrayRef<std::string> AlwaysInstrumentPaths,
                      ArrayRef<std::string> NeverInstrumentPaths,
-                     SourceManager &SM);
+                     ArrayRef<std::string> AttrListPaths, SourceManager &SM);
 
   enum class ImbueAttribute {
     NONE,
index 9367634d8afbd13f42f8f1a759dd24266e5c0f27..58b5341b3487d6bec5036c5c76083e8d1da82a65 100644 (file)
@@ -1095,11 +1095,15 @@ def fxray_instruction_threshold_ :
 def fxray_always_instrument :
   JoinedOrSeparate<["-"], "fxray-always-instrument=">,
   Group<f_Group>, Flags<[CC1Option]>,
-  HelpText<"Filename defining the whitelist for imbuing the 'always instrument' XRay attribute.">;
+  HelpText<"DEPRECATED: Filename defining the whitelist for imbuing the 'always instrument' XRay attribute.">;
 def fxray_never_instrument :
   JoinedOrSeparate<["-"], "fxray-never-instrument=">,
   Group<f_Group>, Flags<[CC1Option]>,
-  HelpText<"Filename defining the whitelist for imbuing the 'never instrument' XRay attribute.">;
+  HelpText<"DEPRECATED: Filename defining the whitelist for imbuing the 'never instrument' XRay attribute.">;
+def fxray_attr_list :
+  JoinedOrSeparate<["-"], "fxray-attr-list=">,
+  Group<f_Group>, Flags<[CC1Option]>,
+  HelpText<"Filename defining the list of functions/types for imbuing XRay attributes.">;
 
 def fxray_always_emit_customevents : Flag<["-"], "fxray-always-emit-customevents">, Group<f_Group>,
   Flags<[CC1Option]>,
index 8eedd1dba1de99498d7c680b502edb451b6f42b3..535c030e8c55505a96348952f9f7c559e96c3e8f 100644 (file)
@@ -21,6 +21,7 @@ class ToolChain;
 class XRayArgs {
   std::vector<std::string> AlwaysInstrumentFiles;
   std::vector<std::string> NeverInstrumentFiles;
+  std::vector<std::string> AttrListFiles;
   std::vector<std::string> ExtraDeps;
   bool XRayInstrument = false;
   int InstructionThreshold = 200;
index 79ac53cde2397e8df50a44eb39eed6a434d1d069..4b095ce45ab4e42bbfa29bcb3e246822e61cb1e4 100644 (file)
@@ -788,7 +788,8 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
       SubstTemplateTemplateParmPacks(this_()), SourceMgr(SM), LangOpts(LOpts),
       SanitizerBL(new SanitizerBlacklist(LangOpts.SanitizerBlacklistFiles, SM)),
       XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles,
-                                        LangOpts.XRayNeverInstrumentFiles, SM)),
+                                        LangOpts.XRayNeverInstrumentFiles,
+                                        LangOpts.XRayAttrListFiles, SM)),
       PrintingPolicy(LOpts), Idents(idents), Selectors(sels),
       BuiltinInfo(builtins), DeclarationNames(*this), Comments(SM),
       CommentCommandTraits(BumpAlloc, LOpts.CommentOpts), LastSDM(nullptr, 0) {
index 462777d53400f68577e11536c69de5ffad42e6ae..ad331899d2e224a302145432e5ae0030d8d28c31 100644 (file)
@@ -16,24 +16,32 @@ using namespace clang;
 
 XRayFunctionFilter::XRayFunctionFilter(
     ArrayRef<std::string> AlwaysInstrumentPaths,
-    ArrayRef<std::string> NeverInstrumentPaths, SourceManager &SM)
+    ArrayRef<std::string> NeverInstrumentPaths,
+    ArrayRef<std::string> AttrListPaths, SourceManager &SM)
     : AlwaysInstrument(
           llvm::SpecialCaseList::createOrDie(AlwaysInstrumentPaths)),
       NeverInstrument(llvm::SpecialCaseList::createOrDie(NeverInstrumentPaths)),
-      SM(SM) {}
+      AttrList(llvm::SpecialCaseList::createOrDie(AttrListPaths)), SM(SM) {}
 
 XRayFunctionFilter::ImbueAttribute
 XRayFunctionFilter::shouldImbueFunction(StringRef FunctionName) const {
   // First apply the always instrument list, than if it isn't an "always" see
   // whether it's treated as a "never" instrument function.
+  // TODO: Remove these as they're deprecated; use the AttrList exclusively.
   if (AlwaysInstrument->inSection("xray_always_instrument", "fun", FunctionName,
-                                  "arg1"))
+                                  "arg1") ||
+      AttrList->inSection("always", "fun", FunctionName, "arg1"))
     return ImbueAttribute::ALWAYS_ARG1;
   if (AlwaysInstrument->inSection("xray_always_instrument", "fun",
-                                  FunctionName))
+                                  FunctionName) ||
+      AttrList->inSection("always", "fun", FunctionName))
     return ImbueAttribute::ALWAYS;
-  if (NeverInstrument->inSection("xray_never_instrument", "fun", FunctionName))
+
+  if (NeverInstrument->inSection("xray_never_instrument", "fun",
+                                 FunctionName) ||
+      AttrList->inSection("never", "fun", FunctionName))
     return ImbueAttribute::NEVER;
+
   return ImbueAttribute::NONE;
 }
 
@@ -41,10 +49,12 @@ XRayFunctionFilter::ImbueAttribute
 XRayFunctionFilter::shouldImbueFunctionsInFile(StringRef Filename,
                                                StringRef Category) const {
   if (AlwaysInstrument->inSection("xray_always_instrument", "src", Filename,
-                                  Category))
+                                  Category) ||
+      AttrList->inSection("always", "src", Filename, Category))
     return ImbueAttribute::ALWAYS;
   if (NeverInstrument->inSection("xray_never_instrument", "src", Filename,
-                                 Category))
+                                 Category) ||
+      AttrList->inSection("never", "src", Filename, Category))
     return ImbueAttribute::NEVER;
   return ImbueAttribute::NONE;
 }
index e151cb907cd9552ddf8d658b929fce955526d6b4..cc109d10cf36e11c6f6c388dc9860ea8d60eb502 100644 (file)
@@ -99,6 +99,15 @@ XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
       } else
         D.Diag(clang::diag::err_drv_no_such_file) << Filename;
     }
+
+    for (const auto &Filename :
+         Args.getAllArgValues(options::OPT_fxray_attr_list)) {
+      if (llvm::sys::fs::exists(Filename)) {
+        AttrListFiles.push_back(Filename);
+        ExtraDeps.push_back(Filename);
+      } else
+        D.Diag(clang::diag::err_drv_no_such_file) << Filename;
+    }
   }
 }
 
@@ -127,6 +136,12 @@ void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
     CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
   }
 
+  for (const auto&AttrFile : AttrListFiles) {
+    SmallString<64> AttrListFileOpt("-fxray-attr-list=");
+    AttrListFileOpt += AttrFile;
+    CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
+  }
+
   for (const auto &Dep : ExtraDeps) {
     SmallString<64> ExtraDepOpt("-fdepfile-entry=");
     ExtraDepOpt += Dep;
index 1a3b67f096831075f130444afd9290dcd2ff50b6..687dc688e1d0cd84933ce4cccf9de0380bb3697c 100644 (file)
@@ -2640,6 +2640,7 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
       Args.getAllArgValues(OPT_fxray_always_instrument);
   Opts.XRayNeverInstrumentFiles =
       Args.getAllArgValues(OPT_fxray_never_instrument);
+  Opts.XRayAttrListFiles = Args.getAllArgValues(OPT_fxray_attr_list);
 
   // -fallow-editor-placeholders
   Opts.AllowEditorPlaceholders = Args.hasArg(OPT_fallow_editor_placeholders);
index 60d8595699584c56565a275dc0c19274d3150c52..fb6690577f580668e7a42943ee473fbd1d8c86de 100644 (file)
@@ -1,6 +1,14 @@
 // RUN: echo "fun:*foo*" > %t.always-instrument
 // RUN: echo "src:*xray-always-instrument.cpp" >> %t.always-instrument
-// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 -fxray-always-instrument=%t.always-instrument -emit-llvm -o - %s -triple x86_64-unknown-linux-gnu | FileCheck %s
+// RUN: echo "[always]" > %t.xray-attrlist
+// RUN: echo "fun:*foo*" >> %t.xray-attrlist
+// RUN: echo "src:*xray-always-instrument.cpp" >> %t.xray-attrlist
+// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 \
+// RUN:     -fxray-always-instrument=%t.always-instrument -emit-llvm -o - %s \
+// RUN:     -triple x86_64-unknown-linux-gnu | FileCheck %s
+// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 \
+// RUN:     -fxray-attr-list=%t.xray-attrlist -emit-llvm -o - %s \
+// RUN:     -triple x86_64-unknown-linux-gnu | FileCheck %s
 
 void foo() {}
 
diff --git a/test/CodeGen/xray-attr-list.cpp b/test/CodeGen/xray-attr-list.cpp
new file mode 100644 (file)
index 0000000..f2e4877
--- /dev/null
@@ -0,0 +1,19 @@
+// RUN: echo "[always]" > %t.xray-attrlist
+// RUN: echo "fun:*always*" >> %t.xray-attrlist
+// RUN: echo "[never]" >> %t.xray-attrlist
+// RUN: echo "fun:*never*" >> %t.xray-attrlist
+// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 \
+// RUN:     -fxray-attr-list=%t.xray-attrlist -emit-llvm -o - %s \
+// RUN:     -triple x86_64-unknown-linux-gnu | FileCheck %s
+
+void always() {}
+void never() {}
+[[clang::xray_never_instrument]] void alwaysNever() {}
+[[clang::xray_always_instrument]] void neverAlways() {}
+
+// CHECK: define void @_Z6alwaysv() #[[ALWAYSATTR:[0-9]+]] {
+// CHECK: define void @_Z5neverv() #[[NEVERATTR:[0-9]+]] {
+// CHECK: define void @_Z11alwaysNeverv() #[[NEVERATTR]] {
+// CHECK: define void @_Z11neverAlwaysv() #[[ALWAYSATTR]] {
+// CHECK: attributes #[[ALWAYSATTR]] = {{.*}} "function-instrument"="xray-always" {{.*}}
+// CHECK: attributes #[[NEVERATTR]] = {{.*}} "function-instrument"="xray-never" {{.*}}
index eb272b97eafb6928f677083483e654ea67b9b7f4..083099ce582d09d0bcaa99a3b83c56e6e8e89943 100644 (file)
@@ -1,5 +1,12 @@
 // RUN: echo "fun:*arg1*=arg1" >> %t.always-instrument
-// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 -fxray-always-instrument=%t.always-instrument -emit-llvm -o - %s -triple x86_64-unknown-linux-gnu | FileCheck %s
+// RUN: echo "[always]" > %t.xray-attrlist
+// RUN: echo "fun:*arg1*=arg1" >> %t.xray-attrlist
+// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 \
+// RUN:     -fxray-always-instrument=%t.always-instrument -emit-llvm -o - %s \
+// RUN:     -triple x86_64-unknown-linux-gnu | FileCheck %s
+// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 \
+// RUN:     -fxray-attr-list=%t.xray-attrlist -emit-llvm -o - %s \
+// RUN:     -triple x86_64-unknown-linux-gnu | FileCheck %s
 
 void foo() {}
 
diff --git a/test/CodeGen/xray-never-instrument.cpp b/test/CodeGen/xray-never-instrument.cpp
new file mode 100644 (file)
index 0000000..4b20edc
--- /dev/null
@@ -0,0 +1,24 @@
+// RUN: echo "fun:*foo*" > %t.never-instrument
+// RUN: echo "src:*xray-never-instrument.cpp" >> %t.never-instrument
+// RUN: echo "[never]" > %t.xray-attrlist
+// RUN: echo "fun:*foo*" >> %t.xray-attrlist
+// RUN: echo "src:*xray-never-instrument.cpp" >> %t.xray-attrlist
+// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 \
+// RUN:     -fxray-never-instrument=%t.never-instrument -emit-llvm -o - %s \
+// RUN:     -triple x86_64-unknown-linux-gnu | FileCheck %s
+// RUN: %clang_cc1 -fxray-instrument -x c++ -std=c++11 \
+// RUN:     -fxray-attr-list=%t.xray-attrlist -emit-llvm -o - %s \
+// RUN:     -triple x86_64-unknown-linux-gnu | FileCheck %s
+
+void foo() {}
+
+[[clang::xray_always_instrument]] void bar() {}
+
+void baz() {}
+
+// CHECK: define void @_Z3foov() #[[NEVERATTR:[0-9]+]] {
+// CHECK: define void @_Z3barv() #[[ALWAYSATTR:[0-9]+]] {
+// CHECK: define void @_Z3bazv() #[[NEVERATTR:[0-9]+]] {
+// CHECK: attributes #[[NEVERATTR]] = {{.*}} "function-instrument"="xray-never" {{.*}}
+// CHECK: attributes #[[ALWAYSATTR]] = {{.*}} "function-instrument"="xray-always" {{.*}}
+