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>
18 EC_LOG_TYPE_REGISTER(main);
20 #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / \
21 ((size_t)(!(sizeof(x) % sizeof(0[x])))))
23 static int log_level = EC_LOG_INFO;
24 static int alloc_fail_proba = 0;
26 static size_t alloc_success = 0;
28 static const char ec_short_options[] =
31 "r:" /* random-alloc-fail */
35 #define EC_OPT_HELP "help"
36 #define EC_OPT_LOG_LEVEL "log-level"
37 #define EC_OPT_RANDOM_ALLOC_FAIL "random-alloc-fail"
38 #define EC_OPT_SEED "seed"
40 static const struct option ec_long_options[] = {
41 {EC_OPT_HELP, 1, NULL, 'h'},
42 {EC_OPT_LOG_LEVEL, 1, NULL, 'l'},
43 {EC_OPT_RANDOM_ALLOC_FAIL, 1, NULL, 'r'},
44 {EC_OPT_SEED, 1, NULL, 's'},
48 static void usage(const char *prgname)
50 printf("%s [options] [test1 test2 test3...]\n"
55 " --"EC_OPT_LOG_LEVEL"=<level>\n"
56 " Set log level (0 = no log, 7 = verbose).\n"
58 " --"EC_OPT_RANDOM_ALLOC_FAIL"=<probability>\n"
59 " Cause malloc to fail randomly. This helps to debug\n"
60 " leaks or crashes in error cases. The probability is\n"
61 " between 0 and 100.\n"
64 " Seeds the random number generator. Default is 0.\n"
69 parse_int(const char *s, int min, int max, int *ret, unsigned int base)
74 n = strtoll(s, &end, base);
75 if ((s[0] == '\0') || (end == NULL) || (*end != '\0'))
86 static int parse_args(int argc, char **argv)
90 while ((opt = getopt_long(argc, argv, ec_short_options,
91 ec_long_options, NULL)) != EOF) {
98 case 'l': /* log-level */
99 if (parse_int(optarg, EC_LOG_EMERG,
100 EC_LOG_DEBUG, &log_level, 10) < 0) {
101 printf("Invalid log value\n");
107 case 'r': /* random-alloc-fail */
108 if (parse_int(optarg, 0, 100, &alloc_fail_proba,
110 printf("Invalid probability value\n");
117 if (parse_int(optarg, 0, INT_MAX, &seed, 10) < 0) {
118 printf("Invalid seed value\n");
136 TAILQ_HEAD(debug_alloc_hdr_list, debug_alloc_hdr);
137 static struct debug_alloc_hdr_list debug_alloc_hdr_list =
138 TAILQ_HEAD_INITIALIZER(debug_alloc_hdr_list);
141 struct debug_alloc_hdr {
142 TAILQ_ENTRY(debug_alloc_hdr) next;
146 void *stack[STACK_SZ];
151 struct debug_alloc_ftr {
153 } __attribute__((packed));
155 static void *debug_malloc(size_t size, const char *file, unsigned int line)
157 struct debug_alloc_hdr *hdr;
158 struct debug_alloc_ftr *ftr;
159 size_t new_size = size + sizeof(*hdr) + sizeof(*ftr);
164 if (alloc_fail_proba != 0 && (r % 100) < alloc_fail_proba)
167 hdr = malloc(new_size);
175 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
176 hdr->cookie = 0x12345678;
177 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
179 ftr = (struct debug_alloc_ftr *)(
180 (char *)hdr + size + sizeof(*hdr));
181 ftr->cookie = 0x87654321;
184 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: malloc(%zd) -> %p seq=%d\n",
185 file, line, size, ret, seq++);
192 static void debug_free(void *ptr, const char *file, unsigned int line)
194 struct debug_alloc_hdr *hdr, *h;
195 struct debug_alloc_ftr *ftr;
200 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: free(%p)\n", file, line, ptr);
205 hdr = (ptr - sizeof(*hdr));
206 if (hdr->cookie != 0x12345678) {
207 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad start cookie\n",
212 ftr = (ptr + hdr->size);
213 if (ftr->cookie != 0x87654321) {
214 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad end cookie\n",
219 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
225 EC_LOG(EC_LOG_ERR, "%s:%d: error: free(%p): bad ptr\n",
230 TAILQ_REMOVE(&debug_alloc_hdr_list, hdr, next);
234 static void *debug_realloc(void *ptr, size_t size, const char *file,
237 struct debug_alloc_hdr *hdr, *h;
238 struct debug_alloc_ftr *ftr;
239 size_t new_size = size + sizeof(*hdr) + sizeof(unsigned int);
243 hdr = (ptr - sizeof(*hdr));
244 if (hdr->cookie != 0x12345678) {
246 "%s:%d: error: realloc(%p): bad start cookie\n",
251 ftr = (ptr + hdr->size);
252 if (ftr->cookie != 0x87654321) {
254 "%s:%d: error: realloc(%p): bad end cookie\n",
259 TAILQ_FOREACH(h, &debug_alloc_hdr_list, next) {
265 EC_LOG(EC_LOG_ERR, "%s:%d: error: realloc(%p): bad ptr\n",
270 TAILQ_REMOVE(&debug_alloc_hdr_list, h, next);
271 hdr = realloc(hdr, new_size);
273 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, h, next);
279 hdr = realloc(NULL, new_size);
290 hdr->stacklen = backtrace(hdr->stack, COUNT_OF(hdr->stack));
291 hdr->cookie = 0x12345678;
292 TAILQ_INSERT_TAIL(&debug_alloc_hdr_list, hdr, next);
293 ftr = (struct debug_alloc_ftr *)(
294 (char *)hdr + size + sizeof(*hdr));
295 ftr->cookie = 0x87654321;
298 EC_LOG(EC_LOG_DEBUG, "%s:%d: info: realloc(%p, %zd) -> %p\n",
299 file, line, ptr, size, ret);
306 static int debug_alloc_dump_leaks(void)
308 struct debug_alloc_hdr *hdr;
312 EC_LOG(EC_LOG_INFO, "%zd successful allocations\n", alloc_success);
314 if (TAILQ_EMPTY(&debug_alloc_hdr_list))
317 TAILQ_FOREACH(hdr, &debug_alloc_hdr_list, next) {
319 "%s:%d: error: memory leak size=%zd ptr=%p\n",
320 hdr->file, hdr->line, hdr->size, hdr + 1);
321 buffer = backtrace_symbols(hdr->stack, hdr->stacklen);
322 if (buffer == NULL) {
323 for (i = 0; i < hdr->stacklen; i++)
324 EC_LOG(EC_LOG_ERR, " %p\n", hdr->stack[i]);
326 for (i = 0; i < hdr->stacklen; i++)
327 EC_LOG(EC_LOG_ERR, " %s\n",
328 buffer ? buffer[i] : "unknown");
334 " missing static syms, use: addr2line -f -e <prog> <addr>\n");
339 static int debug_log(int type, unsigned int level, void *opaque,
345 if (level > (unsigned int)log_level)
348 if (printf("%s", str) < 0)
354 int main(int argc, char **argv)
356 int i, ret = 0, leaks;
358 ret = parse_args(argc, argv);
367 /* register a new malloc to track memleaks */
368 TAILQ_INIT(&debug_alloc_hdr_list);
369 if (ec_malloc_register(debug_malloc, debug_free, debug_realloc) < 0) {
370 EC_LOG(EC_LOG_ERR, "cannot register new malloc\n");
375 fprintf(stderr, "cannot init ecoli: %s\n", strerror(errno));
378 ec_log_fct_register(debug_log, NULL);
384 for (i = 1; i < argc; i++)
385 ret |= ec_test_one(argv[i]);
388 leaks = debug_alloc_dump_leaks();
390 if (alloc_fail_proba == 0 && ret != 0) {
391 printf("tests failed\n");
393 } else if (alloc_fail_proba != 0 && leaks != 0) {
394 printf("tests failed (memory leak)\n");
398 printf("\ntests ok\n");