[PATCH] D85259: [msan] Remove readnone and friends from call sites.

Evgenii Stepanov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 4 16:50:48 PDT 2020


eugenis created this revision.
eugenis added reviewers: vitalybuka, guiand.
Herald added a subscriber: hiraditya.
Herald added a reviewer: jdoerfert.
Herald added a project: LLVM.
eugenis requested review of this revision.

MSan removes readnone/readonly and similar attributes from callees,
because after MSan instrumentation those attributes no longer apply.

This change removes the attributes from call sites, as well.

Failing to do this may cause DSE of paramTLS stores before calls to
readonly/readnone functions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85259

Files:
  llvm/include/llvm/IR/InstrTypes.h
  llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
  llvm/test/Instrumentation/MemorySanitizer/attributes.ll


Index: llvm/test/Instrumentation/MemorySanitizer/attributes.ll
===================================================================
--- llvm/test/Instrumentation/MemorySanitizer/attributes.ll
+++ llvm/test/Instrumentation/MemorySanitizer/attributes.ll
@@ -13,24 +13,28 @@
 define void @a() sanitize_memory readnone {
 entry:
   call void @a_()
+  call void @a_() readnone
   ret void
 }
 
 define void @b() sanitize_memory readonly {
 entry:
   call void @b_()
+  call void @b_() readonly
   ret void
 }
 
 define void @c() sanitize_memory writeonly {
 entry:
   call void @c_()
+  call void @c_() writeonly
   ret void
 }
 
 define void @d(i32* %p) sanitize_memory writeonly argmemonly {
 entry:
   call void @d_(i32* %p)
+  call void @d_(i32* %p) writeonly argmemonly
   ret void
 }
 
Index: llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -3584,14 +3584,15 @@
       // will become a non-readonly function after it is instrumented by us. To
       // prevent this code from being optimized out, mark that function
       // non-readonly in advance.
+      AttrBuilder B;
+      B.addAttribute(Attribute::ReadOnly)
+          .addAttribute(Attribute::ReadNone)
+          .addAttribute(Attribute::WriteOnly)
+          .addAttribute(Attribute::ArgMemOnly)
+          .addAttribute(Attribute::Speculatable);
+
+      Call->removeAttributes(AttributeList::FunctionIndex, B);
       if (Function *Func = Call->getCalledFunction()) {
-        // Clear out readonly/readnone attributes.
-        AttrBuilder B;
-        B.addAttribute(Attribute::ReadOnly)
-            .addAttribute(Attribute::ReadNone)
-            .addAttribute(Attribute::WriteOnly)
-            .addAttribute(Attribute::ArgMemOnly)
-            .addAttribute(Attribute::Speculatable);
         Func->removeAttributes(AttributeList::FunctionIndex, B);
       }
 
Index: llvm/include/llvm/IR/InstrTypes.h
===================================================================
--- llvm/include/llvm/IR/InstrTypes.h
+++ llvm/include/llvm/IR/InstrTypes.h
@@ -1451,6 +1451,12 @@
     setAttributes(PAL);
   }
 
+  void removeAttributes(unsigned i, const AttrBuilder &Attrs) {
+    AttributeList PAL = getAttributes();
+    PAL = PAL.removeAttributes(getContext(), i, Attrs);
+    setAttributes(PAL);
+  }
+
   /// Removes the attribute from the given argument
   void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
     assert(ArgNo < getNumArgOperands() && "Out of bounds");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85259.283062.patch
Type: text/x-patch
Size: 2646 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200804/9f696f4a/attachment.bin>


More information about the llvm-commits mailing list