Debian Codegraph @

vendor/Heap-Layers/heaps/debug/sanitycheckheap.h

raw symbols SCIP
1 /* -*- C++ -*- */cxx . . $ `<file>/vendor/Heap-Layers/heaps/debug/sanitycheckheap.h`/
2
3 #ifndef HL_SANITYCHECKHEAP_H
4 #define HL_SANITYCHECKHEAP_Hcxx . . $ `vendor/Heap-Layers/heaps/debug/sanitycheckheap.h:4:9`!
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"cxx . . $ `<file>/vendor/Heap-Layers/heaps/top/mallocheap.h`/
16 #include "heaps/special/zoneheap.h"cxx . . $ `<file>/vendor/Heap-Layers/heaps/special/zoneheap.h`/
17 #include "wrappers/stlallocator.h"cxx . . $ `<file>/vendor/Heap-Layers/wrappers/stlallocator.h`/
18 #include "heaps/top/mmapheap.h"cxx . . $ `<file>/vendor/Heap-Layers/heaps/top/mmapheap.h`/
19
20 namespace HLcxx . . $ HL/ {
21
22 template <class SuperHeap>
23 class SanityCheckHeapcxx . . $ HL/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 stdcxx . . $ std/::paircxx . . $ std/pair#<const intptr_tcxx . . $ intptr_t#, size_t> objTypecxx . . $ HL/SanityCheckHeap#objType#;
31
32 // The heap is a simple freelist heap.
33 typedef HLcxx . . $ HL/::FreelistHeapcxx . . $ HL/FreelistHeap#<HLcxx . . $ HL/::ZoneHeapcxx . . $ HL/ZoneHeap#<HLcxx . . $ HL/::MmapHeapcxx . . $ HL/MmapHeap#, 16384> > heapTypecxx . . $ HL/SanityCheckHeap#heapType#;
34
35 // And we wrap it up so it can be used as an STL allocator.
36 typedef HLcxx . . $ HL/::STLAllocatorcxx . . $ HL/STLAllocator#<objTypecxx . . $ HL/SanityCheckHeap#objType#, heapTypecxx . . $ HL/SanityCheckHeap#heapType#> localAllocatorcxx . . $ HL/SanityCheckHeap#localAllocator#;
37
38 typedef stdcxx . . $ std/::lesscxx . . $ std/less#<intptr_tcxx . . $ intptr_t#> localComparatorcxx . . $ HL/SanityCheckHeap#localComparator#;
39
40 /// A map of pointers to objects and their allocated sizes.
41 typedef stdcxx . . $ std/::mapcxx . . $ std/map#<intptr_tcxx . . $ intptr_t#, size_t, localComparatorcxx . . $ HL/SanityCheckHeap#localComparator#, localAllocatorcxx . . $ HL/SanityCheckHeap#localAllocator#> mapTypecxx . . $ HL/SanityCheckHeap#mapType#;
42
43 /// A freed object has a special size, -1.
44 enumcxx . . $ HL/SanityCheckHeap#$anonymous_type_0# { FREEDcxx . . $ HL/SanityCheckHeap#FREED. = -1 };
45
46 /**
47 * @brief "Error messages", used in asserts.
48 * These must all equal zero.
49 */
50 enumcxx . . $ HL/SanityCheckHeap#$anonymous_type_1# { MALLOC_RETURNED_ALLOCATED_OBJECTcxx . . $ HL/SanityCheckHeap#MALLOC_RETURNED_ALLOCATED_OBJECT. = 0,
51 FREE_CALLED_ON_OBJECT_I_NEVER_ALLOCATEDcxx . . $ HL/SanityCheckHeap#FREE_CALLED_ON_OBJECT_I_NEVER_ALLOCATED. = 0,
52 FREE_CALLED_TWICE_ON_SAME_OBJECTcxx . . $ HL/SanityCheckHeap#FREE_CALLED_TWICE_ON_SAME_OBJECT. = 0 };
53
54 public:
55
56 inline void * malloccxx . . $ HL/SanityCheckHeap#malloc(bfcfa4d6b7f7ef64). (size_t sz) {
57 void * ptr = SuperHeap::malloc (sz);
58 if (ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
59 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
60 }
61 // Fill the space with a known value.
62 memset (ptr, 'A', sz);
63 // Record this object as allocated.
64 mapTypecxx . . $ HL/SanityCheckHeap#mapType#::iteratorcxx . . $ std/map#iterator# icxx . . $ std/_Rb_tree_iterator#_Rb_tree_iterator(49f6e7a06ebc5aa8).;
65 // Look for this object in the map of allocated objects.
66 i =cxx . . $ std/_Rb_tree_iterator#`operator=`(8381ea85a409369f). allocatedObjectscxx . . $ HL/SanityCheckHeap#allocatedObjects..findcxx . . $ std/map#find(36f8d56706583706). ((intptr_tcxx . . $ intptr_t#)ptr);
67 if (i ==cxx . . $ std/`operator==`(84f83906d8995b82). allocatedObjectscxx . . $ HL/SanityCheckHeap#allocatedObjects..endcxx . . $ std/map#end(fbe2f8c57595edf).()) {
68 // We didn't find it (this is good).
69 // Add the tuple (ptr, sz).
70 allocatedObjectscxx . . $ HL/SanityCheckHeap#allocatedObjects..insertcxx . . $ std/map#insert(92111883a3599ede). (stdcxx . . $ std/::paircxx . . $ std/pair#<intptr_tcxx . . $ intptr_t#, int>((intptr_tcxx . . $ intptr_t#)ptr, sz));
71 } else {
72 // We found it.
73 // It really should have been freed.
74 if ((*cxx . . $ std/_Rb_tree_iterator#`operator*`(c93303b41ac66b6d).i).secondcxx . . $ std/pair#second. !=cxx . . $ HL/`operator!=`(fef5daa2f5cf3d2). FREEDcxx . . $ HL/SanityCheckHeap#FREED.) {
75 // This object is still in use!
76 assertcxx . . $ `/usr/include/assert.h:50:10`! ( MALLOC_RETURNED_ALLOCATED_OBJECT );
77 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
78 } else {
79 // This object has been freed. Mark it as allocated.
80 (*cxx . . $ std/_Rb_tree_iterator#`operator*`(c93303b41ac66b6d).i).secondcxx . . $ std/pair#second. = sz;
81 }
82 }
83 return ptr;
84 }
85
86 inline void freecxx . . $ HL/SanityCheckHeap#free(5d22bdacc48458e8). (void * ptr) {
87 // Look for this object in the list of allocated objects.
88 mapTypecxx . . $ HL/SanityCheckHeap#mapType#::iteratorcxx . . $ std/map#iterator# icxx . . $ std/_Rb_tree_iterator#_Rb_tree_iterator(49f6e7a06ebc5aa8).;
89 i =cxx . . $ std/_Rb_tree_iterator#`operator=`(8381ea85a409369f). allocatedObjectscxx . . $ HL/SanityCheckHeap#allocatedObjects..findcxx . . $ std/map#find(36f8d56706583706). ((intptr_tcxx . . $ intptr_t#)ptr);
90 if (i ==cxx . . $ std/`operator==`(84f83906d8995b82). allocatedObjectscxx . . $ HL/SanityCheckHeap#allocatedObjects..endcxx . . $ std/map#end(fbe2f8c57595edf).()) {
91 assertcxx . . $ `/usr/include/assert.h:50:10`! ( FREE_CALLED_ON_OBJECT_I_NEVER_ALLOCATED );
92 return;
93 }
94 // We found the object. It should not have been freed already.
95 if ((*cxx . . $ std/_Rb_tree_iterator#`operator*`(c93303b41ac66b6d).i).secondcxx . . $ std/pair#second. ==cxx . . $ HL/`operator==`(fef5daa2f5cf3d2). FREEDcxx . . $ HL/SanityCheckHeap#FREED.) {
96 // Oops, this object WAS freed before.
97 assertcxx . . $ `/usr/include/assert.h:50:10`! ( FREE_CALLED_TWICE_ON_SAME_OBJECT );
98 return;
99 }
100 // Fill the space with a known value.
101 memset (ptr, 'F', (*cxx . . $ std/_Rb_tree_iterator#`operator*`(c93303b41ac66b6d).i).secondcxx . . $ std/pair#second.);
102 // Really free the pointer.
103 (*cxx . . $ std/_Rb_tree_iterator#`operator*`(c93303b41ac66b6d).i).secondcxx . . $ std/pair#second. = FREEDcxx . . $ HL/SanityCheckHeap#FREED.;
104 SuperHeap::free (ptr);
105 }
106
107 private:
108
109 /// A map of tuples: (obj address, size).
110 mapTypecxx . . $ HL/SanityCheckHeap#mapType# allocatedObjectscxx . . $ HL/SanityCheckHeap#allocatedObjects.;
111 };
112
113 }
114
115 #endif