2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <ecoli_log.h>
35 #include <ecoli_test.h>
36 #include <ecoli_malloc.h>
38 #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
39 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
41 static int log_level = EC_LOG_INFO;
42 static int alloc_fail_proba = 0;
44 static const char ec_short_options[] =
47 "r:" /* random-alloc-fail */
50 #define EC_OPT_HELP "help"
51 #define EC_OPT_LOG_LEVEL "log-level"
52 #define EC_OPT_RANDOM_ALLOC_FAIL "random-alloc-fail"
54 static const struct option ec_long_options[] = {
55 {EC_OPT_HELP, 1, NULL, 'h'},
56 {EC_OPT_LOG_LEVEL, 1, NULL, 'l'},
57 {EC_OPT_RANDOM_ALLOC_FAIL, 1, NULL, 'r'},
61 static void usage(const char *prgname)
63 printf("%s [options]\n"
68 " --"EC_OPT_LOG_LEVEL"=<level>\n"
69 " Set log level (0 = no log, 6 = verbose).\n"
71 " --"EC_OPT_RANDOM_ALLOC_FAIL"=<probability>\n"
72 " Cause malloc to fail randomly. This helps to debug\n"
73 " leaks or crashes in error cases. The probability is\n"
74 " between 0 and 100.\n"
79 parse_int(const char *s, int min, int max, int *ret, unsigned int base)
84 n = strtoll(s, &end, base);
85 if ((s[0] == '\0') || (end == NULL) || (*end != '\0'))
96 static void parse_args(int argc, char **argv)
100 while ((opt = getopt_long(argc, argv, ec_short_options,
101 ec_long_options, NULL)) != EOF) {
108 case 'l': /* log-level */
109 if (parse_int(optarg, EC_LOG_EMERG,
110 EC_LOG_DEBUG, &log_level, 10) < 0) {
111 printf("Invalid log value\n");
117 case 'r': /* random-alloc-fail */
118 if (parse_int(optarg, 0, 100, &alloc_fail_proba,
120 printf("Invalid probability value\n");
133 TAILQ_HEAD(debug_alloc_hdr_list, debug_alloc_hdr);
134 static struct debug_alloc_hdr_list debug_alloc_hdr_list =
135 TAILQ_HEAD_INITIALIZER(debug_alloc_hdr_list);
138 struct debug_alloc_hdr {
139 TAILQ_ENTRY(debug_alloc_hdr) next;
143 void *stack[STACK_SZ];
148 struct debug_alloc_ftr {
150 } __attribute__((packed));
152 static void *debug_malloc(size_t size, const char *file, unsigned int line)
154 struct debug_alloc_hdr *hdr;
155 struct debug_alloc_ftr *ftr;
156 size_t new_size = size + sizeof(*hdr) + sizeof(*ftr);
160 if (alloc_fail_proba != 0 && (random() % 100) < alloc_fail_proba)
163 hdr = malloc(new_size);
171 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
172 hdr->cookie = 0x12345678;
173 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
175 ftr = (struct debug_alloc_ftr *)(
176 (char *)hdr + size + sizeof(*hdr));
177 ftr->cookie = 0x87654321;
180 ec_log(EC_LOG_DEBUG, "%s:%d: info: malloc(%zd) -> %p\n",
181 file, line, size, ret);
186 static void debug_free(void *ptr, const char *file, unsigned int line)
188 struct debug_alloc_hdr *hdr, *h;
189 struct debug_alloc_ftr *ftr;
194 ec_log(EC_LOG_DEBUG, "%s:%d: info: free(%p)\n", file, line, ptr);
199 hdr = (ptr - sizeof(*hdr));
200 if (hdr->cookie != 0x12345678) {
201 ec_log(EC_LOG_ERR, "%s:%d: error: free(%p): bad start cookie\n",
206 ftr = (ptr + hdr->size);
207 if (ftr->cookie != 0x87654321) {
208 ec_log(EC_LOG_ERR, "%s:%d: error: free(%p): bad end cookie\n",
213 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
219 ec_log(EC_LOG_ERR, "%s:%d: error: free(%p): bad ptr\n",
224 TAILQ_REMOVE(&debug_alloc_hdr_list, hdr, next);
228 static void *debug_realloc(void *ptr, size_t size, const char *file,
231 struct debug_alloc_hdr *hdr, *h;
232 struct debug_alloc_ftr *ftr;
233 size_t new_size = size + sizeof(*hdr) + sizeof(unsigned int);
237 hdr = (ptr - sizeof(*hdr));
238 if (hdr->cookie != 0x12345678) {
240 "%s:%d: error: realloc(%p): bad start cookie\n",
245 ftr = (ptr + hdr->size);
246 if (ftr->cookie != 0x87654321) {
248 "%s:%d: error: realloc(%p): bad end cookie\n",
253 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
259 ec_log(EC_LOG_ERR, "%s:%d: error: realloc(%p): bad ptr\n",
264 TAILQ_REMOVE(&debug_alloc_hdr_list, h, next);
265 hdr = realloc(hdr, new_size);
267 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, h, next);
273 hdr = realloc(NULL, new_size);
284 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
285 hdr->cookie = 0x12345678;
286 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
287 ftr = (struct debug_alloc_ftr *)(
288 (char *)hdr + size + sizeof(*hdr));
289 ftr->cookie = 0x87654321;
292 ec_log(EC_LOG_DEBUG, "%s:%d: info: realloc(%p, %zd) -> %p\n",
293 file, line, ptr, size, ret);
298 static int debug_alloc_dump_leaks(void)
300 struct debug_alloc_hdr *hdr;
304 if (TAILQ_EMPTY(&debug_alloc_hdr_list))
307 TAILQ_FOREACH(hdr, &debug_alloc_hdr_list, next) {
309 "%s:%d: error: memory leak size=%zd ptr=%p\n",
310 hdr->file, hdr->line, hdr->size, hdr + 1);
311 buffer = backtrace_symbols(hdr->stack, hdr->stacklen);
312 if (buffer == NULL) {
313 for (i = 0; i < hdr->stacklen; i++)
314 ec_log(EC_LOG_ERR, " %p\n", hdr->stack[i]);
316 for (i = 0; i < hdr->stacklen; i++)
317 ec_log(EC_LOG_ERR, " %s\n",
318 buffer ? buffer[i] : "unknown");
324 " missing static syms, use: addr2line -f -e <prog> <addr>\n");
329 static int debug_log(unsigned int level, void *opaque, const char *str)
333 if (level > (unsigned int)log_level)
336 return printf("%s", str);
339 int main(int argc, char **argv)
343 parse_args(argc, argv);
345 ec_log_register(debug_log, NULL);
347 /* register a new malloc to track memleaks */
348 TAILQ_INIT(&debug_alloc_hdr_list);
349 if (ec_malloc_register(debug_malloc, debug_free, debug_realloc) < 0) {
350 ec_log(EC_LOG_ERR, "cannot register new malloc\n");
356 ec_malloc_unregister();
357 leaks = debug_alloc_dump_leaks();
359 if (alloc_fail_proba == 0 && ret != 0) {
360 printf("tests failed\n");
362 } else if (alloc_fail_proba != 0 && leaks != 0) {
363 printf("tests failed (memory leak)\n");
367 printf("\ntests ok\n");