[glib/2753-vasprintf-loop] glib/tests: Add test to check that we abort on low-memory




commit 51df0b7412cf7acfa23945a8d41d0063c1ff3885
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Thu Oct 20 15:05:55 2022 +0200

    glib/tests: Add test to check that we abort on low-memory
    
    But we should not crash

 glib/tests/meson.build           |  10 ++++
 glib/tests/messages-low-memory.c | 116 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+)
---
diff --git a/glib/tests/meson.build b/glib/tests/meson.build
index e8f637445b..764b995ac7 100644
--- a/glib/tests/meson.build
+++ b/glib/tests/meson.build
@@ -155,6 +155,15 @@ glib_tests = {
   },
 }
 
+if have_dlopen_dlsym
+  glib_tests += {
+    'messages-low-memory' : {
+      'dependencies' : libdl_dep,
+      'override_options' : ['b_asneeded=false'],
+    },
+  }
+endif
+
 if have_cxx
   glib_tests += {
     'cxx' : {
@@ -290,6 +299,7 @@ foreach test_name, extra_args : glib_tests
     c_args : test_cargs + extra_args.get('c_args', []),
     cpp_args : test_cpp_args + extra_args.get('cpp_args', []),
     link_args : extra_args.get('link_args', []),
+    override_options : extra_args.get('override_options', []),
     dependencies : test_deps + extra_args.get('dependencies', []),
     install_dir: installed_tests_execdir,
     install_tag: 'tests',
diff --git a/glib/tests/messages-low-memory.c b/glib/tests/messages-low-memory.c
new file mode 100644
index 0000000000..ac900c528a
--- /dev/null
+++ b/glib/tests/messages-low-memory.c
@@ -0,0 +1,116 @@
+/* Unit tests for gmessages on low-memory
+ *
+ * Copyright (C) 2022 Marco Trevisan
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Marco Trevisan <marco trevisan canonical com>
+ */
+
+#include "config.h"
+
+#include <dlfcn.h>
+#include <glib.h>
+#include <signal.h>
+
+static gboolean malloc_eom = FALSE;
+static int expected_signal = 0;
+
+#ifdef ENOMEM
+void *malloc(size_t size)
+{
+  static void *(*real_malloc)(size_t);
+  if (!real_malloc)
+      real_malloc = dlsym (RTLD_NEXT, "malloc");
+
+  if (malloc_eom) {
+    errno = ENOMEM;
+    return NULL;
+  }
+
+  return real_malloc (size);
+}
+#endif
+
+static gboolean
+check_can_run (void)
+{
+#ifndef ENOMEM
+  g_test_skip ("ENOMEM Not defined");
+  return FALSE;
+#endif
+  return TRUE;
+}
+
+static void
+test_malloc_failures (void)
+{
+  char *str;
+
+  if (!check_can_run ())
+    return;
+
+  errno = 0;
+  malloc_eom = TRUE;
+  g_assert_null (malloc (sizeof (char) * 10));
+  g_assert_cmpint (errno, ==, ENOMEM);
+
+  errno = 0;
+  malloc_eom = FALSE;
+  str = malloc (sizeof (char) * 10);
+  g_assert_nonnull (str);
+  g_assert_cmpint (errno, ==, 0);
+
+  g_free (str);
+}
+
+G_NORETURN static void
+signal_handler (int signo)
+{
+  malloc_eom = FALSE;
+  g_message ("Got signal %d", signo);
+  g_assert_cmpint (expected_signal, ==, signo);
+  exit (0);
+}
+
+static void
+test_messages_on_low_memory (void)
+{
+  if (!check_can_run ())
+    return;
+
+  signal (SIGABRT, &signal_handler);
+  signal (SIGSEGV, &signal_handler);
+
+  malloc_eom = TRUE;
+  expected_signal = SIGABRT;
+  g_message ("Memory is over, but we'll write anyways: %u", g_random_int ());
+
+  g_assert_not_reached ();
+}
+
+int
+main (int   argc,
+      char *argv[])
+{
+  g_setenv ("LC_ALL", "C", TRUE);
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/overrides/malloc-failures", test_malloc_failures);
+  g_test_add_func ("/messages/on-low-memory", test_messages_on_low_memory);
+
+  return g_test_run ();
+}


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