]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Add a test to check that we can use the specified stack size
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 18 Jan 2023 14:52:57 +0000 (15:52 +0100)
committerOtto Moerbeek <otto.moerbeek@open-xchange.com>
Wed, 18 Jan 2023 14:55:05 +0000 (15:55 +0100)
Actual test of the guard pages can be done, but is hard to fit in the test framework.
To test manually decrease the headroom const to 0

pdns/recursordist/test-mtasker.cc

index fb678897be4113b0c5891d2bc150ab5433ba11b7..83d0930d8ea361c42fa279737fc82e5b6961c034 100644 (file)
@@ -6,6 +6,7 @@
 #endif
 #include <boost/test/unit_test.hpp>
 #include "mtasker.hh"
+#include <fcntl.h>
 
 BOOST_AUTO_TEST_SUITE(mtasker_cc)
 
@@ -40,6 +41,43 @@ BOOST_AUTO_TEST_CASE(test_Simple)
   BOOST_CHECK_EQUAL(g_result, o);
 }
 
+static const size_t stackSize = 8 * 1024;
+static const size_t headroom = 256; // Decrease to hit stackoverflow
+
+static void doAlmostStackoverflow(void* arg)
+{
+  auto* mt = reinterpret_cast<MTasker<>*>(arg);
+  int localvar[stackSize / sizeof(int) - headroom]; // expermimentally derived headroom
+  localvar[0] = 0;
+  localvar[sizeof(localvar) / sizeof(localvar[0]) - 1] = 12;
+  if (mt->waitEvent(localvar[sizeof(localvar) / sizeof(localvar[0]) - 1], &localvar[0]) == 1) {
+    g_result = localvar[0];
+  }
+}
+
+BOOST_AUTO_TEST_CASE(test_AlmostStackOverflow)
+{
+  MTasker<> mt(stackSize);
+  mt.makeThread(doAlmostStackoverflow, &mt);
+  struct timeval now;
+  gettimeofday(&now, 0);
+  bool first = true;
+  int o = 25;
+  for (;;) {
+    while (mt.schedule(&now)) {
+      ;
+    }
+    if (first) {
+      mt.sendEvent(12, &o);
+      first = false;
+    }
+    if (mt.noProcesses()) {
+      break;
+    }
+  }
+  BOOST_CHECK_EQUAL(g_result, o);
+}
+
 #if defined(HAVE_FIBER_SANITIZER) && defined(__APPLE__) && defined(__arm64__)
 
 // This test is buggy on MacOS when compiled with asan. It also causes subsequents tests to report spurious issues.