xref: /freebsd/contrib/expat/tests/memcheck.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /* Debug allocators for the Expat test suite
2                             __  __            _
3                          ___\ \/ /_ __   __ _| |_
4                         / _ \\  /| '_ \ / _` | __|
5                        |  __//  \| |_) | (_| | |_
6                         \___/_/\_\ .__/ \__,_|\__|
7                                  |_| XML parser
8 
9    Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10    Copyright (c) 2000-2017 Expat development team
11    Licensed under the MIT license:
12 
13    Permission is  hereby granted,  free of charge,  to any  person obtaining
14    a  copy  of  this  software   and  associated  documentation  files  (the
15    "Software"),  to  deal in  the  Software  without restriction,  including
16    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17    distribute, sublicense, and/or sell copies of the Software, and to permit
18    persons  to whom  the Software  is  furnished to  do so,  subject to  the
19    following conditions:
20 
21    The above copyright  notice and this permission notice  shall be included
22    in all copies or substantial portions of the Software.
23 
24    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30    USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include "memcheck.h"
36 
37 /* Structures to keep track of what has been allocated.  Speed isn't a
38  * big issue for the tests this is required for, so we will use a
39  * doubly-linked list to make deletion easier.
40  */
41 
42 typedef struct allocation_entry {
43   struct allocation_entry *next;
44   struct allocation_entry *prev;
45   void *allocation;
46   size_t num_bytes;
47 } AllocationEntry;
48 
49 static AllocationEntry *alloc_head = NULL;
50 static AllocationEntry *alloc_tail = NULL;
51 
52 static AllocationEntry *find_allocation(void *ptr);
53 
54 /* Allocate some memory and keep track of it. */
55 void *
56 tracking_malloc(size_t size) {
57   AllocationEntry *entry = malloc(sizeof(AllocationEntry));
58 
59   if (entry == NULL) {
60     printf("Allocator failure\n");
61     return NULL;
62   }
63   entry->num_bytes = size;
64   entry->allocation = malloc(size);
65   if (entry->allocation == NULL) {
66     free(entry);
67     return NULL;
68   }
69   entry->next = NULL;
70 
71   /* Add to the list of allocations */
72   if (alloc_head == NULL) {
73     entry->prev = NULL;
74     alloc_head = alloc_tail = entry;
75   } else {
76     entry->prev = alloc_tail;
77     alloc_tail->next = entry;
78     alloc_tail = entry;
79   }
80 
81   return entry->allocation;
82 }
83 
84 static AllocationEntry *
85 find_allocation(void *ptr) {
86   AllocationEntry *entry;
87 
88   for (entry = alloc_head; entry != NULL; entry = entry->next) {
89     if (entry->allocation == ptr) {
90       return entry;
91     }
92   }
93   return NULL;
94 }
95 
96 /* Free some memory and remove the tracking for it */
97 void
98 tracking_free(void *ptr) {
99   AllocationEntry *entry;
100 
101   if (ptr == NULL) {
102     /* There won't be an entry for this */
103     return;
104   }
105 
106   entry = find_allocation(ptr);
107   if (entry != NULL) {
108     /* This is the relevant allocation.  Unlink it */
109     if (entry->prev != NULL)
110       entry->prev->next = entry->next;
111     else
112       alloc_head = entry->next;
113     if (entry->next != NULL)
114       entry->next->prev = entry->prev;
115     else
116       alloc_tail = entry->next;
117     free(entry);
118   } else {
119     printf("Attempting to free unallocated memory at %p\n", ptr);
120   }
121   free(ptr);
122 }
123 
124 /* Reallocate some memory and keep track of it */
125 void *
126 tracking_realloc(void *ptr, size_t size) {
127   AllocationEntry *entry;
128 
129   if (ptr == NULL) {
130     /* By definition, this is equivalent to malloc(size) */
131     return tracking_malloc(size);
132   }
133   if (size == 0) {
134     /* By definition, this is equivalent to free(ptr) */
135     tracking_free(ptr);
136     return NULL;
137   }
138 
139   /* Find the allocation entry for this memory */
140   entry = find_allocation(ptr);
141   if (entry == NULL) {
142     printf("Attempting to realloc unallocated memory at %p\n", ptr);
143     entry = malloc(sizeof(AllocationEntry));
144     if (entry == NULL) {
145       printf("Reallocator failure\n");
146       return NULL;
147     }
148     entry->allocation = realloc(ptr, size);
149     if (entry->allocation == NULL) {
150       free(entry);
151       return NULL;
152     }
153 
154     /* Add to the list of allocations */
155     entry->next = NULL;
156     if (alloc_head == NULL) {
157       entry->prev = NULL;
158       alloc_head = alloc_tail = entry;
159     } else {
160       entry->prev = alloc_tail;
161       alloc_tail->next = entry;
162       alloc_tail = entry;
163     }
164   } else {
165     entry->allocation = realloc(ptr, size);
166     if (entry->allocation == NULL) {
167       /* Realloc semantics say the original is still allocated */
168       entry->allocation = ptr;
169       return NULL;
170     }
171   }
172 
173   entry->num_bytes = size;
174   return entry->allocation;
175 }
176 
177 int
178 tracking_report(void) {
179   AllocationEntry *entry;
180 
181   if (alloc_head == NULL)
182     return 1;
183 
184   /* Otherwise we have allocations that haven't been freed */
185   for (entry = alloc_head; entry != NULL; entry = entry->next) {
186     printf("Allocated %lu bytes at %p\n", (long unsigned)entry->num_bytes,
187            entry->allocation);
188   }
189   return 0;
190 }
191