tests/mempool: Allocate pool in child for robust tests
authorOlivier Dion <odion@efficios.com>
Tue, 19 Mar 2024 15:39:18 +0000 (11:39 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 19 Mar 2024 15:40:56 +0000 (11:40 -0400)
Depending on the populate policy, it is not possible to use a pool
across a fork.  Since robust tests rely on SIGABRT to be triggered in a
child process, the pool must be allocated in the child instead of the
parent for the RSEQ_MEMPOOL_POPULATE_PRIVATE_NONE policy.

Change-Id: I78bc19d12b4f8bc0c7e2a2f05fab870ed7524e13
Signed-off-by: Olivier Dion <odion@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
tests/mempool_test.c

index e869f4a2a0a2e4c58a309fc3cc1131e9fd79d2f8..6e1fe6df5961302457572cf95d6b5eeee9cf2ec8 100644 (file)
@@ -235,11 +235,53 @@ static void test_robust_poison_corruption_destroy(struct rseq_mempool *pool,
        rseq_mempool_destroy(pool);
 }
 
+static struct rseq_mempool *make_test_pool(enum rseq_mempool_populate_policy policy)
+{
+       struct rseq_mempool_attr *attr;
+       struct rseq_mempool *pool;
+       int ret;
+
+       pool = NULL;
+
+       attr = rseq_mempool_attr_create();
+
+       if (!attr) {
+               goto out;
+       }
+
+       ret = rseq_mempool_attr_set_robust(attr);
+
+       if (0 != ret) {
+               goto err_attr;
+       }
+
+       ret = rseq_mempool_attr_set_percpu(attr, RSEQ_MEMPOOL_STRIDE, 1);
+
+       if (0 != ret) {
+               goto err_attr;
+       }
+
+       ret = rseq_mempool_attr_set_populate_policy(attr, policy);
+
+       if (0 != ret) {
+               goto err_attr;
+       }
+
+       pool = rseq_mempool_create("mempool-robust",
+                               sizeof(struct test_data), attr);
+err_attr:
+       rseq_mempool_attr_destroy(attr);
+out:
+       return pool;
+
+}
+
 static int run_robust_test(void (*test)(struct rseq_mempool *, enum rseq_mempool_populate_policy),
-                       struct rseq_mempool *pool, enum rseq_mempool_populate_policy policy)
+                       enum rseq_mempool_populate_policy policy)
 {
        pid_t cpid;
        int status;
+       struct rseq_mempool *pool;
 
        cpid = fork();
 
@@ -247,6 +289,13 @@ static int run_robust_test(void (*test)(struct rseq_mempool *, enum rseq_mempool
        case -1:
                return 0;
        case 0:
+               /*
+                * Intentional leak of test pool because some tests might want
+                * to do an explicit destroy on it.
+                */
+               pool = make_test_pool(policy);
+               if (!pool)
+                       _exit(EXIT_FAILURE);
                test(pool, policy);
                _exit(EXIT_FAILURE);
        default:
@@ -262,47 +311,24 @@ static int run_robust_test(void (*test)(struct rseq_mempool *, enum rseq_mempool
 
 static void run_robust_tests(enum rseq_mempool_populate_policy policy)
 {
-       struct rseq_mempool_attr *attr;
-       struct rseq_mempool *pool;
-       int ret;
 
-       attr = rseq_mempool_attr_create();
-       ok(attr, "Create mempool attributes");
-
-       ret = rseq_mempool_attr_set_robust(attr);
-       ok(ret == 0, "Setting mempool robust attribute");
-
-       ret = rseq_mempool_attr_set_percpu(attr, RSEQ_MEMPOOL_STRIDE, 1);
-       ok(ret == 0, "Setting mempool percpu type");
-
-       ret = rseq_mempool_attr_set_populate_policy(attr, policy);
-       ok(ret == 0, "Setting mempool populate policy to %s",
-               policy == RSEQ_MEMPOOL_POPULATE_PRIVATE_NONE ? "PRIVATE_NONE" : "PRIVATE_ALL");
-
-       pool = rseq_mempool_create("mempool-robust",
-                               sizeof(struct test_data), attr);
-
-       rseq_mempool_attr_destroy(attr);
-
-       ok(run_robust_test(test_robust_double_free, pool, policy),
+       ok(run_robust_test(test_robust_double_free, policy),
                "robust-double-free");
 
-       ok(run_robust_test(test_robust_memory_leak, pool, policy),
+       ok(run_robust_test(test_robust_memory_leak, policy),
                "robust-memory-leak");
 
-       ok(run_robust_test(test_robust_poison_corruption_malloc, pool, policy),
+       ok(run_robust_test(test_robust_poison_corruption_malloc, policy),
                "robust-poison-corruption-malloc");
 
-       ok(run_robust_test(test_robust_poison_corruption_destroy, pool, policy),
+       ok(run_robust_test(test_robust_poison_corruption_destroy, policy),
                "robust-poison-corruption-destroy");
 
-       ok(run_robust_test(test_robust_corrupt_after_free, pool, policy),
+       ok(run_robust_test(test_robust_corrupt_after_free, policy),
                "robust-corrupt-after-free");
 
-       ok(run_robust_test(test_robust_free_list_corruption, pool, policy),
+       ok(run_robust_test(test_robust_free_list_corruption, policy),
                "robust-free-list-corruption");
-
-       rseq_mempool_destroy(pool);
 }
 
 int main(void)
This page took 0.026373 seconds and 4 git commands to generate.