From 212a563d90396d083b59e3169e021c300b921fa9 Mon Sep 17 00:00:00 2001 From: Alper Akcan Date: Thu, 28 Mar 2013 20:11:04 +0200 Subject: [PATCH] memory corruption tests --- src/hmemory.c | 26 ++++++++++--------- test/README | 64 +++++++++++++++++++++++++++++++++++++++++++++++ test/fail-20.c | 23 +++++++++++++++++ test/fail-21.c | 32 ++++++++++++++++++++++++ test/fail-40.c | 28 +++++++++++++++++++++ test/fail-41.c | 28 +++++++++++++++++++++ test/fail-42.c | 28 +++++++++++++++++++++ test/success-20.c | 27 ++++++++++++++++++++ test/success-21.c | 32 ++++++++++++++++++++++++ test/success-40.c | 28 +++++++++++++++++++++ test/success-41.c | 28 +++++++++++++++++++++ test/success-42.c | 28 +++++++++++++++++++++ 12 files changed, 360 insertions(+), 12 deletions(-) create mode 100644 test/README create mode 100644 test/fail-20.c create mode 100644 test/fail-21.c create mode 100644 test/fail-40.c create mode 100644 test/fail-41.c create mode 100644 test/fail-42.c create mode 100644 test/success-20.c create mode 100644 test/success-21.c create mode 100644 test/success-40.c create mode 100644 test/success-41.c create mode 100644 test/success-42.c diff --git a/src/hmemory.c b/src/hmemory.c index 2ccc551..cbeee36 100644 --- a/src/hmemory.c +++ b/src/hmemory.c @@ -534,7 +534,7 @@ static int debug_memory_check (void *address, const char *command, const char *f goto found_m; } hdebug_lock(); - hinfof("%s with invalid memory (%p)", command, address); + hinfof("%s with invalid address (%p)", command, address); hinfof(" at: %s (%s:%d)", func, file, line); debug_dump_callstack(" "); hinfof(" "); @@ -543,20 +543,20 @@ static int debug_memory_check (void *address, const char *command, const char *f hinfof(" inform author"); hinfof(" at: alper.akcan@gmail.com"); hdebug_unlock(); - hassert((m != NULL) && "invalid memory"); + hassert((m != NULL) && "invalid address"); hmemory_unlock(); return -1; found_m: hdebug_lock(); rcu = memcmp(m->address, &hmemory_signature, sizeof(hmemory_signature)); if (rcu != 0) { - hinfof("%s with corrupted memory (%p), underflow", command, address); + hinfof("%s with corrupted address (%p), underflow", command, address); hinfof(" at: %s (%s:%d)", func, file, line); debug_dump_callstack(" "); } rco = memcmp(m->address + m->size - sizeof(hmemory_signature), &hmemory_signature, sizeof(hmemory_signature)); if (rco != 0) { - hinfof("%s with corrupted memory (%p), overflow", command, address); + hinfof("%s with corrupted address (%p), overflow", command, address); hinfof(" at: %s (%s:%d)", func, file, line); debug_dump_callstack(" "); } @@ -578,7 +578,7 @@ static int debug_memory_del (void *address, const char *command, const char *fun goto found_m; } hdebug_lock(); - hinfof("%s with invalid memory (%p)", command, address); + hinfof("%s with invalid address (%p)", command, address); hinfof(" at: %s (%s:%d)", func, file, line); debug_dump_callstack(" "); hinfof(" "); @@ -587,7 +587,7 @@ static int debug_memory_del (void *address, const char *command, const char *fun hinfof(" inform author"); hinfof(" at: alper.akcan@gmail.com"); hdebug_unlock(); - hassert((m != NULL) && "invalid memory"); + hassert((m != NULL) && "invalid address"); hmemory_unlock(); return -1; found_m: @@ -609,18 +609,20 @@ static void __attribute__ ((destructor)) hmemory_fini (void) struct hmemory_memory *nm; hmemory_lock(); hdebug_lock(); - hinfof("memory trace:") - hinfof(" current: %llu bytes (%.02f mb)", memory_current, ((double) memory_current) / (1024.00 * 1024.00)); - hinfof(" peak : %llu bytes (%.02f mb)", memory_peak, ((double) memory_peak) / (1024.00 * 1024.00)); - hinfof(" total : %llu bytes (%.02f mb)", memory_total, ((double) memory_total) / (1024.00 * 1024.00)); + hinfof("memory information:") + hinfof(" current: %llu bytes (%.02f mb)", memory_current, ((double) memory_current) / (1024.00 * 1024.00)); + hinfof(" peak : %llu bytes (%.02f mb)", memory_peak, ((double) memory_peak) / (1024.00 * 1024.00)); + hinfof(" total : %llu bytes (%.02f mb)", memory_total, ((double) memory_total) / (1024.00 * 1024.00)); + hinfof(" leaks : %d items", HASH_COUNT(debug_memory)); if (HASH_COUNT(debug_memory) > 0) { + hinfof(" memory leaks:"); HASH_ITER(hh, debug_memory, m, nm) { - hinfof(" - %zd bytes at: %s (%s:%u)", m->size, m->func, m->file, m->line); + hinfof(" - %zd bytes at: %s (%s:%u)", m->size, m->func, m->file, m->line); HASH_DEL(debug_memory, m); free(m->address); free(m); } - hassert(0 && "memory leaks found"); + hassert(0 && "memory leak"); } hdebug_unlock(); hmemory_unlock(); diff --git a/test/README b/test/README new file mode 100644 index 0000000..330eee3 --- /dev/null +++ b/test/README @@ -0,0 +1,64 @@ + +unit tests behaviours and expected results +========================================== + + 00-19: leak tests + 20-39: invalid address + 40-59: memory corruption + + success fail + ------------------------------------ ------------------------------------ + +00 malloc malloc + free exit + exit ** memory leak ** + +01 calloc calloc + free exit + exit ** memory leak ** + +02 realloc: null realloc: null + free exit + exit ** memory leak ** + +03 realloc: null realloc: null + realloc: ret realloc: ret + free: ret exit + exit ** memory leak ** + +04 strdup strdup + free exit + exit ** memory leak ** + +05 strndup strndup + free exit + exit ** memory leak ** + +06 asprintf asprintf + free exit + exit ** memory leak ** + +20 malloc free + free ** invalid address ** + exit + +21 realloc: null realloc: null + realloc: ret realloc: ret + 10 + free: ret ** invalid address ** + exit + +40 rc = malloc: 1024 rc = malloc: 1024 + memset: rc, 0, 1024 memset: rc, 0, 1025 + free: rc free: rc + exit ** memory corruption ** + +41 rc = malloc: 1024 rc = malloc: 1024 + memset: rc, 0, 1024 memset: rc - 10, 0, 1024 + free: rc free: rc + exit ** memory corruption ** + +42 rc = malloc: 1024 rc = malloc: 1024 + memset: rc, 0, 1024 memset: rc - 10, 0, 1044 + free: rc free: rc + exit ** memory corruption ** + \ No newline at end of file diff --git a/test/fail-20.c b/test/fail-20.c new file mode 100644 index 0000000..8253e54 --- /dev/null +++ b/test/fail-20.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = argv[0]; + free(rc); + return 0; +} diff --git a/test/fail-21.c b/test/fail-21.c new file mode 100644 index 0000000..045067c --- /dev/null +++ b/test/fail-21.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = realloc(NULL, 1024); + if (rc == NULL) { + fprintf(stderr, "realloc failed\n"); + exit(-1); + } + rc = realloc(rc + 10, 2048); + if (rc == NULL) { + fprintf(stderr, "realloc failed\n"); + exit(-1); + } + free(rc); + return 0; +} diff --git a/test/fail-40.c b/test/fail-40.c new file mode 100644 index 0000000..6b992de --- /dev/null +++ b/test/fail-40.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = malloc(1024); + if (rc == NULL) { + fprintf(stderr, "malloc failed\n"); + exit(-1); + } + memset(rc, 0, 1025); + free(rc); + return 0; +} diff --git a/test/fail-41.c b/test/fail-41.c new file mode 100644 index 0000000..6d1d2df --- /dev/null +++ b/test/fail-41.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = malloc(1024); + if (rc == NULL) { + fprintf(stderr, "malloc failed\n"); + exit(-1); + } + memset(rc - 10, 0, 1024); + free(rc); + return 0; +} diff --git a/test/fail-42.c b/test/fail-42.c new file mode 100644 index 0000000..e93f5c3 --- /dev/null +++ b/test/fail-42.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = malloc(1024); + if (rc == NULL) { + fprintf(stderr, "malloc failed\n"); + exit(-1); + } + memset(rc - 10, 0, 1044); + free(rc); + return 0; +} diff --git a/test/success-20.c b/test/success-20.c new file mode 100644 index 0000000..c91cfb2 --- /dev/null +++ b/test/success-20.c @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = malloc(1024); + if (rc == NULL) { + fprintf(stderr, "malloc failed\n"); + exit(-1); + } + free(rc); + return 0; +} diff --git a/test/success-21.c b/test/success-21.c new file mode 100644 index 0000000..a382bf1 --- /dev/null +++ b/test/success-21.c @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = realloc(NULL, 1024); + if (rc == NULL) { + fprintf(stderr, "realloc failed\n"); + exit(-1); + } + rc = realloc(rc, 2048); + if (rc == NULL) { + fprintf(stderr, "realloc failed\n"); + exit(-1); + } + free(rc); + return 0; +} diff --git a/test/success-40.c b/test/success-40.c new file mode 100644 index 0000000..09d38b6 --- /dev/null +++ b/test/success-40.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = malloc(1024); + if (rc == NULL) { + fprintf(stderr, "malloc failed\n"); + exit(-1); + } + memset(rc, 0, 1024); + free(rc); + return 0; +} diff --git a/test/success-41.c b/test/success-41.c new file mode 100644 index 0000000..09d38b6 --- /dev/null +++ b/test/success-41.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = malloc(1024); + if (rc == NULL) { + fprintf(stderr, "malloc failed\n"); + exit(-1); + } + memset(rc, 0, 1024); + free(rc); + return 0; +} diff --git a/test/success-42.c b/test/success-42.c new file mode 100644 index 0000000..09d38b6 --- /dev/null +++ b/test/success-42.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2008-2013 Alper Akcan + * + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://www.wtfpl.net/ for more details. + */ + +#include +#include +#include + +int main (int argc, char *argv[]) +{ + void *rc; + (void) argc; + (void) argv; + rc = malloc(1024); + if (rc == NULL) { + fprintf(stderr, "malloc failed\n"); + exit(-1); + } + memset(rc, 0, 1024); + free(rc); + return 0; +}