summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYinan Zhang <zyn8950@gmail.com>2020-03-19 01:06:47 (GMT)
committerYinan Zhang <zyn8950@gmail.com>2020-03-19 03:48:26 (GMT)
commitccdc70a5ce7b9dd723d947025f99006e7e78d17e (patch)
treee79c99f42d18925c3520538da43ba7a22f5a634a
parentb30a5c2f9073b6f35f0023a443cd18ca406e972a (diff)
downloadjemalloc-ccdc70a5ce7b9dd723d947025f99006e7e78d17e.zip
jemalloc-ccdc70a5ce7b9dd723d947025f99006e7e78d17e.tar.gz
jemalloc-ccdc70a5ce7b9dd723d947025f99006e7e78d17e.tar.bz2
Fix: assertion could abort on past failures
-rw-r--r--test/include/test/test.h98
-rw-r--r--test/src/test.c5
2 files changed, 55 insertions, 48 deletions
diff --git a/test/include/test/test.h b/test/include/test/test.h
index a1b8ff3..2167e8c 100644
--- a/test/include/test/test.h
+++ b/test/include/test/test.h
@@ -1,6 +1,6 @@
#define ASSERT_BUFSIZE 256
-#define expect_cmp(t, a, b, cmp, neg_cmp, pri, ...) do { \
+#define verify_cmp(may_abort, t, a, b, cmp, neg_cmp, pri, ...) do { \
const t a_ = (a); \
const t b_ = (b); \
if (!(a_ cmp b_)) { \
@@ -13,10 +13,17 @@
__func__, __FILE__, __LINE__, \
#a, #b, a_, b_); \
malloc_snprintf(message, sizeof(message), __VA_ARGS__); \
- p_test_fail(prefix, message); \
+ if (may_abort) { \
+ abort(); \
+ } else { \
+ p_test_fail(prefix, message); \
+ } \
} \
} while (0)
+#define expect_cmp(t, a, b, cmp, neg_cmp, pri, ...) verify_cmp(false, \
+ t, a, b, cmp, neg_cmp, pri, __VA_ARGS__)
+
#define expect_ptr_eq(a, b, ...) expect_cmp(void *, a, b, ==, \
!=, "p", __VA_ARGS__)
#define expect_ptr_ne(a, b, ...) expect_cmp(void *, a, b, !=, \
@@ -210,7 +217,7 @@
#define expect_u64_gt(a, b, ...) expect_cmp(uint64_t, a, b, >, \
<=, FMTu64, __VA_ARGS__)
-#define expect_b_eq(a, b, ...) do { \
+#define verify_b_eq(may_abort, a, b, ...) do { \
bool a_ = (a); \
bool b_ = (b); \
if (!(a_ == b_)) { \
@@ -223,10 +230,15 @@
#a, #b, a_ ? "true" : "false", \
b_ ? "true" : "false"); \
malloc_snprintf(message, sizeof(message), __VA_ARGS__); \
- p_test_fail(prefix, message); \
+ if (may_abort) { \
+ abort(); \
+ } else { \
+ p_test_fail(prefix, message); \
+ } \
} \
} while (0)
-#define expect_b_ne(a, b, ...) do { \
+
+#define verify_b_ne(may_abort, a, b, ...) do { \
bool a_ = (a); \
bool b_ = (b); \
if (!(a_ != b_)) { \
@@ -239,13 +251,21 @@
#a, #b, a_ ? "true" : "false", \
b_ ? "true" : "false"); \
malloc_snprintf(message, sizeof(message), __VA_ARGS__); \
- p_test_fail(prefix, message); \
+ if (may_abort) { \
+ abort(); \
+ } else { \
+ p_test_fail(prefix, message); \
+ } \
} \
} while (0)
+
+#define expect_b_eq(a, b, ...) verify_b_eq(false, a, b, __VA_ARGS__)
+#define expect_b_ne(a, b, ...) verify_b_ne(false, a, b, __VA_ARGS__)
+
#define expect_true(a, ...) expect_b_eq(a, true, __VA_ARGS__)
#define expect_false(a, ...) expect_b_eq(a, false, __VA_ARGS__)
-#define expect_str_eq(a, b, ...) do { \
+#define verify_str_eq(may_abort, a, b, ...) do { \
if (strcmp((a), (b))) { \
char prefix[ASSERT_BUFSIZE]; \
char message[ASSERT_BUFSIZE]; \
@@ -255,10 +275,15 @@
"\"%s\" differs from \"%s\": ", \
__func__, __FILE__, __LINE__, #a, #b, a, b); \
malloc_snprintf(message, sizeof(message), __VA_ARGS__); \
- p_test_fail(prefix, message); \
+ if (may_abort) { \
+ abort(); \
+ } else { \
+ p_test_fail(prefix, message); \
+ } \
} \
} while (0)
-#define expect_str_ne(a, b, ...) do { \
+
+#define verify_str_ne(may_abort, a, b, ...) do { \
if (!strcmp((a), (b))) { \
char prefix[ASSERT_BUFSIZE]; \
char message[ASSERT_BUFSIZE]; \
@@ -268,30 +293,35 @@
"\"%s\" same as \"%s\": ", \
__func__, __FILE__, __LINE__, #a, #b, a, b); \
malloc_snprintf(message, sizeof(message), __VA_ARGS__); \
- p_test_fail(prefix, message); \
+ if (may_abort) { \
+ abort(); \
+ } else { \
+ p_test_fail(prefix, message); \
+ } \
} \
} while (0)
-#define expect_not_reached(...) do { \
+#define expect_str_eq(a, b, ...) verify_str_eq(false, a, b, __VA_ARGS__)
+#define expect_str_ne(a, b, ...) verify_str_ne(false, a, b, __VA_ARGS__)
+
+#define verify_not_reached(may_abort, ...) do { \
char prefix[ASSERT_BUFSIZE]; \
char message[ASSERT_BUFSIZE]; \
malloc_snprintf(prefix, sizeof(prefix), \
"%s:%s:%d: Unreachable code reached: ", \
__func__, __FILE__, __LINE__); \
malloc_snprintf(message, sizeof(message), __VA_ARGS__); \
- p_test_fail(prefix, message); \
-} while (0)
-
-#define p_abort_test_if_failed() do { \
- if (p_test_failed()) { \
+ if (may_abort) { \
abort(); \
+ } else { \
+ p_test_fail(prefix, message); \
} \
} while (0)
-#define assert_cmp(t, a, b, cmp, neg_cmp, pri, ...) do { \
- expect_cmp(t, a, b, cmp, neg_cmp, pri, __VA_ARGS__); \
- p_abort_test_if_failed(); \
-} while (0)
+#define expect_not_reached(...) verify_not_reached(false, __VA_ARGS__)
+
+#define assert_cmp(t, a, b, cmp, neg_cmp, pri, ...) verify_cmp(true, \
+ t, a, b, cmp, neg_cmp, pri, __VA_ARGS__)
#define assert_ptr_eq(a, b, ...) assert_cmp(void *, a, b, ==, \
!=, "p", __VA_ARGS__)
@@ -486,33 +516,16 @@
#define assert_u64_gt(a, b, ...) assert_cmp(uint64_t, a, b, >, \
<=, FMTu64, __VA_ARGS__)
-#define assert_b_eq(a, b, ...) do { \
- expect_b_eq(a, b, __VA_ARGS__); \
- p_abort_test_if_failed(); \
-} while (0)
-
-#define assert_b_ne(a, b, ...) do { \
- expect_b_ne(a, b, __VA_ARGS__); \
- p_abort_test_if_failed(); \
-} while (0)
+#define assert_b_eq(a, b, ...) verify_b_eq(true, a, b, __VA_ARGS__)
+#define assert_b_ne(a, b, ...) verify_b_ne(true, a, b, __VA_ARGS__)
#define assert_true(a, ...) assert_b_eq(a, true, __VA_ARGS__)
#define assert_false(a, ...) assert_b_eq(a, false, __VA_ARGS__)
-#define assert_str_eq(a, b, ...) do { \
- expect_str_eq(a, b, __VA_ARGS__); \
- p_abort_test_if_failed(); \
-} while (0)
+#define assert_str_eq(a, b, ...) verify_str_eq(true, a, b, __VA_ARGS__)
+#define assert_str_ne(a, b, ...) verify_str_ne(true, a, b, __VA_ARGS__)
-#define assert_str_ne(a, b, ...) do { \
- expect_str_ne(a, b, __VA_ARGS__); \
- p_abort_test_if_failed(); \
-} while (0)
-
-#define assert_not_reached(...) do { \
- expect_not_reached(__VA_ARGS__); \
- p_abort_test_if_failed(); \
-} while (0)
+#define assert_not_reached(...) verify_not_reached(true, __VA_ARGS__)
/*
* If this enum changes, corresponding changes in test/test.sh.in are also
@@ -568,6 +581,5 @@ test_status_t p_test_no_malloc_init(test_t *t, ...);
void p_test_init(const char *name);
void p_test_fini(void);
void p_test_fail(const char *prefix, const char *message);
-bool p_test_failed(void);
void strncpy_cond(void *dst, const char *src, bool cond);
diff --git a/test/src/test.c b/test/src/test.c
index b40fbc6..4583e55 100644
--- a/test/src/test.c
+++ b/test/src/test.c
@@ -233,11 +233,6 @@ p_test_fail(const char *prefix, const char *message) {
test_status = test_status_fail;
}
-bool
-p_test_failed() {
- return test_status == test_status_fail;
-}
-
void
strncpy_cond(void *dst, const char *src, bool cond) {
if (cond) {