1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <errno.h>
#include <signal.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <syscall.h>
#include <time.h>
#include <unistd.h>
#include <atomic>
#include "gtest/gtest.h"
#include "absl/flags/flag.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "test/util/cleanup.h"
#include "test/util/logging.h"
#include "test/util/multiprocess_util.h"
#include "test/util/posix_error.h"
#include "test/util/signal_util.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
ABSL_FLAG(bool, timers_test_sleep, false,
"If true, sleep forever instead of running tests.");
using ::testing::_;
using ::testing::AnyOf;
namespace gvisor {
namespace testing {
namespace {
#ifndef CPUCLOCK_PROF
#define CPUCLOCK_PROF 0
#endif // CPUCLOCK_PROF
PosixErrorOr<absl::Duration> ProcessCPUTime(pid_t pid) {
// Use pid-specific CPUCLOCK_PROF, which is the clock used to enforce
// RLIMIT_CPU.
clockid_t clockid = (~static_cast<clockid_t>(pid) << 3) | CPUCLOCK_PROF;
struct timespec ts;
int ret = clock_gettime(clockid, &ts);
if (ret < 0) {
return PosixError(errno, "clock_gettime failed");
}
return absl::DurationFromTimespec(ts);
}
void NoopSignalHandler(int signo) {
TEST_CHECK_MSG(SIGXCPU == signo,
"NoopSigHandler did not receive expected signal");
}
void UninstallingSignalHandler(int signo) {
TEST_CHECK_MSG(SIGXCPU == signo,
"UninstallingSignalHandler did not receive expected signal");
struct sigaction rev_action;
rev_action.sa_handler = SIG_DFL;
rev_action.sa_flags = 0;
sigemptyset(&rev_action.sa_mask);
sigaction(SIGXCPU, &rev_action, nullptr);
}
TEST(TimerTest, ProcessKilledOnCPUSoftLimit) {
constexpr absl::Duration kSoftLimit = absl::Seconds(1);
constexpr absl::Duration kHardLimit = absl::Seconds(3);
struct rlimit cpu_limits;
cpu_limits.rlim_cur = absl::ToInt64Seconds(kSoftLimit);
cpu_limits.rlim_max = absl::ToInt64Seconds(kHardLimit);
int pid = fork();
MaybeSave();
if (pid == 0) {
TEST_PCHECK(setrlimit(RLIMIT_CPU, &cpu_limits) == 0);
MaybeSave();
for (;;) {
}
}
ASSERT_THAT(pid, SyscallSucceeds());
auto c = Cleanup([pid] {
int status;
EXPECT_THAT(waitpid(pid, &status, 0), SyscallSucceedsWithValue(pid));
EXPECT_TRUE(WIFSIGNALED(status));
EXPECT_EQ(WTERMSIG(status), SIGXCPU);
});
// Wait for the child to exit, but do not reap it. This will allow us to check
// its CPU usage while it is zombied.
EXPECT_THAT(waitid(P_PID, pid, nullptr, WEXITED | WNOWAIT),
SyscallSucceeds());
// Assert that the child spent 1s of CPU before getting killed.
//
// We must be careful to use CPUCLOCK_PROF, the same clock used for RLIMIT_CPU
// enforcement, to get correct results. Note that this is slightly different
// from rusage-reported CPU usage:
//
// RLIMIT_CPU, CPUCLOCK_PROF use kernel/sched/cputime.c:thread_group_cputime.
// rusage uses kernel/sched/cputime.c:thread_group_cputime_adjusted.
absl::Duration cpu = ASSERT_NO_ERRNO_AND_VALUE(ProcessCPUTime(pid));
EXPECT_GE(cpu, kSoftLimit);
// Child did not make it to the hard limit.
//
// Linux sends SIGXCPU synchronously with CPU tick updates. See
// kernel/time/timer.c:update_process_times:
// => account_process_tick // update task CPU usage.
// => run_posix_cpu_timers // enforce RLIMIT_CPU, sending signal.
//
// Thus, only chance for this to flake is if the system time required to
// deliver the signal exceeds 2s.
EXPECT_LT(cpu, kHardLimit);
}
TEST(TimerTest, ProcessPingedRepeatedlyAfterCPUSoftLimit) {
struct sigaction new_action;
new_action.sa_handler = UninstallingSignalHandler;
new_action.sa_flags = 0;
sigemptyset(&new_action.sa_mask);
constexpr absl::Duration kSoftLimit = absl::Seconds(1);
constexpr absl::Duration kHardLimit = absl::Seconds(10);
struct rlimit cpu_limits;
cpu_limits.rlim_cur = absl::ToInt64Seconds(kSoftLimit);
cpu_limits.rlim_max = absl::ToInt64Seconds(kHardLimit);
int pid = fork();
MaybeSave();
if (pid == 0) {
TEST_PCHECK(sigaction(SIGXCPU, &new_action, nullptr) == 0);
MaybeSave();
TEST_PCHECK(setrlimit(RLIMIT_CPU, &cpu_limits) == 0);
MaybeSave();
for (;;) {
}
}
ASSERT_THAT(pid, SyscallSucceeds());
auto c = Cleanup([pid] {
int status;
EXPECT_THAT(waitpid(pid, &status, 0), SyscallSucceedsWithValue(pid));
EXPECT_TRUE(WIFSIGNALED(status));
EXPECT_EQ(WTERMSIG(status), SIGXCPU);
});
// Wait for the child to exit, but do not reap it. This will allow us to check
// its CPU usage while it is zombied.
EXPECT_THAT(waitid(P_PID, pid, nullptr, WEXITED | WNOWAIT),
SyscallSucceeds());
absl::Duration cpu = ASSERT_NO_ERRNO_AND_VALUE(ProcessCPUTime(pid));
// Following signals come every CPU second.
EXPECT_GE(cpu, kSoftLimit + absl::Seconds(1));
// Child did not make it to the hard limit.
//
// As above, should not flake.
EXPECT_LT(cpu, kHardLimit);
}
TEST(TimerTest, ProcessKilledOnCPUHardLimit) {
struct sigaction new_action;
new_action.sa_handler = NoopSignalHandler;
new_action.sa_flags = 0;
sigemptyset(&new_action.sa_mask);
constexpr absl::Duration kSoftLimit = absl::Seconds(1);
constexpr absl::Duration kHardLimit = absl::Seconds(3);
struct rlimit cpu_limits;
cpu_limits.rlim_cur = absl::ToInt64Seconds(kSoftLimit);
cpu_limits.rlim_max = absl::ToInt64Seconds(kHardLimit);
int pid = fork();
MaybeSave();
if (pid == 0) {
TEST_PCHECK(sigaction(SIGXCPU, &new_action, nullptr) == 0);
MaybeSave();
TEST_PCHECK(setrlimit(RLIMIT_CPU, &cpu_limits) == 0);
MaybeSave();
for (;;) {
}
}
ASSERT_THAT(pid, SyscallSucceeds());
auto c = Cleanup([pid] {
int status;
EXPECT_THAT(waitpid(pid, &status, 0), SyscallSucceedsWithValue(pid));
EXPECT_TRUE(WIFSIGNALED(status));
EXPECT_EQ(WTERMSIG(status), SIGKILL);
});
// Wait for the child to exit, but do not reap it. This will allow us to check
// its CPU usage while it is zombied.
EXPECT_THAT(waitid(P_PID, pid, nullptr, WEXITED | WNOWAIT),
SyscallSucceeds());
absl::Duration cpu = ASSERT_NO_ERRNO_AND_VALUE(ProcessCPUTime(pid));
EXPECT_GE(cpu, kHardLimit);
}
// RAII type for a kernel "POSIX" interval timer. (The kernel provides system
// calls such as timer_create that behave very similarly, but not identically,
// to those described by timer_create(2); in particular, the kernel does not
// implement SIGEV_THREAD. glibc builds POSIX-compliant interval timers based on
// these kernel interval timers.)
//
// Compare implementation to FileDescriptor.
class IntervalTimer {
public:
IntervalTimer() = default;
explicit IntervalTimer(int id) { set_id(id); }
IntervalTimer(IntervalTimer&& orig) : id_(orig.release()) {}
IntervalTimer& operator=(IntervalTimer&& orig) {
if (this == &orig) return *this;
reset(orig.release());
return *this;
}
IntervalTimer(const IntervalTimer& other) = delete;
IntervalTimer& operator=(const IntervalTimer& other) = delete;
~IntervalTimer() { reset(); }
int get() const { return id_; }
int release() {
int const id = id_;
id_ = -1;
return id;
}
void reset() { reset(-1); }
void reset(int id) {
if (id_ >= 0) {
TEST_PCHECK(syscall(SYS_timer_delete, id_) == 0);
MaybeSave();
}
set_id(id);
}
PosixErrorOr<struct itimerspec> Set(
int flags, const struct itimerspec& new_value) const {
struct itimerspec old_value = {};
if (syscall(SYS_timer_settime, id_, flags, &new_value, &old_value) < 0) {
return PosixError(errno, "timer_settime");
}
MaybeSave();
return old_value;
}
PosixErrorOr<struct itimerspec> Get() const {
struct itimerspec curr_value = {};
if (syscall(SYS_timer_gettime, id_, &curr_value) < 0) {
return PosixError(errno, "timer_gettime");
}
MaybeSave();
return curr_value;
}
PosixErrorOr<int> Overruns() const {
int rv = syscall(SYS_timer_getoverrun, id_);
if (rv < 0) {
return PosixError(errno, "timer_getoverrun");
}
MaybeSave();
return rv;
}
private:
void set_id(int id) { id_ = std::max(id, -1); }
// Kernel timer_t is int; glibc timer_t is void*.
int id_ = -1;
};
PosixErrorOr<IntervalTimer> TimerCreate(clockid_t clockid,
const struct sigevent& sev) {
int timerid;
int ret = syscall(SYS_timer_create, clockid, &sev, &timerid);
if (ret < 0) {
return PosixError(errno, "timer_create");
}
if (ret > 0) {
return PosixError(EINVAL, "timer_create should never return positive");
}
MaybeSave();
return IntervalTimer(timerid);
}
// See timerfd.cc:TimerSlack() for rationale.
constexpr absl::Duration kTimerSlack = absl::Milliseconds(500);
TEST(IntervalTimerTest, IsInitiallyStopped) {
struct sigevent sev = {};
sev.sigev_notify = SIGEV_NONE;
const auto timer =
ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
const struct itimerspec its = ASSERT_NO_ERRNO_AND_VALUE(timer.Get());
EXPECT_EQ(0, its.it_value.tv_sec);
EXPECT_EQ(0, its.it_value.tv_nsec);
}
// Kernel can create multiple timers without issue.
//
// Regression test for gvisor.dev/issue/1738.
TEST(IntervalTimerTest, MultipleTimers) {
struct sigevent sev = {};
sev.sigev_notify = SIGEV_NONE;
const auto timer1 =
ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
const auto timer2 =
ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
}
TEST(IntervalTimerTest, SingleShotSilent) {
struct sigevent sev = {};
sev.sigev_notify = SIGEV_NONE;
const auto timer =
ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
constexpr absl::Duration kDelay = absl::Seconds(1);
struct itimerspec its = {};
its.it_value = absl::ToTimespec(kDelay);
ASSERT_NO_ERRNO(timer.Set(0, its));
// The timer should count down to 0 and stop since the interval is zero. No
// overruns should be counted.
absl::SleepFor(kDelay + kTimerSlack);
its = ASSERT_NO_ERRNO_AND_VALUE(timer.Get());
EXPECT_EQ(0, its.it_value.tv_sec);
EXPECT_EQ(0, its.it_value.tv_nsec);
EXPECT_THAT(timer.Overruns(), IsPosixErrorOkAndHolds(0));
}
TEST(IntervalTimerTest, PeriodicSilent) {
struct sigevent sev = {};
sev.sigev_notify = SIGEV_NONE;
const auto timer =
ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
constexpr absl::Duration kPeriod = absl::Seconds(1);
struct itimerspec its = {};
its.it_value = its.it_interval = absl::ToTimespec(kPeriod);
ASSERT_NO_ERRNO(timer.Set(0, its));
absl::SleepFor(kPeriod * 3 + kTimerSlack);
// The timer should still be running.
its = ASSERT_NO_ERRNO_AND_VALUE(timer.Get());
EXPECT_TRUE(its.it_value.tv_nsec != 0 || its.it_value.tv_sec != 0);
// Timer expirations are not counted as overruns under SIGEV_NONE.
EXPECT_THAT(timer.Overruns(), IsPosixErrorOkAndHolds(0));
}
std::atomic<int> counted_signals;
void IntervalTimerCountingSignalHandler(int sig, siginfo_t* info,
void* ucontext) {
counted_signals.fetch_add(1 + info->si_overrun);
}
TEST(IntervalTimerTest, PeriodicGroupDirectedSignal) {
constexpr int kSigno = SIGUSR1;
constexpr int kSigvalue = 42;
// Install our signal handler.
counted_signals.store(0);
struct sigaction sa = {};
sa.sa_sigaction = IntervalTimerCountingSignalHandler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
const auto scoped_sigaction =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSigaction(kSigno, sa));
// Ensure that kSigno is unblocked on at least one thread.
const auto scoped_sigmask =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, kSigno));
struct sigevent sev = {};
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = kSigno;
sev.sigev_value.sival_int = kSigvalue;
auto timer = ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
constexpr absl::Duration kPeriod = absl::Seconds(1);
constexpr int kCycles = 3;
struct itimerspec its = {};
its.it_value = its.it_interval = absl::ToTimespec(kPeriod);
ASSERT_NO_ERRNO(timer.Set(0, its));
absl::SleepFor(kPeriod * kCycles + kTimerSlack);
EXPECT_GE(counted_signals.load(), kCycles);
}
// From Linux's include/uapi/asm-generic/siginfo.h.
#ifndef sigev_notify_thread_id
#define sigev_notify_thread_id _sigev_un._tid
#endif
TEST(IntervalTimerTest, PeriodicThreadDirectedSignal) {
constexpr int kSigno = SIGUSR1;
constexpr int kSigvalue = 42;
// Block kSigno so that we can accumulate overruns.
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, kSigno);
const auto scoped_sigmask =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_BLOCK, mask));
struct sigevent sev = {};
sev.sigev_notify = SIGEV_THREAD_ID;
sev.sigev_signo = kSigno;
sev.sigev_value.sival_int = kSigvalue;
sev.sigev_notify_thread_id = gettid();
auto timer = ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
constexpr absl::Duration kPeriod = absl::Seconds(1);
constexpr int kCycles = 3;
struct itimerspec its = {};
its.it_value = its.it_interval = absl::ToTimespec(kPeriod);
ASSERT_NO_ERRNO(timer.Set(0, its));
absl::SleepFor(kPeriod * kCycles + kTimerSlack);
// At least kCycles expirations should have occurred, resulting in kCycles-1
// overruns (the first expiration sent the signal successfully).
siginfo_t si;
struct timespec zero_ts = absl::ToTimespec(absl::ZeroDuration());
ASSERT_THAT(sigtimedwait(&mask, &si, &zero_ts),
SyscallSucceedsWithValue(kSigno));
EXPECT_EQ(si.si_signo, kSigno);
EXPECT_EQ(si.si_code, SI_TIMER);
EXPECT_EQ(si.si_timerid, timer.get());
EXPECT_GE(si.si_overrun, kCycles - 1);
EXPECT_EQ(si.si_int, kSigvalue);
// Kill the timer, then drain any additional signal it may have enqueued. We
// can't do this before the preceding sigtimedwait because stopping or
// deleting the timer resets si_overrun to 0.
timer.reset();
sigtimedwait(&mask, &si, &zero_ts);
}
TEST(IntervalTimerTest, OtherThreadGroup) {
constexpr int kSigno = SIGUSR1;
// Create a subprocess that does nothing until killed.
pid_t child_pid;
const auto sp = ASSERT_NO_ERRNO_AND_VALUE(ForkAndExec(
"/proc/self/exe", ExecveArray({"timers", "--timers_test_sleep"}),
ExecveArray(), &child_pid, nullptr));
// Verify that we can't create a timer that would send signals to it.
struct sigevent sev = {};
sev.sigev_notify = SIGEV_THREAD_ID;
sev.sigev_signo = kSigno;
sev.sigev_notify_thread_id = child_pid;
EXPECT_THAT(TimerCreate(CLOCK_MONOTONIC, sev), PosixErrorIs(EINVAL, _));
}
TEST(IntervalTimerTest, RealTimeSignalsAreNotDuplicated) {
const int kSigno = SIGRTMIN;
constexpr int kSigvalue = 42;
// Block signo so that we can accumulate overruns.
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, kSigno);
const auto scoped_sigmask = ScopedSignalMask(SIG_BLOCK, mask);
struct sigevent sev = {};
sev.sigev_notify = SIGEV_THREAD_ID;
sev.sigev_signo = kSigno;
sev.sigev_value.sival_int = kSigvalue;
sev.sigev_notify_thread_id = gettid();
const auto timer =
ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
constexpr absl::Duration kPeriod = absl::Seconds(1);
constexpr int kCycles = 3;
struct itimerspec its = {};
its.it_value = its.it_interval = absl::ToTimespec(kPeriod);
ASSERT_NO_ERRNO(timer.Set(0, its));
absl::SleepFor(kPeriod * kCycles + kTimerSlack);
// Stop the timer so that no further signals are enqueued after sigtimedwait.
struct timespec zero_ts = absl::ToTimespec(absl::ZeroDuration());
its.it_value = its.it_interval = zero_ts;
ASSERT_NO_ERRNO(timer.Set(0, its));
// The timer should have sent only a single signal, even though the kernel
// supports enqueueing of multiple RT signals.
siginfo_t si;
ASSERT_THAT(sigtimedwait(&mask, &si, &zero_ts),
SyscallSucceedsWithValue(kSigno));
EXPECT_EQ(si.si_signo, kSigno);
EXPECT_EQ(si.si_code, SI_TIMER);
EXPECT_EQ(si.si_timerid, timer.get());
// si_overrun was reset by timer_settime.
EXPECT_EQ(si.si_overrun, 0);
EXPECT_EQ(si.si_int, kSigvalue);
EXPECT_THAT(sigtimedwait(&mask, &si, &zero_ts),
SyscallFailsWithErrno(EAGAIN));
}
TEST(IntervalTimerTest, AlreadyPendingSignal) {
constexpr int kSigno = SIGUSR1;
constexpr int kSigvalue = 42;
// Block kSigno so that we can accumulate overruns.
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, kSigno);
const auto scoped_sigmask =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_BLOCK, mask));
// Send ourselves a signal, preventing the timer from enqueuing.
ASSERT_THAT(tgkill(getpid(), gettid(), kSigno), SyscallSucceeds());
struct sigevent sev = {};
sev.sigev_notify = SIGEV_THREAD_ID;
sev.sigev_signo = kSigno;
sev.sigev_value.sival_int = kSigvalue;
sev.sigev_notify_thread_id = gettid();
auto timer = ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
constexpr absl::Duration kPeriod = absl::Seconds(1);
constexpr int kCycles = 3;
struct itimerspec its = {};
its.it_value = its.it_interval = absl::ToTimespec(kPeriod);
ASSERT_NO_ERRNO(timer.Set(0, its));
// End the sleep one cycle short; we will sleep for one more cycle below.
absl::SleepFor(kPeriod * (kCycles - 1));
// Dequeue the first signal, which we sent to ourselves with tgkill.
siginfo_t si;
struct timespec zero_ts = absl::ToTimespec(absl::ZeroDuration());
ASSERT_THAT(sigtimedwait(&mask, &si, &zero_ts),
SyscallSucceedsWithValue(kSigno));
EXPECT_EQ(si.si_signo, kSigno);
// glibc sigtimedwait silently replaces SI_TKILL with SI_USER:
// sysdeps/unix/sysv/linux/sigtimedwait.c:__sigtimedwait(). This isn't
// documented, so we don't depend on it.
EXPECT_THAT(si.si_code, AnyOf(SI_USER, SI_TKILL));
// Sleep for 1 more cycle to give the timer time to send a signal.
absl::SleepFor(kPeriod + kTimerSlack);
// At least kCycles expirations should have occurred, resulting in kCycles-1
// overruns (the last expiration sent the signal successfully).
ASSERT_THAT(sigtimedwait(&mask, &si, &zero_ts),
SyscallSucceedsWithValue(kSigno));
EXPECT_EQ(si.si_signo, kSigno);
EXPECT_EQ(si.si_code, SI_TIMER);
EXPECT_EQ(si.si_timerid, timer.get());
EXPECT_GE(si.si_overrun, kCycles - 1);
EXPECT_EQ(si.si_int, kSigvalue);
// Kill the timer, then drain any additional signal it may have enqueued. We
// can't do this before the preceding sigtimedwait because stopping or
// deleting the timer resets si_overrun to 0.
timer.reset();
sigtimedwait(&mask, &si, &zero_ts);
}
TEST(IntervalTimerTest, IgnoredSignalCountsAsOverrun) {
constexpr int kSigno = SIGUSR1;
constexpr int kSigvalue = 42;
// Ignore kSigno.
struct sigaction sa = {};
sa.sa_handler = SIG_IGN;
const auto scoped_sigaction =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSigaction(kSigno, sa));
// Unblock kSigno so that ignored signals will be discarded.
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, kSigno);
auto scoped_sigmask =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, mask));
struct sigevent sev = {};
sev.sigev_notify = SIGEV_THREAD_ID;
sev.sigev_signo = kSigno;
sev.sigev_value.sival_int = kSigvalue;
sev.sigev_notify_thread_id = gettid();
auto timer = ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
constexpr absl::Duration kPeriod = absl::Seconds(1);
constexpr int kCycles = 3;
struct itimerspec its = {};
its.it_value = its.it_interval = absl::ToTimespec(kPeriod);
ASSERT_NO_ERRNO(timer.Set(0, its));
// End the sleep one cycle short; we will sleep for one more cycle below.
absl::SleepFor(kPeriod * (kCycles - 1));
// Block kSigno so that ignored signals will be enqueued.
scoped_sigmask.Release()();
scoped_sigmask = ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_BLOCK, mask));
// Sleep for 1 more cycle to give the timer time to send a signal.
absl::SleepFor(kPeriod + kTimerSlack);
// At least kCycles expirations should have occurred, resulting in kCycles-1
// overruns (the last expiration sent the signal successfully).
siginfo_t si;
struct timespec zero_ts = absl::ToTimespec(absl::ZeroDuration());
ASSERT_THAT(sigtimedwait(&mask, &si, &zero_ts),
SyscallSucceedsWithValue(kSigno));
EXPECT_EQ(si.si_signo, kSigno);
EXPECT_EQ(si.si_code, SI_TIMER);
EXPECT_EQ(si.si_timerid, timer.get());
EXPECT_GE(si.si_overrun, kCycles - 1);
EXPECT_EQ(si.si_int, kSigvalue);
// Kill the timer, then drain any additional signal it may have enqueued. We
// can't do this before the preceding sigtimedwait because stopping or
// deleting the timer resets si_overrun to 0.
timer.reset();
sigtimedwait(&mask, &si, &zero_ts);
}
} // namespace
} // namespace testing
} // namespace gvisor
int main(int argc, char** argv) {
gvisor::testing::TestInit(&argc, &argv);
if (absl::GetFlag(FLAGS_timers_test_sleep)) {
while (true) {
absl::SleepFor(absl::Seconds(10));
}
}
return RUN_ALL_TESTS();
}
|