Debian Codegraph @

subprojects/libcrbtree-3/src/test-parallel.c

raw
1 /*
2 * Tests Lockless Tree Lookups
3 * The RB-Tree implementation supports lockless tree lookups on shared
4 * data-structures. While it does not guarantee correct results (you might skip
5 * entire sub-trees), it does guarantee valid behavior (the traversal is
6 * guaranteed to end and produce some valid result).
7 * This test uses ptrace to run tree operations step-by-step in a separate
8 * process, and after each instruction verify the pseudo-validity of the tree.
9 * This means, a tree must only have valid left/right pointers (or NULL), and
10 * must not contain any loops in those pointers.
11 *
12 * This test runs two processes with a shared context and tree. It runs them in
13 * this order:
14 *
15 * | PARENT | CHILD |
16 * +--------------------+-----------+
17 * ~ ~ ~
18 * test_parent_start
19 * test_child1
20 * test_parent_middle
21 * test_child2
22 * test_parent_end
23 * ~ ~ ~
24 * +--------------------+-----------+
25 *
26 * Additionally, on each TRAP of CHILD, the parent runs test_parent_step(). The
27 * ptrace infrastructure generates a TRAP after each instruction, so this test
28 * is very CPU aggressive in the parent.
29 */
30
31 #undef NDEBUG
32 #include <assert.h>
33 #include <c-stdaux.h>
34 #include <errno.h>
35 #include <inttypes.h>
36 #include <sched.h>
37 #include <signal.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/mman.h>
43 #include <sys/ptrace.h>
44 #include <sys/resource.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47 #include <sys/syscall.h>
48 #include <time.h>
49 #include <unistd.h>
50 #include "c-rbtree.h"
51 #include "c-rbtree-private.h"
52
53 typedef struct {
54 CRBNode rb;
55 bool visited;
56 } TestNode;
57
58 typedef struct {
59 size_t mapsize;
60 char *map;
61 CRBTree *tree;
62 TestNode *node_mem;
63 CRBNode **nodes;
64 CRBNode **cache;
65 size_t n_nodes;
66 } TestContext;
67
68 /* avoid ptrace-sigstop by using SIGKILL errors in traced children */
69 #define child_assert(_expr) ((void)(!!(_expr) ? 1 : (raise(SIGKILL), 0)))
70
71 static int compare(CRBTree *t, void *k, CRBNode *n) {
72 return (char *)n - (char *)k;
73 }
74
75 static void shuffle(CRBNode **nodes, size_t n_memb) {
76 unsigned int i, j;
77 CRBNode *t;
78
79 for (i = 0; i < n_memb; ++i) {
80 j = rand() % n_memb;
81 t = nodes[j];
82 nodes[j] = nodes[i];
83 nodes[i] = t;
84 }
85 }
86
87 static void toggle_visit(CRBNode *n, bool set) {
88 c_rbnode_entry(n, TestNode, rb)->visited = set;
89 }
90
91 static bool fetch_visit(CRBNode *n) {
92 return c_rbnode_entry(n, TestNode, rb)->visited;
93 }
94
95 static void test_child1(TestContext *ctx) {
96 CRBNode *p, **slot;
97 size_t i;
98
99 for (i = 0; i < ctx->n_nodes; ++i) {
100 child_assert(!c_rbnode_is_linked(ctx->nodes[i]));
101 slot = c_rbtree_find_slot(ctx->tree, compare, ctx->nodes[i], &p);
102 c_rbtree_add(ctx->tree, p, slot, ctx->nodes[i]);
103 }
104 }
105
106 static void test_child2(TestContext *ctx) {
107 size_t i;
108
109 for (i = 0; i < ctx->n_nodes; ++i) {
110 child_assert(c_rbnode_is_linked(ctx->nodes[i]));
111 c_rbnode_unlink(ctx->nodes[i]);
112 }
113 }
114
115 static void test_parent_start(TestContext *ctx) {
116 size_t i;
117
118 /*
119 * Generate a tree with @n_nodes entries. We store the entries in
120 * @ctx->node_mem, generate a randomized access-map in @ctx->nodes
121 * (i.e., an array of pointers to entries in @ctx->node_mem, but in
122 * random order), and a temporary cache for free use in the parent.
123 *
124 * All this is stored in a MAP_SHARED memory region so it is equivalent
125 * in child and parent.
126 */
127
128 ctx->n_nodes = 32;
129 ctx->mapsize = sizeof(CRBTree);
130 ctx->mapsize += ctx->n_nodes * sizeof(TestNode);
131 ctx->mapsize += ctx->n_nodes * sizeof(CRBNode*);
132 ctx->mapsize += ctx->n_nodes * sizeof(CRBNode*);
133
134 ctx->map = mmap(NULL, ctx->mapsize, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
135 c_assert(ctx->map != MAP_FAILED);
136
137 ctx->tree = (void *)ctx->map;
138 ctx->node_mem = (void *)(ctx->tree + 1);
139 ctx->nodes = (void *)(ctx->node_mem + ctx->n_nodes);
140 ctx->cache = (void *)(ctx->nodes + ctx->n_nodes);
141
142 for (i = 0; i < ctx->n_nodes; ++i) {
143 ctx->nodes[i] = &ctx->node_mem[i].rb;
144 c_rbnode_init(ctx->nodes[i]);
145 }
146
147 shuffle(ctx->nodes, ctx->n_nodes);
148 }
149
150 static void test_parent_middle(TestContext *ctx) {
151 size_t i;
152
153 shuffle(ctx->nodes, ctx->n_nodes);
154
155 for (i = 0; i < ctx->n_nodes; ++i)
156 child_assert(c_rbnode_is_linked(ctx->nodes[i]));
157 }
158
159 static void test_parent_end(TestContext *ctx) {
160 size_t i;
161 int r;
162
163 for (i = 0; i < ctx->n_nodes; ++i)
164 c_assert(!c_rbnode_is_linked(ctx->nodes[i]));
165
166 r = munmap(ctx->map, ctx->mapsize);
167 c_assert(r >= 0);
168 }
169
170 static void test_parent_step(TestContext *ctx) {
171 size_t i, i_level;
172 CRBNode *n, *p;
173
174 n = ctx->tree->root;
175 i_level = 0;
176
177 while (n) {
178 /* verify that we haven't visited @n, yet */
179 c_assert(!fetch_visit(n));
180
181 /* verify @n is a valid node */
182 for (i = 0; i < ctx->n_nodes; ++i)
183 if (n == ctx->nodes[i])
184 break;
185 c_assert(i < ctx->n_nodes);
186
187 /* pre-order traversal and marker for cycle detection */
188 if (n->left) {
189 toggle_visit(n, true);
190 ctx->cache[i_level++] = n;
191 n = n->left;
192 } else if (n->right) {
193 toggle_visit(n, true);
194 ctx->cache[i_level++] = n;
195 n = n->right;
196 } else {
197 while (i_level > 0) {
198 p = ctx->cache[i_level - 1];
199 if (p->right && n != p->right) {
200 n = p->right;
201 break;
202 }
203 --i_level;
204 n = p;
205 toggle_visit(n, false);
206 }
207 if (i_level == 0)
208 break;
209 }
210 }
211 }
212
213 static int test_parallel_child(TestContext *ctx) {
214 int r;
215
216 /*
217 * Make parent trace us and enter stopped state. In case of EPERM, we
218 * are either ptraced already, or are not privileged to run ptrace.
219 * Exit via 0xdf to signal this condition to our parent.
220 */
221 r = ptrace(PTRACE_TRACEME, 0, 0, 0);
222 if (r < 0 && errno == EPERM)
223 return 0xdf;
224
225 child_assert(r >= 0);
226
227 /* SIGUSR1 to signal readiness */
228 r = raise(SIGUSR1);
229 child_assert(r >= 0);
230
231 /* run first part */
232 test_child1(ctx);
233
234 /* SIGURG to cause re-shuffle */
235 r = raise(SIGURG);
236 child_assert(r >= 0);
237
238 /* run second part */
239 test_child2(ctx);
240
241 /* SIGUSR2 to signal end */
242 r = raise(SIGUSR2);
243 child_assert(r >= 0);
244
245 /* return known exit code to parent */
246 return 0xef;
247 }
248
249 static int test_parallel(void) {
250 TestContext ctx = {};
251 int r, pid, status;
252 uint64_t n_instr, n_event;
253
254 /* create shared area for tree verification */
255 test_parent_start(&ctx);
256
257 /* run child */
258 pid = fork();
259 c_assert(pid >= 0);
260 if (pid == 0) {
261 r = test_parallel_child(&ctx);
262 _exit(r);
263 }
264
265 /*
266 * After setup, the child immediately enters TRACE-operation and raises
267 * SIGUSR1. Once continued, the child performs the pre-configured tree
268 * operations. When done, it raises SIGUSR2, and then exits.
269 *
270 * Here in the parent we catch all trace-stops of the child via waitpid
271 * until we get no more such stop-events. Based on the stop-event we
272 * get, we verify child-state, STEP it, or perform other state tracking.
273 * We repeat this as long as we catch trace-stops from the child.
274 */
275 n_instr = 0;
276 n_event = 0;
277 for (r = waitpid(pid, &status, 0);
278 r == pid && WIFSTOPPED(status);
279 r = waitpid(pid, &status, 0)) {
280
281 switch (WSTOPSIG(status)) {
282 case SIGUSR1:
283 n_event |= 0x1;
284
285 /* step child */
286 r = ptrace(PTRACE_SINGLESTEP, pid, 0, 0);
287
288 /*
289 * Some architectures (e.g., armv7hl) do not implement
290 * SINGLESTEP, but return EIO. Skip the entire test in
291 * this case.
292 */
293 if (r < 0 && errno == EIO)
294 return 77;
295
296 c_assert(r >= 0);
297 break;
298
299 case SIGURG:
300 n_event |= 0x2;
301 test_parent_middle(&ctx);
302
303 /* step child */
304 r = ptrace(PTRACE_SINGLESTEP, pid, 0, 0);
305 c_assert(r >= 0);
306 break;
307
308 case SIGUSR2:
309 n_event |= 0x4;
310 test_parent_end(&ctx);
311
312 /* continue child */
313 r = ptrace(PTRACE_CONT, pid, 0, 0);
314 c_assert(r >= 0);
315 break;
316
317 case SIGTRAP:
318 ++n_instr;
319 test_parent_step(&ctx);
320
321 /* step repeatedly as long as we get SIGTRAP */
322 r = ptrace(PTRACE_SINGLESTEP, pid, 0, 0);
323 c_assert(r >= 0);
324 break;
325
326 default:
327 c_assert(0);
328 break;
329 }
330 }
331
332 /* verify our child exited cleanly */
333 c_assert(r == pid);
334 c_assert(!!WIFEXITED(status));
335
336 /*
337 * 0xdf is signalled if ptrace is not allowed or we are already
338 * ptraced. In this case we skip the test.
339 *
340 * 0xef is signalled on success.
341 *
342 * In any other case something went wobbly and we should fail hard.
343 */
344 switch (WEXITSTATUS(status)) {
345 case 0xef:
346 break;
347 case 0xdf:
348 return 77;
349 default:
350 c_assert(0);
351 break;
352 }
353
354 /* verify we hit all child states */
355 c_assert(n_event & 0x1);
356 c_assert(n_event & 0x2);
357 c_assert(n_event & 0x4);
358 c_assert(n_instr > 0);
359
360 return 0;
361 }
362
363 int main(int argc, char **argv) {
364 unsigned int i;
365 int r;
366
367 /* we want stable tests, so use fixed seed */
368 srand(0xdeadbeef);
369
370 /*
371 * The tests are pseudo random; run them multiple times, each run will
372 * have different orders and thus different results.
373 */
374 for (i = 0; i < 4; ++i) {
375 r = test_parallel();
376 if (r)
377 return r;
378 }
379
380 return 0;
381 }