]> granicus.if.org Git - llvm/commitdiff
[Recycler] Add asan/msan annotations.
authorBenjamin Kramer <benny.kra@googlemail.com>
Thu, 20 Apr 2017 18:29:37 +0000 (18:29 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Thu, 20 Apr 2017 18:29:37 +0000 (18:29 +0000)
This enables use after free and uninit memory checking for memory
returned by a recycler. SelectionDAG currently relies on the opcode of a
free'd node being ISD::DELETED_NODE, so poke a hole in the asan poison
for SDNode opcodes. This means that we won't find some issues, but only
in SDag.

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

include/llvm/Support/ArrayRecycler.h
include/llvm/Support/Recycler.h
lib/CodeGen/SelectionDAG/SelectionDAG.cpp

index 4698f12b3bbc81ce01f83ad850c546756ee2b811..b4222ca92c71563778551e41f966b8541eb7f3bd 100644 (file)
@@ -48,12 +48,16 @@ template <class T, size_t Align = alignof(T)> class ArrayRecycler {
     if (!Entry)
       return nullptr;
     Bucket[Idx] = Entry->Next;
+    __msan_allocated_memory(Entry, Capacity::get(Idx).getSize());
+    __asan_unpoison_memory_region(Entry, Capacity::get(Idx).getSize());
     return reinterpret_cast<T*>(Entry);
   }
 
   // Add an entry to the free list at Bucket[Idx].
   void push(unsigned Idx, T *Ptr) {
     assert(Ptr && "Cannot recycle NULL pointer");
+    __asan_poison_memory_region(Ptr, Capacity::get(Idx).getSize());
+    __asan_unpoison_memory_region(Ptr, sizeof(FreeList));
     FreeList *Entry = reinterpret_cast<FreeList*>(Ptr);
     if (Idx >= Bucket.size())
       Bucket.resize(size_t(Idx) + 1);
index 1523aad38d46fbfd356a95780da993ed1ee17f2a..dc8b246ebf27492f9e18d8a4108b486590509ec7 100644 (file)
@@ -43,10 +43,14 @@ class Recycler {
   FreeNode *pop_val() {
     auto *Val = FreeList;
     FreeList = FreeList->Next;
+    __msan_allocated_memory(Val, Size);
+    __asan_unpoison_memory_region(Val, Size);
     return Val;
   }
 
   void push(FreeNode *N) {
+    __asan_poison_memory_region(N, Size);
+    __asan_unpoison_memory_region(N, sizeof(FreeNode));
     N->Next = FreeList;
     FreeList = N;
   }
index ad169d33fd1369e0dc6cf2b65e095460ae66ec70..523f409e6b2cb80e556177668264aea689c05b81 100644 (file)
@@ -639,12 +639,15 @@ void SelectionDAG::DeallocateNode(SDNode *N) {
   // If we have operands, deallocate them.
   removeOperands(N);
 
+  NodeAllocator.Deallocate(AllNodes.remove(N));
+
   // Set the opcode to DELETED_NODE to help catch bugs when node
   // memory is reallocated.
+  // FIXME: There are places in SDag that have grown a dependency on the opcode
+  // value in the released node.
+  __asan_unpoison_memory_region(&N->NodeType, sizeof(N->NodeType));
   N->NodeType = ISD::DELETED_NODE;
 
-  NodeAllocator.Deallocate(AllNodes.remove(N));
-
   // If any of the SDDbgValue nodes refer to this SDNode, invalidate
   // them and forget about that node.
   DbgInfo->erase(N);