[gwp_asan] Employ EXPECT_DEATH for zxtest compatibility
authorCaslyn Tonelli <caslyn@google.com>
Thu, 30 Mar 2023 00:03:05 +0000 (00:03 +0000)
committerCaslyn Tonelli <caslyn@google.com>
Tue, 18 Apr 2023 21:57:30 +0000 (21:57 +0000)
Employ a similar tactic introduced by https://reviews.llvm.org/D94362
for gwp_asan tests. zxtest `ASSERT_DEATH` syntax differs from gtest in
that it expects a lambda.

zxtest does not have `EXPECT_DEATH`, so it introduced for Fuchsia builds
and wraps the expression with a lambda to create a compatible syntax
between zxtest and gtest for death tests.

An example of where this compatiblity is needed is in
`never_allocated.cpp`.

Differential Revision: https://reviews.llvm.org/D147189

compiler-rt/lib/gwp_asan/tests/harness.h
compiler-rt/lib/gwp_asan/tests/never_allocated.cpp

index 3e859358c96bd507ca8dcedd7b97c0f1976f1e5f..ae39a443bd0a090f75aaf76922e15a5889d70473 100644 (file)
 #define ZXTEST_USE_STREAMABLE_MACROS
 #include <zxtest/zxtest.h>
 namespace testing = zxtest;
+// zxtest defines a different ASSERT_DEATH, taking a lambda and an error message
+// if death didn't occur, versus gtest taking a statement and a string to search
+// for in the dying process. zxtest doesn't define an EXPECT_DEATH, so we use
+// that in the tests below (which works as intended for gtest), and we define
+// EXPECT_DEATH as a wrapper for zxtest's ASSERT_DEATH. Note that zxtest drops
+// the functionality for checking for a particular message in death.
+#define EXPECT_DEATH(X, Y) ASSERT_DEATH(([&] { X; }), "")
 #else
 #include "gtest/gtest.h"
 #endif
index bd43b22daa1e84cbb1ea99f2ba9c0f1a98574d70..2f695b4379861fcc7b77c7514cce9ef033fc60f4 100644 (file)
@@ -25,7 +25,7 @@ TEST_P(BacktraceGuardedPoolAllocatorDeathTest, NeverAllocated) {
   // in the posix crash handler.
   char *volatile NeverAllocatedPtr = static_cast<char *>(Ptr) + 0x3000;
   if (!Recoverable) {
-    ASSERT_DEATH(*NeverAllocatedPtr = 0, DeathNeedle);
+    EXPECT_DEATH(*NeverAllocatedPtr = 0, DeathNeedle);
     return;
   }