diff options
author | stefw <stefw@localhost> | 2009-04-02 03:30:11 +0000 |
---|---|---|
committer | stefw <stefw@localhost> | 2009-04-02 03:30:11 +0000 |
commit | 22630c40c2861a9fed3db07430ee7d7a56c06ab4 (patch) | |
tree | b63f7cd2d84d00d21336739b6a2a0d32387c7b5c | |
parent | af0a2846fa83c7b07b843aea12ec194c4eb30860 (diff) | |
download | gnome-keyring-22630c40c2861a9fed3db07430ee7d7a56c06ab4.tar.gz |
Put in a semi-random test excercising 1000 iterations of allocation,
reallocation, freeing.
Validates between each step.
svn path=/trunk/; revision=1709
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | egg/egg-secure-memory.c | 33 | ||||
-rw-r--r-- | egg/tests/unit-test-secmem.c | 69 |
3 files changed, 92 insertions, 15 deletions
@@ -10,6 +10,11 @@ * egg/egg-secure-memory.c: Fix problem where freed memory is not inserted into the unused ring properly. This caused enless loops. Should fix bug #575247 + + * egg/egg-secure-memory.c: + * egg/tests/unit-test-secmem.c: Put in a semi-random test + excercising 1000 iterations of allocation, reallocation, + freeing. Validates between each step. 2009-03-20 Stef Walter <stef@memberwebs.com> diff --git a/egg/egg-secure-memory.c b/egg/egg-secure-memory.c index ba0cf5ce..ddee8ffa 100644 --- a/egg/egg-secure-memory.c +++ b/egg/egg-secure-memory.c @@ -78,7 +78,7 @@ egg_memory_unlock (); static int lock_warning = 1; - +int egg_secure_warnings = 1; /* * We allocate all memory in units of sizeof(void*). This @@ -791,7 +791,7 @@ sec_acquire_pages (size_t *sz) #if defined(HAVE_MLOCK) pages = mmap (0, *sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if (pages == MAP_FAILED) { - if (lock_warning) + if (lock_warning && egg_secure_warnings) fprintf (stderr, "couldn't map %lu bytes of private memory: %s\n", (unsigned long)*sz, strerror (errno)); lock_warning = 0; @@ -799,7 +799,7 @@ sec_acquire_pages (size_t *sz) } if (mlock (pages, *sz) < 0) { - if (lock_warning && errno != EPERM) { + if (lock_warning && egg_secure_warnings && errno != EPERM) { fprintf (stderr, "couldn't lock %lu bytes of private memory: %s\n", (unsigned long)*sz, strerror (errno)); lock_warning = 0; @@ -814,7 +814,7 @@ sec_acquire_pages (size_t *sz) return pages; #else - if (lock_warning) + if (lock_warning && egg_secure_warnings) fprintf (stderr, "your system does not support private memory"); lock_warning = 0; return NULL; @@ -829,10 +829,10 @@ sec_release_pages (void *pages, size_t sz) ASSERT (sz % getpagesize () == 0); #if defined(HAVE_MLOCK) - if (munlock (pages, sz) < 0) + if (munlock (pages, sz) < 0 && egg_secure_warnings) fprintf (stderr, "couldn't unlock private memory: %s\n", strerror (errno)); - if (munmap (pages, sz) < 0) + if (munmap (pages, sz) < 0 && egg_secure_warnings) fprintf (stderr, "couldn't unmap private anonymous memory: %s\n", strerror (errno)); DEBUG_ALLOC ("gkr-secure-memory: freed block ", sz); @@ -949,8 +949,9 @@ egg_secure_alloc_full (size_t length, int flags) void *memory = NULL; if (length > 0xFFFFFFFF / 2) { - fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n", - (unsigned long)length); + if (egg_secure_warnings) + fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n", + (unsigned long)length); return NULL; } @@ -1007,9 +1008,9 @@ egg_secure_realloc_full (void *memory, size_t length, int flags) void *alloc = NULL; if (length > 0xFFFFFFFF / 2) { - fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n", - (unsigned long)length); - ASSERT (0 && "tried to allocate an insane amount of memory"); + if (egg_secure_warnings) + fprintf (stderr, "tried to allocate an insane amount of memory: %lu\n", + (unsigned long)length); return NULL; } @@ -1061,8 +1062,9 @@ egg_secure_realloc_full (void *memory, size_t length, int flags) */ return egg_memory_fallback (memory, length); } else { - fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n", - (unsigned long)memory); + if (egg_secure_warnings) + fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n", + (unsigned long)memory); ASSERT (0 && "memory does does not belong to gnome-keyring"); return NULL; } @@ -1122,8 +1124,9 @@ egg_secure_free_full (void *memory, int flags) if ((flags & GKR_SECURE_USE_FALLBACK)) { egg_memory_fallback (memory, 0); } else { - fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n", - (unsigned long)memory); + if (egg_secure_warnings) + fprintf (stderr, "memory does not belong to gnome-keyring: 0x%08lx\n", + (unsigned long)memory); ASSERT (0 && "memory does does not belong to gnome-keyring"); } } diff --git a/egg/tests/unit-test-secmem.c b/egg/tests/unit-test-secmem.c index bf7ebc5b..33d6b700 100644 --- a/egg/tests/unit-test-secmem.c +++ b/egg/tests/unit-test-secmem.c @@ -29,6 +29,9 @@ #include "egg/egg-secure-memory.h" +/* Declared in egg-secure-memory.c */ +extern int egg_secure_warnings; + /* * Each test looks like (on one line): * void unit_test_xxxxx (CuTest* cu) @@ -135,3 +138,69 @@ DEFINE_TEST(secmem_realloc) g_assert (p == NULL); } +DEFINE_TEST(secmem_multialloc) +{ + GPtrArray *memory; + gpointer data; + gsize size; + int i, action, index; + + /* A predetermined seed to get a predetermined pattern */ + g_random_set_seed (15); + memory = g_ptr_array_new (); + + /* Don't print "can't allocate" warnings */ + egg_secure_warnings = 0; + + for (i = 0; TRUE; ++i) { + + /* Determine what we want to do */ + if (memory->len > 0) { + if (i > 1000) /* Once we've done 1000 alocations start freeing */ + action = 2; + else + action = g_random_int_range (0, 3); + } else { + action = 0; /* No allocations, so allocate */ + } + + switch (action) { + case 0: /* Allocate some memory */ + size = g_random_int_range (1, 16384); + data = egg_secure_alloc (size); + g_assert (data); + memset (data, 0xCAFEBABE, size); + g_ptr_array_add (memory, data); + break; + case 1: /* Reallocate some memory */ + index = g_random_int_range (0, memory->len); + data = g_ptr_array_index (memory, index); + g_assert (data); + size = g_random_int_range (1, 16384); + data = egg_secure_realloc (data, size); + g_assert (data); + memset (data, 0xCAFEBABE, size); + g_ptr_array_index (memory, index) = data; + break; + case 2: /* Free some memory */ + index = g_random_int_range (0, memory->len); + data = g_ptr_array_index (memory, index); + g_assert (data); + egg_secure_free (data); + g_ptr_array_remove_index_fast (memory, index); + break; + default: + g_assert_not_reached (); + } + + egg_secure_validate (); + + if (i > 1000 && !memory->len) + break; + } + + g_assert (memory->len == 0); + g_ptr_array_free (memory, TRUE); + + egg_secure_warnings = 1; +} |