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 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | # include "config.h" |
| 30 | #endif |
| 31 | |
| 32 | #include <assert.h> |
| 33 | #include <errno.h> |
| 34 | #include <fcntl.h> |
| 35 | #include <stdio.h> |
| 36 | #include <stdint.h> |
| 37 | #include <unistd.h> |
| 38 | #include <sys/mman.h> |
| 39 | #include <sys/socket.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <sys/syscall.h> |
| 42 | |
| 43 | #ifdef __NR_sendfile |
| 44 | |
| 45 | int |
| 46 | main(int ac, const char **av) |
| 47 | { |
| 48 | assert(ac == 2); |
| 49 | |
| 50 | (void) close(0); |
| 51 | if (open("/dev/zero", O_RDONLY) != 0) |
| 52 | return 77; |
| 53 | |
| 54 | int sv[2]; |
| 55 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) |
| 56 | return 77; |
| 57 | |
| 58 | int reg_in = open(av[1], O_RDONLY); |
| 59 | if (reg_in < 0) |
| 60 | return 77; |
| 61 | |
| 62 | struct stat stb; |
| 63 | if (fstat(reg_in, &stb)) |
| 64 | return 77; |
| 65 | const size_t blen = stb.st_size / 3; |
| 66 | const size_t alen = stb.st_size - blen; |
| 67 | assert(S_ISREG(stb.st_mode) && blen > 0); |
| 68 | |
| 69 | const size_t page_len = sysconf(_SC_PAGESIZE); |
| 70 | if (!syscall(__NR_sendfile, 0, 1, NULL, page_len) || |
| 71 | EBADF != errno) |
| 72 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 73 | 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] | 74 | (unsigned long) page_len); |
| 75 | |
| 76 | void *p = mmap(NULL, page_len * 2, PROT_READ | PROT_WRITE, |
| 77 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 78 | if (MAP_FAILED == p || munmap(p + page_len, page_len)) |
| 79 | return 77; |
| 80 | |
| 81 | if (!syscall(__NR_sendfile, 0, 1, p + page_len, page_len)) |
| 82 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 83 | printf("sendfile(0, 1, %#lx, %lu) = -1 EFAULT (Bad address)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 84 | (unsigned long) p + page_len, (unsigned long) page_len); |
| 85 | |
| 86 | if (syscall(__NR_sendfile, sv[1], reg_in, NULL, alen) != (long) alen) |
| 87 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 88 | printf("sendfile(%d, %d, NULL, %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 89 | sv[1], reg_in, (unsigned long) alen, |
| 90 | (unsigned long) alen); |
| 91 | |
| 92 | uint32_t *p_off = p + page_len - sizeof(uint32_t); |
| 93 | 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] | 94 | printf("sendfile(%d, %d, %#lx, %lu) = -1 EFAULT (Bad address)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 95 | sv[1], reg_in, (unsigned long) p_off, |
| 96 | (unsigned long) alen); |
| 97 | --p_off; |
| 98 | if (syscall(__NR_sendfile, sv[1], reg_in, p_off, alen) |
| 99 | != (long) alen) |
| 100 | return 77; |
| 101 | } |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 102 | printf("sendfile(%d, %d, [0] => [%lu], %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 103 | sv[1], reg_in, (unsigned long) alen, |
| 104 | (unsigned long) alen, (unsigned long) alen); |
| 105 | |
| 106 | if (syscall(__NR_sendfile, sv[1], reg_in, p_off, stb.st_size + 1) |
| 107 | != (long) blen) |
| 108 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 109 | printf("sendfile(%d, %d, [%lu] => [%lu], %lu) = %lu\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 110 | sv[1], reg_in, (unsigned long) alen, |
| 111 | (unsigned long) stb.st_size, |
| 112 | (unsigned long) stb.st_size + 1, |
| 113 | (unsigned long) blen); |
| 114 | |
| 115 | if (p_off == p + page_len - sizeof(uint64_t)) { |
| 116 | uint64_t *p_off64 = (uint64_t *) p_off; |
| 117 | *p_off64 = 0xcafef00dfacefeed; |
| 118 | if (!syscall(__NR_sendfile, sv[1], reg_in, p_off64, 1)) |
| 119 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 120 | printf("sendfile(%d, %d, [14627392582579060461], 1) = -1 EINVAL (Invalid argument)\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 121 | sv[1], reg_in); |
| 122 | *p_off64 = 0xdefaced; |
| 123 | } else { |
| 124 | *p_off = 0xdefaced; |
| 125 | } |
| 126 | if (syscall(__NR_sendfile, sv[1], reg_in, p_off, 1)) |
| 127 | return 77; |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 128 | printf("sendfile(%d, %d, [233811181], 1) = 0\n", |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 129 | sv[1], reg_in); |
| 130 | |
Dmitry V. Levin | f14a8e1 | 2015-08-26 23:25:06 +0000 | [diff] [blame] | 131 | puts("+++ exited with 0 +++"); |
Dmitry V. Levin | 4918285 | 2015-08-19 01:25:39 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | #else |
| 136 | |
| 137 | int |
| 138 | main(void) |
| 139 | { |
| 140 | return 77; |
| 141 | } |
| 142 | |
| 143 | #endif |