[PATCH wayland] test: remove memory leak checks

Kristian Høgsberg krh at bitplanet.net
Thu Aug 16 12:46:39 PDT 2012


On Wed, Aug 8, 2012 at 4:22 PM, U. Artie Eoff <ullysses.a.eoff at intel.com> wrote:
> From: "U. Artie Eoff" <ullysses.a.eoff at intel.com>
>
> Wrapping dynamic memory functions (malloc, realloc, free, calloc)
> can be problematic, for instance, when instrumenting code with
> coverage hooks.
> In general, the community consensus is to avoid writing your own
> memory leak checking and to use an existing tool such as valgrind
> to do this job for you.  The same could probably be said for
> fd leak checks, but will leave that for a separate exercise.

I'm still not sure I like just removing the leak check.  These checks
are fast and always available, while running under valgrind requires
valgrind and is slow.  I like Robert Bradfords patch that lets you
disable them with an environment variable, and we could make that work
by setting a flag if the leak checker is enabled.  Then the self-tests
that are supposed to fail when using a leak check can just do

  assert(leak_check_enabled);

Kristian


> Signed-off-by: U. Artie Eoff <ullysses.a.eoff at intel.com>
> ---
>  tests/sanity-test.c | 22 ----------------------
>  tests/test-runner.c | 51 ---------------------------------------------------
>  2 files changed, 73 deletions(-)
>
> diff --git a/tests/sanity-test.c b/tests/sanity-test.c
> index 67ca663..03044af 100644
> --- a/tests/sanity-test.c
> +++ b/tests/sanity-test.c
> @@ -64,28 +64,6 @@ FAIL_TEST(sanity_assert)
>         assert(0);
>  }
>
> -FAIL_TEST(sanity_malloc_direct)
> -{
> -       void *p;
> -
> -       p = malloc(10); /* memory leak */
> -       assert(p);      /* assert that we got memory, also prevents
> -                        * the malloc from getting optimized away. */
> -       free(NULL);     /* NULL must not be counted */
> -}
> -
> -FAIL_TEST(sanity_malloc_indirect)
> -{
> -       struct wl_array array;
> -
> -       wl_array_init(&array);
> -
> -       /* call into library that calls malloc */
> -       wl_array_add(&array, 14);
> -
> -       /* not freeing array, must leak */
> -}
> -
>  FAIL_TEST(sanity_fd_leak)
>  {
>         int fd[2];
> diff --git a/tests/test-runner.c b/tests/test-runner.c
> index 63ce384..354091d 100644
> --- a/tests/test-runner.c
> +++ b/tests/test-runner.c
> @@ -20,8 +20,6 @@
>   * OF THIS SOFTWARE.
>   */
>
> -#define _GNU_SOURCE
> -
>  #include <unistd.h>
>  #include <stdio.h>
>  #include <stdlib.h>
> @@ -29,52 +27,11 @@
>  #include <sys/wait.h>
>  #include <string.h>
>  #include <assert.h>
> -#include <dlfcn.h>
>  #include <errno.h>
>  #include "test-runner.h"
>
> -static int num_alloc;
> -static void* (*sys_malloc)(size_t);
> -static void (*sys_free)(void*);
> -static void* (*sys_realloc)(void*, size_t);
> -static void* (*sys_calloc)(size_t, size_t);
> -
>  extern const struct test __start_test_section, __stop_test_section;
>
> -__attribute__ ((visibility("default"))) void *
> -malloc(size_t size)
> -{
> -       num_alloc++;
> -       return sys_malloc(size);
> -}
> -
> -__attribute__ ((visibility("default"))) void
> -free(void* mem)
> -{
> -       if (mem != NULL)
> -               num_alloc--;
> -       sys_free(mem);
> -}
> -
> -__attribute__ ((visibility("default"))) void *
> -realloc(void* mem, size_t size)
> -{
> -       if (mem == NULL)
> -               num_alloc++;
> -       return sys_realloc(mem, size);
> -}
> -
> -__attribute__ ((visibility("default"))) void *
> -calloc(size_t nmemb, size_t size)
> -{
> -       if (sys_calloc == NULL)
> -               return NULL;
> -
> -       num_alloc++;
> -
> -       return sys_calloc(nmemb, size);
> -}
> -
>  static const struct test *
>  find_test(const char *name)
>  {
> @@ -90,12 +47,10 @@ find_test(const char *name)
>  static void
>  run_test(const struct test *t)
>  {
> -       int cur_alloc = num_alloc;
>         int cur_fds;
>
>         cur_fds = count_open_fds();
>         t->run();
> -       assert(cur_alloc == num_alloc && "memory leak detected in test.");
>         assert(cur_fds == count_open_fds() && "fd leak detected");
>         exit(EXIT_SUCCESS);
>  }
> @@ -107,12 +62,6 @@ int main(int argc, char *argv[])
>         int total, pass;
>         siginfo_t info;
>
> -       /* Load system malloc, free, and realloc */
> -       sys_calloc = dlsym(RTLD_NEXT, "calloc");
> -       sys_realloc = dlsym(RTLD_NEXT, "realloc");
> -       sys_malloc = dlsym(RTLD_NEXT, "malloc");
> -       sys_free = dlsym(RTLD_NEXT, "free");
> -
>         if (argc == 2) {
>                 t = find_test(argv[1]);
>                 if (t == NULL) {
> --
> 1.7.11.2
>
> _______________________________________________
> wayland-devel mailing list
> wayland-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/wayland-devel


More information about the wayland-devel mailing list