| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <signal.h> |
| 4 |
|
| 5 |
#if HAVE_VALGRIND_VALGRIND_H |
| 6 |
# include <valgrind/valgrind.h> |
| 7 |
#endif |
| 8 |
|
| 9 |
#include "sd-event.h" |
| 10 |
#include "sd-future.h" |
| 11 |
|
| 12 |
#include "architecture.h" |
| 13 |
#include "log-context.h" |
| 14 |
#include "memory-util.h" |
| 15 |
#include "pidref.h" |
| 16 |
#include "process-util.h" |
| 17 |
#include "tests.h" |
| 18 |
#include "time-util.h" |
| 19 |
|
| 20 |
static int simple_fiber(void *userdata) { |
| 21 |
int *value = ASSERT_PTR(userdata); |
| 22 |
return *value; |
| 23 |
} |
| 24 |
|
| 25 |
TEST(fiber_simple) { |
| 26 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 27 |
ASSERT_OK(sd_event_new(&e)); |
| 28 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 29 |
|
| 30 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 31 |
int value = 5; |
| 32 |
ASSERT_OK(sd_fiber_new(e, "simple", simple_fiber, &value, NULL, &f)); |
| 33 |
ASSERT_OK(sd_event_loop(e)); |
| 34 |
ASSERT_EQ(sd_future_result(f), 5); |
| 35 |
} |
| 36 |
|
| 37 |
/* Fiber that yields once */ |
| 38 |
static int yielding_fiber(void *userdata) { |
| 39 |
int *counter = userdata; |
| 40 |
(*counter)++; |
| 41 |
|
| 42 |
sd_fiber_yield(); |
| 43 |
|
| 44 |
(*counter)++; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
/* Test: Single fiber that yields */ |
| 49 |
TEST(fiber_single_yield) { |
| 50 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 51 |
ASSERT_OK(sd_event_new(&e)); |
| 52 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 53 |
|
| 54 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 55 |
int counter = 0; |
| 56 |
ASSERT_OK(sd_fiber_new(e, "yielding", yielding_fiber, &counter, NULL, &f)); |
| 57 |
|
| 58 |
/* First iteration: fiber runs until first yield */ |
| 59 |
ASSERT_EQ(counter, 0); |
| 60 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 61 |
ASSERT_EQ(counter, 1); |
| 62 |
|
| 63 |
/* Second iteration: fiber runs from yield to completion */ |
| 64 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 65 |
ASSERT_EQ(counter, 2); |
| 66 |
|
| 67 |
/* No more fibers to run */ |
| 68 |
ASSERT_OK_ZERO(sd_event_loop(e)); |
| 69 |
} |
| 70 |
|
| 71 |
static int counting_fiber(void *userdata) { |
| 72 |
int counter = 0; |
| 73 |
|
| 74 |
for (int i = 0; i < 5; i++) { |
| 75 |
counter++; |
| 76 |
sd_fiber_yield(); |
| 77 |
} |
| 78 |
|
| 79 |
return counter; |
| 80 |
} |
| 81 |
|
| 82 |
/* Test: Multiple fibers yielding cooperatively */ |
| 83 |
TEST(fiber_multiple_yield) { |
| 84 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 85 |
ASSERT_OK(sd_event_new(&e)); |
| 86 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 87 |
|
| 88 |
sd_future *fibers[5] = {}; |
| 89 |
CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear); |
| 90 |
|
| 91 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) { |
| 92 |
_cleanup_free_ char *name = NULL; |
| 93 |
ASSERT_OK(asprintf(&name, "counting-%zu", i)); |
| 94 |
ASSERT_OK(sd_fiber_new(e, name, counting_fiber, NULL, NULL, &fibers[i])); |
| 95 |
} |
| 96 |
|
| 97 |
ASSERT_OK(sd_event_loop(e)); |
| 98 |
|
| 99 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) |
| 100 |
ASSERT_OK_EQ(sd_future_result(fibers[i]), 5); |
| 101 |
} |
| 102 |
|
| 103 |
static int priority_fiber(void *userdata) { |
| 104 |
int *counter = ASSERT_PTR(userdata); |
| 105 |
|
| 106 |
(*counter)++; |
| 107 |
sd_fiber_yield(); |
| 108 |
|
| 109 |
return *counter; |
| 110 |
} |
| 111 |
|
| 112 |
/* Test: Priority-based scheduling */ |
| 113 |
TEST(fiber_priority_ascending) { |
| 114 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 115 |
ASSERT_OK(sd_event_new(&e)); |
| 116 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 117 |
|
| 118 |
sd_future *fibers[5] = {}; |
| 119 |
CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear); |
| 120 |
int counter = 0; |
| 121 |
|
| 122 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) { |
| 123 |
_cleanup_free_ char *name = NULL; |
| 124 |
ASSERT_OK(asprintf(&name, "priority-%zu", i)); |
| 125 |
ASSERT_OK(sd_fiber_new(e, name, priority_fiber, &counter, NULL, &fibers[i])); |
| 126 |
ASSERT_OK(sd_future_set_priority(fibers[i], i)); |
| 127 |
} |
| 128 |
|
| 129 |
ASSERT_OK(sd_event_loop(e)); |
| 130 |
|
| 131 |
/* The fibers have ascending priorities, so we the first one to run to completion, |
| 132 |
* followed by the second one, etc. */ |
| 133 |
|
| 134 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) |
| 135 |
ASSERT_EQ(sd_future_result(fibers[i]), (int) i + 1); |
| 136 |
} |
| 137 |
|
| 138 |
TEST(fiber_priority_identical) { |
| 139 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 140 |
ASSERT_OK(sd_event_new(&e)); |
| 141 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 142 |
|
| 143 |
sd_future *fibers[5] = {}; |
| 144 |
CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear); |
| 145 |
int counter = 0; |
| 146 |
|
| 147 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) { |
| 148 |
_cleanup_free_ char *name = NULL; |
| 149 |
ASSERT_OK(asprintf(&name, "priority-%zu", i)); |
| 150 |
ASSERT_OK(sd_fiber_new(e, name, priority_fiber, &counter, NULL, &fibers[i])); |
| 151 |
} |
| 152 |
|
| 153 |
ASSERT_OK(sd_event_loop(e)); |
| 154 |
|
| 155 |
/* The fibers have the same priorities, so we expect all of them to run once first, and then they'll |
| 156 |
* all run again another time, so they should all return the same value. */ |
| 157 |
|
| 158 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) |
| 159 |
ASSERT_EQ(sd_future_result(fibers[i]), (int) 5); |
| 160 |
} |
| 161 |
|
| 162 |
static int error_fiber(void *userdata) { |
| 163 |
return -ENOENT; |
| 164 |
} |
| 165 |
|
| 166 |
TEST(fiber_error_return) { |
| 167 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 168 |
ASSERT_OK(sd_event_new(&e)); |
| 169 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 170 |
|
| 171 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 172 |
ASSERT_OK(sd_fiber_new(e, "error", error_fiber, NULL, NULL, &f)); |
| 173 |
|
| 174 |
ASSERT_OK(sd_event_loop(e)); |
| 175 |
ASSERT_EQ(sd_future_result(f), -ENOENT); |
| 176 |
} |
| 177 |
|
| 178 |
static int cancel_fiber(void *userdata) { |
| 179 |
return sd_fiber_yield(); |
| 180 |
} |
| 181 |
|
| 182 |
TEST(fiber_cancel_basic) { |
| 183 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 184 |
ASSERT_OK(sd_event_new(&e)); |
| 185 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 186 |
|
| 187 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 188 |
int value = 42; |
| 189 |
ASSERT_OK(sd_fiber_new(e, "cancel", cancel_fiber, &value, NULL, &f)); |
| 190 |
|
| 191 |
ASSERT_OK(sd_future_cancel(f)); |
| 192 |
ASSERT_OK(sd_event_loop(e)); |
| 193 |
ASSERT_ERROR(sd_future_result(f), ECANCELED); |
| 194 |
} |
| 195 |
|
| 196 |
static int fiber_that_yields(void *userdata) { |
| 197 |
int *yield_count = userdata; |
| 198 |
int r; |
| 199 |
|
| 200 |
for (int i = 0; i < 5; i++) { |
| 201 |
(*yield_count)++; |
| 202 |
r = sd_fiber_yield(); |
| 203 |
if (r < 0) |
| 204 |
return r; /* Propagate cancellation error */ |
| 205 |
} |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
/* Test: fiber_yield() returns error when fiber is cancelled externally */ |
| 211 |
TEST(fiber_cancel_propagation_via_yield) { |
| 212 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 213 |
ASSERT_OK(sd_event_new(&e)); |
| 214 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 215 |
|
| 216 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 217 |
int yield_count = 0; |
| 218 |
ASSERT_OK(sd_fiber_new(e, "yielding", fiber_that_yields, &yield_count, NULL, &f)); |
| 219 |
|
| 220 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 221 |
ASSERT_EQ(yield_count, 1); |
| 222 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 223 |
ASSERT_EQ(yield_count, 2); |
| 224 |
|
| 225 |
ASSERT_OK(sd_future_cancel(f)); |
| 226 |
|
| 227 |
ASSERT_OK(sd_event_loop(e)); |
| 228 |
|
| 229 |
/* sd_fiber should have been cancelled */ |
| 230 |
ASSERT_ERROR(sd_future_result(f), ECANCELED); |
| 231 |
ASSERT_EQ(yield_count, 2); |
| 232 |
} |
| 233 |
|
| 234 |
/* Test: Cancel a fiber that has already completed */ |
| 235 |
TEST(fiber_cancel_completed) { |
| 236 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 237 |
ASSERT_OK(sd_event_new(&e)); |
| 238 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 239 |
|
| 240 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 241 |
int value = 42; |
| 242 |
ASSERT_OK(sd_fiber_new(e, "simple", simple_fiber, &value, NULL, &f)); |
| 243 |
|
| 244 |
/* Run the fiber to completion */ |
| 245 |
ASSERT_OK(sd_event_loop(e)); |
| 246 |
|
| 247 |
/* Canceling a completed fiber should be a no-op */ |
| 248 |
ASSERT_OK(sd_future_cancel(f)); |
| 249 |
ASSERT_EQ(sd_future_result(f), 42); |
| 250 |
} |
| 251 |
|
| 252 |
static int multiple_yield_fiber(void *userdata) { |
| 253 |
int *counter = userdata; |
| 254 |
int r; |
| 255 |
|
| 256 |
for (int i = 0; i < 3; i++) { |
| 257 |
(*counter)++; |
| 258 |
r = sd_fiber_yield(); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
} |
| 262 |
|
| 263 |
return 0; |
| 264 |
} |
| 265 |
|
| 266 |
/* Test: Cancel one fiber among multiple */ |
| 267 |
TEST(fiber_cancel_one_of_many) { |
| 268 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 269 |
ASSERT_OK(sd_event_new(&e)); |
| 270 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 271 |
|
| 272 |
sd_future *fibers[3] = {}; |
| 273 |
CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear); |
| 274 |
int counters[3] = {0, 0, 0}; |
| 275 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) |
| 276 |
ASSERT_OK(sd_fiber_new(e, "multiple-yield", multiple_yield_fiber, &counters[i], NULL, &fibers[i])); |
| 277 |
|
| 278 |
/* Run one iteration - all fibers yield after incrementing once */ |
| 279 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 280 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 281 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 282 |
ASSERT_EQ(counters[0], 1); |
| 283 |
ASSERT_EQ(counters[1], 1); |
| 284 |
ASSERT_EQ(counters[2], 1); |
| 285 |
|
| 286 |
/* Cancel the second fiber */ |
| 287 |
ASSERT_OK(sd_future_cancel(fibers[1])); |
| 288 |
|
| 289 |
/* Run to completion */ |
| 290 |
ASSERT_OK(sd_event_loop(e)); |
| 291 |
|
| 292 |
/* First and third fibers should complete normally */ |
| 293 |
ASSERT_EQ(counters[0], 3); |
| 294 |
ASSERT_EQ(counters[2], 3); |
| 295 |
ASSERT_EQ(sd_future_result(fibers[0]), 0); |
| 296 |
ASSERT_EQ(sd_future_result(fibers[2]), 0); |
| 297 |
|
| 298 |
/* Second fiber should be canceled with counter at 1 */ |
| 299 |
ASSERT_EQ(counters[1], 1); |
| 300 |
ASSERT_EQ(sd_future_result(fibers[1]), -ECANCELED); |
| 301 |
} |
| 302 |
|
| 303 |
/* Test: sd_fiber_await() - wait for a fiber to complete */ |
| 304 |
static int slow_fiber(void *userdata) { |
| 305 |
int *counter = userdata; |
| 306 |
|
| 307 |
for (int i = 0; i < 3; i++) { |
| 308 |
(*counter)++; |
| 309 |
sd_fiber_yield(); |
| 310 |
} |
| 311 |
|
| 312 |
return 42; |
| 313 |
} |
| 314 |
|
| 315 |
static int waiting_fiber(void *userdata) { |
| 316 |
sd_future *target = userdata; |
| 317 |
int r; |
| 318 |
|
| 319 |
r = sd_fiber_await(target); |
| 320 |
if (r < 0) |
| 321 |
return r; |
| 322 |
|
| 323 |
r = sd_future_result(target); |
| 324 |
return r == 42 ? 0 : -EIO; |
| 325 |
} |
| 326 |
|
| 327 |
TEST(fiber_wait_for_basic) { |
| 328 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 329 |
ASSERT_OK(sd_event_new(&e)); |
| 330 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 331 |
|
| 332 |
/* Create target fiber with lower priority (runs second) */ |
| 333 |
_cleanup_(sd_future_unrefp) sd_future *target = NULL, *waiter = NULL; |
| 334 |
int counter = 0; |
| 335 |
ASSERT_OK(sd_fiber_new(e, "slow", slow_fiber, &counter, NULL, &target)); |
| 336 |
ASSERT_OK(sd_future_set_priority(target, 1)); |
| 337 |
|
| 338 |
/* Create waiter fiber with higher priority (runs first) */ |
| 339 |
ASSERT_OK(sd_fiber_new(e, "waiting", waiting_fiber, target, NULL, &waiter)); |
| 340 |
ASSERT_OK(sd_future_set_priority(waiter, 0)); |
| 341 |
|
| 342 |
ASSERT_OK(sd_event_loop(e)); |
| 343 |
|
| 344 |
ASSERT_OK(sd_future_result(waiter)); |
| 345 |
ASSERT_OK_EQ(sd_future_result(target), 42); |
| 346 |
ASSERT_EQ(counter, 3); |
| 347 |
} |
| 348 |
|
| 349 |
/* Test: wait for already completed fiber */ |
| 350 |
static int wait_for_completed_fiber(void *userdata) { |
| 351 |
sd_future *target = userdata; |
| 352 |
int r; |
| 353 |
|
| 354 |
r = sd_fiber_await(target); |
| 355 |
if (r < 0) |
| 356 |
return r; |
| 357 |
|
| 358 |
return sd_future_result(target); |
| 359 |
} |
| 360 |
|
| 361 |
TEST(fiber_wait_for_completed) { |
| 362 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 363 |
ASSERT_OK(sd_event_new(&e)); |
| 364 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 365 |
|
| 366 |
_cleanup_(sd_future_unrefp) sd_future *target = NULL, *waiter = NULL; |
| 367 |
int value = 100; |
| 368 |
|
| 369 |
/* Create target fiber with higher priority (runs first) */ |
| 370 |
ASSERT_OK(sd_fiber_new(e, "simple", simple_fiber, &value, NULL, &target)); |
| 371 |
ASSERT_OK(sd_future_set_priority(target, 0)); |
| 372 |
/* Create waiter fiber with lower priority (runs second, after target completes) */ |
| 373 |
ASSERT_OK(sd_fiber_new(e, "wait-for-completed", wait_for_completed_fiber, target, NULL, &waiter)); |
| 374 |
ASSERT_OK(sd_future_set_priority(waiter, 1)); |
| 375 |
|
| 376 |
ASSERT_OK(sd_event_loop(e)); |
| 377 |
|
| 378 |
ASSERT_OK_EQ(sd_future_result(waiter), 100); |
| 379 |
ASSERT_OK_EQ(sd_future_result(target), 100); |
| 380 |
} |
| 381 |
|
| 382 |
/* Test: awaiting an already-resolved future returns the future's result directly */ |
| 383 |
static int await_resolved_fiber(void *userdata) { |
| 384 |
sd_future *target = userdata; |
| 385 |
|
| 386 |
ASSERT_EQ((int) sd_future_state(target), (int) SD_FUTURE_RESOLVED); |
| 387 |
ASSERT_OK_EQ(sd_fiber_await(target), 77); |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
TEST(fiber_await_resolved_returns_result) { |
| 392 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 393 |
ASSERT_OK(sd_event_new(&e)); |
| 394 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 395 |
|
| 396 |
_cleanup_(sd_future_unrefp) sd_future *target = NULL, *waiter = NULL; |
| 397 |
int value = 77; |
| 398 |
|
| 399 |
/* Higher-priority target runs to completion before the waiter starts. */ |
| 400 |
ASSERT_OK(sd_fiber_new(e, "target", simple_fiber, &value, NULL, &target)); |
| 401 |
ASSERT_OK(sd_future_set_priority(target, 0)); |
| 402 |
ASSERT_OK(sd_fiber_new(e, "await-resolved", await_resolved_fiber, target, NULL, &waiter)); |
| 403 |
ASSERT_OK(sd_future_set_priority(waiter, 1)); |
| 404 |
|
| 405 |
ASSERT_OK(sd_event_loop(e)); |
| 406 |
|
| 407 |
ASSERT_OK(sd_future_result(waiter)); |
| 408 |
ASSERT_OK_EQ(sd_future_result(target), 77); |
| 409 |
} |
| 410 |
|
| 411 |
/* Test: wait for cancelled fiber */ |
| 412 |
static int wait_for_cancelled_fiber(void *userdata) { |
| 413 |
sd_future *target = userdata; |
| 414 |
int r; |
| 415 |
|
| 416 |
r = sd_fiber_await(target); |
| 417 |
if (r < 0) |
| 418 |
return r; |
| 419 |
|
| 420 |
return sd_future_result(target); |
| 421 |
} |
| 422 |
|
| 423 |
TEST(fiber_wait_for_cancelled) { |
| 424 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 425 |
ASSERT_OK(sd_event_new(&e)); |
| 426 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 427 |
|
| 428 |
_cleanup_(sd_future_unrefp) sd_future *target = NULL, *waiter = NULL; |
| 429 |
int counter = 0; |
| 430 |
ASSERT_OK(sd_fiber_new(e, "yielding", fiber_that_yields, &counter, NULL, &target)); |
| 431 |
ASSERT_OK(sd_fiber_new(e, "wait-for-cancelled", wait_for_cancelled_fiber, target, NULL, &waiter)); |
| 432 |
|
| 433 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 434 |
ASSERT_OK_POSITIVE(sd_event_run(e, 0)); |
| 435 |
|
| 436 |
ASSERT_OK(sd_future_cancel(target)); |
| 437 |
|
| 438 |
ASSERT_OK(sd_event_loop(e)); |
| 439 |
|
| 440 |
ASSERT_ERROR(sd_future_result(waiter), ECANCELED); |
| 441 |
ASSERT_ERROR(sd_future_result(target), ECANCELED); |
| 442 |
} |
| 443 |
|
| 444 |
/* Test: multiple fibers waiting for the same target */ |
| 445 |
static int multi_waiter_fiber(void *userdata) { |
| 446 |
sd_future *target = userdata; |
| 447 |
int r; |
| 448 |
|
| 449 |
r = sd_fiber_await(target); |
| 450 |
if (r < 0) |
| 451 |
return r; |
| 452 |
|
| 453 |
return sd_future_result(target); |
| 454 |
} |
| 455 |
|
| 456 |
TEST(fiber_wait_for_multiple_waiters) { |
| 457 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 458 |
ASSERT_OK(sd_event_new(&e)); |
| 459 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 460 |
|
| 461 |
_cleanup_(sd_future_unrefp) sd_future *target = NULL; |
| 462 |
int counter = 0; |
| 463 |
ASSERT_OK(sd_fiber_new(e, "slow", slow_fiber, &counter, NULL, &target)); |
| 464 |
|
| 465 |
sd_future *waiters[3] = {}; |
| 466 |
CLEANUP_ELEMENTS(waiters, sd_future_unref_array_clear); |
| 467 |
for (size_t i = 0; i < ELEMENTSOF(waiters); i++) |
| 468 |
ASSERT_OK(sd_fiber_new(e, "multi-waiter", multi_waiter_fiber, target, NULL, &waiters[i])); |
| 469 |
|
| 470 |
ASSERT_OK(sd_event_loop(e)); |
| 471 |
|
| 472 |
for (size_t i = 0; i < ELEMENTSOF(waiters); i++) |
| 473 |
ASSERT_OK_EQ(sd_future_result(waiters[i]), 42); |
| 474 |
|
| 475 |
ASSERT_OK_EQ(sd_future_result(target), 42); |
| 476 |
ASSERT_EQ(counter, 3); |
| 477 |
} |
| 478 |
|
| 479 |
/* Test: chain of waiting fibers */ |
| 480 |
static int chain_waiter_fiber(void *userdata) { |
| 481 |
sd_future *target = userdata; |
| 482 |
int r; |
| 483 |
|
| 484 |
r = sd_fiber_await(target); |
| 485 |
if (r < 0) |
| 486 |
return r; |
| 487 |
|
| 488 |
r = sd_future_result(target); |
| 489 |
return r + 1; |
| 490 |
} |
| 491 |
|
| 492 |
TEST(fiber_wait_for_chain) { |
| 493 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 494 |
ASSERT_OK(sd_event_new(&e)); |
| 495 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 496 |
|
| 497 |
sd_future *fibers[5] = {}; |
| 498 |
CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear); |
| 499 |
int value = 10; |
| 500 |
|
| 501 |
ASSERT_OK(sd_fiber_new(e, "simple", simple_fiber, &value, NULL, &fibers[0])); |
| 502 |
|
| 503 |
/* Each subsequent fiber waits for the previous and adds 1 */ |
| 504 |
for (size_t i = 1; i < ELEMENTSOF(fibers); i++) |
| 505 |
ASSERT_OK(sd_fiber_new(e, "chain-waiter", chain_waiter_fiber, fibers[i - 1], NULL, &fibers[i])); |
| 506 |
|
| 507 |
ASSERT_OK(sd_event_loop(e)); |
| 508 |
|
| 509 |
/* Check results: 10, 11, 12, 13, 14 */ |
| 510 |
for (size_t i = 0; i < ELEMENTSOF(fibers); i++) |
| 511 |
ASSERT_OK_EQ(sd_future_result(fibers[i]), 10 + (int) i); |
| 512 |
} |
| 513 |
|
| 514 |
static int nested_run_inner_fiber(void *userdata) { |
| 515 |
int *counter = ASSERT_PTR(userdata); |
| 516 |
|
| 517 |
(*counter)++; |
| 518 |
int r = sd_fiber_yield(); |
| 519 |
if (r < 0) |
| 520 |
return r; |
| 521 |
(*counter)++; |
| 522 |
|
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
static int nested_run_outer_fiber(void *userdata) { |
| 527 |
int *counter = ASSERT_PTR(userdata); |
| 528 |
_cleanup_(sd_event_unrefp) sd_event *inner = NULL; |
| 529 |
_cleanup_(sd_future_unrefp) sd_future *nested = NULL; |
| 530 |
int r; |
| 531 |
|
| 532 |
/* Yield once before the nested loop: this forces the outer fiber to later resume through its own |
| 533 |
* siglongjmp back to its resume_context after the inner fiber_run() has executed, which is |
| 534 |
* exactly the path that breaks when the resume context is stored thread-globally instead of |
| 535 |
* per-fiber. */ |
| 536 |
r = sd_fiber_yield(); |
| 537 |
if (r < 0) |
| 538 |
return r; |
| 539 |
|
| 540 |
r = sd_event_new(&inner); |
| 541 |
if (r < 0) |
| 542 |
return r; |
| 543 |
|
| 544 |
r = sd_event_set_exit_on_idle(inner, true); |
| 545 |
if (r < 0) |
| 546 |
return r; |
| 547 |
|
| 548 |
/* Spawn a fiber on the inner event loop. Driving it via sd_event_loop(inner) causes fiber_run() to |
| 549 |
* be invoked while we are already executing inside fiber_run() for the outer fiber. */ |
| 550 |
r = sd_fiber_new(inner, "inner", nested_run_inner_fiber, counter, NULL, &nested); |
| 551 |
if (r < 0) |
| 552 |
return r; |
| 553 |
|
| 554 |
r = sd_event_loop(inner); |
| 555 |
if (r < 0) |
| 556 |
return r; |
| 557 |
|
| 558 |
r = sd_future_result(nested); |
| 559 |
if (r < 0) |
| 560 |
return r; |
| 561 |
|
| 562 |
/* Yield again after the inner loop has returned. If the outer fiber's resume context was clobbered |
| 563 |
* by the nested fiber_run(), the siglongjmp underneath this yield would jump into an already |
| 564 |
* unwound stack frame. */ |
| 565 |
return sd_fiber_yield(); |
| 566 |
} |
| 567 |
|
| 568 |
TEST(fiber_nested_run) { |
| 569 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 570 |
ASSERT_OK(sd_event_new(&e)); |
| 571 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 572 |
|
| 573 |
_cleanup_(sd_future_unrefp) sd_future *outer = NULL; |
| 574 |
int counter = 0; |
| 575 |
ASSERT_OK(sd_fiber_new(e, "outer", nested_run_outer_fiber, &counter, NULL, &outer)); |
| 576 |
|
| 577 |
ASSERT_OK(sd_event_loop(e)); |
| 578 |
ASSERT_OK(sd_future_result(outer)); |
| 579 |
|
| 580 |
/* The inner fiber incremented the counter once before yielding and once after resuming. */ |
| 581 |
ASSERT_EQ(counter, 2); |
| 582 |
} |
| 583 |
|
| 584 |
static int nested_current_check_inner_fiber(void *userdata) { |
| 585 |
sd_future **slots = ASSERT_PTR(userdata); |
| 586 |
|
| 587 |
slots[1] = sd_fiber_get_current(); |
| 588 |
int r = sd_fiber_yield(); |
| 589 |
if (r < 0) |
| 590 |
return r; |
| 591 |
/* After resuming, the current fiber must still be us, not the outer fiber that was current when |
| 592 |
* fiber_run() re-entered. */ |
| 593 |
if (sd_fiber_get_current() != slots[1]) |
| 594 |
return -EBADF; |
| 595 |
|
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
static int nested_current_check_outer_fiber(void *userdata) { |
| 600 |
sd_future **slots = ASSERT_PTR(userdata); |
| 601 |
_cleanup_(sd_event_unrefp) sd_event *inner = NULL; |
| 602 |
_cleanup_(sd_future_unrefp) sd_future *nested = NULL; |
| 603 |
int r; |
| 604 |
|
| 605 |
slots[0] = sd_fiber_get_current(); |
| 606 |
|
| 607 |
r = sd_event_new(&inner); |
| 608 |
if (r < 0) |
| 609 |
return r; |
| 610 |
|
| 611 |
r = sd_event_set_exit_on_idle(inner, true); |
| 612 |
if (r < 0) |
| 613 |
return r; |
| 614 |
|
| 615 |
r = sd_fiber_new(inner, "inner", nested_current_check_inner_fiber, slots, NULL, &nested); |
| 616 |
if (r < 0) |
| 617 |
return r; |
| 618 |
|
| 619 |
r = sd_event_loop(inner); |
| 620 |
if (r < 0) |
| 621 |
return r; |
| 622 |
|
| 623 |
r = sd_future_result(nested); |
| 624 |
if (r < 0) |
| 625 |
return r; |
| 626 |
|
| 627 |
/* After the nested fiber_run() has returned, the current fiber must have been restored to the |
| 628 |
* outer fiber rather than left as NULL or pointing at the (now freed) inner fiber. */ |
| 629 |
if (sd_fiber_get_current() != slots[0]) |
| 630 |
return -EBADF; |
| 631 |
|
| 632 |
return 0; |
| 633 |
} |
| 634 |
|
| 635 |
TEST(fiber_nested_run_current_restored) { |
| 636 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 637 |
ASSERT_OK(sd_event_new(&e)); |
| 638 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 639 |
|
| 640 |
sd_future *slots[2] = {}; |
| 641 |
_cleanup_(sd_future_unrefp) sd_future *outer = NULL; |
| 642 |
ASSERT_OK(sd_fiber_new(e, "outer", nested_current_check_outer_fiber, slots, NULL, &outer)); |
| 643 |
|
| 644 |
ASSERT_OK(sd_event_loop(e)); |
| 645 |
ASSERT_OK(sd_future_result(outer)); |
| 646 |
|
| 647 |
ASSERT_NOT_NULL(slots[0]); |
| 648 |
ASSERT_NOT_NULL(slots[1]); |
| 649 |
ASSERT_TRUE(slots[0] != slots[1]); |
| 650 |
} |
| 651 |
|
| 652 |
static int nested_cancellation_fiber(void *userdata) { |
| 653 |
int *counter = ASSERT_PTR(userdata); |
| 654 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *nested = NULL; |
| 655 |
int r; |
| 656 |
|
| 657 |
if (*counter >= 5) |
| 658 |
return sd_fiber_sleep(10 * USEC_PER_SEC); |
| 659 |
|
| 660 |
(*counter)++; |
| 661 |
|
| 662 |
_cleanup_free_ char *name = NULL; |
| 663 |
if (asprintf(&name, "nested-cancellation-%i", *counter) < 0) |
| 664 |
return -ENOMEM; |
| 665 |
|
| 666 |
/* Create a nested fiber within this fiber */ |
| 667 |
r = sd_fiber_new(sd_fiber_get_event(), name, nested_cancellation_fiber, counter, NULL, &nested); |
| 668 |
if (r < 0) |
| 669 |
return r; |
| 670 |
|
| 671 |
/* Wait for the nested fiber to complete */ |
| 672 |
r = sd_fiber_await(nested); |
| 673 |
if (r < 0) |
| 674 |
return r; |
| 675 |
|
| 676 |
/* If we got here without cancellation, verify the nested fiber completed */ |
| 677 |
return sd_future_result(nested); |
| 678 |
} |
| 679 |
|
| 680 |
static int exit_loop_fiber(void *userdata) { |
| 681 |
/* Just exit the event loop, causing the outer fiber to be cancelled */ |
| 682 |
return sd_event_exit(sd_fiber_get_event(), 0); |
| 683 |
} |
| 684 |
|
| 685 |
TEST(fiber_nested_cancellation) { |
| 686 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 687 |
ASSERT_OK(sd_event_new(&e)); |
| 688 |
|
| 689 |
int counter = 0; |
| 690 |
|
| 691 |
/* Create outer fiber with higher priority (runs first) */ |
| 692 |
_cleanup_(sd_future_unrefp) sd_future *outer = NULL; |
| 693 |
ASSERT_OK(sd_fiber_new(e, "outer", nested_cancellation_fiber, &counter, NULL, &outer)); |
| 694 |
|
| 695 |
/* Create exit fiber with lower priority (runs after all nested fibers have suspended) */ |
| 696 |
_cleanup_(sd_future_unrefp) sd_future *exit_fiber = NULL; |
| 697 |
ASSERT_OK(sd_fiber_new(e, "exit-loop", exit_loop_fiber, NULL, NULL, &exit_fiber)); |
| 698 |
ASSERT_OK(sd_future_set_priority(exit_fiber, 1)); |
| 699 |
|
| 700 |
/* Run the event loop - the exit fiber should cause it to exit, |
| 701 |
* which should cancel the outer fiber, which should cancel the nested fiber, and so forth. */ |
| 702 |
ASSERT_OK(sd_event_loop(e)); |
| 703 |
|
| 704 |
/* The exit fiber should have completed successfully */ |
| 705 |
ASSERT_OK(sd_future_result(exit_fiber)); |
| 706 |
|
| 707 |
/* The outer fiber should have been cancelled */ |
| 708 |
ASSERT_ERROR(sd_future_result(outer), ECANCELED); |
| 709 |
|
| 710 |
/* The nested fiber was created and incremented counter once before being cancelled */ |
| 711 |
ASSERT_GT(counter, 0); |
| 712 |
} |
| 713 |
|
| 714 |
static int nested_fiber_cleanup_nested_fiber(void *userdata) { |
| 715 |
int *counter = ASSERT_PTR(userdata); |
| 716 |
int r; |
| 717 |
|
| 718 |
r = sd_fiber_sleep(10 * USEC_PER_SEC); |
| 719 |
if (r == -ECANCELED) |
| 720 |
(*counter)++; |
| 721 |
else if (r < 0) |
| 722 |
return r; |
| 723 |
|
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
static int nested_fiber_cleanup_fiber(void *userdata) { |
| 728 |
_cleanup_(sd_future_cancel_wait_unrefp) sd_future *nested = NULL; |
| 729 |
int r; |
| 730 |
|
| 731 |
/* Create a nested fiber within this fiber. */ |
| 732 |
r = sd_fiber_new(sd_fiber_get_event(), "nested", nested_fiber_cleanup_nested_fiber, userdata, NULL, &nested); |
| 733 |
if (r < 0) |
| 734 |
return r; |
| 735 |
|
| 736 |
/* Yield and then exit, the nested fiber should be cancelled. */ |
| 737 |
return sd_fiber_yield(); |
| 738 |
} |
| 739 |
|
| 740 |
TEST(nested_fiber_cleanup) { |
| 741 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 742 |
ASSERT_OK(sd_event_new(&e)); |
| 743 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 744 |
|
| 745 |
_cleanup_(sd_future_unrefp) sd_future *outer = NULL; |
| 746 |
int counter = 0; |
| 747 |
ASSERT_OK(sd_fiber_new(e, "outer", nested_fiber_cleanup_fiber, &counter, NULL, &outer)); |
| 748 |
|
| 749 |
ASSERT_OK(sd_event_loop(e)); |
| 750 |
|
| 751 |
/* The outer fiber should have finished normally */ |
| 752 |
ASSERT_OK(sd_future_result(outer)); |
| 753 |
|
| 754 |
/* The nested fiber was created and incremented its counter once when it was cancelled. */ |
| 755 |
ASSERT_GT(counter, 0); |
| 756 |
} |
| 757 |
|
| 758 |
static int priority_check_fiber(void *userdata) { |
| 759 |
int64_t *ret = ASSERT_PTR(userdata); |
| 760 |
|
| 761 |
/* Verify that sd_fiber_get_priority() returns the value set via sd_future_set_priority() */ |
| 762 |
ASSERT_OK(sd_fiber_get_priority(ret)); |
| 763 |
|
| 764 |
/* Exercise sd_fiber_sleep() which internally creates a time future. This verifies that the priority |
| 765 |
* is correctly propagated to the time event source (via f->time.source, not f->io.source). */ |
| 766 |
return sd_fiber_sleep(1); |
| 767 |
} |
| 768 |
|
| 769 |
TEST(fiber_priority_get) { |
| 770 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 771 |
ASSERT_OK(sd_event_new(&e)); |
| 772 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 773 |
|
| 774 |
int64_t got_priority = 0; |
| 775 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 776 |
ASSERT_OK(sd_fiber_new(e, "priority-check", priority_check_fiber, &got_priority, NULL, &f)); |
| 777 |
ASSERT_OK(sd_future_set_priority(f, 10)); |
| 778 |
|
| 779 |
ASSERT_OK(sd_event_loop(e)); |
| 780 |
ASSERT_OK(sd_future_result(f)); |
| 781 |
|
| 782 |
/* Verify priority was stored and retrievable */ |
| 783 |
ASSERT_EQ(got_priority, 10); |
| 784 |
} |
| 785 |
|
| 786 |
static int floating_fiber(void *userdata) { |
| 787 |
int *counter = ASSERT_PTR(userdata); |
| 788 |
|
| 789 |
(*counter)++; |
| 790 |
int r = sd_fiber_yield(); |
| 791 |
if (r < 0) |
| 792 |
return r; |
| 793 |
(*counter)++; |
| 794 |
|
| 795 |
return 0; |
| 796 |
} |
| 797 |
|
| 798 |
TEST(fiber_floating) { |
| 799 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 800 |
ASSERT_OK(sd_event_new(&e)); |
| 801 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 802 |
|
| 803 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 804 |
int counter = 0; |
| 805 |
ASSERT_OK(sd_fiber_new(e, "floating", floating_fiber, &counter, NULL, &f)); |
| 806 |
|
| 807 |
ASSERT_OK_ZERO(sd_fiber_get_floating(f)); |
| 808 |
ASSERT_OK(sd_fiber_set_floating(f, true)); |
| 809 |
ASSERT_OK_POSITIVE(sd_fiber_get_floating(f)); |
| 810 |
|
| 811 |
/* Drop our handle: the floating ref keeps the future alive until the fiber resolves, after |
| 812 |
* which the self-unref frees it. If this didn't work we'd either leak (visible under ASan) or |
| 813 |
* trip fiber_free()'s "state == COMPLETED" assertion. */ |
| 814 |
f = sd_future_unref(f); |
| 815 |
|
| 816 |
ASSERT_OK(sd_event_loop(e)); |
| 817 |
ASSERT_EQ(counter, 2); |
| 818 |
} |
| 819 |
|
| 820 |
static int drop_extra_ref(sd_future *f) { |
| 821 |
/* Drop an extra ref the test installed before the callback fires. After this returns, the |
| 822 |
* floating self-ref is the only thing keeping the future alive — exercising the path where |
| 823 |
* the floating unref in fiber_run() is the last unref. */ |
| 824 |
sd_future_unref(f); |
| 825 |
return 0; |
| 826 |
} |
| 827 |
|
| 828 |
TEST(fiber_floating_callback_drops_ref) { |
| 829 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 830 |
ASSERT_OK(sd_event_new(&e)); |
| 831 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 832 |
|
| 833 |
sd_future *f = NULL; |
| 834 |
int counter = 0; |
| 835 |
ASSERT_OK(sd_fiber_new(e, "floating-cb", floating_fiber, &counter, NULL, &f)); |
| 836 |
|
| 837 |
ASSERT_OK(sd_fiber_set_floating(f, true)); |
| 838 |
|
| 839 |
/* Bump the ref for the callback to drop, then install the callback. */ |
| 840 |
sd_future_ref(f); |
| 841 |
ASSERT_OK(sd_future_set_callback(f, drop_extra_ref, NULL)); |
| 842 |
|
| 843 |
/* Drop our handle. Refs remaining: floating self-ref + the extra ref the callback will drop. */ |
| 844 |
f = sd_future_unref(f); |
| 845 |
|
| 846 |
ASSERT_OK(sd_event_loop(e)); |
| 847 |
ASSERT_EQ(counter, 2); |
| 848 |
} |
| 849 |
|
| 850 |
TEST(fiber_floating_toggle) { |
| 851 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 852 |
ASSERT_OK(sd_event_new(&e)); |
| 853 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 854 |
|
| 855 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 856 |
int counter = 0; |
| 857 |
ASSERT_OK(sd_fiber_new(e, "floating-toggle", floating_fiber, &counter, NULL, &f)); |
| 858 |
|
| 859 |
/* Toggling floating on and off again should leave the refcount unchanged: set_floating(true) |
| 860 |
* takes a ref and set_floating(false) drops it. If the accounting were off, the subsequent |
| 861 |
* event loop would either free the future while the fiber still runs (fiber_free assertion) |
| 862 |
* or leak it. */ |
| 863 |
ASSERT_OK(sd_fiber_set_floating(f, true)); |
| 864 |
ASSERT_OK(sd_fiber_set_floating(f, false)); |
| 865 |
ASSERT_OK_ZERO(sd_fiber_get_floating(f)); |
| 866 |
|
| 867 |
/* Setting floating to the same value twice should be a no-op. */ |
| 868 |
ASSERT_OK(sd_fiber_set_floating(f, false)); |
| 869 |
ASSERT_OK(sd_fiber_set_floating(f, true)); |
| 870 |
ASSERT_OK(sd_fiber_set_floating(f, true)); |
| 871 |
|
| 872 |
/* Drop our handle; the still-floating ref drives cleanup. */ |
| 873 |
f = sd_future_unref(f); |
| 874 |
|
| 875 |
ASSERT_OK(sd_event_loop(e)); |
| 876 |
ASSERT_EQ(counter, 2); |
| 877 |
} |
| 878 |
|
| 879 |
/* Test: SD_FIBER_TIMEOUT scope expires while the fiber is suspended with no other wakeup source. */ |
| 880 |
static int timeout_suspend_fiber(void *userdata) { |
| 881 |
SD_FIBER_TIMEOUT(50 * USEC_PER_MSEC); |
| 882 |
|
| 883 |
/* Plain suspend with no other future to wake us — only the deadline timer can resume. */ |
| 884 |
return sd_fiber_suspend(); |
| 885 |
} |
| 886 |
|
| 887 |
TEST(fiber_timeout_suspend_expires) { |
| 888 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 889 |
ASSERT_OK(sd_event_new(&e)); |
| 890 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 891 |
|
| 892 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 893 |
ASSERT_OK(sd_fiber_new(e, "timeout-suspend", timeout_suspend_fiber, NULL, NULL, &f)); |
| 894 |
|
| 895 |
ASSERT_OK(sd_event_loop(e)); |
| 896 |
ASSERT_ERROR(sd_future_result(f), ETIME); |
| 897 |
} |
| 898 |
|
| 899 |
/* Test: SD_FIBER_TIMEOUT scope around a sleep that finishes before the deadline expires; the |
| 900 |
* cleanup must cancel the timer cleanly without leaving a stale wakeup. */ |
| 901 |
static int timeout_in_time_fiber(void *userdata) { |
| 902 |
SD_FIBER_TIMEOUT(1 * USEC_PER_SEC); |
| 903 |
return sd_fiber_sleep(10 * USEC_PER_MSEC); |
| 904 |
} |
| 905 |
|
| 906 |
TEST(fiber_timeout_sleep_in_time) { |
| 907 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 908 |
ASSERT_OK(sd_event_new(&e)); |
| 909 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 910 |
|
| 911 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 912 |
ASSERT_OK(sd_fiber_new(e, "in-time", timeout_in_time_fiber, NULL, NULL, &f)); |
| 913 |
|
| 914 |
ASSERT_OK(sd_event_loop(e)); |
| 915 |
ASSERT_OK_ZERO(sd_future_result(f)); |
| 916 |
} |
| 917 |
|
| 918 |
/* Test: SD_FIBER_TIMEOUT(USEC_INFINITY) is a no-op — no timer is created and the fiber completes |
| 919 |
* normally. */ |
| 920 |
static int timeout_infinite_fiber(void *userdata) { |
| 921 |
SD_FIBER_TIMEOUT(USEC_INFINITY); |
| 922 |
return sd_fiber_sleep(10 * USEC_PER_MSEC); |
| 923 |
} |
| 924 |
|
| 925 |
TEST(fiber_timeout_infinite_no_op) { |
| 926 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 927 |
ASSERT_OK(sd_event_new(&e)); |
| 928 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 929 |
|
| 930 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 931 |
ASSERT_OK(sd_fiber_new(e, "infinite", timeout_infinite_fiber, NULL, NULL, &f)); |
| 932 |
|
| 933 |
ASSERT_OK(sd_event_loop(e)); |
| 934 |
ASSERT_OK_ZERO(sd_future_result(f)); |
| 935 |
} |
| 936 |
|
| 937 |
/* Test: SD_FIBER_WITH_TIMEOUT block form returns -ETIME from the suspend inside it. */ |
| 938 |
static int with_timeout_block_fiber(void *userdata) { |
| 939 |
int r = 0; |
| 940 |
SD_FIBER_WITH_TIMEOUT(50 * USEC_PER_MSEC) |
| 941 |
r = sd_fiber_suspend(); |
| 942 |
return r; |
| 943 |
} |
| 944 |
|
| 945 |
TEST(fiber_with_timeout_block) { |
| 946 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 947 |
ASSERT_OK(sd_event_new(&e)); |
| 948 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 949 |
|
| 950 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 951 |
ASSERT_OK(sd_fiber_new(e, "with-timeout", with_timeout_block_fiber, NULL, NULL, &f)); |
| 952 |
|
| 953 |
ASSERT_OK(sd_event_loop(e)); |
| 954 |
ASSERT_ERROR(sd_future_result(f), ETIME); |
| 955 |
} |
| 956 |
|
| 957 |
/* Test: nested SD_FIBER_TIMEOUT — inner scope's timer fires first; once we're back in just the |
| 958 |
* outer scope, suspending again must time out via the still-armed outer timer. */ |
| 959 |
static int nested_timeout_fiber(void *userdata) { |
| 960 |
int *fired = ASSERT_PTR(userdata); |
| 961 |
|
| 962 |
SD_FIBER_TIMEOUT(50 * USEC_PER_MSEC); /* outer */ |
| 963 |
|
| 964 |
SD_FIBER_WITH_TIMEOUT(20 * USEC_PER_MSEC) { /* inner — expires first */ |
| 965 |
int r = sd_fiber_suspend(); |
| 966 |
if (r != -ETIME) |
| 967 |
return -ENOTRECOVERABLE; |
| 968 |
(*fired)++; |
| 969 |
} |
| 970 |
|
| 971 |
/* Inner scope is gone, but the outer timer is still armed (it only used ~20ms of its |
| 972 |
* 100ms budget). Suspending again must eventually wake us with -ETIME. */ |
| 973 |
int r = sd_fiber_suspend(); |
| 974 |
if (r != -ETIME) |
| 975 |
return -ENOTRECOVERABLE; |
| 976 |
(*fired)++; |
| 977 |
|
| 978 |
return 0; |
| 979 |
} |
| 980 |
|
| 981 |
TEST(fiber_timeout_nested) { |
| 982 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 983 |
ASSERT_OK(sd_event_new(&e)); |
| 984 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 985 |
|
| 986 |
int fired = 0; |
| 987 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 988 |
ASSERT_OK(sd_fiber_new(e, "nested-timeout", nested_timeout_fiber, &fired, NULL, &f)); |
| 989 |
|
| 990 |
ASSERT_OK(sd_event_loop(e)); |
| 991 |
ASSERT_OK_ZERO(sd_future_result(f)); |
| 992 |
ASSERT_EQ(fired, 2); |
| 993 |
} |
| 994 |
|
| 995 |
/* Test: signal mask is per-thread, not per-fiber. Changes one fiber makes via pthread_sigmask |
| 996 |
* must be visible to other fibers on the same thread, both while the modifying fiber is |
| 997 |
* suspended and after it resumes. The fiber switch (sigsetjmp/siglongjmp with savesigs=0) |
| 998 |
* deliberately doesn't save or restore the mask. */ |
| 999 |
static int sigmask_peer_fiber(void *userdata) { |
| 1000 |
sigset_t set, current; |
| 1001 |
|
| 1002 |
/* The waiter blocked SIGUSR1 before await'ing us; the per-thread mask should still |
| 1003 |
* have it blocked here. */ |
| 1004 |
ASSERT_OK_ZERO(-pthread_sigmask(SIG_SETMASK, NULL, ¤t)); |
| 1005 |
ASSERT_TRUE(sigismember(¤t, SIGUSR1)); |
| 1006 |
|
| 1007 |
ASSERT_OK(sigemptyset(&set)); |
| 1008 |
ASSERT_OK(sigaddset(&set, SIGUSR1)); |
| 1009 |
ASSERT_OK_ZERO(-pthread_sigmask(SIG_UNBLOCK, &set, NULL)); |
| 1010 |
|
| 1011 |
return 0; |
| 1012 |
} |
| 1013 |
|
| 1014 |
static int sigmask_waiter_fiber(void *userdata) { |
| 1015 |
sd_future *peer = ASSERT_PTR(userdata); |
| 1016 |
sigset_t set, current; |
| 1017 |
|
| 1018 |
ASSERT_OK(sigemptyset(&set)); |
| 1019 |
ASSERT_OK(sigaddset(&set, SIGUSR1)); |
| 1020 |
ASSERT_OK_ZERO(-pthread_sigmask(SIG_BLOCK, &set, NULL)); |
| 1021 |
|
| 1022 |
ASSERT_OK_ZERO(-pthread_sigmask(SIG_SETMASK, NULL, ¤t)); |
| 1023 |
ASSERT_TRUE(sigismember(¤t, SIGUSR1)); |
| 1024 |
|
| 1025 |
int r = sd_fiber_await(peer); |
| 1026 |
if (r < 0) |
| 1027 |
return r; |
| 1028 |
|
| 1029 |
/* The peer unblocked SIGUSR1 while we were suspended. The change is per-thread, so |
| 1030 |
* we must observe it here. */ |
| 1031 |
ASSERT_OK_ZERO(-pthread_sigmask(SIG_SETMASK, NULL, ¤t)); |
| 1032 |
ASSERT_FALSE(sigismember(¤t, SIGUSR1)); |
| 1033 |
|
| 1034 |
return 0; |
| 1035 |
} |
| 1036 |
|
| 1037 |
TEST(fiber_signal_mask_is_per_thread) { |
| 1038 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1039 |
ASSERT_OK(sd_event_new(&e)); |
| 1040 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1041 |
|
| 1042 |
sigset_t saved; |
| 1043 |
ASSERT_OK_ZERO(-pthread_sigmask(SIG_SETMASK, NULL, &saved)); |
| 1044 |
|
| 1045 |
_cleanup_(sd_future_unrefp) sd_future *waiter = NULL, *peer = NULL; |
| 1046 |
ASSERT_OK(sd_fiber_new(e, "sigmask-peer", sigmask_peer_fiber, NULL, NULL, &peer)); |
| 1047 |
ASSERT_OK(sd_future_set_priority(peer, 1)); |
| 1048 |
ASSERT_OK(sd_fiber_new(e, "sigmask-waiter", sigmask_waiter_fiber, peer, NULL, &waiter)); |
| 1049 |
ASSERT_OK(sd_future_set_priority(waiter, 0)); |
| 1050 |
|
| 1051 |
ASSERT_OK(sd_event_loop(e)); |
| 1052 |
ASSERT_OK(sd_future_result(waiter)); |
| 1053 |
ASSERT_OK(sd_future_result(peer)); |
| 1054 |
|
| 1055 |
ASSERT_OK_ZERO(-pthread_sigmask(SIG_SETMASK, &saved, NULL)); |
| 1056 |
} |
| 1057 |
|
| 1058 |
/* Test: log context is per-fiber. fiber_run() swaps the thread-local log context (and prefix) with |
| 1059 |
* a per-fiber stash on entry and exit, so fields pushed by one fiber must not leak into another |
| 1060 |
* fiber that runs while the first is suspended, and must be restored when the first resumes. */ |
| 1061 |
static int log_context_peer_fiber(void *userdata) { |
| 1062 |
size_t *peer_observed = ASSERT_PTR(userdata); |
| 1063 |
|
| 1064 |
/* The waiter pushed a field before await'ing us. If log context were shared across fibers, |
| 1065 |
* we would observe it here. Record what we see and let the caller verify. */ |
| 1066 |
*peer_observed = log_context_num_fields(); |
| 1067 |
|
| 1068 |
return 0; |
| 1069 |
} |
| 1070 |
|
| 1071 |
static int log_context_waiter_fiber(void *userdata) { |
| 1072 |
sd_future *peer = ASSERT_PTR(userdata); |
| 1073 |
|
| 1074 |
size_t before_push = log_context_num_fields(); |
| 1075 |
|
| 1076 |
LOG_CONTEXT_PUSH("WAITER=here"); |
| 1077 |
size_t after_push = log_context_num_fields(); |
| 1078 |
if (after_push != before_push + 1) |
| 1079 |
return -EBADF; |
| 1080 |
|
| 1081 |
int r = sd_fiber_await(peer); |
| 1082 |
if (r < 0) |
| 1083 |
return r; |
| 1084 |
|
| 1085 |
/* Our pushed field must be visible again after the peer ran and resumed us. */ |
| 1086 |
if (log_context_num_fields() != after_push) |
| 1087 |
return -EBADF; |
| 1088 |
|
| 1089 |
return 0; |
| 1090 |
} |
| 1091 |
|
| 1092 |
TEST(fiber_log_context_per_fiber) { |
| 1093 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1094 |
ASSERT_OK(sd_event_new(&e)); |
| 1095 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1096 |
|
| 1097 |
size_t baseline = log_context_num_fields(); |
| 1098 |
|
| 1099 |
size_t peer_observed = 0; |
| 1100 |
_cleanup_(sd_future_unrefp) sd_future *waiter = NULL, *peer = NULL; |
| 1101 |
ASSERT_OK(sd_fiber_new(e, "log-peer", log_context_peer_fiber, &peer_observed, NULL, &peer)); |
| 1102 |
ASSERT_OK(sd_future_set_priority(peer, 1)); |
| 1103 |
ASSERT_OK(sd_fiber_new(e, "log-waiter", log_context_waiter_fiber, peer, NULL, &waiter)); |
| 1104 |
ASSERT_OK(sd_future_set_priority(waiter, 0)); |
| 1105 |
|
| 1106 |
ASSERT_OK(sd_event_loop(e)); |
| 1107 |
ASSERT_OK(sd_future_result(waiter)); |
| 1108 |
ASSERT_OK(sd_future_result(peer)); |
| 1109 |
|
| 1110 |
/* Inside the peer, only the peer's own FIBER= field (pushed by fiber_run) should have been |
| 1111 |
* active — the waiter's WAITER= push must have been swapped out. */ |
| 1112 |
ASSERT_EQ(peer_observed, baseline + 1); |
| 1113 |
|
| 1114 |
/* The thread-local log context should be exactly as it was before the test ran. */ |
| 1115 |
ASSERT_EQ(log_context_num_fields(), baseline); |
| 1116 |
} |
| 1117 |
|
| 1118 |
static int stack_overflow_fiber(void *userdata) { |
| 1119 |
volatile char anchor; |
| 1120 |
size_t pagesz = page_size(); |
| 1121 |
|
| 1122 |
/* Walk one page at a time away from the fiber's current SP toward the guard page, |
| 1123 |
* writing one byte per page until the kernel raises a fatal signal. On downward |
| 1124 |
* stacks we walk to lower addresses (guard at the base); on upward stacks like |
| 1125 |
* hppa we walk to higher addresses (guard at the top of the mapping). The 64 MiB |
| 1126 |
* ceiling is purely a safety net so the test fails loudly instead of looping if |
| 1127 |
* the guard isn't there. */ |
| 1128 |
for (size_t i = 1; i < (64U * U64_MB) / pagesz; i++) { |
| 1129 |
uintptr_t off = i * pagesz; |
| 1130 |
volatile char *p = (volatile char *) (STACK_GROWS_UP |
| 1131 |
? (uintptr_t) &anchor + off |
| 1132 |
: (uintptr_t) &anchor - off); |
| 1133 |
*p = 0; |
| 1134 |
} |
| 1135 |
return 0; |
| 1136 |
} |
| 1137 |
|
| 1138 |
TEST(fiber_stack_guard) { |
| 1139 |
#if HAS_FEATURE_ADDRESS_SANITIZER |
| 1140 |
(void) log_tests_skipped("ASan intercepts deliberate stack OOB writes"); |
| 1141 |
return; |
| 1142 |
#endif |
| 1143 |
#if HAVE_VALGRIND_VALGRIND_H |
| 1144 |
if (RUNNING_ON_VALGRIND) { |
| 1145 |
(void) log_tests_skipped("Valgrind intercepts deliberate stack OOB writes"); |
| 1146 |
return; |
| 1147 |
} |
| 1148 |
#endif |
| 1149 |
|
| 1150 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 1151 |
int r = pidref_safe_fork("(stack-overflow)", FORK_RESET_SIGNALS|FORK_LOG, &pidref); |
| 1152 |
ASSERT_OK(r); |
| 1153 |
|
| 1154 |
if (r == 0) { |
| 1155 |
_cleanup_(sd_event_unrefp) sd_event *e = NULL; |
| 1156 |
ASSERT_OK(sd_event_new(&e)); |
| 1157 |
ASSERT_OK(sd_event_set_exit_on_idle(e, true)); |
| 1158 |
|
| 1159 |
_cleanup_(sd_future_unrefp) sd_future *f = NULL; |
| 1160 |
ASSERT_OK(sd_fiber_new(e, "overflow", stack_overflow_fiber, NULL, NULL, &f)); |
| 1161 |
(void) sd_event_loop(e); |
| 1162 |
_exit(EXIT_SUCCESS); /* unreachable if the guard fires */ |
| 1163 |
} |
| 1164 |
|
| 1165 |
siginfo_t si; |
| 1166 |
ASSERT_OK(pidref_wait_for_terminate(&pidref, &si)); |
| 1167 |
ASSERT_TRUE(IN_SET(si.si_code, CLD_KILLED, CLD_DUMPED)); |
| 1168 |
ASSERT_TRUE(IN_SET(si.si_status, SIGSEGV, SIGBUS)); |
| 1169 |
} |
| 1170 |
|
| 1171 |
DEFINE_TEST_MAIN(LOG_DEBUG); |