mcheck.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Standard debugging hooks for `malloc'.
  2. Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3. Written May 1989 by Mike Haertel.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  15. Cambridge, MA 02139, USA.
  16. The author may be reached (Email) at the address mike@ai.mit.edu,
  17. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  18. #ifndef _MALLOC_INTERNAL
  19. #define _MALLOC_INTERNAL
  20. #include <malloc.h>
  21. #include <stdio.h>
  22. #endif
  23. /* Old hook values. */
  24. static void (*old_free_hook) __P ((__ptr_t ptr));
  25. static __ptr_t (*old_malloc_hook) __P ((__malloc_size_t size));
  26. static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, __malloc_size_t size));
  27. /* Function to call when something awful happens. */
  28. static void (*abortfunc) __P ((enum mcheck_status));
  29. /* Arbitrary magical numbers. */
  30. #define MAGICWORD 0xfedabeeb
  31. #define MAGICFREE 0xd8675309
  32. #define MAGICBYTE ((char) 0xd7)
  33. #define MALLOCFLOOD ((char) 0x93)
  34. #define FREEFLOOD ((char) 0x95)
  35. struct hdr
  36. {
  37. __malloc_size_t size; /* Exact size requested by user. */
  38. unsigned long int magic; /* Magic number to check header integrity. */
  39. };
  40. #if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
  41. #define flood memset
  42. #else
  43. static void flood __P ((__ptr_t, int, __malloc_size_t));
  44. static void
  45. flood (ptr, val, size)
  46. __ptr_t ptr;
  47. int val;
  48. __malloc_size_t size;
  49. {
  50. char *cp = ptr;
  51. while (size--)
  52. *cp++ = val;
  53. }
  54. #endif
  55. static enum mcheck_status checkhdr __P ((const struct hdr *));
  56. static enum mcheck_status
  57. checkhdr (hdr)
  58. const struct hdr *hdr;
  59. {
  60. enum mcheck_status status;
  61. switch (hdr->magic)
  62. {
  63. default:
  64. status = MCHECK_HEAD;
  65. break;
  66. case MAGICFREE:
  67. status = MCHECK_FREE;
  68. break;
  69. case MAGICWORD:
  70. if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
  71. status = MCHECK_TAIL;
  72. else
  73. status = MCHECK_OK;
  74. break;
  75. }
  76. if (status != MCHECK_OK)
  77. (*abortfunc) (status);
  78. return status;
  79. }
  80. static void freehook __P ((__ptr_t));
  81. static void
  82. freehook (ptr)
  83. __ptr_t ptr;
  84. {
  85. struct hdr *hdr = ((struct hdr *) ptr) - 1;
  86. checkhdr (hdr);
  87. hdr->magic = MAGICFREE;
  88. flood (ptr, FREEFLOOD, hdr->size);
  89. __free_hook = old_free_hook;
  90. free (hdr);
  91. __free_hook = freehook;
  92. }
  93. static __ptr_t mallochook __P ((__malloc_size_t));
  94. static __ptr_t
  95. mallochook (size)
  96. __malloc_size_t size;
  97. {
  98. struct hdr *hdr;
  99. __malloc_hook = old_malloc_hook;
  100. hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
  101. __malloc_hook = mallochook;
  102. if (hdr == NULL)
  103. return NULL;
  104. hdr->size = size;
  105. hdr->magic = MAGICWORD;
  106. ((char *) &hdr[1])[size] = MAGICBYTE;
  107. flood ((__ptr_t) (hdr + 1), MALLOCFLOOD, size);
  108. return (__ptr_t) (hdr + 1);
  109. }
  110. static __ptr_t reallochook __P ((__ptr_t, __malloc_size_t));
  111. static __ptr_t
  112. reallochook (ptr, size)
  113. __ptr_t ptr;
  114. __malloc_size_t size;
  115. {
  116. struct hdr *hdr = ((struct hdr *) ptr) - 1;
  117. __malloc_size_t osize = hdr->size;
  118. checkhdr (hdr);
  119. if (size < osize)
  120. flood ((char *) ptr + size, FREEFLOOD, osize - size);
  121. __free_hook = old_free_hook;
  122. __malloc_hook = old_malloc_hook;
  123. __realloc_hook = old_realloc_hook;
  124. hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
  125. __free_hook = freehook;
  126. __malloc_hook = mallochook;
  127. __realloc_hook = reallochook;
  128. if (hdr == NULL)
  129. return NULL;
  130. hdr->size = size;
  131. hdr->magic = MAGICWORD;
  132. ((char *) &hdr[1])[size] = MAGICBYTE;
  133. if (size > osize)
  134. flood ((char *) (hdr + 1) + osize, MALLOCFLOOD, size - osize);
  135. return (__ptr_t) (hdr + 1);
  136. }
  137. static void
  138. mabort (status)
  139. enum mcheck_status status;
  140. {
  141. const char *msg;
  142. switch (status)
  143. {
  144. case MCHECK_OK:
  145. msg = "memory is consistent, library is buggy";
  146. break;
  147. case MCHECK_HEAD:
  148. msg = "memory clobbered before allocated block";
  149. break;
  150. case MCHECK_TAIL:
  151. msg = "memory clobbered past end of allocated block";
  152. break;
  153. case MCHECK_FREE:
  154. msg = "block freed twice";
  155. break;
  156. default:
  157. msg = "bogus mcheck_status, library is buggy";
  158. break;
  159. }
  160. #ifdef __GNU_LIBRARY__
  161. __libc_fatal (msg);
  162. #else
  163. fprintf (stderr, "mcheck: %s\n", msg);
  164. fflush (stderr);
  165. abort ();
  166. #endif
  167. }
  168. static int mcheck_used = 0;
  169. int
  170. mcheck (func)
  171. void (*func) __P ((enum mcheck_status));
  172. {
  173. abortfunc = (func != NULL) ? func : &mabort;
  174. /* These hooks may not be safely inserted if malloc is already in use. */
  175. if (!__malloc_initialized && !mcheck_used)
  176. {
  177. old_free_hook = __free_hook;
  178. __free_hook = freehook;
  179. old_malloc_hook = __malloc_hook;
  180. __malloc_hook = mallochook;
  181. old_realloc_hook = __realloc_hook;
  182. __realloc_hook = reallochook;
  183. mcheck_used = 1;
  184. }
  185. return mcheck_used ? 0 : -1;
  186. }
  187. enum mcheck_status
  188. mprobe (__ptr_t ptr)
  189. {
  190. return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED;
  191. }