1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
* Copyright (c) 1996-1999 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Implemement memdebug_free()
*/
#include <assert.h>
#include <string.h>
#include <oskit/lmm.h>
#include "memdebug.h"
/*
* memdebug_free()
*
* returns -1 if there was an error. 0 otherwise.
*/
int
memdebug_free(void* mem, int wipeval, char caller, int blocksize, char *file, int line)
{
memdebug_mhead *head;
memdebug_mtail *tail;
int rc;
#if ALLOW_MORALLY_QUESTIONABLE_PRACTICE
/* free(NULL) is technically legal. */
if (!mem) return 0;
#else
if (!mem)
{
memdebug_info_dump("MEMDEBUG BOGOSITY: Tried to free(NULL) at",
file, line);
return -1;
}
#endif
head = (memdebug_mhead*)mem - 1;
tail = (memdebug_mtail*)((void*)(head + 1) + head->size);
/*
* Do the "already free" check here, as the memdebug_ptrchk() message
* isn't appropriate.
*/
if (head->magic == FREE_MAGIC)
{
memdebug_info_dump("MEMDEBUG BOGOSITY: Block has already been free()'d; detected at",
file, line);
memdebug_bogosity(head);
}
/*
* Do the standard set of sanity checks on the raw memory pointer.
*/
rc = memdebug_traced_ptrchk(mem, file, line);
if (rc)
{
memdebug_printf("--Detected during free()\n");
#if 0
/*
* enable this if you want a trace of the stack at the
* point where the free is called
*/
dump_stack_trace_bis(memdebug_printf, 20);
#endif
/* Only bail on really bad errors */
if (rc == -1)
return -1;
}
DPRINTF("Doing free()-specific checks.\n");
/*
* Make sure the user isn't smalloc()'ing and free()'ing or
* vice-versa.
*/
if ((caller == SMALLOC_CALLER) &&
(!(head->flags & SMALLOC_CREATED)))
{
memdebug_printf("MEMDEBUG BOGOSITY:"
" sfree() of block allocated by malloc() ");
memdebug_info_dump("at", file, line);
memdebug_bogosity(head);
/* Don't return, we can continue and do the correct thing. */
}
else if ((caller == MALLOC_CALLER)
&& (head->flags & SMALLOC_CREATED))
{
memdebug_printf("MEMDEBUG BOGOSITY:"
" free() of block allocated by smalloc() ");
memdebug_info_dump("at", file, line);
memdebug_bogosity(head);
/* Don't return, we can continue and do the correct thing. */
}
/*
* If this is an sfree() call, then check to make sure the
* user is passing in the correct size.
*/
if ((blocksize != -1) && (blocksize != head->size))
{
memdebug_printf("MEMDEBUG BOGOSITY:"
" sfree()'ing a block with incorrect size, %d (should be %u) ",
blocksize, head->size);
memdebug_info_dump("at", file, line);
memdebug_bogosity(head);
/* Don't return, we can continue and do the correct thing. */
}
/* Remove the block from the allocated list. Keep thread-safe.*/
mem_lock();
head->next->prev = head->prev;
head->prev->next = head->next;
mem_unlock();
/* Mark the block freed. */
head->magic = FREE_MAGIC;
tail->magic = FREE_MAGIC;
/*
* Fill the freed block with bogus data to reveal reads from
* freed memory blocks.
*/
memset(mem, wipeval, head->size);
/* Free it. */
memdebug_untraced_free(head,
head->size + sizeof(memdebug_mhead) +
sizeof(memdebug_mtail));
DPRINTF("finished.\n");
return 0;
}
|