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.
35 #include <ecoli_log.h>
36 #include <ecoli_test.h>
37 #include <ecoli_malloc.h>
39 #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
40 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
42 static int log_level = EC_LOG_INFO;
43 static int alloc_fail_proba = 0;
46 static const char ec_short_options[] =
49 "r:" /* random-alloc-fail */
53 #define EC_OPT_HELP "help"
54 #define EC_OPT_LOG_LEVEL "log-level"
55 #define EC_OPT_RANDOM_ALLOC_FAIL "random-alloc-fail"
56 #define EC_OPT_SEED "seed"
58 static const struct option ec_long_options[] = {
59 {EC_OPT_HELP, 1, NULL, 'h'},
60 {EC_OPT_LOG_LEVEL, 1, NULL, 'l'},
61 {EC_OPT_RANDOM_ALLOC_FAIL, 1, NULL, 'r'},
62 {EC_OPT_SEED, 1, NULL, 's'},
66 static void usage(const char *prgname)
68 /* XXX add a parameter to test only one testcase */
69 printf("%s [options]\n"
74 " --"EC_OPT_LOG_LEVEL"=<level>\n"
75 " Set log level (0 = no log, 7 = verbose).\n"
77 " --"EC_OPT_RANDOM_ALLOC_FAIL"=<probability>\n"
78 " Cause malloc to fail randomly. This helps to debug\n"
79 " leaks or crashes in error cases. The probability is\n"
80 " between 0 and 100.\n"
83 " Seeds the random number generator. Default is 0.\n"
88 parse_int(const char *s, int min, int max, int *ret, unsigned int base)
93 n = strtoll(s, &end, base);
94 if ((s[0] == '\0') || (end == NULL) || (*end != '\0'))
105 static int parse_args(int argc, char **argv)
109 while ((opt = getopt_long(argc, argv, ec_short_options,
110 ec_long_options, NULL)) != EOF) {
117 case 'l': /* log-level */
118 if (parse_int(optarg, EC_LOG_EMERG,
119 EC_LOG_DEBUG, &log_level, 10) < 0) {
120 printf("Invalid log value\n");
126 case 'r': /* random-alloc-fail */
127 if (parse_int(optarg, 0, 100, &alloc_fail_proba,
129 printf("Invalid probability value\n");
136 if (parse_int(optarg, 0, INT_MAX, &seed, 10) < 0) {
137 printf("Invalid seed value\n");
155 TAILQ_HEAD(debug_alloc_hdr_list, debug_alloc_hdr);
156 static struct debug_alloc_hdr_list debug_alloc_hdr_list =
157 TAILQ_HEAD_INITIALIZER(debug_alloc_hdr_list);
160 struct debug_alloc_hdr {
161 TAILQ_ENTRY(debug_alloc_hdr) next;
165 void *stack[STACK_SZ];
170 struct debug_alloc_ftr {
172 } __attribute__((packed));
174 static void *debug_malloc(size_t size, const char *file, unsigned int line)
176 struct debug_alloc_hdr *hdr;
177 struct debug_alloc_ftr *ftr;
178 size_t new_size = size + sizeof(*hdr) + sizeof(*ftr);
182 if (alloc_fail_proba != 0 && (random() % 100) < alloc_fail_proba)
185 hdr = malloc(new_size);
193 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
194 hdr->cookie = 0x12345678;
195 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
197 ftr = (struct debug_alloc_ftr *)(
198 (char *)hdr + size + sizeof(*hdr));
199 ftr->cookie = 0x87654321;
202 ec_log(EC_LOG_DEBUG, "%s:%d: info: malloc(%zd) -> %p\n",
203 file, line, size, ret);
208 static void debug_free(void *ptr, const char *file, unsigned int line)
210 struct debug_alloc_hdr *hdr, *h;
211 struct debug_alloc_ftr *ftr;
216 ec_log(EC_LOG_DEBUG, "%s:%d: info: free(%p)\n", file, line, ptr);
221 hdr = (ptr - sizeof(*hdr));
222 if (hdr->cookie != 0x12345678) {
223 ec_log(EC_LOG_ERR, "%s:%d: error: free(%p): bad start cookie\n",
228 ftr = (ptr + hdr->size);
229 if (ftr->cookie != 0x87654321) {
230 ec_log(EC_LOG_ERR, "%s:%d: error: free(%p): bad end cookie\n",
235 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
241 ec_log(EC_LOG_ERR, "%s:%d: error: free(%p): bad ptr\n",
246 TAILQ_REMOVE(&debug_alloc_hdr_list, hdr, next);
250 static void *debug_realloc(void *ptr, size_t size, const char *file,
253 struct debug_alloc_hdr *hdr, *h;
254 struct debug_alloc_ftr *ftr;
255 size_t new_size = size + sizeof(*hdr) + sizeof(unsigned int);
259 hdr = (ptr - sizeof(*hdr));
260 if (hdr->cookie != 0x12345678) {
262 "%s:%d: error: realloc(%p): bad start cookie\n",
267 ftr = (ptr + hdr->size);
268 if (ftr->cookie != 0x87654321) {
270 "%s:%d: error: realloc(%p): bad end cookie\n",
275 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
281 ec_log(EC_LOG_ERR, "%s:%d: error: realloc(%p): bad ptr\n",
286 TAILQ_REMOVE(&debug_alloc_hdr_list, h, next);
287 hdr = realloc(hdr, new_size);
289 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, h, next);
295 hdr = realloc(NULL, new_size);
306 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
307 hdr->cookie = 0x12345678;
308 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
309 ftr = (struct debug_alloc_ftr *)(
310 (char *)hdr + size + sizeof(*hdr));
311 ftr->cookie = 0x87654321;
314 ec_log(EC_LOG_DEBUG, "%s:%d: info: realloc(%p, %zd) -> %p\n",
315 file, line, ptr, size, ret);
320 static int debug_alloc_dump_leaks(void)
322 struct debug_alloc_hdr *hdr;
326 if (TAILQ_EMPTY(&debug_alloc_hdr_list))
329 TAILQ_FOREACH(hdr, &debug_alloc_hdr_list, next) {
331 "%s:%d: error: memory leak size=%zd ptr=%p\n",
332 hdr->file, hdr->line, hdr->size, hdr + 1);
333 buffer = backtrace_symbols(hdr->stack, hdr->stacklen);
334 if (buffer == NULL) {
335 for (i = 0; i < hdr->stacklen; i++)
336 ec_log(EC_LOG_ERR, " %p\n", hdr->stack[i]);
338 for (i = 0; i < hdr->stacklen; i++)
339 ec_log(EC_LOG_ERR, " %s\n",
340 buffer ? buffer[i] : "unknown");
346 " missing static syms, use: addr2line -f -e <prog> <addr>\n");
351 static int debug_log(unsigned int level, void *opaque, const char *str)
355 if (level > (unsigned int)log_level)
358 return printf("%s", str);
361 int main(int argc, char **argv)
363 int i, ret = 0, leaks;
365 ret = parse_args(argc, argv);
374 ec_log_register(debug_log, NULL);
376 /* register a new malloc to track memleaks */
377 TAILQ_INIT(&debug_alloc_hdr_list);
378 if (ec_malloc_register(debug_malloc, debug_free, debug_realloc) < 0) {
379 ec_log(EC_LOG_ERR, "cannot register new malloc\n");
387 for (i = 1; i < argc; i++)
388 ret |= ec_test_one(argv[i]);
391 ec_malloc_unregister();
392 leaks = debug_alloc_dump_leaks();
394 if (alloc_fail_proba == 0 && ret != 0) {
395 printf("tests failed\n");
397 } else if (alloc_fail_proba != 0 && leaks != 0) {
398 printf("tests failed (memory leak)\n");
402 printf("\ntests ok\n");