summaryrefslogtreecommitdiffhomepage
path: root/test/syscalls/linux/readv.cc
blob: a50d98d214efa08ddd456b4be58c5cc087d459cd (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
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
// 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 <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <unistd.h>

#include "gtest/gtest.h"
#include "test/syscalls/linux/file_base.h"
#include "test/syscalls/linux/readv_common.h"
#include "test/util/file_descriptor.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
#include "test/util/timer_util.h"

namespace gvisor {
namespace testing {

namespace {

class ReadvTest : public FileTest {
  void SetUp() override {
    FileTest::SetUp();

    ASSERT_THAT(write(test_file_fd_.get(), kReadvTestData, kReadvTestDataSize),
                SyscallSucceedsWithValue(kReadvTestDataSize));
    ASSERT_THAT(lseek(test_file_fd_.get(), 0, SEEK_SET),
                SyscallSucceedsWithValue(0));
    ASSERT_THAT(write(test_pipe_[1], kReadvTestData, kReadvTestDataSize),
                SyscallSucceedsWithValue(kReadvTestDataSize));
  }
};

TEST_F(ReadvTest, ReadOneBufferPerByte_File) {
  ReadOneBufferPerByte(test_file_fd_.get());
}

TEST_F(ReadvTest, ReadOneBufferPerByte_Pipe) {
  ReadOneBufferPerByte(test_pipe_[0]);
}

TEST_F(ReadvTest, ReadOneHalfAtATime_File) {
  ReadOneHalfAtATime(test_file_fd_.get());
}

TEST_F(ReadvTest, ReadOneHalfAtATime_Pipe) {
  ReadOneHalfAtATime(test_pipe_[0]);
}

TEST_F(ReadvTest, ReadAllOneBuffer_File) {
  ReadAllOneBuffer(test_file_fd_.get());
}

TEST_F(ReadvTest, ReadAllOneBuffer_Pipe) { ReadAllOneBuffer(test_pipe_[0]); }

TEST_F(ReadvTest, ReadAllOneLargeBuffer_File) {
  ReadAllOneLargeBuffer(test_file_fd_.get());
}

TEST_F(ReadvTest, ReadAllOneLargeBuffer_Pipe) {
  ReadAllOneLargeBuffer(test_pipe_[0]);
}

TEST_F(ReadvTest, ReadBuffersOverlapping_File) {
  ReadBuffersOverlapping(test_file_fd_.get());
}

TEST_F(ReadvTest, ReadBuffersOverlapping_Pipe) {
  ReadBuffersOverlapping(test_pipe_[0]);
}

TEST_F(ReadvTest, ReadBuffersDiscontinuous_File) {
  ReadBuffersDiscontinuous(test_file_fd_.get());
}

TEST_F(ReadvTest, ReadBuffersDiscontinuous_Pipe) {
  ReadBuffersDiscontinuous(test_pipe_[0]);
}

TEST_F(ReadvTest, ReadIovecsCompletelyFilled_File) {
  ReadIovecsCompletelyFilled(test_file_fd_.get());
}

TEST_F(ReadvTest, ReadIovecsCompletelyFilled_Pipe) {
  ReadIovecsCompletelyFilled(test_pipe_[0]);
}

TEST_F(ReadvTest, BadFileDescriptor) {
  char buffer[1024];
  struct iovec iov[1];
  iov[0].iov_base = buffer;
  iov[0].iov_len = 1024;

  ASSERT_THAT(readv(-1, iov, 1024), SyscallFailsWithErrno(EBADF));
}

TEST_F(ReadvTest, BadIovecsPointer_File) {
  ASSERT_THAT(readv(test_file_fd_.get(), nullptr, 1),
              SyscallFailsWithErrno(EFAULT));
}

TEST_F(ReadvTest, BadIovecsPointer_Pipe) {
  ASSERT_THAT(readv(test_pipe_[0], nullptr, 1), SyscallFailsWithErrno(EFAULT));
}

TEST_F(ReadvTest, BadIovecBase_File) {
  struct iovec iov[1];
  iov[0].iov_base = nullptr;
  iov[0].iov_len = 1024;
  ASSERT_THAT(readv(test_file_fd_.get(), iov, 1),
              SyscallFailsWithErrno(EFAULT));
}

TEST_F(ReadvTest, BadIovecBase_Pipe) {
  struct iovec iov[1];
  iov[0].iov_base = nullptr;
  iov[0].iov_len = 1024;
  ASSERT_THAT(readv(test_pipe_[0], iov, 1), SyscallFailsWithErrno(EFAULT));
}

TEST_F(ReadvTest, ZeroIovecs_File) {
  struct iovec iov[1];
  iov[0].iov_base = 0;
  iov[0].iov_len = 0;
  ASSERT_THAT(readv(test_file_fd_.get(), iov, 1), SyscallSucceeds());
}

TEST_F(ReadvTest, ZeroIovecs_Pipe) {
  struct iovec iov[1];
  iov[0].iov_base = 0;
  iov[0].iov_len = 0;
  ASSERT_THAT(readv(test_pipe_[0], iov, 1), SyscallSucceeds());
}

TEST_F(ReadvTest, NotReadable_File) {
  char buffer[1024];
  struct iovec iov[1];
  iov[0].iov_base = buffer;
  iov[0].iov_len = 1024;

  std::string wronly_file = NewTempAbsPath();
  FileDescriptor fd = ASSERT_NO_ERRNO_AND_VALUE(
      Open(wronly_file, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR));
  ASSERT_THAT(readv(fd.get(), iov, 1), SyscallFailsWithErrno(EBADF));
  fd.reset();  // Close before unlinking.
  ASSERT_THAT(unlink(wronly_file.c_str()), SyscallSucceeds());
}

TEST_F(ReadvTest, NotReadable_Pipe) {
  char buffer[1024];
  struct iovec iov[1];
  iov[0].iov_base = buffer;
  iov[0].iov_len = 1024;
  ASSERT_THAT(readv(test_pipe_[1], iov, 1), SyscallFailsWithErrno(EBADF));
}

TEST_F(ReadvTest, DirNotReadable) {
  char buffer[1024];
  struct iovec iov[1];
  iov[0].iov_base = buffer;
  iov[0].iov_len = 1024;

  const FileDescriptor fd =
      ASSERT_NO_ERRNO_AND_VALUE(Open(GetAbsoluteTestTmpdir(), O_RDONLY));
  ASSERT_THAT(readv(fd.get(), iov, 1), SyscallFailsWithErrno(EISDIR));
}

TEST_F(ReadvTest, OffsetIncremented) {
  char* buffer = reinterpret_cast<char*>(malloc(kReadvTestDataSize));
  struct iovec iov[1];
  iov[0].iov_base = buffer;
  iov[0].iov_len = kReadvTestDataSize;

  ASSERT_THAT(readv(test_file_fd_.get(), iov, 1),
              SyscallSucceedsWithValue(kReadvTestDataSize));
  ASSERT_THAT(lseek(test_file_fd_.get(), 0, SEEK_CUR),
              SyscallSucceedsWithValue(kReadvTestDataSize));

  free(buffer);
}

TEST_F(ReadvTest, EndOfFile) {
  char* buffer = reinterpret_cast<char*>(malloc(kReadvTestDataSize));
  struct iovec iov[1];
  iov[0].iov_base = buffer;
  iov[0].iov_len = kReadvTestDataSize;
  ASSERT_THAT(readv(test_file_fd_.get(), iov, 1),
              SyscallSucceedsWithValue(kReadvTestDataSize));
  free(buffer);

  buffer = reinterpret_cast<char*>(malloc(kReadvTestDataSize));
  iov[0].iov_base = buffer;
  iov[0].iov_len = kReadvTestDataSize;
  ASSERT_THAT(readv(test_file_fd_.get(), iov, 1), SyscallSucceedsWithValue(0));
  free(buffer);
}

TEST_F(ReadvTest, WouldBlock_Pipe) {
  struct iovec iov[1];
  iov[0].iov_base = reinterpret_cast<char*>(malloc(kReadvTestDataSize));
  iov[0].iov_len = kReadvTestDataSize;
  ASSERT_THAT(readv(test_pipe_[0], iov, 1),
              SyscallSucceedsWithValue(kReadvTestDataSize));
  free(iov[0].iov_base);

  iov[0].iov_base = reinterpret_cast<char*>(malloc(kReadvTestDataSize));
  ASSERT_THAT(readv(test_pipe_[0], iov, 1), SyscallFailsWithErrno(EAGAIN));
  free(iov[0].iov_base);
}

TEST_F(ReadvTest, ZeroBuffer) {
  char buf[10];
  struct iovec iov[1];
  iov[0].iov_base = buf;
  iov[0].iov_len = 0;
  ASSERT_THAT(readv(test_pipe_[0], iov, 1), SyscallSucceedsWithValue(0));
}

TEST_F(ReadvTest, NullIovecInNonemptyArray) {
  std::vector<char> buf(kReadvTestDataSize);
  struct iovec iov[2];
  iov[0].iov_base = nullptr;
  iov[0].iov_len = 0;
  iov[1].iov_base = buf.data();
  iov[1].iov_len = kReadvTestDataSize;
  ASSERT_THAT(readv(test_file_fd_.get(), iov, 2),
              SyscallSucceedsWithValue(kReadvTestDataSize));
}

TEST_F(ReadvTest, IovecOutsideTaskAddressRangeInNonemptyArray) {
  std::vector<char> buf(kReadvTestDataSize);
  struct iovec iov[2];
  iov[0].iov_base = reinterpret_cast<void*>(~static_cast<uintptr_t>(0));
  iov[0].iov_len = 0;
  iov[1].iov_base = buf.data();
  iov[1].iov_len = kReadvTestDataSize;
  ASSERT_THAT(readv(test_file_fd_.get(), iov, 2),
              SyscallFailsWithErrno(EFAULT));
}

TEST_F(ReadvTest, ReadvWithOpath) {
  SKIP_IF(IsRunningWithVFS1());
  char buffer[1024];
  struct iovec iov[1];
  iov[0].iov_base = buffer;
  iov[0].iov_len = 1024;

  TempPath tmpfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
  FileDescriptor fd =
      ASSERT_NO_ERRNO_AND_VALUE(Open(tmpfile.path().c_str(), O_PATH));

  ASSERT_THAT(readv(fd.get(), iov, 1), SyscallFailsWithErrno(EBADF));
}

// This test depends on the maximum extent of a single readv() syscall, so
// we can't tolerate interruption from saving.
TEST(ReadvTestNoFixture, TruncatedAtMax) {
  // Ensure that we won't be interrupted by ITIMER_PROF. This is particularly
  // important in environments where automated profiling tools may start
  // ITIMER_PROF automatically.
  struct itimerval itv = {};
  auto const cleanup_itimer =
      ASSERT_NO_ERRNO_AND_VALUE(ScopedItimer(ITIMER_PROF, itv));

  // From Linux's include/linux/fs.h.
  size_t const MAX_RW_COUNT = INT_MAX & ~(kPageSize - 1);

  // Create an iovec array with 3 segments pointing to consecutive parts of a
  // buffer. The first covers all but the last three pages, and should be
  // written to in its entirety. The second covers the last page before
  // MAX_RW_COUNT and the first page after; only the first page should be
  // written to. The third covers the last page of the buffer, and should be
  // skipped entirely.
  size_t const kBufferSize = MAX_RW_COUNT + 2 * kPageSize;
  size_t const kFirstOffset = MAX_RW_COUNT - kPageSize;
  size_t const kSecondOffset = MAX_RW_COUNT + kPageSize;
  // The buffer is too big to fit on the stack.
  std::vector<char> buf(kBufferSize);
  struct iovec iov[3];
  iov[0].iov_base = buf.data();
  iov[0].iov_len = kFirstOffset;
  iov[1].iov_base = buf.data() + kFirstOffset;
  iov[1].iov_len = kSecondOffset - kFirstOffset;
  iov[2].iov_base = buf.data() + kSecondOffset;
  iov[2].iov_len = kBufferSize - kSecondOffset;

  const FileDescriptor fd =
      ASSERT_NO_ERRNO_AND_VALUE(Open("/dev/zero", O_RDONLY));
  EXPECT_THAT(readv(fd.get(), iov, 3), SyscallSucceedsWithValue(MAX_RW_COUNT));
}

}  // namespace

}  // namespace testing
}  // namespace gvisor