[glib/ebassi/aligned-alloc: 2/3] Add tests for g_aligned_alloc()




commit b37c99c44e44bea670663fcabb49c50b3e014259
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Fri Jan 7 17:24:03 2022 +0000

    Add tests for g_aligned_alloc()
    
    We want to test the API contract, by checking the return value for
    zero-sized allocations, invalid alignments, and overflows.

 glib/tests/mem-overflow.c |  21 +++++++++-
 glib/tests/utils.c        | 100 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+), 2 deletions(-)
---
diff --git a/glib/tests/mem-overflow.c b/glib/tests/mem-overflow.c
index 1654ab7fc..fd926854f 100644
--- a/glib/tests/mem-overflow.c
+++ b/glib/tests/mem-overflow.c
@@ -34,13 +34,14 @@ static gsize a = G_MAXSIZE / 10 + 10;
 static gsize b = 10;
 typedef char X[10];
 
-#define MEM_OVERFLOW_TEST(name, code) \
+#define MEM_OVERFLOW_TEST(name, code) MEM_OVERFLOW_TEST_FULL(name, code, g_free)
+#define MEM_OVERFLOW_TEST_FULL(name, code, free_func) \
 static void                           \
 mem_overflow_ ## name (void)          \
 {                                     \
   gpointer p;                         \
   code;                               \
-  g_free (p);                         \
+  free_func (p);                      \
   exit (0);                           \
 }
 
@@ -68,6 +69,12 @@ MEM_OVERFLOW_TEST (new0_b, p = g_new0 (X, b))
 MEM_OVERFLOW_TEST (renew_a, p = g_malloc (1); p = g_renew (X, p, a))
 MEM_OVERFLOW_TEST (renew_b, p = g_malloc (1); p = g_renew (X, p, b))
 
+MEM_OVERFLOW_TEST_FULL (aligned_alloc_a, p = g_aligned_alloc (sizeof(X), a, 16), g_aligned_free)
+MEM_OVERFLOW_TEST_FULL (aligned_alloc_b, p = g_aligned_alloc (sizeof(X), b, 16), g_aligned_free)
+
+MEM_OVERFLOW_TEST_FULL (aligned_alloc0_a, p = g_aligned_alloc0 (sizeof(X), a, 16), g_aligned_free)
+MEM_OVERFLOW_TEST_FULL (aligned_alloc0_b, p = g_aligned_alloc0 (sizeof(X), b, 16), g_aligned_free)
+
 static void
 mem_overflow_malloc_0 (void)
 {
@@ -171,6 +178,12 @@ mem_overflow (void)
 
   CHECK_SUBPROCESS_PASS (malloc_0);
   CHECK_SUBPROCESS_PASS (realloc_0);
+
+  CHECK_SUBPROCESS_FAIL (aligned_alloc_a);
+  CHECK_SUBPROCESS_PASS (aligned_alloc_b);
+
+  CHECK_SUBPROCESS_FAIL (aligned_alloc0_a);
+  CHECK_SUBPROCESS_PASS (aligned_alloc0_b);
 }
 
 #ifdef __GNUC__
@@ -231,6 +244,10 @@ main (int   argc,
   g_test_add_func ("/mem/overflow/subprocess/renew_b", mem_overflow_renew_b);
   g_test_add_func ("/mem/overflow/subprocess/malloc_0", mem_overflow_malloc_0);
   g_test_add_func ("/mem/overflow/subprocess/realloc_0", mem_overflow_realloc_0);
+  g_test_add_func ("/mem/overflow/subprocess/aligned_alloc_a", mem_overflow_aligned_alloc_a);
+  g_test_add_func ("/mem/overflow/subprocess/aligned_alloc_b", mem_overflow_aligned_alloc_b);
+  g_test_add_func ("/mem/overflow/subprocess/aligned_alloc0_a", mem_overflow_aligned_alloc0_a);
+  g_test_add_func ("/mem/overflow/subprocess/aligned_alloc0_b", mem_overflow_aligned_alloc0_b);
 
 #ifdef __GNUC__
   g_test_add_func ("/mem/empty-alloc", empty_alloc);
diff --git a/glib/tests/utils.c b/glib/tests/utils.c
index 643978149..9dbdd2c8c 100644
--- a/glib/tests/utils.c
+++ b/glib/tests/utils.c
@@ -915,6 +915,100 @@ test_misc_mem (void)
   g_assert (a == NULL);
 }
 
+static void
+aligned_alloc_nz (void)
+{
+  gpointer a;
+
+  /* Test an alignment that’s zero */
+  a = g_aligned_alloc (16, sizeof(char), 0);
+  g_assert_null (a);
+  exit (0);
+}
+
+static void
+aligned_alloc_npot (void)
+{
+  gpointer a;
+
+  /* Test an alignment that’s not a power of two */
+  a = g_aligned_alloc (16, sizeof(char), 15);
+  g_assert_null (a);
+  exit (0);
+}
+
+static void
+aligned_alloc_nmov (void)
+{
+  gpointer a;
+
+  /* Test an alignment that’s not a multiple of sizeof(void*) */
+  a = g_aligned_alloc (16, sizeof(char), 4);
+  g_assert_null (a);
+  exit (0);
+}
+
+static void
+test_aligned_mem (void)
+{
+  gpointer a;
+
+  g_test_summary ("Aligned memory allocator");
+
+  a = g_aligned_alloc (0, sizeof(int), 8);
+  g_assert_null (a);
+
+  a = g_aligned_alloc0 (0, sizeof(int), 8);
+  g_assert_null (a);
+
+  a = g_aligned_alloc (16, 0, 8);
+  g_assert_null (a);
+
+#define CHECK_SUBPROCESS_FAIL(name,msg) do { \
+        { \
+          g_test_message (msg); \
+          g_test_trap_subprocess ("/utils/aligned-mem/subprocess/" #name, 0, 0); \
+          g_test_trap_assert_failed (); \
+        } \
+    } while (0)
+
+  CHECK_SUBPROCESS_FAIL (aligned_alloc_nz, "Alignment must not be zero");
+  CHECK_SUBPROCESS_FAIL (aligned_alloc_npot, "Alignment must be a power of two");
+  CHECK_SUBPROCESS_FAIL (aligned_alloc_nmov, "Alignment must be a multiple of sizeof(void*)");
+}
+
+static void
+test_aligned_mem_alignment (void)
+{
+  gchar *p;
+
+  g_test_summary ("Check that g_aligned_alloc() returns a correctly aligned pointer");
+
+  p = g_aligned_alloc (5, sizeof (*p), 256);
+  g_assert_nonnull (p);
+  g_assert_cmpuint (((guintptr) p) % 256, ==, 0);
+
+  g_aligned_free (p);
+}
+
+static void
+test_aligned_mem_zeroed (void)
+{
+  gsize n_blocks = 10;
+  guint *p;
+  gsize i;
+
+  g_test_summary ("Check that g_aligned_alloc0() zeroes out its allocation");
+
+  p = g_aligned_alloc0 (n_blocks, sizeof (*p), 16);
+  g_assert_nonnull (p);
+
+  for (i = 0; i < n_blocks; i++)
+    g_assert_cmpuint (p[i], ==, 0);
+
+  g_aligned_free (p);
+}
+
 static void
 test_nullify (void)
 {
@@ -1084,6 +1178,12 @@ main (int   argc,
   g_test_add_func ("/utils/take-pointer", test_take_pointer);
   g_test_add_func ("/utils/clear-source", test_clear_source);
   g_test_add_func ("/utils/misc-mem", test_misc_mem);
+  g_test_add_func ("/utils/aligned-mem", test_aligned_mem);
+  g_test_add_func ("/utils/aligned-mem/subprocess/aligned_alloc_nz", aligned_alloc_nz);
+  g_test_add_func ("/utils/aligned-mem/subprocess/aligned_alloc_npot", aligned_alloc_npot);
+  g_test_add_func ("/utils/aligned-mem/subprocess/aligned_alloc_nmov", aligned_alloc_nmov);
+  g_test_add_func ("/utils/aligned-mem/alignment", test_aligned_mem_alignment);
+  g_test_add_func ("/utils/aligned-mem/zeroed", test_aligned_mem_zeroed);
   g_test_add_func ("/utils/nullify", test_nullify);
   g_test_add_func ("/utils/atexit", test_atexit);
   g_test_add_func ("/utils/check-setuid", test_check_setuid);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]