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 void *debug_malloc(size_t size, const char *file, unsigned int line)
158 struct debug_alloc_hdr *hdr;
159 struct debug_alloc_ftr *ftr;
160 size_t new_size = size + sizeof(*hdr) + sizeof(*ftr);
165 if (alloc_fail_proba != 0 && (r % 100) < alloc_fail_proba)
168 hdr = malloc(new_size);
176 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
177 hdr->cookie = 0x12345678;
178 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
180 ftr = (struct debug_alloc_ftr *)(
181 (char *)hdr + size + sizeof(*hdr));
182 ftr->cookie = 0x87654321;
185 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: malloc(%zd) -> %p seq=%d\n",
186 file, line, size, ret, seq++);
193 static void debug_free(void *ptr, const char *file, unsigned int line)
195 struct debug_alloc_hdr *hdr, *h;
196 struct debug_alloc_ftr *ftr;
201 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: free(%p)\n", file, line, ptr);
206 hdr = (ptr - sizeof(*hdr));
207 if (hdr->cookie != 0x12345678) {
208 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad start cookie\n",
213 ftr = (ptr + hdr->size);
214 if (ftr->cookie != 0x87654321) {
215 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad end cookie\n",
220 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
226 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad ptr\n",
231 TAILQ_REMOVE(&debug_alloc_hdr_list, hdr, next);
235 static void *debug_realloc(void *ptr, size_t size, const char *file,
238 struct debug_alloc_hdr *hdr, *h;
239 struct debug_alloc_ftr *ftr;
240 size_t new_size = size + sizeof(*hdr) + sizeof(unsigned int);
244 hdr = (ptr - sizeof(*hdr));
245 if (hdr->cookie != 0x12345678) {
247 "%s:%d: error: realloc(%p): bad start cookie\n",
252 ftr = (ptr + hdr->size);
253 if (ftr->cookie != 0x87654321) {
255 "%s:%d: error: realloc(%p): bad end cookie\n",
260 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
266 EC_LOG(EC_LOG_ERR, "%s:%d: error: realloc(%p): bad ptr\n",
271 TAILQ_REMOVE(&debug_alloc_hdr_list, h, next);
272 hdr = realloc(hdr, new_size);
274 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, h, next);
280 hdr = realloc(NULL, new_size);
291 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
292 hdr->cookie = 0x12345678;
293 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
294 ftr = (struct debug_alloc_ftr *)(
295 (char *)hdr + size + sizeof(*hdr));
296 ftr->cookie = 0x87654321;
299 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: realloc(%p, %zd) -> %p\n",
300 file, line, ptr, size, ret);
307 static int debug_alloc_dump_leaks(void)
309 struct debug_alloc_hdr *hdr;
313 EC_LOG(EC_LOG_INFO, "%zd successful allocations\n", alloc_success);
315 if (TAILQ_EMPTY(&debug_alloc_hdr_list))
318 TAILQ_FOREACH(hdr, &debug_alloc_hdr_list, next) {
320 "%s:%d: error: memory leak size=%zd ptr=%p\n",
321 hdr->file, hdr->line, hdr->size, hdr + 1);
322 buffer = backtrace_symbols(hdr->stack, hdr->stacklen);
323 if (buffer == NULL) {
324 for (i = 0; i < hdr->stacklen; i++)
325 EC_LOG(EC_LOG_ERR, " %p\n", hdr->stack[i]);
327 for (i = 0; i < hdr->stacklen; i++)
328 EC_LOG(EC_LOG_ERR, " %s\n",
329 buffer ? buffer[i] : "unknown");
335 " missing static syms, use: addr2line -f -e <prog> <addr>\n");
340 static int debug_log(int type, unsigned int level, void *opaque,
346 if (level > (unsigned int)log_level)
349 if (printf("%s", str) < 0)
355 int main(int argc, char **argv)
357 int i, ret = 0, leaks;
359 ret = parse_args(argc, argv);
368 /* register a new malloc to track memleaks */
369 TAILQ_INIT(&debug_alloc_hdr_list);
370 if (ec_malloc_register(debug_malloc, debug_free, debug_realloc) < 0) {
371 EC_LOG(EC_LOG_ERR, "cannot register new malloc\n");
376 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
379 ec_log_fct_register(debug_log, NULL);
385 for (i = 1; i < argc; i++)
386 ret |= ec_test_one(argv[i]);
389 leaks = debug_alloc_dump_leaks();
391 if (alloc_fail_proba == 0 && ret != 0) {
392 printf("tests failed\n");
394 } else if (alloc_fail_proba != 0 && leaks != 0) {
395 printf("tests failed (memory leak)\n");
399 printf("\ntests ok\n");