diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-08 11:48:57 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-04-08 11:48:57 +0000 |
commit | c73b70c7013aa98a86653ad7e7d15bcca16622f2 (patch) | |
tree | 2f2534d588201af85d09335848cef63e93cc83c1 /shell/hush.c | |
parent | 3dfb035d8df33e59492e78a97bf42e961ba178e4 (diff) | |
download | busybox-w32-c73b70c7013aa98a86653ad7e7d15bcca16622f2.tar.gz busybox-w32-c73b70c7013aa98a86653ad7e7d15bcca16622f2.tar.bz2 busybox-w32-c73b70c7013aa98a86653ad7e7d15bcca16622f2.zip |
hush: add leak detector helper; fix/add tests for it
function old new delta
builtin_memleak - 92 +92
bltins 288 300 +12
Diffstat (limited to 'shell/hush.c')
-rw-r--r-- | shell/hush.c | 50 |
1 files changed, 46 insertions, 4 deletions
diff --git a/shell/hush.c b/shell/hush.c index 5594aaea4..6075f514a 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -117,7 +117,10 @@ | |||
117 | #define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ | 117 | #define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__ |
118 | #endif | 118 | #endif |
119 | 119 | ||
120 | /* Keep unconditionally on for now */ | 120 | /* Enable/disable sanity checks. Ok to enable in production, |
121 | * only adds a bit of bloat. | ||
122 | * Keeping unconditionally on for now. | ||
123 | */ | ||
121 | #define HUSH_DEBUG 1 | 124 | #define HUSH_DEBUG 1 |
122 | /* In progress... */ | 125 | /* In progress... */ |
123 | #define ENABLE_HUSH_FUNCTIONS 0 | 126 | #define ENABLE_HUSH_FUNCTIONS 0 |
@@ -524,6 +527,9 @@ struct globals { | |||
524 | char **traps; /* char *traps[NSIG] */ | 527 | char **traps; /* char *traps[NSIG] */ |
525 | sigset_t blocked_set; | 528 | sigset_t blocked_set; |
526 | sigset_t inherited_set; | 529 | sigset_t inherited_set; |
530 | #if HUSH_DEBUG | ||
531 | unsigned long memleak_value; | ||
532 | #endif | ||
527 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; | 533 | char user_input_buf[ENABLE_FEATURE_EDITING ? BUFSIZ : 2]; |
528 | #if ENABLE_FEATURE_SH_STANDALONE | 534 | #if ENABLE_FEATURE_SH_STANDALONE |
529 | struct nofork_save_area nofork_save; | 535 | struct nofork_save_area nofork_save; |
@@ -555,14 +561,17 @@ static int builtin_jobs(char **argv); | |||
555 | #if ENABLE_HUSH_HELP | 561 | #if ENABLE_HUSH_HELP |
556 | static int builtin_help(char **argv); | 562 | static int builtin_help(char **argv); |
557 | #endif | 563 | #endif |
564 | #if HUSH_DEBUG | ||
565 | static int builtin_memleak(char **argv); | ||
566 | #endif | ||
558 | static int builtin_pwd(char **argv); | 567 | static int builtin_pwd(char **argv); |
559 | static int builtin_read(char **argv); | 568 | static int builtin_read(char **argv); |
560 | static int builtin_test(char **argv); | ||
561 | static int builtin_trap(char **argv); | ||
562 | static int builtin_true(char **argv); | ||
563 | static int builtin_set(char **argv); | 569 | static int builtin_set(char **argv); |
564 | static int builtin_shift(char **argv); | 570 | static int builtin_shift(char **argv); |
565 | static int builtin_source(char **argv); | 571 | static int builtin_source(char **argv); |
572 | static int builtin_test(char **argv); | ||
573 | static int builtin_trap(char **argv); | ||
574 | static int builtin_true(char **argv); | ||
566 | static int builtin_umask(char **argv); | 575 | static int builtin_umask(char **argv); |
567 | static int builtin_unset(char **argv); | 576 | static int builtin_unset(char **argv); |
568 | static int builtin_wait(char **argv); | 577 | static int builtin_wait(char **argv); |
@@ -618,6 +627,9 @@ static const struct built_in_command bltins[] = { | |||
618 | #if ENABLE_HUSH_JOB | 627 | #if ENABLE_HUSH_JOB |
619 | BLTIN("jobs" , builtin_jobs , "List active jobs"), | 628 | BLTIN("jobs" , builtin_jobs , "List active jobs"), |
620 | #endif | 629 | #endif |
630 | #if HUSH_DEBUG | ||
631 | BLTIN("memleak" , builtin_memleak , "Debug tool"), | ||
632 | #endif | ||
621 | BLTIN("pwd" , builtin_pwd , "Print current directory"), | 633 | BLTIN("pwd" , builtin_pwd , "Print current directory"), |
622 | BLTIN("read" , builtin_read , "Input environment variable"), | 634 | BLTIN("read" , builtin_read , "Input environment variable"), |
623 | // BLTIN("return" , builtin_return , "Return from a function"), | 635 | // BLTIN("return" , builtin_return , "Return from a function"), |
@@ -5962,6 +5974,36 @@ static int builtin_jobs(char **argv UNUSED_PARAM) | |||
5962 | } | 5974 | } |
5963 | #endif | 5975 | #endif |
5964 | 5976 | ||
5977 | #if HUSH_DEBUG | ||
5978 | static int builtin_memleak(char **argv UNUSED_PARAM) | ||
5979 | { | ||
5980 | void *p; | ||
5981 | unsigned long l; | ||
5982 | |||
5983 | /* Crude attempt to find where "free memory" starts, | ||
5984 | * sans fragmentation. */ | ||
5985 | p = malloc(240); | ||
5986 | l = (unsigned long)p; | ||
5987 | free(p); | ||
5988 | p = malloc(3400); | ||
5989 | if (l < (unsigned long)p) l = (unsigned long)p; | ||
5990 | free(p); | ||
5991 | |||
5992 | if (!G.memleak_value) | ||
5993 | G.memleak_value = l; | ||
5994 | |||
5995 | l -= G.memleak_value; | ||
5996 | if ((long)l < 0) | ||
5997 | l = 0; | ||
5998 | l /= 1024; | ||
5999 | if (l > 127) | ||
6000 | l = 127; | ||
6001 | |||
6002 | /* Exitcode is "how many kilobytes we leaked since 1st call" */ | ||
6003 | return l; | ||
6004 | } | ||
6005 | #endif | ||
6006 | |||
5965 | static int builtin_pwd(char **argv UNUSED_PARAM) | 6007 | static int builtin_pwd(char **argv UNUSED_PARAM) |
5966 | { | 6008 | { |
5967 | puts(set_cwd()); | 6009 | puts(set_cwd()); |