summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/alarm.cc
blob: 940c97285fe1f4c59eb359679878805255e6ceb7 (plain)
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
// 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 <signal.h>
#include <unistd.h>

#include "gtest/gtest.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "test/util/file_descriptor.h"
#include "test/util/logging.h"
#include "test/util/signal_util.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"

namespace gvisor {
namespace testing {

namespace {

// N.B. Below, main blocks SIGALRM. Test cases must unblock it if they want
// delivery.

void do_nothing_handler(int sig, siginfo_t* siginfo, void* arg) {}

// No random save as the test relies on alarm timing. Cooperative save tests
// already cover the save between alarm and read.
TEST(AlarmTest, Interrupt_NoRandomSave) {
  int pipe_fds[2];
  ASSERT_THAT(pipe(pipe_fds), SyscallSucceeds());

  FileDescriptor read_fd(pipe_fds[0]);
  FileDescriptor write_fd(pipe_fds[1]);

  // Use a signal handler that interrupts but does nothing rather than using the
  // default terminate action.
  struct sigaction sa;
  sa.sa_sigaction = do_nothing_handler;
  sigfillset(&sa.sa_mask);
  sa.sa_flags = 0;
  auto sa_cleanup = ASSERT_NO_ERRNO_AND_VALUE(ScopedSigaction(SIGALRM, sa));

  // Actually allow SIGALRM delivery.
  auto mask_cleanup =
      ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, SIGALRM));

  // Alarm in 20 second, which should be well after read blocks below.
  ASSERT_THAT(alarm(20), SyscallSucceeds());

  char buf;
  ASSERT_THAT(read(read_fd.get(), &buf, 1), SyscallFailsWithErrno(EINTR));
}

/* Count of the number of SIGALARMS handled. */
static volatile int alarms_received = 0;

void inc_alarms_handler(int sig, siginfo_t* siginfo, void* arg) {
  alarms_received++;
}

// No random save as the test relies on alarm timing. Cooperative save tests
// already cover the save between alarm and read.
TEST(AlarmTest, Restart_NoRandomSave) {
  alarms_received = 0;

  int pipe_fds[2];
  ASSERT_THAT(pipe(pipe_fds), SyscallSucceeds());

  FileDescriptor read_fd(pipe_fds[0]);
  // Write end closed by thread below.

  struct sigaction sa;
  sa.sa_sigaction = inc_alarms_handler;
  sigfillset(&sa.sa_mask);
  sa.sa_flags = SA_RESTART;
  auto sa_cleanup = ASSERT_NO_ERRNO_AND_VALUE(ScopedSigaction(SIGALRM, sa));

  // Spawn a thread to eventually unblock the read below.
  ScopedThread t([pipe_fds] {
    absl::SleepFor(absl::Seconds(30));
    EXPECT_THAT(close(pipe_fds[1]), SyscallSucceeds());
  });

  // Actually allow SIGALRM delivery.
  auto mask_cleanup =
      ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, SIGALRM));

  // Alarm in 20 second, which should be well after read blocks below, but
  // before it returns.
  ASSERT_THAT(alarm(20), SyscallSucceeds());

  // Read and eventually get an EOF from the writer closing.  If SA_RESTART
  // didn't work, then the alarm would not have fired and we wouldn't increment
  // our alarms_received count in our signal handler, or we would have not
  // restarted the syscall gracefully, which we expect below in order to be
  // able to get the final EOF on the pipe.
  char buf;
  ASSERT_THAT(read(read_fd.get(), &buf, 1), SyscallSucceeds());
  EXPECT_EQ(alarms_received, 1);

  t.Join();
}

// No random save as the test relies on alarm timing. Cooperative save tests
// already cover the save between alarm and pause.
TEST(AlarmTest, SaSiginfo_NoRandomSave) {
  // Use a signal handler that interrupts but does nothing rather than using the
  // default terminate action.
  struct sigaction sa;
  sa.sa_sigaction = do_nothing_handler;
  sigfillset(&sa.sa_mask);
  sa.sa_flags = SA_SIGINFO;
  auto sa_cleanup = ASSERT_NO_ERRNO_AND_VALUE(ScopedSigaction(SIGALRM, sa));

  // Actually allow SIGALRM delivery.
  auto mask_cleanup =
      ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, SIGALRM));

  // Alarm in 20 second, which should be well after pause blocks below.
  ASSERT_THAT(alarm(20), SyscallSucceeds());
  ASSERT_THAT(pause(), SyscallFailsWithErrno(EINTR));
}

// No random save as the test relies on alarm timing. Cooperative save tests
// already cover the save between alarm and pause.
TEST(AlarmTest, SaInterrupt_NoRandomSave) {
  // Use a signal handler that interrupts but does nothing rather than using the
  // default terminate action.
  struct sigaction sa;
  sa.sa_sigaction = do_nothing_handler;
  sigfillset(&sa.sa_mask);
  sa.sa_flags = SA_INTERRUPT;
  auto sa_cleanup = ASSERT_NO_ERRNO_AND_VALUE(ScopedSigaction(SIGALRM, sa));

  // Actually allow SIGALRM delivery.
  auto mask_cleanup =
      ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, SIGALRM));

  // Alarm in 20 second, which should be well after pause blocks below.
  ASSERT_THAT(alarm(20), SyscallSucceeds());
  ASSERT_THAT(pause(), SyscallFailsWithErrno(EINTR));
}

TEST(AlarmTest, UserModeSpinning) {
  alarms_received = 0;

  struct sigaction sa = {};
  sa.sa_sigaction = inc_alarms_handler;
  sigfillset(&sa.sa_mask);
  sa.sa_flags = SA_SIGINFO;
  auto sa_cleanup = ASSERT_NO_ERRNO_AND_VALUE(ScopedSigaction(SIGALRM, sa));

  // Actually allow SIGALRM delivery.
  auto mask_cleanup =
      ASSERT_NO_ERRNO_AND_VALUE(ScopedSignalMask(SIG_UNBLOCK, SIGALRM));

  // Alarm in 20 second, which should be well into the loop below.
  ASSERT_THAT(alarm(20), SyscallSucceeds());
  // Make sure that the signal gets delivered even if we are spinning in user
  // mode when it arrives.
  while (!alarms_received) {
  }
}

}  // namespace

}  // namespace testing
}  // namespace gvisor

int main(int argc, char** argv) {
  // These tests depend on delivering SIGALRM to the main thread. Block SIGALRM
  // so that any other threads created by TestInit will also have SIGALRM
  // blocked.
  sigset_t set;
  sigemptyset(&set);
  sigaddset(&set, SIGALRM);
  TEST_PCHECK(sigprocmask(SIG_BLOCK, &set, nullptr) == 0);

  gvisor::testing::TestInit(&argc, &argv);
  return gvisor::testing::RunAllTests();
}