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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
#ifndef MEMDEBUG_H
#define MEMDEBUG_H
/* OPTIONS:
* - MEMDEBUG_IMPLEMENTATION - include the implementation
* (should be inlcuded in only one source file)
* - MEMDEBUG_(MALLOC|REALLOC|CALLOC|FREE)_SYMBOL - change
* the function used for the operation
* - MEMDEBUG_OUT_OF_BOUNDS - enable the out-of-bounds
* check
* - MEMDEBUG_VOID - the main function is main(void)
* instead of main(int argc, char **argv)
* - MEMDEBUG_OUTPUT_LOG - output the log in a file
* (format is "memdebug-<timestamp>.log")
* - MEMDEBUG_OUTPUT_DIR - set the directory for the log,
* by default it is the current directory (automatically
* enables the MEMDEBUG_OUTPUT_LOG
*/
void *__memdebug_malloc(size_t size, char *file, int line);
void *__memdebug_calloc(size_t nmemb, size_t size, char *file, int line);
void *__memdebug_realloc(void *ptr, size_t size, char *file, int line);
void __memdebug_free(void *ptr, char *file, int line);
int memdebug_log(char *format, ...);
#ifdef MEMDEBUG_IMPLEMENTATION
// Default memory allocation functions
#ifndef MEMDEBUG_MALLOC_SYMBOL
#define MEMDEBUG_MALLOC_SYMBOL malloc
#endif
#ifndef MEMDEBUG_REALLOC_SYMBOL
#define MEMDEBUG_REALLOC_SYMBOL realloc
#endif
#ifndef MEMDEBUG_CALLOC_SYMBOL
#define MEMDEBUG_CALLOC_SYMBOL calloc
#endif
#ifndef MEMDEBUG_FREE_SYMBOL
#define MEMDEBUG_FREE_SYMBOL free
#endif
// Log output
#ifdef MEMDEBUG_OUTPUT_DIR
#define MEMDEBUG_OUTPUT_LOG
#else
#define MEMDEBUG_OUTPUT_DIR "."
#endif
#ifdef MEMDEBUG_OUTPUT_LOG
#define MEMDEBUG_OUTPUT_FMT "memdebug-%lu.log"
#endif
// Out-of-bounds check
#define MEMDEBUG_MAGIC_SUFFIX 0xDEADB00BCAFEF00D // 64 bit
typedef long long int memdebug_suffix;
#define MEMDEBUG_OUT_OF_BOUNDS_EXTRA_SIZE \
(sizeof(size_t) + sizeof(memdebug_suffix))
// Implementation
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
static FILE *_memdebug_fp = NULL;
#define MEMDEBUG_LOG(...) \
fprintf(_memdebug_fp, __VA_ARGS__)
#define MEMDEBUG_LOG_FUNC(func, ret, file, line) \
do { \
MEMDEBUG_LOG("(%s:%4d) %-8s ", file, line, #func); \
if(ret == NULL) \
MEMDEBUG_LOG("FAILED %d (%s) ", \
errno, strerror(errno)); \
} while(0)
static inline void *memdebug_add_out_of_bounds_check(void *addr, size_t size)
{
uintptr_t size_addr = (uintptr_t)addr;
uintptr_t suffix_addr = size_addr +
sizeof(size) + size;
memcpy((void *)size_addr, &size, sizeof(size));
memdebug_suffix suffix = MEMDEBUG_MAGIC_SUFFIX;
memcpy((void *)suffix_addr, &suffix, sizeof(suffix));
return (void *)((uintptr_t)addr + sizeof(size));
}
int memdebug_log(char *format, ...)
{
int ret = 0;
va_list args;
va_start(args, format);
ret = vfprintf(_memdebug_fp, format, args);
va_end(args);
return ret;
}
void *__memdebug_malloc(size_t size, char *file, int line)
{
#ifndef MEMDEBUG_OUT_OF_BOUNDS
void *addr = MEMDEBUG_MALLOC_SYMBOL(size);
#else
void *addr = MEMDEBUG_MALLOC_SYMBOL(
size + MEMDEBUG_OUT_OF_BOUNDS_EXTRA_SIZE);
if(addr) addr = memdebug_add_out_of_bounds_check(addr, size);
#endif
MEMDEBUG_LOG_FUNC(malloc, addr, file, line);
MEMDEBUG_LOG("size: %zu, ret: %p", size, addr);
MEMDEBUG_LOG("\n");
fflush(_memdebug_fp);
return addr;
}
void *__memdebug_realloc(void *ptr, size_t size, char *file, int line)
{
#ifndef MEMDEBUG_OUT_OF_BOUNDS
void *addr = MEMDEBUG_REALLOC_SYMBOL(ptr, size);
#else
void *addr = MEMDEBUG_REALLOC_SYMBOL(
ptr, size + MEMDEBUG_OUT_OF_BOUNDS_EXTRA_SIZE);
if(addr) addr = memdebug_add_out_of_bounds_check(addr, size);
#endif
MEMDEBUG_LOG_FUNC(realloc, addr, file, line);
MEMDEBUG_LOG("ptr: %p, size: %zu, ret: %p", ptr, size, addr);
MEMDEBUG_LOG("\n");
fflush(_memdebug_fp);
return addr;
}
void *__memdebug_calloc(size_t nmemb, size_t size, char *file, int line)
{
#ifndef MEMDEBUG_OUT_OF_BOUNDS
void *addr = MEMDEBUG_CALLOC_SYMBOL(nmemb, size);
#else
void *addr = MEMDEBUG_MALLOC_SYMBOL(
nmemb * size + MEMDEBUG_OUT_OF_BOUNDS_EXTRA_SIZE);
if(addr) addr = memdebug_add_out_of_bounds_check(addr, nmemb * size);
memset(addr, 0, nmemb * size);
#endif
MEMDEBUG_LOG_FUNC(calloc, addr, file, line);
MEMDEBUG_LOG("nmemb: %zu, size: %zu, ret: %p", nmemb, size, addr);
MEMDEBUG_LOG("\n");
fflush(_memdebug_fp);
return addr;
}
void __memdebug_free(void *ptr, char *file, int line)
{
MEMDEBUG_LOG_FUNC(free, (void *)1, file, line);
MEMDEBUG_LOG("ptr: %p", ptr);
#ifdef MEMDEBUG_OUT_OF_BOUNDS
if(ptr != NULL) {
size_t size = *(size_t *)((uintptr_t)ptr - sizeof(size_t));
memdebug_suffix suffix = 0;
memcpy(&suffix, (void *)((uintptr_t)ptr + size), sizeof(suffix));
MEMDEBUG_LOG(", ");
MEMDEBUG_LOG("out-of-bounds-check: ");
if(suffix == (memdebug_suffix)MEMDEBUG_MAGIC_SUFFIX)
MEMDEBUG_LOG("SUCCESS");
else MEMDEBUG_LOG("FAILED");
ptr = (void *)((uintptr_t)ptr + sizeof(size_t));
}
#endif
MEMDEBUG_LOG("\n");
fflush(_memdebug_fp);
MEMDEBUG_FREE_SYMBOL(ptr);
}
#ifdef MEMDEBUG_MAIN_VOID
int real_main(void);
#define CALL_MAIN real_main()
#else
int real_main(int argc, char **argv);
#define CALL_MAIN real_main(argc, argv)
#endif
int main(int argc, char **argv)
{
#ifdef MEMDEBUG_MAIN_VOID
(void)argc;
(void)argv;
#endif
#ifdef MEMDEBUG_OUTPUT_LOG
size_t filename_sz = 64 + sizeof(MEMDEBUG_OUTPUT_DIR) + sizeof(MEMDEBUG_OUTPUT_FMT);
char *filename = malloc(filename_sz);
if(!filename) return -ENOMEM;
memset(filename, 0, filename_sz);
snprintf(filename, filename_sz, MEMDEBUG_OUTPUT_DIR"/"MEMDEBUG_OUTPUT_FMT, time(NULL));
_memdebug_fp = fopen(filename, "w");
if(!_memdebug_fp) {
perror(filename);
free(filename);
return errno;
}
int ret = CALL_MAIN;
fclose(_memdebug_fp);
free(filename);
return ret;
#else
_memdebug_fp = stdout;
return CALL_MAIN;
#endif
}
#define main real_main
#endif
#define __MEMDEBUG_CALL(f, ...) f(__VA_ARGS__, __FILE__, __LINE__)
#define malloc(size) __MEMDEBUG_CALL(__memdebug_malloc, size)
#define calloc(nmemb, size) __MEMDEBUG_CALL(__memdebug_calloc, nmemb, size)
#define realloc(ptr, size) __MEMDEBUG_CALL(__memdebug_realloc, ptr, size)
#define free(ptr) __MEMDEBUG_CALL(__memdebug_free, ptr)
#endif
|