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.
36 #include <ecoli_init.h>
37 #include <ecoli_log.h>
38 #include <ecoli_test.h>
39 #include <ecoli_malloc.h>
41 EC_LOG_TYPE_REGISTER(main);
43 #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
44 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
46 static int log_level = EC_LOG_INFO;
47 static int alloc_fail_proba = 0;
49 static size_t alloc_success = 0;
51 static const char ec_short_options[] =
54 "r:" /* random-alloc-fail */
58 #define EC_OPT_HELP "help"
59 #define EC_OPT_LOG_LEVEL "log-level"
60 #define EC_OPT_RANDOM_ALLOC_FAIL "random-alloc-fail"
61 #define EC_OPT_SEED "seed"
63 static const struct option ec_long_options[] = {
64 {EC_OPT_HELP, 1, NULL, 'h'},
65 {EC_OPT_LOG_LEVEL, 1, NULL, 'l'},
66 {EC_OPT_RANDOM_ALLOC_FAIL, 1, NULL, 'r'},
67 {EC_OPT_SEED, 1, NULL, 's'},
71 static void usage(const char *prgname)
73 printf("%s [options] [test1 test2 test3...]\n"
78 " --"EC_OPT_LOG_LEVEL"=<level>\n"
79 " Set log level (0 = no log, 7 = verbose).\n"
81 " --"EC_OPT_RANDOM_ALLOC_FAIL"=<probability>\n"
82 " Cause malloc to fail randomly. This helps to debug\n"
83 " leaks or crashes in error cases. The probability is\n"
84 " between 0 and 100.\n"
87 " Seeds the random number generator. Default is 0.\n"
92 parse_int(const char *s, int min, int max, int *ret, unsigned int base)
97 n = strtoll(s, &end, base);
98 if ((s[0] == '\0') || (end == NULL) || (*end != '\0'))
109 static int parse_args(int argc, char **argv)
113 while ((opt = getopt_long(argc, argv, ec_short_options,
114 ec_long_options, NULL)) != EOF) {
121 case 'l': /* log-level */
122 if (parse_int(optarg, EC_LOG_EMERG,
123 EC_LOG_DEBUG, &log_level, 10) < 0) {
124 printf("Invalid log value\n");
130 case 'r': /* random-alloc-fail */
131 if (parse_int(optarg, 0, 100, &alloc_fail_proba,
133 printf("Invalid probability value\n");
140 if (parse_int(optarg, 0, INT_MAX, &seed, 10) < 0) {
141 printf("Invalid seed value\n");
159 TAILQ_HEAD(debug_alloc_hdr_list, debug_alloc_hdr);
160 static struct debug_alloc_hdr_list debug_alloc_hdr_list =
161 TAILQ_HEAD_INITIALIZER(debug_alloc_hdr_list);
164 struct debug_alloc_hdr {
165 TAILQ_ENTRY(debug_alloc_hdr) next;
169 void *stack[STACK_SZ];
174 struct debug_alloc_ftr {
176 } __attribute__((packed));
178 static void *debug_malloc(size_t size, const char *file, unsigned int line)
180 struct debug_alloc_hdr *hdr;
181 struct debug_alloc_ftr *ftr;
182 size_t new_size = size + sizeof(*hdr) + sizeof(*ftr);
187 if (alloc_fail_proba != 0 && (r % 100) < alloc_fail_proba)
190 hdr = malloc(new_size);
198 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
199 hdr->cookie = 0x12345678;
200 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
202 ftr = (struct debug_alloc_ftr *)(
203 (char *)hdr + size + sizeof(*hdr));
204 ftr->cookie = 0x87654321;
207 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: malloc(%zd) -> %p seq=%d\n",
208 file, line, size, ret, seq++);
217 static void debug_free(void *ptr, const char *file, unsigned int line)
219 struct debug_alloc_hdr *hdr, *h;
220 struct debug_alloc_ftr *ftr;
225 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: free(%p)\n", file, line, ptr);
230 hdr = (ptr - sizeof(*hdr));
231 if (hdr->cookie != 0x12345678) {
232 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad start cookie\n",
237 ftr = (ptr + hdr->size);
238 if (ftr->cookie != 0x87654321) {
239 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad end cookie\n",
244 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
250 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad ptr\n",
255 TAILQ_REMOVE(&debug_alloc_hdr_list, hdr, next);
259 static void *debug_realloc(void *ptr, size_t size, const char *file,
262 struct debug_alloc_hdr *hdr, *h;
263 struct debug_alloc_ftr *ftr;
264 size_t new_size = size + sizeof(*hdr) + sizeof(unsigned int);
268 hdr = (ptr - sizeof(*hdr));
269 if (hdr->cookie != 0x12345678) {
271 "%s:%d: error: realloc(%p): bad start cookie\n",
276 ftr = (ptr + hdr->size);
277 if (ftr->cookie != 0x87654321) {
279 "%s:%d: error: realloc(%p): bad end cookie\n",
284 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
290 EC_LOG(EC_LOG_ERR, "%s:%d: error: realloc(%p): bad ptr\n",
295 TAILQ_REMOVE(&debug_alloc_hdr_list, h, next);
296 hdr = realloc(hdr, new_size);
298 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, h, next);
304 hdr = realloc(NULL, new_size);
315 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
316 hdr->cookie = 0x12345678;
317 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
318 ftr = (struct debug_alloc_ftr *)(
319 (char *)hdr + size + sizeof(*hdr));
320 ftr->cookie = 0x87654321;
323 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: realloc(%p, %zd) -> %p\n",
324 file, line, ptr, size, ret);
331 static int debug_alloc_dump_leaks(void)
333 struct debug_alloc_hdr *hdr;
337 EC_LOG(EC_LOG_INFO, "%zd successful allocations\n", alloc_success);
339 if (TAILQ_EMPTY(&debug_alloc_hdr_list))
342 TAILQ_FOREACH(hdr, &debug_alloc_hdr_list, next) {
344 "%s:%d: error: memory leak size=%zd ptr=%p\n",
345 hdr->file, hdr->line, hdr->size, hdr + 1);
346 buffer = backtrace_symbols(hdr->stack, hdr->stacklen);
347 if (buffer == NULL) {
348 for (i = 0; i < hdr->stacklen; i++)
349 EC_LOG(EC_LOG_ERR, " %p\n", hdr->stack[i]);
351 for (i = 0; i < hdr->stacklen; i++)
352 EC_LOG(EC_LOG_ERR, " %s\n",
353 buffer ? buffer[i] : "unknown");
359 " missing static syms, use: addr2line -f -e <prog> <addr>\n");
364 static int debug_log(int type, unsigned int level, void *opaque,
370 if (level > (unsigned int)log_level)
373 if (printf("%s", str) < 0)
379 int main(int argc, char **argv)
381 int i, ret = 0, leaks;
383 ret = parse_args(argc, argv);
392 /* register a new malloc to track memleaks */
393 TAILQ_INIT(&debug_alloc_hdr_list);
394 if (ec_malloc_register(debug_malloc, debug_free, debug_realloc) < 0) {
395 EC_LOG(EC_LOG_ERR, "cannot register new malloc\n");
400 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
403 ec_log_fct_register(debug_log, NULL);
409 for (i = 1; i < argc; i++)
410 ret |= ec_test_one(argv[i]);
413 leaks = debug_alloc_dump_leaks();
415 if (alloc_fail_proba == 0 && ret != 0) {
416 printf("tests failed\n");
418 } else if (alloc_fail_proba != 0 && leaks != 0) {
419 printf("tests failed (memory leak)\n");
423 printf("\ntests ok\n");