Heap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2020 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs - use tree data structure instead of list
  16. * Ian Craggs - change roundup to Heap_roundup to avoid macro name clash on MacOSX
  17. *******************************************************************************/
  18. /**
  19. * @file
  20. * \brief functions to manage the heap with the goal of eliminating memory leaks
  21. *
  22. * For any module to use these functions transparently, simply include the Heap.h
  23. * header file. Malloc and free will be redefined, but will behave in exactly the same
  24. * way as normal, so no recoding is necessary.
  25. *
  26. * */
  27. #include "Tree.h"
  28. #include "Log.h"
  29. #include "StackTrace.h"
  30. #include "Thread.h"
  31. #if defined(HEAP_UNIT_TESTS)
  32. char* Broker_recordFFDC(char* symptoms);
  33. #endif /* HEAP_UNIT_TESTS */
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <stddef.h>
  38. #include <inttypes.h>
  39. #include "Heap.h"
  40. #if !defined(NO_HEAP_TRACKING)
  41. #undef malloc
  42. #undef realloc
  43. #undef free
  44. #if defined(_WIN32) || defined(_WIN64)
  45. mutex_type heap_mutex;
  46. #else
  47. static pthread_mutex_t heap_mutex_store = PTHREAD_MUTEX_INITIALIZER;
  48. static mutex_type heap_mutex = &heap_mutex_store;
  49. #endif
  50. static heap_info state = {0, 0}; /**< global heap state information */
  51. typedef uint64_t eyecatcherType;
  52. static eyecatcherType eyecatcher = (eyecatcherType)0x8888888888888888;
  53. #define PRIeyecatcher PRIx64 /**< print eyecatcher in HEX notation */
  54. /*#define HEAP_STACK 1 */
  55. /**
  56. * Each item on the heap is recorded with this structure.
  57. */
  58. typedef struct
  59. {
  60. char* file; /**< the name of the source file where the storage was allocated */
  61. int line; /**< the line no in the source file where it was allocated */
  62. void* ptr; /**< pointer to the allocated storage */
  63. size_t size; /**< size of the allocated storage */
  64. #if defined(HEAP_STACK)
  65. char* stack;
  66. #endif
  67. } storageElement;
  68. static Tree heap; /**< Tree that holds the allocation records */
  69. static const char *errmsg = "Memory allocation error";
  70. static size_t Heap_roundup(size_t size);
  71. static int ptrCompare(void* a, void* b, int value);
  72. /*static void Heap_check(char* string, void* ptr);*/
  73. static void checkEyecatchers(char* file, int line, void* p, size_t size);
  74. static int Internal_heap_unlink(char* file, int line, void* p);
  75. static void HeapScan(enum LOG_LEVELS log_level);
  76. /**
  77. * Round allocation size up to a multiple of the size of an int. Apart from possibly reducing fragmentation,
  78. * on the old v3 gcc compilers I was hitting some weird behaviour, which might have been errors in
  79. * sizeof() used on structures and related to packing. In any case, this fixes that too.
  80. * @param size the size actually needed
  81. * @return the rounded up size
  82. */
  83. static size_t Heap_roundup(size_t size)
  84. {
  85. static int multsize = 4*sizeof(int);
  86. if (size % multsize != 0)
  87. size += multsize - (size % multsize);
  88. return size;
  89. }
  90. /**
  91. * List callback function for comparing storage elements
  92. * @param a pointer to the current content in the tree (storageElement*)
  93. * @param b pointer to the memory to free
  94. * @return boolean indicating whether a and b are equal
  95. */
  96. static int ptrCompare(void* a, void* b, int value)
  97. {
  98. a = ((storageElement*)a)->ptr;
  99. if (value)
  100. b = ((storageElement*)b)->ptr;
  101. return (a > b) ? -1 : (a == b) ? 0 : 1;
  102. }
  103. /*
  104. static void Heap_check(char* string, void* ptr)
  105. {
  106. Node* curnode = NULL;
  107. storageElement* prev, *s = NULL;
  108. printf("Heap_check start %p\n", ptr);
  109. while ((curnode = TreeNextElement(&heap, curnode)) != NULL)
  110. {
  111. prev = s;
  112. s = (storageElement*)(curnode->content);
  113. if (prev)
  114. {
  115. if (ptrCompare(s, prev, 1) != -1)
  116. {
  117. printf("%s: heap order error %d %p %p\n", string, ptrCompare(s, prev, 1), prev->ptr, s->ptr);
  118. exit(99);
  119. }
  120. else
  121. printf("%s: heap order good %d %p %p\n", string, ptrCompare(s, prev, 1), prev->ptr, s->ptr);
  122. }
  123. }
  124. }*/
  125. /**
  126. * Allocates a block of memory. A direct replacement for malloc, but keeps track of items
  127. * allocated in a list, so that free can check that a item is being freed correctly and that
  128. * we can check that all memory is freed at shutdown.
  129. * @param file use the __FILE__ macro to indicate which file this item was allocated in
  130. * @param line use the __LINE__ macro to indicate which line this item was allocated at
  131. * @param size the size of the item to be allocated
  132. * @return pointer to the allocated item, or NULL if there was an error
  133. */
  134. void* mymalloc(char* file, int line, size_t size)
  135. {
  136. storageElement* s = NULL;
  137. size_t space = sizeof(storageElement);
  138. size_t filenamelen = strlen(file)+1;
  139. void* rc = NULL;
  140. Thread_lock_mutex(heap_mutex);
  141. size = Heap_roundup(size);
  142. if ((s = malloc(sizeof(storageElement))) == NULL)
  143. {
  144. Log(LOG_ERROR, 13, errmsg);
  145. goto exit;
  146. }
  147. memset(s, 0, sizeof(storageElement));
  148. s->size = size; /* size without eyecatchers */
  149. if ((s->file = malloc(filenamelen)) == NULL)
  150. {
  151. Log(LOG_ERROR, 13, errmsg);
  152. free(s);
  153. goto exit;
  154. }
  155. memset(s->file, 0, sizeof(filenamelen));
  156. space += filenamelen;
  157. strcpy(s->file, file);
  158. #if defined(HEAP_STACK)
  159. #define STACK_LEN 300
  160. if ((s->stack = malloc(STACK_LEN)) == NULL)
  161. {
  162. Log(LOG_ERROR, 13, errmsg);
  163. free(s->file);
  164. free(s);
  165. goto exit;
  166. }
  167. memset(s->stack, 0, sizeof(filenamelen));
  168. StackTrace_get(Thread_getid(), s->stack, STACK_LEN);
  169. #endif
  170. s->line = line;
  171. /* Add space for eyecatcher at each end */
  172. if ((s->ptr = malloc(size + 2*sizeof(eyecatcherType))) == NULL)
  173. {
  174. Log(LOG_ERROR, 13, errmsg);
  175. free(s->file);
  176. free(s);
  177. goto exit;
  178. }
  179. memset(s->ptr, 0, size + 2*sizeof(eyecatcherType));
  180. space += size + 2*sizeof(eyecatcherType);
  181. *(eyecatcherType*)(s->ptr) = eyecatcher; /* start eyecatcher */
  182. *(eyecatcherType*)(((char*)(s->ptr)) + (sizeof(eyecatcherType) + size)) = eyecatcher; /* end eyecatcher */
  183. Log(TRACE_MAX, -1, "Allocating %d bytes in heap at file %s line %d ptr %p\n", (int)size, file, line, s->ptr);
  184. TreeAdd(&heap, s, space);
  185. state.current_size += size;
  186. if (state.current_size > state.max_size)
  187. state.max_size = state.current_size;
  188. rc = ((eyecatcherType*)(s->ptr)) + 1; /* skip start eyecatcher */
  189. exit:
  190. Thread_unlock_mutex(heap_mutex);
  191. return rc;
  192. }
  193. static void checkEyecatchers(char* file, int line, void* p, size_t size)
  194. {
  195. eyecatcherType *sp = (eyecatcherType*)p;
  196. char *cp = (char*)p;
  197. eyecatcherType us;
  198. static const char *msg = "Invalid %s eyecatcher %" PRIeyecatcher " in heap item at file %s line %d";
  199. if ((us = *--sp) != eyecatcher)
  200. Log(LOG_ERROR, 13, msg, "start", us, file, line);
  201. cp += size;
  202. if ((us = *(eyecatcherType*)cp) != eyecatcher)
  203. Log(LOG_ERROR, 13, msg, "end", us, file, line);
  204. }
  205. /**
  206. * Remove an item from the recorded heap without actually freeing it.
  207. * Use sparingly!
  208. * @param file use the __FILE__ macro to indicate which file this item was allocated in
  209. * @param line use the __LINE__ macro to indicate which line this item was allocated at
  210. * @param p pointer to the item to be removed
  211. */
  212. static int Internal_heap_unlink(char* file, int line, void* p)
  213. {
  214. Node* e = NULL;
  215. int rc = 0;
  216. e = TreeFind(&heap, ((eyecatcherType*)p)-1);
  217. if (e == NULL)
  218. Log(LOG_ERROR, 13, "Failed to remove heap item at file %s line %d", file, line);
  219. else
  220. {
  221. storageElement* s = (storageElement*)(e->content);
  222. Log(TRACE_MAX, -1, "Freeing %d bytes in heap at file %s line %d, heap use now %d bytes\n",
  223. (int)s->size, file, line, (int)state.current_size);
  224. checkEyecatchers(file, line, p, s->size);
  225. /* free(s->ptr); */
  226. free(s->file);
  227. state.current_size -= s->size;
  228. TreeRemoveNodeIndex(&heap, e, 0);
  229. free(s);
  230. rc = 1;
  231. }
  232. return rc;
  233. }
  234. /**
  235. * Frees a block of memory. A direct replacement for free, but checks that a item is in
  236. * the allocates list first.
  237. * @param file use the __FILE__ macro to indicate which file this item was allocated in
  238. * @param line use the __LINE__ macro to indicate which line this item was allocated at
  239. * @param p pointer to the item to be freed
  240. */
  241. void myfree(char* file, int line, void* p)
  242. {
  243. if (p) /* it is legal und usual to call free(NULL) */
  244. {
  245. Thread_lock_mutex(heap_mutex);
  246. if (Internal_heap_unlink(file, line, p))
  247. free(((eyecatcherType*)p)-1);
  248. Thread_unlock_mutex(heap_mutex);
  249. }
  250. else
  251. {
  252. Log(LOG_ERROR, -1, "Call of free(NULL) in %s,%d",file,line);
  253. }
  254. }
  255. /**
  256. * Remove an item from the recorded heap without actually freeing it.
  257. * Use sparingly!
  258. * @param file use the __FILE__ macro to indicate which file this item was allocated in
  259. * @param line use the __LINE__ macro to indicate which line this item was allocated at
  260. * @param p pointer to the item to be removed
  261. */
  262. void Heap_unlink(char* file, int line, void* p)
  263. {
  264. Thread_lock_mutex(heap_mutex);
  265. Internal_heap_unlink(file, line, p);
  266. Thread_unlock_mutex(heap_mutex);
  267. }
  268. /**
  269. * Reallocates a block of memory. A direct replacement for realloc, but keeps track of items
  270. * allocated in a list, so that free can check that a item is being freed correctly and that
  271. * we can check that all memory is freed at shutdown.
  272. * We have to remove the item from the tree, as the memory is in order and so it needs to
  273. * be reinserted in the correct place.
  274. * @param file use the __FILE__ macro to indicate which file this item was reallocated in
  275. * @param line use the __LINE__ macro to indicate which line this item was reallocated at
  276. * @param p pointer to the item to be reallocated
  277. * @param size the new size of the item
  278. * @return pointer to the allocated item, or NULL if there was an error
  279. */
  280. void *myrealloc(char* file, int line, void* p, size_t size)
  281. {
  282. void* rc = NULL;
  283. storageElement* s = NULL;
  284. Thread_lock_mutex(heap_mutex);
  285. s = TreeRemoveKey(&heap, ((eyecatcherType*)p)-1);
  286. if (s == NULL)
  287. Log(LOG_ERROR, 13, "Failed to reallocate heap item at file %s line %d", file, line);
  288. else
  289. {
  290. size_t space = sizeof(storageElement);
  291. size_t filenamelen = strlen(file)+1;
  292. checkEyecatchers(file, line, p, s->size);
  293. size = Heap_roundup(size);
  294. state.current_size += size - s->size;
  295. if (state.current_size > state.max_size)
  296. state.max_size = state.current_size;
  297. if ((s->ptr = realloc(s->ptr, size + 2*sizeof(eyecatcherType))) == NULL)
  298. {
  299. Log(LOG_ERROR, 13, errmsg);
  300. goto exit;
  301. }
  302. space += size + 2*sizeof(eyecatcherType) - s->size;
  303. *(eyecatcherType*)(s->ptr) = eyecatcher; /* start eyecatcher */
  304. *(eyecatcherType*)(((char*)(s->ptr)) + (sizeof(eyecatcherType) + size)) = eyecatcher; /* end eyecatcher */
  305. s->size = size;
  306. space -= strlen(s->file);
  307. s->file = realloc(s->file, filenamelen);
  308. space += filenamelen;
  309. strcpy(s->file, file);
  310. s->line = line;
  311. rc = s->ptr;
  312. TreeAdd(&heap, s, space);
  313. }
  314. exit:
  315. Thread_unlock_mutex(heap_mutex);
  316. return (rc == NULL) ? NULL : ((eyecatcherType*)(rc)) + 1; /* skip start eyecatcher */
  317. }
  318. /**
  319. * Utility to find an item in the heap. Lets you know if the heap already contains
  320. * the memory location in question.
  321. * @param p pointer to a memory location
  322. * @return pointer to the storage element if found, or NULL
  323. */
  324. void* Heap_findItem(void* p)
  325. {
  326. Node* e = NULL;
  327. Thread_lock_mutex(heap_mutex);
  328. e = TreeFind(&heap, ((eyecatcherType*)p)-1);
  329. Thread_unlock_mutex(heap_mutex);
  330. return (e == NULL) ? NULL : e->content;
  331. }
  332. /**
  333. * Scans the heap and reports any items currently allocated.
  334. * To be used at shutdown if any heap items have not been freed.
  335. */
  336. static void HeapScan(enum LOG_LEVELS log_level)
  337. {
  338. Node* current = NULL;
  339. Thread_lock_mutex(heap_mutex);
  340. Log(log_level, -1, "Heap scan start, total %d bytes", (int)state.current_size);
  341. while ((current = TreeNextElement(&heap, current)) != NULL)
  342. {
  343. storageElement* s = (storageElement*)(current->content);
  344. Log(log_level, -1, "Heap element size %d, line %d, file %s, ptr %p", (int)s->size, s->line, s->file, s->ptr);
  345. Log(log_level, -1, " Content %.*s", (10 > current->size) ? (int)s->size : 10, (char*)(((eyecatcherType*)s->ptr) + 1));
  346. #if defined(HEAP_STACK)
  347. Log(log_level, -1, " Stack:\n%s", s->stack);
  348. #endif
  349. }
  350. Log(log_level, -1, "Heap scan end");
  351. Thread_unlock_mutex(heap_mutex);
  352. }
  353. /**
  354. * Heap initialization.
  355. */
  356. int Heap_initialize(void)
  357. {
  358. TreeInitializeNoMalloc(&heap, ptrCompare);
  359. heap.heap_tracking = 0; /* no recursive heap tracking! */
  360. return 0;
  361. }
  362. /**
  363. * Heap termination.
  364. */
  365. void Heap_terminate(void)
  366. {
  367. Log(TRACE_MIN, -1, "Maximum heap use was %d bytes", (int)state.max_size);
  368. if (state.current_size > 20) /* One log list is freed after this function is called */
  369. {
  370. Log(LOG_ERROR, -1, "Some memory not freed at shutdown, possible memory leak");
  371. HeapScan(LOG_ERROR);
  372. }
  373. }
  374. /**
  375. * Access to heap state
  376. * @return pointer to the heap state structure
  377. */
  378. heap_info* Heap_get_info(void)
  379. {
  380. return &state;
  381. }
  382. /**
  383. * Dump a string from the heap so that it can be displayed conveniently
  384. * @param file file handle to dump the heap contents to
  385. * @param str the string to dump, could be NULL
  386. */
  387. int HeapDumpString(FILE* file, char* str)
  388. {
  389. int rc = 0;
  390. size_t len = str ? strlen(str) + 1 : 0; /* include the trailing null */
  391. if (fwrite(&(str), sizeof(char*), 1, file) != 1)
  392. rc = -1;
  393. else if (fwrite(&(len), sizeof(int), 1 ,file) != 1)
  394. rc = -1;
  395. else if (len > 0 && fwrite(str, len, 1, file) != 1)
  396. rc = -1;
  397. return rc;
  398. }
  399. /**
  400. * Dump the state of the heap
  401. * @param file file handle to dump the heap contents to
  402. */
  403. int HeapDump(FILE* file)
  404. {
  405. int rc = 0;
  406. Node* current = NULL;
  407. while (rc == 0 && (current = TreeNextElement(&heap, current)))
  408. {
  409. storageElement* s = (storageElement*)(current->content);
  410. if (fwrite(&(s->ptr), sizeof(s->ptr), 1, file) != 1)
  411. rc = -1;
  412. else if (fwrite(&(current->size), sizeof(current->size), 1, file) != 1)
  413. rc = -1;
  414. else if (fwrite(s->ptr, current->size, 1, file) != 1)
  415. rc = -1;
  416. }
  417. return rc;
  418. }
  419. #endif
  420. #if defined(HEAP_UNIT_TESTS)
  421. void Log(enum LOG_LEVELS log_level, int msgno, char* format, ...)
  422. {
  423. printf("Log %s", format);
  424. }
  425. char* Broker_recordFFDC(char* symptoms)
  426. {
  427. printf("recordFFDC");
  428. return "";
  429. }
  430. #define malloc(x) mymalloc(__FILE__, __LINE__, x)
  431. #define realloc(a, b) myrealloc(__FILE__, __LINE__, a, b)
  432. #define free(x) myfree(__FILE__, __LINE__, x)
  433. int main(int argc, char *argv[])
  434. {
  435. char* h = NULL;
  436. Heap_initialize();
  437. h = malloc(12);
  438. free(h);
  439. printf("freed h\n");
  440. h = malloc(12);
  441. h = realloc(h, 14);
  442. h = realloc(h, 25);
  443. h = realloc(h, 255);
  444. h = realloc(h, 2225);
  445. h = realloc(h, 22225);
  446. printf("freeing h\n");
  447. free(h);
  448. Heap_terminate();
  449. printf("Finishing\n");
  450. return 0;
  451. }
  452. #endif /* HEAP_UNIT_TESTS */
  453. /* Local Variables: */
  454. /* indent-tabs-mode: t */
  455. /* c-basic-offset: 8 */
  456. /* End: */