Skip to content

Commit

Permalink
memory corruption tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alperakcan committed Mar 28, 2013
1 parent 390195f commit 212a563
Show file tree
Hide file tree
Showing 12 changed files with 360 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/hmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(" ");
Expand All @@ -543,20 +543,20 @@ static int debug_memory_check (void *address, const char *command, const char *f
hinfof(" inform author");
hinfof(" at: [email protected]");
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(" ");
}
Expand All @@ -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(" ");
Expand All @@ -587,7 +587,7 @@ static int debug_memory_del (void *address, const char *command, const char *fun
hinfof(" inform author");
hinfof(" at: [email protected]");
hdebug_unlock();
hassert((m != NULL) && "invalid memory");
hassert((m != NULL) && "invalid address");
hmemory_unlock();
return -1;
found_m:
Expand All @@ -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();
Expand Down
64 changes: 64 additions & 0 deletions test/README
Original file line number Diff line number Diff line change
@@ -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 **

23 changes: 23 additions & 0 deletions test/fail-20.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2008-2013 Alper Akcan <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
void *rc;
(void) argc;
(void) argv;
rc = argv[0];
free(rc);
return 0;
}
32 changes: 32 additions & 0 deletions test/fail-21.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2008-2013 Alper Akcan <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
28 changes: 28 additions & 0 deletions test/fail-40.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2008-2013 Alper Akcan <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
28 changes: 28 additions & 0 deletions test/fail-41.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2008-2013 Alper Akcan <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
28 changes: 28 additions & 0 deletions test/fail-42.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2008-2013 Alper Akcan <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
27 changes: 27 additions & 0 deletions test/success-20.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2008-2013 Alper Akcan <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
32 changes: 32 additions & 0 deletions test/success-21.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2008-2013 Alper Akcan <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
28 changes: 28 additions & 0 deletions test/success-40.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2008-2013 Alper Akcan <[email protected]>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
Loading

0 comments on commit 212a563

Please sign in to comment.