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