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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
// 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 <fcntl.h>
#include <linux/unistd.h>
#include <sys/eventfd.h>
#include <sys/sendfile.h>
#include <unistd.h>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "test/util/eventfd_util.h"
#include "test/util/file_descriptor.h"
#include "test/util/signal_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
#include "test/util/timer_util.h"
namespace gvisor {
namespace testing {
namespace {
TEST(SendFileTest, SendZeroBytes) {
// Create temp files.
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as write only.
const FileDescriptor outf =
ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Send data and verify that sendfile returns the correct value.
EXPECT_THAT(sendfile(outf.get(), inf.get(), nullptr, 0),
SyscallSucceedsWithValue(0));
}
TEST(SendFileTest, InvalidOffset) {
// Create temp files.
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as write only.
const FileDescriptor outf =
ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Send data and verify that sendfile returns the correct value.
off_t offset = -1;
EXPECT_THAT(sendfile(outf.get(), inf.get(), &offset, 0),
SyscallFailsWithErrno(EINVAL));
}
int memfd_create(const std::string& name, unsigned int flags) {
return syscall(__NR_memfd_create, name.c_str(), flags);
}
TEST(SendFileTest, Overflow) {
// Create input file.
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file.
int fd;
EXPECT_THAT(fd = memfd_create("overflow", 0), SyscallSucceeds());
const FileDescriptor outf(fd);
// out_offset + kSize overflows INT64_MAX.
loff_t out_offset = 0x7ffffffffffffffeull;
constexpr int kSize = 3;
EXPECT_THAT(sendfile(outf.get(), inf.get(), &out_offset, kSize),
SyscallFailsWithErrno(EINVAL));
}
TEST(SendFileTest, SendTrivially) {
// Create temp files.
constexpr char kData[] = "To be, or not to be, that is the question:";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as write only.
FileDescriptor outf;
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Send data and verify that sendfile returns the correct value.
int bytes_sent;
EXPECT_THAT(bytes_sent = sendfile(outf.get(), inf.get(), nullptr, kDataSize),
SyscallSucceedsWithValue(kDataSize));
// Close outf to avoid leak.
outf.reset();
// Open the output file as read only.
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_RDONLY));
// Verify that the output file has the correct data.
char actual[kDataSize];
ASSERT_THAT(read(outf.get(), &actual, bytes_sent),
SyscallSucceedsWithValue(kDataSize));
EXPECT_EQ(kData, absl::string_view(actual, bytes_sent));
}
TEST(SendFileTest, SendTriviallyWithBothFilesReadWrite) {
// Create temp files.
constexpr char kData[] = "Whether 'tis nobler in the mind to suffer";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as readwrite.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR));
// Open the output file as readwrite.
FileDescriptor outf;
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_RDWR));
// Send data and verify that sendfile returns the correct value.
int bytes_sent;
EXPECT_THAT(bytes_sent = sendfile(outf.get(), inf.get(), nullptr, kDataSize),
SyscallSucceedsWithValue(kDataSize));
// Close outf to avoid leak.
outf.reset();
// Open the output file as read only.
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_RDONLY));
// Verify that the output file has the correct data.
char actual[kDataSize];
ASSERT_THAT(read(outf.get(), &actual, bytes_sent),
SyscallSucceedsWithValue(kDataSize));
EXPECT_EQ(kData, absl::string_view(actual, bytes_sent));
}
TEST(SendFileTest, SendAndUpdateFileOffset) {
// Create temp files.
// Test input string length must be > 2 AND even.
constexpr char kData[] = "The slings and arrows of outrageous fortune,";
constexpr int kDataSize = sizeof(kData) - 1;
constexpr int kHalfDataSize = kDataSize / 2;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as write only.
FileDescriptor outf;
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Send data and verify that sendfile returns the correct value.
int bytes_sent;
EXPECT_THAT(
bytes_sent = sendfile(outf.get(), inf.get(), nullptr, kHalfDataSize),
SyscallSucceedsWithValue(kHalfDataSize));
// Close outf to avoid leak.
outf.reset();
// Open the output file as read only.
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_RDONLY));
// Verify that the output file has the correct data.
char actual[kHalfDataSize];
ASSERT_THAT(read(outf.get(), &actual, bytes_sent),
SyscallSucceedsWithValue(kHalfDataSize));
EXPECT_EQ(absl::string_view(kData, kHalfDataSize),
absl::string_view(actual, bytes_sent));
// Verify that the input file offset has been updated.
ASSERT_THAT(read(inf.get(), &actual, kDataSize - bytes_sent),
SyscallSucceedsWithValue(kHalfDataSize));
EXPECT_EQ(
absl::string_view(kData + kDataSize - bytes_sent, kDataSize - bytes_sent),
absl::string_view(actual, kHalfDataSize));
}
TEST(SendFileTest, SendToDevZeroAndUpdateFileOffset) {
// Create temp files.
// Test input string length must be > 2 AND even.
constexpr char kData[] = "The slings and arrows of outrageous fortune,";
constexpr int kDataSize = sizeof(kData) - 1;
constexpr int kHalfDataSize = kDataSize / 2;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open /dev/zero as write only.
const FileDescriptor outf =
ASSERT_NO_ERRNO_AND_VALUE(Open("/dev/zero", O_WRONLY));
// Send data and verify that sendfile returns the correct value.
int bytes_sent;
EXPECT_THAT(
bytes_sent = sendfile(outf.get(), inf.get(), nullptr, kHalfDataSize),
SyscallSucceedsWithValue(kHalfDataSize));
char actual[kHalfDataSize];
// Verify that the input file offset has been updated.
ASSERT_THAT(read(inf.get(), &actual, kDataSize - bytes_sent),
SyscallSucceedsWithValue(kHalfDataSize));
EXPECT_EQ(
absl::string_view(kData + kDataSize - bytes_sent, kDataSize - bytes_sent),
absl::string_view(actual, kHalfDataSize));
}
TEST(SendFileTest, SendAndUpdateFileOffsetFromNonzeroStartingPoint) {
// Create temp files.
// Test input string length must be > 2 AND divisible by 4.
constexpr char kData[] = "The slings and arrows of outrageous fortune,";
constexpr int kDataSize = sizeof(kData) - 1;
constexpr int kHalfDataSize = kDataSize / 2;
constexpr int kQuarterDataSize = kHalfDataSize / 2;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as write only.
FileDescriptor outf;
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Read a quarter of the data from the infile which should update the file
// offset, we don't actually care about the data so it goes into the garbage.
char garbage[kQuarterDataSize];
ASSERT_THAT(read(inf.get(), &garbage, kQuarterDataSize),
SyscallSucceedsWithValue(kQuarterDataSize));
// Send data and verify that sendfile returns the correct value.
int bytes_sent;
EXPECT_THAT(
bytes_sent = sendfile(outf.get(), inf.get(), nullptr, kHalfDataSize),
SyscallSucceedsWithValue(kHalfDataSize));
// Close out_fd to avoid leak.
outf.reset();
// Open the output file as read only.
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_RDONLY));
// Verify that the output file has the correct data.
char actual[kHalfDataSize];
ASSERT_THAT(read(outf.get(), &actual, bytes_sent),
SyscallSucceedsWithValue(kHalfDataSize));
EXPECT_EQ(absl::string_view(kData + kQuarterDataSize, kHalfDataSize),
absl::string_view(actual, bytes_sent));
// Verify that the input file offset has been updated.
ASSERT_THAT(read(inf.get(), &actual, kQuarterDataSize),
SyscallSucceedsWithValue(kQuarterDataSize));
EXPECT_EQ(
absl::string_view(kData + kDataSize - kQuarterDataSize, kQuarterDataSize),
absl::string_view(actual, kQuarterDataSize));
}
TEST(SendFileTest, SendAndUpdateGivenOffset) {
// Create temp files.
// Test input string length must be >= 4 AND divisible by 4.
constexpr char kData[] = "Or to take Arms against a Sea of troubles,";
constexpr int kDataSize = sizeof(kData) + 1;
constexpr int kHalfDataSize = kDataSize / 2;
constexpr int kQuarterDataSize = kHalfDataSize / 2;
constexpr int kThreeFourthsDataSize = 3 * kDataSize / 4;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as write only.
FileDescriptor outf;
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Create offset for sending.
off_t offset = kQuarterDataSize;
// Send data and verify that sendfile returns the correct value.
int bytes_sent;
EXPECT_THAT(
bytes_sent = sendfile(outf.get(), inf.get(), &offset, kHalfDataSize),
SyscallSucceedsWithValue(kHalfDataSize));
// Close out_fd to avoid leak.
outf.reset();
// Open the output file as read only.
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_RDONLY));
// Verify that the output file has the correct data.
char actual[kHalfDataSize];
ASSERT_THAT(read(outf.get(), &actual, bytes_sent),
SyscallSucceedsWithValue(kHalfDataSize));
EXPECT_EQ(absl::string_view(kData + kQuarterDataSize, kHalfDataSize),
absl::string_view(actual, bytes_sent));
// Verify that the input file offset has NOT been updated.
ASSERT_THAT(read(inf.get(), &actual, kHalfDataSize),
SyscallSucceedsWithValue(kHalfDataSize));
EXPECT_EQ(absl::string_view(kData, kHalfDataSize),
absl::string_view(actual, kHalfDataSize));
// Verify that the offset pointer has been updated.
EXPECT_EQ(offset, kThreeFourthsDataSize);
}
TEST(SendFileTest, DoNotSendfileIfOutfileIsAppendOnly) {
// Create temp files.
constexpr char kData[] = "And by opposing end them: to die, to sleep";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as append only.
const FileDescriptor outf =
ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY | O_APPEND));
// Send data and verify that sendfile returns the correct errno.
EXPECT_THAT(sendfile(outf.get(), inf.get(), nullptr, kDataSize),
SyscallFailsWithErrno(EINVAL));
}
TEST(SendFileTest, AppendCheckOrdering) {
constexpr char kData[] = "And by opposing end them: to die, to sleep";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const FileDescriptor read =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_RDONLY));
const FileDescriptor write =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_WRONLY));
const FileDescriptor append =
ASSERT_NO_ERRNO_AND_VALUE(Open(file.path(), O_APPEND));
// Check that read/write file mode is verified before append.
EXPECT_THAT(sendfile(append.get(), read.get(), nullptr, kDataSize),
SyscallFailsWithErrno(EBADF));
EXPECT_THAT(sendfile(write.get(), write.get(), nullptr, kDataSize),
SyscallFailsWithErrno(EBADF));
}
TEST(SendFileTest, DoNotSendfileIfOutfileIsNotWritable) {
// Create temp files.
constexpr char kData[] = "No more; and by a sleep, to say we end";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as read only.
const FileDescriptor outf =
ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_RDONLY));
// Send data and verify that sendfile returns the correct errno.
EXPECT_THAT(sendfile(outf.get(), inf.get(), nullptr, kDataSize),
SyscallFailsWithErrno(EBADF));
}
TEST(SendFileTest, DoNotSendfileIfInfileIsNotReadable) {
// Create temp files.
constexpr char kData[] = "the heart-ache, and the thousand natural shocks";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as write only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_WRONLY));
// Open the output file as write only.
const FileDescriptor outf =
ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Send data and verify that sendfile returns the correct errno.
EXPECT_THAT(sendfile(outf.get(), inf.get(), nullptr, kDataSize),
SyscallFailsWithErrno(EBADF));
}
TEST(SendFileTest, DoNotSendANegativeNumberOfBytes) {
// Create temp files.
constexpr char kData[] = "that Flesh is heir to? 'Tis a consummation";
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as write only.
const FileDescriptor outf =
ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Send data and verify that sendfile returns the correct errno.
EXPECT_THAT(sendfile(outf.get(), inf.get(), nullptr, -1),
SyscallFailsWithErrno(EINVAL));
}
TEST(SendFileTest, SendTheCorrectNumberOfBytesEvenIfWeTryToSendTooManyBytes) {
// Create temp files.
constexpr char kData[] = "devoutly to be wished. To die, to sleep,";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Open the output file as write only.
FileDescriptor outf;
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Send data and verify that sendfile returns the correct value.
int bytes_sent;
EXPECT_THAT(
bytes_sent = sendfile(outf.get(), inf.get(), nullptr, kDataSize + 100),
SyscallSucceedsWithValue(kDataSize));
// Close outf to avoid leak.
outf.reset();
// Open the output file as read only.
outf = ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_RDONLY));
// Verify that the output file has the correct data.
char actual[kDataSize];
ASSERT_THAT(read(outf.get(), &actual, bytes_sent),
SyscallSucceedsWithValue(kDataSize));
EXPECT_EQ(kData, absl::string_view(actual, bytes_sent));
}
TEST(SendFileTest, SendToNotARegularFile) {
// Make temp input directory and open as read only.
const TempPath dir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(dir.path(), O_RDONLY));
// Make temp output file and open as write only.
const TempPath out_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const FileDescriptor outf =
ASSERT_NO_ERRNO_AND_VALUE(Open(out_file.path(), O_WRONLY));
// Receive an error since a directory is not a regular file.
EXPECT_THAT(sendfile(outf.get(), inf.get(), nullptr, 0),
SyscallFailsWithErrno(EINVAL));
}
TEST(SendFileTest, SendPipeWouldBlock) {
// Create temp file.
constexpr char kData[] =
"The fool doth think he is wise, but the wise man knows himself to be a "
"fool.";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Setup the output named pipe.
int fds[2];
ASSERT_THAT(pipe2(fds, O_NONBLOCK), SyscallSucceeds());
const FileDescriptor rfd(fds[0]);
const FileDescriptor wfd(fds[1]);
// Fill up the pipe's buffer.
int pipe_size = -1;
ASSERT_THAT(pipe_size = fcntl(wfd.get(), F_GETPIPE_SZ), SyscallSucceeds());
std::vector<char> buf(2 * pipe_size);
ASSERT_THAT(write(wfd.get(), buf.data(), buf.size()),
SyscallSucceedsWithValue(pipe_size));
EXPECT_THAT(sendfile(wfd.get(), inf.get(), nullptr, kDataSize),
SyscallFailsWithErrno(EWOULDBLOCK));
}
TEST(SendFileTest, SendPipeEOF) {
// Create and open an empty input file.
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Setup the output named pipe.
int fds[2];
ASSERT_THAT(pipe2(fds, O_NONBLOCK), SyscallSucceeds());
const FileDescriptor rfd(fds[0]);
const FileDescriptor wfd(fds[1]);
EXPECT_THAT(sendfile(wfd.get(), inf.get(), nullptr, 123),
SyscallSucceedsWithValue(0));
}
TEST(SendFileTest, SendPipeBlocks) {
// Create temp file.
constexpr char kData[] =
"The fault, dear Brutus, is not in our stars, but in ourselves.";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
// Open the input file as read only.
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Setup the output named pipe.
int fds[2];
ASSERT_THAT(pipe(fds), SyscallSucceeds());
const FileDescriptor rfd(fds[0]);
const FileDescriptor wfd(fds[1]);
// Fill up the pipe's buffer.
int pipe_size = -1;
ASSERT_THAT(pipe_size = fcntl(wfd.get(), F_GETPIPE_SZ), SyscallSucceeds());
std::vector<char> buf(pipe_size);
ASSERT_THAT(write(wfd.get(), buf.data(), buf.size()),
SyscallSucceedsWithValue(pipe_size));
ScopedThread t([&]() {
absl::SleepFor(absl::Milliseconds(100));
ASSERT_THAT(read(rfd.get(), buf.data(), buf.size()),
SyscallSucceedsWithValue(pipe_size));
});
EXPECT_THAT(sendfile(wfd.get(), inf.get(), nullptr, kDataSize),
SyscallSucceedsWithValue(kDataSize));
}
TEST(SendFileTest, SendToSpecialFile) {
// Create temp file.
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), "", TempPath::kDefaultFileMode));
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR));
constexpr int kSize = 0x7ff;
ASSERT_THAT(ftruncate(inf.get(), kSize), SyscallSucceeds());
auto eventfd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD());
// eventfd can accept a number of bytes which is a multiple of 8.
EXPECT_THAT(sendfile(eventfd.get(), inf.get(), nullptr, 0xfffff),
SyscallSucceedsWithValue(kSize & (~7)));
}
TEST(SendFileTest, SendFileToPipe) {
// Create temp file.
constexpr char kData[] = "<insert-quote-here>";
constexpr int kDataSize = sizeof(kData) - 1;
const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFileWith(
GetAbsoluteTestTmpdir(), kData, TempPath::kDefaultFileMode));
const FileDescriptor inf =
ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDONLY));
// Create a pipe for sending to a pipe.
int fds[2];
ASSERT_THAT(pipe(fds), SyscallSucceeds());
const FileDescriptor rfd(fds[0]);
const FileDescriptor wfd(fds[1]);
// Expect to read up to the given size.
std::vector<char> buf(kDataSize);
ScopedThread t([&]() {
absl::SleepFor(absl::Milliseconds(100));
ASSERT_THAT(read(rfd.get(), buf.data(), buf.size()),
SyscallSucceedsWithValue(kDataSize));
});
// Send with twice the size of the file, which should hit EOF.
EXPECT_THAT(sendfile(wfd.get(), inf.get(), nullptr, kDataSize * 2),
SyscallSucceedsWithValue(kDataSize));
}
static volatile int signaled = 0;
void SigUsr1Handler(int sig, siginfo_t* info, void* context) { signaled = 1; }
TEST(SendFileTest, ToEventFDDoesNotSpin_NoRandomSave) {
FileDescriptor efd = ASSERT_NO_ERRNO_AND_VALUE(NewEventFD(0, 0));
// Write the maximum value of an eventfd to a file.
const uint64_t kMaxEventfdValue = 0xfffffffffffffffe;
const auto tempfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const auto tempfd = ASSERT_NO_ERRNO_AND_VALUE(Open(tempfile.path(), O_RDWR));
ASSERT_THAT(
pwrite(tempfd.get(), &kMaxEventfdValue, sizeof(kMaxEventfdValue), 0),
SyscallSucceedsWithValue(sizeof(kMaxEventfdValue)));
// Set the eventfd's value to 1.
const uint64_t kOne = 1;
ASSERT_THAT(write(efd.get(), &kOne, sizeof(kOne)),
SyscallSucceedsWithValue(sizeof(kOne)));
// Set up signal handler.
struct sigaction sa = {};
sa.sa_sigaction = SigUsr1Handler;
sa.sa_flags = SA_SIGINFO;
const auto cleanup_sigact =
ASSERT_NO_ERRNO_AND_VALUE(ScopedSigaction(SIGUSR1, sa));
// Send SIGUSR1 to this thread in 1 second.
struct sigevent sev = {};
sev.sigev_notify = SIGEV_THREAD_ID;
sev.sigev_signo = SIGUSR1;
sev.sigev_notify_thread_id = gettid();
auto timer = ASSERT_NO_ERRNO_AND_VALUE(TimerCreate(CLOCK_MONOTONIC, sev));
struct itimerspec its = {};
its.it_value = absl::ToTimespec(absl::Seconds(1));
DisableSave ds; // Asserting an EINTR.
ASSERT_NO_ERRNO(timer.Set(0, its));
// Sendfile from tempfd to the eventfd. Since the eventfd is not already at
// its maximum value, the eventfd is "ready for writing"; however, since the
// eventfd's existing value plus the new value would exceed the maximum, the
// write should internally fail with EWOULDBLOCK. In this case, sendfile()
// should block instead of spinning, and eventually be interrupted by our
// timer. See b/172075629.
EXPECT_THAT(
sendfile(efd.get(), tempfd.get(), nullptr, sizeof(kMaxEventfdValue)),
SyscallFailsWithErrno(EINTR));
// Signal should have been handled.
EXPECT_EQ(signaled, 1);
}
} // namespace
} // namespace testing
} // namespace gvisor
|