1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
13 #include <ecoli_init.h>
14 #include <ecoli_log.h>
15 #include <ecoli_test.h>
16 #include <ecoli_malloc.h>
19 EC_LOG_TYPE_REGISTER(main);
21 #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
22 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
24 static int log_level = EC_LOG_INFO;
25 static int alloc_fail_proba = 0;
27 static size_t alloc_success = 0;
29 static const char ec_short_options[] =
32 "r:" /* random-alloc-fail */
36 #define EC_OPT_HELP "help"
37 #define EC_OPT_LOG_LEVEL "log-level"
38 #define EC_OPT_RANDOM_ALLOC_FAIL "random-alloc-fail"
39 #define EC_OPT_SEED "seed"
41 static const struct option ec_long_options[] = {
42 {EC_OPT_HELP, 1, NULL, 'h'},
43 {EC_OPT_LOG_LEVEL, 1, NULL, 'l'},
44 {EC_OPT_RANDOM_ALLOC_FAIL, 1, NULL, 'r'},
45 {EC_OPT_SEED, 1, NULL, 's'},
49 static void usage(const char *prgname)
51 printf("%s [options] [test1 test2 test3...]\n"
56 " --"EC_OPT_LOG_LEVEL"=<level>\n"
57 " Set log level (0 = no log, 7 = verbose).\n"
59 " --"EC_OPT_RANDOM_ALLOC_FAIL"=<probability>\n"
60 " Cause malloc to fail randomly. This helps to debug\n"
61 " leaks or crashes in error cases. The probability is\n"
62 " between 0 and 100.\n"
65 " Seeds the random number generator. Default is 0.\n"
70 parse_int(const char *s, int min, int max, int *ret, unsigned int base)
75 n = strtoll(s, &end, base);
76 if ((s[0] == '\0') || (end == NULL) || (*end != '\0'))
87 static int parse_args(int argc, char **argv)
91 while ((opt = getopt_long(argc, argv, ec_short_options,
92 ec_long_options, NULL)) != EOF) {
99 case 'l': /* log-level */
100 if (parse_int(optarg, EC_LOG_EMERG,
101 EC_LOG_DEBUG, &log_level, 10) < 0) {
102 printf("Invalid log value\n");
108 case 'r': /* random-alloc-fail */
109 if (parse_int(optarg, 0, 100, &alloc_fail_proba,
111 printf("Invalid probability value\n");
118 if (parse_int(optarg, 0, INT_MAX, &seed, 10) < 0) {
119 printf("Invalid seed value\n");
137 TAILQ_HEAD(debug_alloc_hdr_list, debug_alloc_hdr);
138 static struct debug_alloc_hdr_list debug_alloc_hdr_list =
139 TAILQ_HEAD_INITIALIZER(debug_alloc_hdr_list);
142 struct debug_alloc_hdr {
143 TAILQ_ENTRY(debug_alloc_hdr) next;
147 void *stack[STACK_SZ];
152 struct debug_alloc_ftr {
154 } __attribute__((packed));
156 static int malloc_seq;
158 static void *debug_malloc(size_t size, const char *file, unsigned int line)
160 struct debug_alloc_hdr *hdr;
161 struct debug_alloc_ftr *ftr;
162 size_t new_size = size + sizeof(*hdr) + sizeof(*ftr);
166 if (alloc_fail_proba != 0 && (r % 100) < alloc_fail_proba)
169 hdr = malloc(new_size);
177 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
178 hdr->cookie = 0x12345678;
179 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
181 ftr = (struct debug_alloc_ftr *)(
182 (char *)hdr + size + sizeof(*hdr));
183 ftr->cookie = 0x87654321;
186 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: malloc(%zd) -> %p seq=%d\n",
187 file, line, size, ret, malloc_seq++);
194 static void debug_free(void *ptr, const char *file, unsigned int line)
196 struct debug_alloc_hdr *hdr, *h;
197 struct debug_alloc_ftr *ftr;
202 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: free(%p)\n", file, line, ptr);
207 hdr = (ptr - sizeof(*hdr));
208 if (hdr->cookie != 0x12345678) {
209 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad start cookie\n",
214 ftr = (ptr + hdr->size);
215 if (ftr->cookie != 0x87654321) {
216 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad end cookie\n",
221 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
227 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad ptr\n",
232 TAILQ_REMOVE(&debug_alloc_hdr_list, hdr, next);
236 static void *debug_realloc(void *ptr, size_t size, const char *file,
239 struct debug_alloc_hdr *hdr, *h;
240 struct debug_alloc_ftr *ftr;
241 size_t new_size = size + sizeof(*hdr) + sizeof(unsigned int);
245 hdr = (ptr - sizeof(*hdr));
246 if (hdr->cookie != 0x12345678) {
248 "%s:%d: error: realloc(%p): bad start cookie\n",
253 ftr = (ptr + hdr->size);
254 if (ftr->cookie != 0x87654321) {
256 "%s:%d: error: realloc(%p): bad end cookie\n",
261 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
267 EC_LOG(EC_LOG_ERR, "%s:%d: error: realloc(%p): bad ptr\n",
272 TAILQ_REMOVE(&debug_alloc_hdr_list, h, next);
273 hdr = realloc(hdr, new_size);
275 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, h, next);
281 hdr = realloc(NULL, new_size);
292 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
293 hdr->cookie = 0x12345678;
294 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
295 ftr = (struct debug_alloc_ftr *)(
296 (char *)hdr + size + sizeof(*hdr));
297 ftr->cookie = 0x87654321;
300 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: realloc(%p, %zd) -> %p seq=%d\n",
301 file, line, ptr, size, ret, malloc_seq++);
308 static int debug_alloc_dump_leaks(void)
310 struct debug_alloc_hdr *hdr;
314 EC_LOG(EC_LOG_INFO, "%zd successful allocations\n", alloc_success);
316 if (TAILQ_EMPTY(&debug_alloc_hdr_list))
319 TAILQ_FOREACH(hdr, &debug_alloc_hdr_list, next) {
321 "%s:%d: error: memory leak size=%zd ptr=%p\n",
322 hdr->file, hdr->line, hdr->size, hdr + 1);
323 buffer = backtrace_symbols(hdr->stack, hdr->stacklen);
324 if (buffer == NULL) {
325 for (i = 0; i < hdr->stacklen; i++)
326 EC_LOG(EC_LOG_ERR, " %p\n", hdr->stack[i]);
328 for (i = 0; i < hdr->stacklen; i++)
329 EC_LOG(EC_LOG_ERR, " %s\n",
330 buffer ? buffer[i] : "unknown");
336 " missing static syms, use: addr2line -f -e <prog> <addr>\n");
341 static int debug_log(int type, unsigned int level, void *opaque,
347 if (level > (unsigned int)log_level)
350 if (printf("%s", str) < 0)
356 int main(int argc, char **argv)
358 int i, ret = 0, leaks;
360 ret = parse_args(argc, argv);
369 /* register a new malloc to track memleaks */
370 TAILQ_INIT(&debug_alloc_hdr_list);
371 if (ec_malloc_register(debug_malloc, debug_free, debug_realloc) < 0) {
372 EC_LOG(EC_LOG_ERR, "cannot register new malloc\n");
377 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
380 ec_log_fct_register(debug_log, NULL);
386 for (i = 1; i < argc; i++)
387 ret |= ec_test_one(argv[i]);
390 leaks = debug_alloc_dump_leaks();
392 if (alloc_fail_proba == 0 && ret != 0) {
393 printf("tests failed\n");
395 } else if (alloc_fail_proba != 0 && leaks != 0) {
396 printf("tests failed (memory leak)\n");
400 printf("\ntests ok\n");