Dmitry V. Levin | 9f3a6af | 2016-04-02 01:08:24 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Check decoding of pread64 and pwrite64 syscalls. |
| 3 | * |
| 4 | * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include "tests.h" |
| 31 | |
| 32 | #include <fcntl.h> |
| 33 | #include <stdio.h> |
| 34 | #include <sys/uio.h> |
| 35 | #include <unistd.h> |
| 36 | |
| 37 | int |
| 38 | main(void) |
| 39 | { |
| 40 | tprintf("%s", ""); |
| 41 | |
| 42 | static char tmp[] = "pread64-pwrite64-tmpfile"; |
| 43 | if (open(tmp, O_CREAT|O_RDONLY|O_TRUNC, 0600) != 0) |
| 44 | perror_msg_and_fail("creat: %s", tmp); |
| 45 | if (open(tmp, O_WRONLY) != 1) |
| 46 | perror_msg_and_fail("open: %s", tmp); |
| 47 | |
| 48 | char *nil = tail_alloc(1); |
| 49 | *nil = '\0'; |
| 50 | |
| 51 | static const char w_c[] = "0123456789abcde"; |
| 52 | const unsigned int w_len = LENGTH_OF(w_c); |
| 53 | const char *w_d = hexdump_strdup(w_c); |
| 54 | const void *w = tail_memdup(w_c, w_len); |
| 55 | |
| 56 | static const char r0_c[] = "01234567"; |
| 57 | const char *r0_d = hexdump_strdup(r0_c); |
| 58 | const unsigned int r0_len = (w_len + 1) / 2; |
| 59 | void *r0 = tail_alloc(r0_len); |
| 60 | |
| 61 | static const char r1_c[] = "89abcde"; |
| 62 | const char *r1_d = hexdump_strdup(r1_c); |
| 63 | const unsigned int r1_len = w_len - r0_len; |
| 64 | void *r1 = tail_alloc(w_len); |
| 65 | |
| 66 | void *efault = r1 - get_page_size(); |
| 67 | |
| 68 | long rc; |
| 69 | |
| 70 | rc = pwrite(1, w, 0, 0); |
| 71 | if (rc) |
| 72 | perror_msg_and_fail("pwrite64: expected 0, returned %ld", rc); |
| 73 | tprintf("pwrite64(1, \"\", 0, 0) = 0\n"); |
| 74 | |
| 75 | rc = pwrite(1, w, w_len + 1, 0); |
| 76 | if (rc != -1) |
| 77 | perror_msg_and_fail("pwrite64: expected -1 EFAULT" |
| 78 | ", returned %ld", rc); |
| 79 | tprintf("pwrite64(1, %p, %u, 0) = -1 EFAULT (%m)\n", |
| 80 | w, w_len + 1); |
| 81 | |
| 82 | rc = pwrite(1, nil, 1, -3); |
| 83 | if (rc != -1) |
| 84 | perror_msg_and_fail("pwrite64: expected -1, returned %ld", rc); |
| 85 | tprintf("pwrite64(1, \"\\0\", 1, -3) = -1 EINVAL (%m)\n"); |
| 86 | |
| 87 | rc = pwrite(1, w, w_len, 0); |
| 88 | if (rc != (int) w_len) |
| 89 | perror_msg_and_fail("pwrite64: expected %u, returned %ld", |
| 90 | w_len, rc); |
| 91 | tprintf("pwrite64(1, \"%s\", %u, 0) = %ld\n" |
| 92 | " | 00000 %-49s %-16s |\n", |
| 93 | w_c, w_len, rc, w_d, w_c); |
| 94 | close(1); |
| 95 | |
| 96 | rc = pread(0, efault, 1, 0); |
| 97 | if (rc != -1) |
| 98 | perror_msg_and_fail("pread64: expected -1, returned %ld", rc); |
| 99 | tprintf("pread64(0, %p, 1, 0) = -1 EFAULT (%m)\n", efault); |
| 100 | |
| 101 | rc = pread(0, efault, 2, -7); |
| 102 | if (rc != -1) |
| 103 | perror_msg_and_fail("pread64: expected -1, returned %ld", rc); |
| 104 | tprintf("pread64(0, %p, 2, -7) = -1 EINVAL (%m)\n", efault); |
| 105 | |
| 106 | rc = pread(0, r0, r0_len, 0); |
| 107 | if (rc != (int) r0_len) |
| 108 | perror_msg_and_fail("pread64: expected %u, returned %ld", |
| 109 | r0_len, rc); |
| 110 | tprintf("pread64(0, \"%s\", %u, 0) = %ld\n" |
| 111 | " | 00000 %-49s %-16s |\n", |
| 112 | r0_c, r0_len, rc, r0_d, r0_c); |
| 113 | |
| 114 | rc = pread(0, r1, w_len, r0_len); |
| 115 | if (rc != (int) r1_len) |
| 116 | perror_msg_and_fail("pread64: expected %u, returned %ld", |
| 117 | r1_len, rc); |
| 118 | tprintf("pread64(0, \"%s\", %u, %u) = %ld\n" |
| 119 | " | 00000 %-49s %-16s |\n", |
| 120 | r1_c, w_len, r0_len, rc, r1_d, r1_c); |
| 121 | close(0); |
| 122 | |
| 123 | const off_t offset = 0xdefaceddeadbeefLL; |
| 124 | const int rw_len = 8; |
| 125 | char *rw_buf = tail_alloc(rw_len); |
| 126 | |
| 127 | if (open("/dev/zero", O_RDONLY)) |
| 128 | perror_msg_and_fail("open"); |
| 129 | |
| 130 | rc = pread(0, rw_buf, rw_len, offset); |
| 131 | if (rc != rw_len) |
| 132 | perror_msg_and_fail("pread64: expected %d, returned %ld", |
| 133 | rw_len, rc); |
| 134 | |
| 135 | tprintf("%s(%d, \"%s\", %d, %lld) = %ld\n" |
| 136 | " | 00000 %-49s %-16s |\n", |
| 137 | "pread64", 0, "\\0\\0\\0\\0\\0\\0\\0\\0", |
| 138 | rw_len, (long long) offset, rc, |
| 139 | " 00 00 00 00 00 00 00 00", "........"); |
| 140 | |
| 141 | if (open("/dev/null", O_WRONLY) != 1) |
| 142 | perror_msg_and_fail("open"); |
| 143 | |
| 144 | rc = pwrite(1, rw_buf, rw_len, offset); |
| 145 | if (rc != rw_len) |
| 146 | perror_msg_and_fail("pwrite64: expected %d, returned %ld", |
| 147 | rw_len, rc); |
| 148 | |
| 149 | tprintf("%s(%d, \"%s\", %d, %lld) = %ld\n" |
| 150 | " | 00000 %-49s %-16s |\n", |
| 151 | "pwrite64", 1, "\\0\\0\\0\\0\\0\\0\\0\\0", |
| 152 | rw_len, (long long) offset, rc, |
| 153 | " 00 00 00 00 00 00 00 00", "........"); |
| 154 | |
| 155 | tprintf("+++ exited with 0 +++\n"); |
| 156 | return 0; |
| 157 | } |