-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
390195f
commit 212a563
Showing
12 changed files
with
360 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: [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(" "); | ||
} | ||
|
@@ -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: [email protected]"); | ||
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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.