Skip to content

Commit

Permalink
corruption check interval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alperakcan committed Mar 28, 2013
1 parent 212a563 commit bb50f59
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 3 deletions.
57 changes: 54 additions & 3 deletions src/hmemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,15 +520,14 @@ static int debug_memory_add (const char *name, void *address, size_t size, const
return 0;
}

static int debug_memory_check (void *address, const char *command, const char *func, const char *file, const int line)
static int debug_memory_check_actual (void *address, const char *command, const char *func, const char *file, const int line)
{
int rcu;
int rco;
struct hmemory_memory *m;
if (address == NULL) {
return 0;
}
hmemory_lock();
HASH_FIND_PTR(debug_memory, &address, m);
if (m != NULL) {
goto found_m;
Expand All @@ -544,7 +543,6 @@ static int debug_memory_check (void *address, const char *command, const char *f
hinfof(" at: [email protected]");
hdebug_unlock();
hassert((m != NULL) && "invalid address");
hmemory_unlock();
return -1;
found_m:
hdebug_lock();
Expand All @@ -562,6 +560,13 @@ static int debug_memory_check (void *address, const char *command, const char *f
}
hdebug_unlock();
hassert(((rcu == 0) && (rco == 0)) && "memory corruption");
return 0;
}

static int debug_memory_check (void *address, const char *command, const char *func, const char *file, const int line)
{
hmemory_lock();
debug_memory_check_actual(address, command, func, file, line);
hmemory_unlock();
return 0;
}
Expand Down Expand Up @@ -599,15 +604,61 @@ static int debug_memory_del (void *address, const char *command, const char *fun
return 0;
}

static int hmemory_worker_started = 0;
static int hmemory_worker_running = 0;
static pthread_t hmemory_thread;

static void * hmemory_worker (void *arg)
{
unsigned int v;
struct hmemory_memory *m;
struct hmemory_memory *nm;
(void) arg;
while (1) {
v = hmemory_getenv_int(HMEMORY_CORRUPTION_CHECK_INTERVAL_NAME);
if (v == (unsigned int) -1) {
v = HMEMORY_CORRUPTION_CHECK_INTERVAL;
}
usleep(v * 1000);
hmemory_lock();
if (hmemory_worker_running == 0) {
hmemory_unlock();
break;
}
HASH_ITER(hh, debug_memory, m, nm) {
debug_memory_check_actual(m->address, "worker check", __FUNCTION__, __FILE__, __LINE__);
}
hmemory_unlock();
}
return NULL;
}

static void __attribute__ ((constructor)) hmemory_init (void)
{
int rc;
hmemory_lock();
hmemory_worker_started = 1;
hmemory_worker_running = 1;
rc = pthread_create(&hmemory_thread, NULL, hmemory_worker, NULL);
if (rc != 0) {
herrorf("failed to create worker");
hmemory_worker_started = 0;
hmemory_worker_running = 0;
}
hmemory_unlock();
}

static void __attribute__ ((destructor)) hmemory_fini (void)
{
struct hmemory_memory *m;
struct hmemory_memory *nm;
hmemory_lock();
if (hmemory_worker_running == 1) {
hmemory_worker_running = 0;
}
hmemory_unlock();
pthread_join(hmemory_thread, NULL);
hmemory_lock();
hdebug_lock();
hinfof("memory information:")
hinfof(" current: %llu bytes (%.02f mb)", memory_current, ((double) memory_current) / (1024.00 * 1024.00));
Expand Down
5 changes: 5 additions & 0 deletions src/hmemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
#endif
#define HMEMORY_ASSERT_ON_ERROR_NAME "hmemory_assert_on_error"

#if !defined(HMEMORY_CORRUPTION_CHECK_INTERVAL)
#define HMEMORY_CORRUPTION_CHECK_INTERVAL 500
#endif
#define HMEMORY_CORRUPTION_CHECK_INTERVAL_NAME "hmemory_corruption_check_interval"

#if defined(HMEMORY_DEBUG) && (HMEMORY_DEBUG == 1)

#if !defined(HMEMORY_INTERNAL) || (HMEMORY_INTERNAL == 0)
Expand Down
3 changes: 3 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ define test-debug-defaults
$1_includes-y = \
../src

$1_ldflags-y += \
-lpthread

$1_ldflags-${HMEMORY_ENABLE_CALLSTACK} += \
-rdynamic \
-ldl \
Expand Down
18 changes: 18 additions & 0 deletions test/README
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,22 @@ unit tests behaviours and expected results
memset: rc, 0, 1024 memset: rc - 10, 0, 1044
free: rc free: rc
exit ** memory corruption **

43 rc = malloc: 1024 rc = malloc: 1024
memset: rc, 0, 1024 memset: rc, 0, 1025
sleep: 3 sleep: 3
free: rc ** memory corruption **
exit

44 rc = malloc: 1024 rc = malloc: 1024
memset: rc, 0, 1024 memset: rc - 10, 0, 1024
sleep: 3 sleep: 3
free: rc ** memory corruption **
exit

45 rc = malloc: 1024 rc = malloc: 1024
memset: rc, 0, 1024 memset: rc - 10, 0, 1044
sleep: 3 sleep: 3
free: rc ** memory corruption **
exit

30 changes: 30 additions & 0 deletions test/fail-43.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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>
#include <unistd.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);
sleep(1);
free(rc);
return 0;
}
30 changes: 30 additions & 0 deletions test/fail-44.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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>
#include <unistd.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);
sleep(1);
free(rc);
return 0;
}
30 changes: 30 additions & 0 deletions test/fail-45.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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>
#include <unistd.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);
sleep(1);
free(rc);
return 0;
}
30 changes: 30 additions & 0 deletions test/success-43.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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>
#include <unistd.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);
sleep(1);
free(rc);
return 0;
}
30 changes: 30 additions & 0 deletions test/success-44.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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>
#include <unistd.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);
sleep(1);
free(rc);
return 0;
}
30 changes: 30 additions & 0 deletions test/success-45.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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>
#include <unistd.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);
sleep(1);
free(rc);
return 0;
}

0 comments on commit bb50f59

Please sign in to comment.