| 1 | /* -*- C++ -*- */ |
| 2 | |
| 3 | #ifndef HL_SANITYCHECKHEAP_H |
| 4 | #define HL_SANITYCHECKHEAP_H |
| 5 | |
| 6 | /** |
| 7 | * @class SanityCheckHeap |
| 8 | * @brief Checks for memory allocation usage errors at runtime. |
| 9 | * See the "error messages" for the kinds of errors this layer can catch. |
| 10 | * @author Emery Berger |
| 11 | */ |
| 12 | |
| 13 | #include <map> |
| 14 | |
| 15 | #include "heaps/top/mallocheap.h" |
| 16 | #include "heaps/special/zoneheap.h" |
| 17 | #include "wrappers/stlallocator.h" |
| 18 | #include "heaps/top/mmapheap.h" |
| 19 | |
| 20 | namespace HL { |
| 21 | |
| 22 | template <class SuperHeap> |
| 23 | class SanityCheckHeap : public SuperHeap { |
| 24 | private: |
| 25 | |
| 26 | /// Define a local allocator that lets us use the SuperHeap for the map. |
| 27 | /// This approach lets us use SanityCheckHeaps when we're replacing malloc. |
| 28 | |
| 29 | // The objects are pairs, mapping void * pointers to sizes. |
| 30 | typedef std::pair<const intptr_t, size_t> objType; |
| 31 | |
| 32 | // The heap is a simple freelist heap. |
| 33 | typedef HL::FreelistHeap<HL::ZoneHeap<HL::MmapHeap, 16384> > heapType; |
| 34 | |
| 35 | // And we wrap it up so it can be used as an STL allocator. |
| 36 | typedef HL::STLAllocator<objType, heapType> localAllocator; |
| 37 | |
| 38 | typedef std::less<intptr_t> localComparator; |
| 39 | |
| 40 | /// A map of pointers to objects and their allocated sizes. |
| 41 | typedef std::map<intptr_t, size_t, localComparator, localAllocator> mapType; |
| 42 | |
| 43 | /// A freed object has a special size, -1. |
| 44 | enum { FREED = -1 }; |
| 45 | |
| 46 | /** |
| 47 | * @brief "Error messages", used in asserts. |
| 48 | * These must all equal zero. |
| 49 | */ |
| 50 | enum { MALLOC_RETURNED_ALLOCATED_OBJECT = 0, |
| 51 | FREE_CALLED_ON_OBJECT_I_NEVER_ALLOCATED = 0, |
| 52 | FREE_CALLED_TWICE_ON_SAME_OBJECT = 0 }; |
| 53 | |
| 54 | public: |
| 55 | |
| 56 | inline void * malloc (size_t sz) { |
| 57 | void * ptr = SuperHeap::malloc (sz); |
| 58 | if (ptr == NULL) { |
| 59 | return NULL; |
| 60 | } |
| 61 | // Fill the space with a known value. |
| 62 | memset (ptr, 'A', sz); |
| 63 | // Record this object as allocated. |
| 64 | mapType::iterator i; |
| 65 | // Look for this object in the map of allocated objects. |
| 66 | i = allocatedObjects.find ((intptr_t)ptr); |
| 67 | if (i == allocatedObjects.end()) { |
| 68 | // We didn't find it (this is good). |
| 69 | // Add the tuple (ptr, sz). |
| 70 | allocatedObjects.insert (std::pair<intptr_t, int>((intptr_t)ptr, sz)); |
| 71 | } else { |
| 72 | // We found it. |
| 73 | // It really should have been freed. |
| 74 | if ((*i).second != FREED) { |
| 75 | // This object is still in use! |
| 76 | assert ( MALLOC_RETURNED_ALLOCATED_OBJECT ); |
| 77 | return NULL; |
| 78 | } else { |
| 79 | // This object has been freed. Mark it as allocated. |
| 80 | (*i).second = sz; |
| 81 | } |
| 82 | } |
| 83 | return ptr; |
| 84 | } |
| 85 | |
| 86 | inline void free (void * ptr) { |
| 87 | // Look for this object in the list of allocated objects. |
| 88 | mapType::iterator i; |
| 89 | i = allocatedObjects.find ((intptr_t)ptr); |
| 90 | if (i == allocatedObjects.end()) { |
| 91 | assert ( FREE_CALLED_ON_OBJECT_I_NEVER_ALLOCATED ); |
| 92 | return; |
| 93 | } |
| 94 | // We found the object. It should not have been freed already. |
| 95 | if ((*i).second == FREED) { |
| 96 | // Oops, this object WAS freed before. |
| 97 | assert ( FREE_CALLED_TWICE_ON_SAME_OBJECT ); |
| 98 | return; |
| 99 | } |
| 100 | // Fill the space with a known value. |
| 101 | memset (ptr, 'F', (*i).second); |
| 102 | // Really free the pointer. |
| 103 | (*i).second = FREED; |
| 104 | SuperHeap::free (ptr); |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | |
| 109 | /// A map of tuples: (obj address, size). |
| 110 | mapType allocatedObjects; |
| 111 | }; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | #endif |