1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
32 * @author José Fonseca <jfonseca@vmware.com>
35 #include "pipe/p_config.h"
37 #define DEBUG_MEMORY_IMPLEMENTATION
39 #include "os/os_thread.h"
41 #include "util/u_debug.h"
42 #include "util/u_debug_gallium.h"
43 #include "util/u_debug_stack.h"
44 #include "util/list.h"
45 #include "util/os_memory.h"
46 #include "util/os_memory_debug.h"
49 #define DEBUG_MEMORY_MAGIC 0x6e34090aU
50 #define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */
53 * Set to 1 to enable checking of freed blocks of memory.
54 * Basically, don't really deallocate freed memory; keep it in the list
55 * but mark it as freed and do extra checking in debug_memory_check().
56 * This can detect some cases of use-after-free. But note that since we
57 * never really free anything this will use a lot of memory.
59 #define DEBUG_FREED_MEMORY 0
60 #define DEBUG_FREED_BYTE 0x33
63 struct debug_memory_header
65 struct list_head head
;
71 #if DEBUG_MEMORY_STACK
72 struct debug_stack_frame backtrace
[DEBUG_MEMORY_STACK
];
75 #if DEBUG_FREED_MEMORY
76 boolean freed
; /**< Is this a freed block? */
83 struct debug_memory_footer
89 static struct list_head list
= { &list
, &list
};
91 static mtx_t list_mutex
= _MTX_INITIALIZER_NP
;
93 static unsigned long last_no
= 0;
96 static inline struct debug_memory_header
*
97 header_from_data(void *data
)
100 return (struct debug_memory_header
*)((char *)data
- sizeof(struct debug_memory_header
));
106 data_from_header(struct debug_memory_header
*hdr
)
109 return (void *)((char *)hdr
+ sizeof(struct debug_memory_header
));
114 static inline struct debug_memory_footer
*
115 footer_from_header(struct debug_memory_header
*hdr
)
118 return (struct debug_memory_footer
*)((char *)hdr
+ sizeof(struct debug_memory_header
) + hdr
->size
);
125 debug_malloc(const char *file
, unsigned line
, const char *function
,
128 struct debug_memory_header
*hdr
;
129 struct debug_memory_footer
*ftr
;
131 hdr
= os_malloc(sizeof(*hdr
) + size
+ sizeof(*ftr
));
133 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
134 file
, line
, function
,
135 (long unsigned)size
);
142 hdr
->function
= function
;
144 hdr
->magic
= DEBUG_MEMORY_MAGIC
;
146 #if DEBUG_FREED_MEMORY
150 #if DEBUG_MEMORY_STACK
151 debug_backtrace_capture(hdr
->backtrace
, 0, DEBUG_MEMORY_STACK
);
154 ftr
= footer_from_header(hdr
);
155 ftr
->magic
= DEBUG_MEMORY_MAGIC
;
157 mtx_lock(&list_mutex
);
158 LIST_ADDTAIL(&hdr
->head
, &list
);
159 mtx_unlock(&list_mutex
);
161 return data_from_header(hdr
);
165 debug_free(const char *file
, unsigned line
, const char *function
,
168 struct debug_memory_header
*hdr
;
169 struct debug_memory_footer
*ftr
;
174 hdr
= header_from_data(ptr
);
175 if (hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
176 debug_printf("%s:%u:%s: freeing bad or corrupted memory %p\n",
177 file
, line
, function
,
183 ftr
= footer_from_header(hdr
);
184 if (ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
185 debug_printf("%s:%u:%s: buffer overflow %p\n",
186 hdr
->file
, hdr
->line
, hdr
->function
,
191 #if DEBUG_FREED_MEMORY
192 /* Check for double-free */
194 /* Mark the block as freed but don't really free it */
196 /* Save file/line where freed */
199 /* set freed memory to special value */
200 memset(ptr
, DEBUG_FREED_BYTE
, hdr
->size
);
202 mtx_lock(&list_mutex
);
203 LIST_DEL(&hdr
->head
);
204 mtx_unlock(&list_mutex
);
213 debug_calloc(const char *file
, unsigned line
, const char *function
,
214 size_t count
, size_t size
)
216 void *ptr
= debug_malloc( file
, line
, function
, count
* size
);
218 memset( ptr
, 0, count
* size
);
223 debug_realloc(const char *file
, unsigned line
, const char *function
,
224 void *old_ptr
, size_t old_size
, size_t new_size
)
226 struct debug_memory_header
*old_hdr
, *new_hdr
;
227 struct debug_memory_footer
*old_ftr
, *new_ftr
;
231 return debug_malloc( file
, line
, function
, new_size
);
234 debug_free( file
, line
, function
, old_ptr
);
238 old_hdr
= header_from_data(old_ptr
);
239 if (old_hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
240 debug_printf("%s:%u:%s: reallocating bad or corrupted memory %p\n",
241 file
, line
, function
,
247 old_ftr
= footer_from_header(old_hdr
);
248 if (old_ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
249 debug_printf("%s:%u:%s: buffer overflow %p\n",
250 old_hdr
->file
, old_hdr
->line
, old_hdr
->function
,
256 new_hdr
= os_malloc(sizeof(*new_hdr
) + new_size
+ sizeof(*new_ftr
));
258 debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
259 file
, line
, function
,
260 (long unsigned)new_size
);
263 new_hdr
->no
= old_hdr
->no
;
264 new_hdr
->file
= old_hdr
->file
;
265 new_hdr
->line
= old_hdr
->line
;
266 new_hdr
->function
= old_hdr
->function
;
267 new_hdr
->size
= new_size
;
268 new_hdr
->magic
= DEBUG_MEMORY_MAGIC
;
270 #if DEBUG_FREED_MEMORY
271 new_hdr
->freed
= FALSE
;
274 new_ftr
= footer_from_header(new_hdr
);
275 new_ftr
->magic
= DEBUG_MEMORY_MAGIC
;
277 mtx_lock(&list_mutex
);
278 LIST_REPLACE(&old_hdr
->head
, &new_hdr
->head
);
279 mtx_unlock(&list_mutex
);
282 new_ptr
= data_from_header(new_hdr
);
283 memcpy( new_ptr
, old_ptr
, old_size
< new_size
? old_size
: new_size
);
294 debug_memory_begin(void)
300 debug_memory_end(unsigned long start_no
)
302 size_t total_size
= 0;
303 struct list_head
*entry
;
305 if (start_no
== last_no
)
309 for (; entry
!= &list
; entry
= entry
->prev
) {
310 struct debug_memory_header
*hdr
;
312 struct debug_memory_footer
*ftr
;
314 hdr
= LIST_ENTRY(struct debug_memory_header
, entry
, head
);
315 ptr
= data_from_header(hdr
);
316 ftr
= footer_from_header(hdr
);
318 if (hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
319 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
320 hdr
->file
, hdr
->line
, hdr
->function
,
325 if ((start_no
<= hdr
->no
&& hdr
->no
< last_no
) ||
326 (last_no
< start_no
&& (hdr
->no
< last_no
|| start_no
<= hdr
->no
))) {
327 debug_printf("%s:%u:%s: %lu bytes at %p not freed\n",
328 hdr
->file
, hdr
->line
, hdr
->function
,
329 (unsigned long) hdr
->size
, ptr
);
330 #if DEBUG_MEMORY_STACK
331 debug_backtrace_dump(hdr
->backtrace
, DEBUG_MEMORY_STACK
);
333 total_size
+= hdr
->size
;
336 if (ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
337 debug_printf("%s:%u:%s: buffer overflow %p\n",
338 hdr
->file
, hdr
->line
, hdr
->function
,
345 debug_printf("Total of %lu KB of system memory apparently leaked\n",
346 (unsigned long) (total_size
+ 1023)/1024);
349 debug_printf("No memory leaks detected.\n");
355 * Put a tag (arbitrary integer) on a memory block.
356 * Can be useful for debugging.
359 debug_memory_tag(void *ptr
, unsigned tag
)
361 struct debug_memory_header
*hdr
;
366 hdr
= header_from_data(ptr
);
367 if (hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
368 debug_printf("%s corrupted memory at %p\n", __FUNCTION__
, ptr
);
377 * Check the given block of memory for validity/corruption.
380 debug_memory_check_block(void *ptr
)
382 struct debug_memory_header
*hdr
;
383 struct debug_memory_footer
*ftr
;
388 hdr
= header_from_data(ptr
);
389 ftr
= footer_from_header(hdr
);
391 if (hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
392 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
393 hdr
->file
, hdr
->line
, hdr
->function
, ptr
);
397 if (ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
398 debug_printf("%s:%u:%s: buffer overflow %p\n",
399 hdr
->file
, hdr
->line
, hdr
->function
, ptr
);
407 * We can periodically call this from elsewhere to do a basic sanity
408 * check of the heap memory we've allocated.
411 debug_memory_check(void)
413 struct list_head
*entry
;
416 for (; entry
!= &list
; entry
= entry
->prev
) {
417 struct debug_memory_header
*hdr
;
418 struct debug_memory_footer
*ftr
;
421 hdr
= LIST_ENTRY(struct debug_memory_header
, entry
, head
);
422 ftr
= footer_from_header(hdr
);
423 ptr
= (const char *) data_from_header(hdr
);
425 if (hdr
->magic
!= DEBUG_MEMORY_MAGIC
) {
426 debug_printf("%s:%u:%s: bad or corrupted memory %p\n",
427 hdr
->file
, hdr
->line
, hdr
->function
, ptr
);
431 if (ftr
->magic
!= DEBUG_MEMORY_MAGIC
) {
432 debug_printf("%s:%u:%s: buffer overflow %p\n",
433 hdr
->file
, hdr
->line
, hdr
->function
, ptr
);
437 #if DEBUG_FREED_MEMORY
438 /* If this block is marked as freed, check that it hasn't been touched */
441 for (i
= 0; i
< hdr
->size
; i
++) {
442 if (ptr
[i
] != DEBUG_FREED_BYTE
) {
443 debug_printf("Memory error: byte %d of block at %p of size %d is 0x%x\n",
444 i
, ptr
, hdr
->size
, ptr
[i
]);
445 debug_printf("Block was freed at %s:%d\n", hdr
->file
, hdr
->line
);
447 assert(ptr
[i
] == DEBUG_FREED_BYTE
);