|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Greg Burd |
| 3 | + * |
| 4 | + * This work is open source software, licensed under the terms of the |
| 5 | + * BSD license as described in the LICENSE file in the top-level directory. |
| 6 | + */ |
| 7 | + |
| 8 | +// Exercises the relaxed tgkill(2) and the rt_sigtimedwait(2) timeout path. |
| 9 | +// Built and run on the OSv test image. |
| 10 | + |
| 11 | +#include <sys/syscall.h> |
| 12 | +#include <signal.h> |
| 13 | +#include <unistd.h> |
| 14 | +#include <time.h> |
| 15 | +#include <errno.h> |
| 16 | +#include <pthread.h> |
| 17 | + |
| 18 | +#include <cassert> |
| 19 | +#include <atomic> |
| 20 | +#include <iostream> |
| 21 | + |
| 22 | +static std::atomic<int> g_caught{0}; |
| 23 | +static void handler(int) { g_caught.fetch_add(1, std::memory_order_relaxed); } |
| 24 | + |
| 25 | +static long gettid_() { return syscall(SYS_gettid); } |
| 26 | + |
| 27 | +int main() |
| 28 | +{ |
| 29 | + std::cerr << "Running signal-fills tests\n"; |
| 30 | + |
| 31 | + // ---- tgkill ---- |
| 32 | + struct sigaction sa {}; |
| 33 | + sa.sa_handler = handler; |
| 34 | + sigaction(SIGUSR1, &sa, nullptr); |
| 35 | + |
| 36 | + // tgkill to our own tgid/tid delivers the signal. |
| 37 | + g_caught = 0; |
| 38 | + assert(syscall(SYS_tgkill, getpid(), gettid_(), SIGUSR1) == 0); |
| 39 | + for (int i = 0; i < 100 && g_caught == 0; i++) usleep(1000); |
| 40 | + assert(g_caught >= 1); |
| 41 | + |
| 42 | + // tgkill with a wrong tgid -> ESRCH. |
| 43 | + errno = 0; |
| 44 | + assert(syscall(SYS_tgkill, getpid() + 12345, gettid_(), SIGUSR1) == -1 && errno == ESRCH); |
| 45 | + |
| 46 | + // tgkill to a non-existent tid -> ESRCH (not a crash / not silently OK). |
| 47 | + errno = 0; |
| 48 | + assert(syscall(SYS_tgkill, getpid(), 0x7fffff, SIGUSR1) == -1 && errno == ESRCH); |
| 49 | + |
| 50 | + // tgkill targeting another *live* thread is accepted and the signal is |
| 51 | + // delivered process-wide (OSv's delivery model). |
| 52 | + pthread_t th; |
| 53 | + struct thread_args { |
| 54 | + std::atomic<long> tid{0}; |
| 55 | + std::atomic<bool> stop{false}; |
| 56 | + } targs; |
| 57 | + // targs lives on this stack frame until pthread_join() below, so the child |
| 58 | + // can safely reference it; no heap allocation to leak. |
| 59 | + int rc = pthread_create(&th, nullptr, [](void *p) -> void * { |
| 60 | + auto *a = static_cast<thread_args *>(p); |
| 61 | + a->tid.store(syscall(SYS_gettid)); |
| 62 | + while (!a->stop.load()) usleep(1000); |
| 63 | + return nullptr; |
| 64 | + }, &targs); |
| 65 | + assert(rc == 0); |
| 66 | + while (targs.tid == 0) usleep(1000); |
| 67 | + g_caught = 0; |
| 68 | + assert(syscall(SYS_tgkill, getpid(), targs.tid.load(), SIGUSR1) == 0); |
| 69 | + for (int i = 0; i < 100 && g_caught == 0; i++) usleep(1000); |
| 70 | + assert(g_caught >= 1); |
| 71 | + targs.stop = true; |
| 72 | + pthread_join(th, nullptr); |
| 73 | + |
| 74 | + // ---- rt_sigtimedwait timeout path ---- |
| 75 | + // Block SIGUSR2, then rt_sigtimedwait with a short timeout and no pending |
| 76 | + // signal -> times out with EAGAIN (previously returned ENOSYS). |
| 77 | + sigset_t set; |
| 78 | + sigemptyset(&set); |
| 79 | + sigaddset(&set, SIGUSR2); |
| 80 | + sigprocmask(SIG_BLOCK, &set, nullptr); |
| 81 | + |
| 82 | + struct timespec ts { 0, 150 * 1000 * 1000 }; // 150 ms |
| 83 | + siginfo_t si; |
| 84 | + struct timespec before, after; |
| 85 | + clock_gettime(CLOCK_MONOTONIC, &before); |
| 86 | + errno = 0; |
| 87 | + int r = syscall(SYS_rt_sigtimedwait, &set, &si, &ts, sizeof(sigset_t)); |
| 88 | + clock_gettime(CLOCK_MONOTONIC, &after); |
| 89 | + assert(r == -1 && errno == EAGAIN); |
| 90 | + long long ms = (after.tv_sec - before.tv_sec) * 1000 + |
| 91 | + (after.tv_nsec - before.tv_nsec) / 1000000; |
| 92 | + assert(ms >= 130); // actually waited ~150 ms, did not return ENOSYS instantly |
| 93 | + |
| 94 | + std::cerr << "signal-fills tests PASSED\n"; |
| 95 | + return 0; |
| 96 | +} |
0 commit comments