| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
/* |
| 4 |
* IPC barrier tests |
| 5 |
* These tests verify the correct behavior of the IPC Barrier implementation. |
| 6 |
* Note that the tests use alarm-timers to verify dead-locks and timeouts. These |
| 7 |
* might not work on slow machines where 20ms are too short to perform specific |
| 8 |
* operations (though, very unlikely). In case that turns out true, we have to |
| 9 |
* increase it at the slightly cost of lengthen test-duration on other machines. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <stdlib.h> |
| 13 |
#include <sys/time.h> |
| 14 |
#include <sys/wait.h> |
| 15 |
#include <unistd.h> |
| 16 |
|
| 17 |
#include "barrier.h" |
| 18 |
#include "errno-util.h" |
| 19 |
#include "tests.h" |
| 20 |
#include "time-util.h" |
| 21 |
#include "virt.h" |
| 22 |
|
| 23 |
/* 20ms to test deadlocks; All timings use multiples of this constant as |
| 24 |
* alarm/sleep timers. If this timeout is too small for slow machines to perform |
| 25 |
* the requested operations, we have to increase it. On an i7 this works fine |
| 26 |
* with 1ms base-time, so 20ms should be just fine for everyone. */ |
| 27 |
#define BASE_TIME (20 * USEC_PER_MSEC) |
| 28 |
|
| 29 |
static void set_alarm(usec_t usecs) { |
| 30 |
struct itimerval v = { }; |
| 31 |
|
| 32 |
timeval_store(&v.it_value, usecs); |
| 33 |
ASSERT_OK(setitimer(ITIMER_REAL, &v, NULL)); |
| 34 |
} |
| 35 |
|
| 36 |
#define TEST_BARRIER(_FUNCTION, _CHILD_CODE, _WAIT_CHILD, _PARENT_CODE, _WAIT_PARENT) \ |
| 37 |
TEST(_FUNCTION) { \ |
| 38 |
Barrier b = BARRIER_NULL; \ |
| 39 |
pid_t pid1, pid2; \ |
| 40 |
\ |
| 41 |
ASSERT_OK(barrier_create(&b)); \ |
| 42 |
ASSERT_GT(b.me, 0); \ |
| 43 |
ASSERT_GT(b.them, 0); \ |
| 44 |
ASSERT_GT(b.pipe[0], 0); \ |
| 45 |
ASSERT_GT(b.pipe[1], 0); \ |
| 46 |
\ |
| 47 |
pid1 = fork(); \ |
| 48 |
ASSERT_OK(pid1); \ |
| 49 |
if (pid1 == 0) { \ |
| 50 |
barrier_set_role(&b, BARRIER_CHILD); \ |
| 51 |
{ _CHILD_CODE; } \ |
| 52 |
exit(42); \ |
| 53 |
} \ |
| 54 |
\ |
| 55 |
pid2 = fork(); \ |
| 56 |
ASSERT_OK(pid2); \ |
| 57 |
if (pid2 == 0) { \ |
| 58 |
barrier_set_role(&b, BARRIER_PARENT); \ |
| 59 |
{ _PARENT_CODE; } \ |
| 60 |
exit(42); \ |
| 61 |
} \ |
| 62 |
\ |
| 63 |
barrier_destroy(&b); \ |
| 64 |
set_alarm(999999); \ |
| 65 |
{ _WAIT_CHILD; } \ |
| 66 |
{ _WAIT_PARENT; } \ |
| 67 |
set_alarm(0); \ |
| 68 |
} |
| 69 |
|
| 70 |
#define TEST_BARRIER_WAIT_SUCCESS(_pid) \ |
| 71 |
({ \ |
| 72 |
int pidr, status; \ |
| 73 |
pidr = waitpid(_pid, &status, 0); \ |
| 74 |
ASSERT_EQ(pidr, _pid); \ |
| 75 |
assert_se(WIFEXITED(status)); \ |
| 76 |
ASSERT_EQ(WEXITSTATUS(status), 42); \ |
| 77 |
}) |
| 78 |
|
| 79 |
#define TEST_BARRIER_WAIT_ALARM(_pid) \ |
| 80 |
({ \ |
| 81 |
int pidr, status; \ |
| 82 |
pidr = waitpid(_pid, &status, 0); \ |
| 83 |
ASSERT_EQ(pidr, _pid); \ |
| 84 |
assert_se(WIFSIGNALED(status)); \ |
| 85 |
assert_se(WTERMSIG(status) == SIGALRM); \ |
| 86 |
}) |
| 87 |
|
| 88 |
/* |
| 89 |
* Test basic sync points |
| 90 |
* This places a barrier in both processes and waits synchronously for them. |
| 91 |
* The timeout makes sure the sync works as expected. The usleep_safe() on one side |
| 92 |
* makes sure the exit of the parent does not overwrite previous barriers. Due |
| 93 |
* to the usleep_safe(), we know that the parent already exited, thus there's a |
| 94 |
* pending HUP on the pipe. However, the barrier_sync() prefers reads on the |
| 95 |
* eventfd, thus we can safely wait on the barrier. |
| 96 |
*/ |
| 97 |
TEST_BARRIER(barrier_sync, |
| 98 |
({ |
| 99 |
set_alarm(BASE_TIME * 10); |
| 100 |
assert_se(barrier_place(&b)); |
| 101 |
usleep_safe(BASE_TIME * 2); |
| 102 |
assert_se(barrier_sync(&b)); |
| 103 |
}), |
| 104 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 105 |
({ |
| 106 |
set_alarm(BASE_TIME * 10); |
| 107 |
assert_se(barrier_place(&b)); |
| 108 |
assert_se(barrier_sync(&b)); |
| 109 |
}), |
| 110 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 111 |
|
| 112 |
/* |
| 113 |
* Test wait_next() |
| 114 |
* This places a barrier in the parent and syncs on it. The child sleeps while |
| 115 |
* the parent places the barrier and then waits for a barrier. The wait will |
| 116 |
* succeed as the child hasn't read the parent's barrier, yet. The following |
| 117 |
* barrier and sync synchronize the exit. |
| 118 |
*/ |
| 119 |
TEST_BARRIER(barrier_wait_next, |
| 120 |
({ |
| 121 |
usleep_safe(BASE_TIME); |
| 122 |
set_alarm(BASE_TIME * 10); |
| 123 |
assert_se(barrier_wait_next(&b)); |
| 124 |
assert_se(barrier_place(&b)); |
| 125 |
assert_se(barrier_sync(&b)); |
| 126 |
}), |
| 127 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 128 |
({ |
| 129 |
set_alarm(BASE_TIME * 4); |
| 130 |
assert_se(barrier_place(&b)); |
| 131 |
assert_se(barrier_sync(&b)); |
| 132 |
}), |
| 133 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 134 |
|
| 135 |
/* |
| 136 |
* Test wait_next() multiple times |
| 137 |
* This places two barriers in the parent and waits for the child to exit. The |
| 138 |
* child sleeps 20ms so both barriers _should_ be in place. It then waits for |
| 139 |
* the parent to place the next barrier twice. The first call will fetch both |
| 140 |
* barriers and return. However, the second call will stall as the parent does |
| 141 |
* not place a 3rd barrier (the sleep caught two barriers). wait_next() is does |
| 142 |
* not look at barrier-links so this stall is expected. Thus this test times |
| 143 |
* out. |
| 144 |
*/ |
| 145 |
TEST_BARRIER(barrier_wait_next_twice, |
| 146 |
({ |
| 147 |
usleep_safe(BASE_TIME); |
| 148 |
set_alarm(BASE_TIME); |
| 149 |
assert_se(barrier_wait_next(&b)); |
| 150 |
assert_se(barrier_wait_next(&b)); |
| 151 |
assert_se(0); |
| 152 |
}), |
| 153 |
TEST_BARRIER_WAIT_ALARM(pid1), |
| 154 |
({ |
| 155 |
set_alarm(BASE_TIME * 10); |
| 156 |
assert_se(barrier_place(&b)); |
| 157 |
assert_se(barrier_place(&b)); |
| 158 |
usleep_safe(BASE_TIME * 4); |
| 159 |
}), |
| 160 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 161 |
|
| 162 |
/* |
| 163 |
* Test wait_next() with local barriers |
| 164 |
* This is the same as test_barrier_wait_next_twice, but places local barriers |
| 165 |
* between both waits. This does not have any effect on the wait so it times out |
| 166 |
* like the other test. |
| 167 |
*/ |
| 168 |
TEST_BARRIER(barrier_wait_next_twice_local, |
| 169 |
({ |
| 170 |
usleep_safe(BASE_TIME); |
| 171 |
set_alarm(BASE_TIME); |
| 172 |
assert_se(barrier_wait_next(&b)); |
| 173 |
assert_se(barrier_place(&b)); |
| 174 |
assert_se(barrier_place(&b)); |
| 175 |
assert_se(barrier_wait_next(&b)); |
| 176 |
assert_se(0); |
| 177 |
}), |
| 178 |
TEST_BARRIER_WAIT_ALARM(pid1), |
| 179 |
({ |
| 180 |
set_alarm(BASE_TIME * 10); |
| 181 |
assert_se(barrier_place(&b)); |
| 182 |
assert_se(barrier_place(&b)); |
| 183 |
usleep_safe(BASE_TIME * 4); |
| 184 |
}), |
| 185 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 186 |
|
| 187 |
/* |
| 188 |
* Test wait_next() with sync_next() |
| 189 |
* This is again the same as test_barrier_wait_next_twice but uses a |
| 190 |
* synced wait as the second wait. This works just fine because the local state |
| 191 |
* has no barriers placed, therefore, the remote is always in sync. |
| 192 |
*/ |
| 193 |
TEST_BARRIER(barrier_wait_next_twice_sync, |
| 194 |
({ |
| 195 |
usleep_safe(BASE_TIME); |
| 196 |
set_alarm(BASE_TIME); |
| 197 |
assert_se(barrier_wait_next(&b)); |
| 198 |
assert_se(barrier_sync_next(&b)); |
| 199 |
}), |
| 200 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 201 |
({ |
| 202 |
set_alarm(BASE_TIME * 10); |
| 203 |
assert_se(barrier_place(&b)); |
| 204 |
assert_se(barrier_place(&b)); |
| 205 |
}), |
| 206 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 207 |
|
| 208 |
/* |
| 209 |
* Test wait_next() with sync_next() and local barriers |
| 210 |
* This is again the same as test_barrier_wait_next_twice_local but uses a |
| 211 |
* synced wait as the second wait. This works just fine because the local state |
| 212 |
* is in sync with the remote. |
| 213 |
*/ |
| 214 |
TEST_BARRIER(barrier_wait_next_twice_local_sync, |
| 215 |
({ |
| 216 |
usleep_safe(BASE_TIME); |
| 217 |
set_alarm(BASE_TIME); |
| 218 |
assert_se(barrier_wait_next(&b)); |
| 219 |
assert_se(barrier_place(&b)); |
| 220 |
assert_se(barrier_place(&b)); |
| 221 |
assert_se(barrier_sync_next(&b)); |
| 222 |
}), |
| 223 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 224 |
({ |
| 225 |
set_alarm(BASE_TIME * 10); |
| 226 |
assert_se(barrier_place(&b)); |
| 227 |
assert_se(barrier_place(&b)); |
| 228 |
}), |
| 229 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 230 |
|
| 231 |
/* |
| 232 |
* Test sync_next() and sync() |
| 233 |
* This tests sync_*() synchronizations and makes sure they work fine if the |
| 234 |
* local state is behind the remote state. |
| 235 |
*/ |
| 236 |
TEST_BARRIER(barrier_sync_next, |
| 237 |
({ |
| 238 |
set_alarm(BASE_TIME * 10); |
| 239 |
assert_se(barrier_sync_next(&b)); |
| 240 |
assert_se(barrier_sync(&b)); |
| 241 |
assert_se(barrier_place(&b)); |
| 242 |
assert_se(barrier_place(&b)); |
| 243 |
assert_se(barrier_sync_next(&b)); |
| 244 |
assert_se(barrier_sync_next(&b)); |
| 245 |
assert_se(barrier_sync(&b)); |
| 246 |
}), |
| 247 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 248 |
({ |
| 249 |
set_alarm(BASE_TIME * 10); |
| 250 |
usleep_safe(BASE_TIME); |
| 251 |
assert_se(barrier_place(&b)); |
| 252 |
assert_se(barrier_place(&b)); |
| 253 |
assert_se(barrier_sync(&b)); |
| 254 |
}), |
| 255 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 256 |
|
| 257 |
/* |
| 258 |
* Test sync_next() and sync() with local barriers |
| 259 |
* This tests timeouts if sync_*() is used if local barriers are placed but the |
| 260 |
* remote didn't place any. |
| 261 |
*/ |
| 262 |
TEST_BARRIER(barrier_sync_next_local, |
| 263 |
({ |
| 264 |
set_alarm(BASE_TIME); |
| 265 |
assert_se(barrier_place(&b)); |
| 266 |
assert_se(barrier_sync_next(&b)); |
| 267 |
assert_se(0); |
| 268 |
}), |
| 269 |
TEST_BARRIER_WAIT_ALARM(pid1), |
| 270 |
({ |
| 271 |
usleep_safe(BASE_TIME * 2); |
| 272 |
}), |
| 273 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 274 |
|
| 275 |
/* |
| 276 |
* Test sync_next() and sync() with local barriers and abortion |
| 277 |
* This is the same as test_barrier_sync_next_local but aborts the sync in the |
| 278 |
* parent. Therefore, the sync_next() succeeds just fine due to the abortion. |
| 279 |
*/ |
| 280 |
TEST_BARRIER(barrier_sync_next_local_abort, |
| 281 |
({ |
| 282 |
set_alarm(BASE_TIME * 10); |
| 283 |
assert_se(barrier_place(&b)); |
| 284 |
assert_se(!barrier_sync_next(&b)); |
| 285 |
}), |
| 286 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 287 |
({ |
| 288 |
assert_se(barrier_abort(&b)); |
| 289 |
}), |
| 290 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 291 |
|
| 292 |
/* |
| 293 |
* Test matched wait_abortion() |
| 294 |
* This runs wait_abortion() with remote abortion. |
| 295 |
*/ |
| 296 |
TEST_BARRIER(barrier_wait_abortion, |
| 297 |
({ |
| 298 |
set_alarm(BASE_TIME * 10); |
| 299 |
assert_se(barrier_wait_abortion(&b)); |
| 300 |
}), |
| 301 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 302 |
({ |
| 303 |
assert_se(barrier_abort(&b)); |
| 304 |
}), |
| 305 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 306 |
|
| 307 |
/* |
| 308 |
* Test unmatched wait_abortion() |
| 309 |
* This runs wait_abortion() without any remote abortion going on. It thus must |
| 310 |
* timeout. |
| 311 |
*/ |
| 312 |
TEST_BARRIER(barrier_wait_abortion_unmatched, |
| 313 |
({ |
| 314 |
set_alarm(BASE_TIME); |
| 315 |
assert_se(barrier_wait_abortion(&b)); |
| 316 |
assert_se(0); |
| 317 |
}), |
| 318 |
TEST_BARRIER_WAIT_ALARM(pid1), |
| 319 |
({ |
| 320 |
usleep_safe(BASE_TIME * 2); |
| 321 |
}), |
| 322 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 323 |
|
| 324 |
/* |
| 325 |
* Test matched wait_abortion() with local abortion |
| 326 |
* This runs wait_abortion() with local and remote abortion. |
| 327 |
*/ |
| 328 |
TEST_BARRIER(barrier_wait_abortion_local, |
| 329 |
({ |
| 330 |
set_alarm(BASE_TIME * 10); |
| 331 |
assert_se(barrier_abort(&b)); |
| 332 |
assert_se(!barrier_wait_abortion(&b)); |
| 333 |
}), |
| 334 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 335 |
({ |
| 336 |
assert_se(barrier_abort(&b)); |
| 337 |
}), |
| 338 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 339 |
|
| 340 |
/* |
| 341 |
* Test unmatched wait_abortion() with local abortion |
| 342 |
* This runs wait_abortion() with only local abortion. This must time out. |
| 343 |
*/ |
| 344 |
TEST_BARRIER(barrier_wait_abortion_local_unmatched, |
| 345 |
({ |
| 346 |
set_alarm(BASE_TIME); |
| 347 |
assert_se(barrier_abort(&b)); |
| 348 |
assert_se(!barrier_wait_abortion(&b)); |
| 349 |
assert_se(0); |
| 350 |
}), |
| 351 |
TEST_BARRIER_WAIT_ALARM(pid1), |
| 352 |
({ |
| 353 |
usleep_safe(BASE_TIME * 2); |
| 354 |
}), |
| 355 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 356 |
|
| 357 |
/* |
| 358 |
* Test child exit |
| 359 |
* Place barrier and sync with the child. The child only exits()s, which should |
| 360 |
* cause an implicit abortion and wake the parent. |
| 361 |
*/ |
| 362 |
TEST_BARRIER(barrier_exit, |
| 363 |
({ |
| 364 |
}), |
| 365 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 366 |
({ |
| 367 |
set_alarm(BASE_TIME * 10); |
| 368 |
assert_se(barrier_place(&b)); |
| 369 |
assert_se(!barrier_sync(&b)); |
| 370 |
}), |
| 371 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 372 |
|
| 373 |
/* |
| 374 |
* Test child exit with sleep |
| 375 |
* Same as test_barrier_exit but verifies the test really works due to the |
| 376 |
* child-exit. We add a usleep_safe() which triggers the alarm in the parent and |
| 377 |
* causes the test to time out. |
| 378 |
*/ |
| 379 |
TEST_BARRIER(barrier_no_exit, |
| 380 |
({ |
| 381 |
usleep_safe(BASE_TIME * 2); |
| 382 |
}), |
| 383 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 384 |
({ |
| 385 |
set_alarm(BASE_TIME); |
| 386 |
assert_se(barrier_place(&b)); |
| 387 |
assert_se(!barrier_sync(&b)); |
| 388 |
}), |
| 389 |
TEST_BARRIER_WAIT_ALARM(pid2)); |
| 390 |
|
| 391 |
/* |
| 392 |
* Test pending exit against sync |
| 393 |
* The parent places a barrier *and* exits. The 20ms wait in the child |
| 394 |
* guarantees both are pending. However, our logic prefers pending barriers over |
| 395 |
* pending exit-abortions (unlike normal abortions), thus the wait_next() must |
| 396 |
* succeed, same for the sync_next() as our local barrier-count is smaller than |
| 397 |
* the remote. Once we place a barrier our count is equal, so the sync still |
| 398 |
* succeeds. Only if we place one more barrier, we're ahead of the remote, thus |
| 399 |
* we will fail due to HUP on the pipe. |
| 400 |
*/ |
| 401 |
TEST_BARRIER(barrier_pending_exit, |
| 402 |
({ |
| 403 |
set_alarm(BASE_TIME * 4); |
| 404 |
usleep_safe(BASE_TIME * 2); |
| 405 |
assert_se(barrier_wait_next(&b)); |
| 406 |
assert_se(barrier_sync_next(&b)); |
| 407 |
assert_se(barrier_place(&b)); |
| 408 |
assert_se(barrier_sync_next(&b)); |
| 409 |
assert_se(barrier_place(&b)); |
| 410 |
assert_se(!barrier_sync_next(&b)); |
| 411 |
}), |
| 412 |
TEST_BARRIER_WAIT_SUCCESS(pid1), |
| 413 |
({ |
| 414 |
assert_se(barrier_place(&b)); |
| 415 |
}), |
| 416 |
TEST_BARRIER_WAIT_SUCCESS(pid2)); |
| 417 |
|
| 418 |
static int intro(void) { |
| 419 |
if (!slow_tests_enabled()) |
| 420 |
return log_tests_skipped("slow tests are disabled"); |
| 421 |
|
| 422 |
/* |
| 423 |
* This test uses real-time alarms and sleeps to test for CPU races explicitly. This is highly |
| 424 |
* fragile if your system is under load. We already increased the BASE_TIME value to make the tests |
| 425 |
* more robust, but that just makes the test take significantly longer. Given the recent issues when |
| 426 |
* running the test in a virtualized environments, limit it to bare metal machines only, to minimize |
| 427 |
* false-positives in CIs. |
| 428 |
*/ |
| 429 |
|
| 430 |
Virtualization v = detect_virtualization(); |
| 431 |
if (ERRNO_IS_NEG_PRIVILEGE(v)) |
| 432 |
return log_tests_skipped("Cannot detect virtualization"); |
| 433 |
|
| 434 |
if (v != VIRTUALIZATION_NONE) |
| 435 |
return log_tests_skipped("This test requires a baremetal machine"); |
| 436 |
|
| 437 |
return EXIT_SUCCESS; |
| 438 |
} |
| 439 |
|
| 440 |
DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); |