Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame^] | 28 | #include "tests.h" |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 29 | #include <assert.h> |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdint.h> |
| 34 | #include <unistd.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/syscall.h> |
| 39 | |
| 40 | #ifdef __NR_sendfile |
| 41 | |
| 42 | int |
| 43 | main(int ac, const char **av) |
| 44 | { |
| 45 | assert(ac == 2); |
| 46 | |
| 47 | (void) close(0); |
| 48 | if (open("/dev/zero", O_RDONLY) != 0) |
| 49 | return 77; |
| 50 | |
| 51 | int sv[2]; |
| 52 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) |
| 53 | return 77; |
| 54 | |
| 55 | int reg_in = open(av[1], O_RDONLY); |
| 56 | if (reg_in < 0) |
| 57 | return 77; |
| 58 | |
| 59 | struct stat stb; |
| 60 | if (fstat(reg_in, &stb)) |
| 61 | return 77; |
| 62 | const size_t blen = stb.st_size / 3; |
| 63 | const size_t alen = stb.st_size - blen; |
| 64 | assert(S_ISREG(stb.st_mode) && blen > 0); |
| 65 | |
| 66 | const size_t page_len = sysconf(_SC_PAGESIZE); |
| 67 | if (!syscall(__NR_sendfile, 0, 1, NULL, page_len) || |
| 68 | EBADF != errno) |
| 69 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 70 | printf("sendfile(0, 1, NULL, %lu) = -1 EBADF (Bad file descriptor)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 71 | (unsigned long) page_len); |
| 72 | |
| 73 | void *p = mmap(NULL, page_len * 2, PROT_READ | PROT_WRITE, |
| 74 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 75 | if (MAP_FAILED == p || munmap(p + page_len, page_len)) |
| 76 | return 77; |
| 77 | |
| 78 | if (!syscall(__NR_sendfile, 0, 1, p + page_len, page_len)) |
| 79 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 80 | printf("sendfile(0, 1, %#lx, %lu) = -1 EFAULT (Bad address)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 81 | (unsigned long) p + page_len, (unsigned long) page_len); |
| 82 | |
| 83 | if (syscall(__NR_sendfile, sv[1], reg_in, NULL, alen) != (long) alen) |
| 84 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 85 | printf("sendfile(%d, %d, NULL, %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 86 | sv[1], reg_in, (unsigned long) alen, |
| 87 | (unsigned long) alen); |
| 88 | |
| 89 | uint32_t *p_off = p + page_len - sizeof(uint32_t); |
| 90 | if (syscall(__NR_sendfile, sv[1], reg_in, p_off, alen) != (long) alen) { |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 91 | printf("sendfile(%d, %d, %#lx, %lu) = -1 EFAULT (Bad address)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 92 | sv[1], reg_in, (unsigned long) p_off, |
| 93 | (unsigned long) alen); |
| 94 | --p_off; |
| 95 | if (syscall(__NR_sendfile, sv[1], reg_in, p_off, alen) |
| 96 | != (long) alen) |
| 97 | return 77; |
| 98 | } |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 99 | printf("sendfile(%d, %d, [0] => [%lu], %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 100 | sv[1], reg_in, (unsigned long) alen, |
| 101 | (unsigned long) alen, (unsigned long) alen); |
| 102 | |
| 103 | if (syscall(__NR_sendfile, sv[1], reg_in, p_off, stb.st_size + 1) |
| 104 | != (long) blen) |
| 105 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 106 | printf("sendfile(%d, %d, [%lu] => [%lu], %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 107 | sv[1], reg_in, (unsigned long) alen, |
| 108 | (unsigned long) stb.st_size, |
| 109 | (unsigned long) stb.st_size + 1, |
| 110 | (unsigned long) blen); |
| 111 | |
| 112 | if (p_off == p + page_len - sizeof(uint64_t)) { |
| 113 | uint64_t *p_off64 = (uint64_t *) p_off; |
| 114 | *p_off64 = 0xcafef00dfacefeed; |
| 115 | if (!syscall(__NR_sendfile, sv[1], reg_in, p_off64, 1)) |
| 116 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 117 | printf("sendfile(%d, %d, [14627392582579060461], 1) = -1 EINVAL (Invalid argument)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 118 | sv[1], reg_in); |
| 119 | *p_off64 = 0xdefaced; |
| 120 | } else { |
| 121 | *p_off = 0xdefaced; |
| 122 | } |
| 123 | if (syscall(__NR_sendfile, sv[1], reg_in, p_off, 1)) |
| 124 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 125 | printf("sendfile(%d, %d, [233811181], 1) = 0\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 126 | sv[1], reg_in); |
| 127 | |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 128 | puts("+++ exited with 0 +++"); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | #else |
| 133 | |
| 134 | int |
| 135 | main(void) |
| 136 | { |
| 137 | return 77; |
| 138 | } |
| 139 | |
| 140 | #endif |