Elliott Hughes | be57a40 | 2015-06-10 17:24:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
Elliott Hughes | cf59e19 | 2021-10-13 18:25:21 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/types.h> |
Elliott Hughes | be57a40 | 2015-06-10 17:24:20 -0700 | [diff] [blame] | 22 | #include <sys/uio.h> |
Elliott Hughes | cf59e19 | 2021-10-13 18:25:21 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Elliott Hughes | be57a40 | 2015-06-10 17:24:20 -0700 | [diff] [blame] | 24 | |
Mark Salyzyn | 68a3bcc | 2018-11-13 07:35:21 -0800 | [diff] [blame] | 25 | #include <android-base/file.h> |
Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 26 | |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 27 | #include "utils.h" |
| 28 | |
Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 29 | TEST(sys_uio, readv_writev) { |
| 30 | TemporaryFile tf; |
| 31 | |
| 32 | char buf1[] = "hello"; |
| 33 | char buf2[] = "world"; |
| 34 | iovec ios[] = { { buf1, 5 }, { buf2, 5 } }; |
| 35 | |
| 36 | ASSERT_EQ(10, writev(tf.fd, ios, 2)); |
| 37 | |
| 38 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_SET)); |
| 39 | |
| 40 | memset(buf1, '1', sizeof(buf1)); |
| 41 | memset(buf2, '2', sizeof(buf2)); |
| 42 | |
| 43 | ASSERT_EQ(10, readv(tf.fd, ios, 2)); |
| 44 | buf1[5] = buf2[5] = '\0'; |
| 45 | ASSERT_STREQ("hello", buf1); |
| 46 | ASSERT_STREQ("world", buf2); |
| 47 | } |
| 48 | |
| 49 | template <typename ReadFn, typename WriteFn> |
| 50 | void TestPreadVPwriteV(ReadFn read_fn, WriteFn write_fn) { |
| 51 | TemporaryFile tf; |
| 52 | |
| 53 | char buf[] = "world"; |
| 54 | iovec ios[] = { { buf, 5 } }; |
| 55 | |
| 56 | ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 5)); |
| 57 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR)); |
| 58 | |
| 59 | strcpy(buf, "hello"); |
| 60 | ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 0)); |
| 61 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR)); |
| 62 | |
| 63 | ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 5)); |
| 64 | ASSERT_STREQ("world", buf); |
| 65 | ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 0)); |
| 66 | ASSERT_STREQ("hello", buf); |
| 67 | } |
| 68 | |
| 69 | TEST(sys_uio, preadv_pwritev) { |
| 70 | TestPreadVPwriteV(preadv, pwritev); |
| 71 | } |
| 72 | |
| 73 | TEST(sys_uio, preadv64_pwritev64) { |
| 74 | TestPreadVPwriteV(preadv64, pwritev64); |
| 75 | } |
| 76 | |
Elliott Hughes | cf59e19 | 2021-10-13 18:25:21 -0700 | [diff] [blame] | 77 | template <typename ReadFn, typename WriteFn> |
| 78 | void TestPreadV2PwriteV2(ReadFn read_fn, WriteFn write_fn) { |
| 79 | TemporaryFile tf; |
| 80 | |
| 81 | char buf[] = "world"; |
| 82 | iovec ios[] = {{buf, 5}}; |
| 83 | |
| 84 | ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 5, 0)) << strerror(errno); |
| 85 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR)); |
| 86 | |
| 87 | strcpy(buf, "hello"); |
| 88 | ASSERT_EQ(5, write_fn(tf.fd, ios, 1, 0, 0)) << strerror(errno); |
| 89 | ASSERT_EQ(0, lseek(tf.fd, 0, SEEK_CUR)); |
| 90 | |
| 91 | ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 5, 0)) << strerror(errno); |
| 92 | ASSERT_STREQ("world", buf); |
| 93 | ASSERT_EQ(5, read_fn(tf.fd, ios, 1, 0, 0)) << strerror(errno); |
| 94 | ASSERT_STREQ("hello", buf); |
| 95 | } |
| 96 | |
| 97 | TEST(sys_uio, preadv2_pwritev2) { |
| 98 | #if defined(__BIONIC__) |
| 99 | TestPreadV2PwriteV2(preadv2, pwritev2); |
| 100 | #else |
| 101 | GTEST_SKIP() << "preadv2/pwritev2 not available"; |
| 102 | #endif |
| 103 | } |
| 104 | |
| 105 | TEST(sys_uio, preadv64v2_pwritev64v2) { |
| 106 | #if defined(__BIONIC__) |
| 107 | TestPreadV2PwriteV2(preadv64v2, pwritev64v2); |
| 108 | #else |
| 109 | GTEST_SKIP() << "preadv2/pwritev2 not available"; |
| 110 | #endif |
| 111 | } |
| 112 | |
Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 113 | TEST(sys_uio, process_vm_readv) { |
Elliott Hughes | be57a40 | 2015-06-10 17:24:20 -0700 | [diff] [blame] | 114 | ASSERT_EQ(0, process_vm_readv(0, nullptr, 0, nullptr, 0, 0)); |
Pavel Labath | cc44100 | 2017-01-12 11:56:39 +0000 | [diff] [blame] | 115 | |
| 116 | // Test that we can read memory from our own process |
| 117 | char src[1024] = "This is the source buffer containing some data"; |
| 118 | char dst[1024] = ""; |
| 119 | iovec remote = { src, sizeof src }; |
| 120 | iovec local = { dst, sizeof dst }; |
| 121 | ASSERT_EQ(ssize_t(sizeof src), process_vm_readv(getpid(), &local, 1, &remote, 1, 0)); |
| 122 | // Check whether data was copied (in the correct direction) |
| 123 | ASSERT_EQ('T', dst[0]); |
| 124 | ASSERT_EQ(0, memcmp(src, dst, sizeof src)); |
| 125 | |
| 126 | // Reading from non-allocated memory should return an error |
| 127 | remote = { nullptr, sizeof dst }; |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 128 | errno = 0; |
Pavel Labath | cc44100 | 2017-01-12 11:56:39 +0000 | [diff] [blame] | 129 | ASSERT_EQ(-1, process_vm_readv(getpid(), &local, 1, &remote, 1, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 130 | ASSERT_ERRNO(EFAULT); |
Elliott Hughes | be57a40 | 2015-06-10 17:24:20 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Elliott Hughes | 6f4594d | 2015-08-26 13:27:43 -0700 | [diff] [blame] | 133 | TEST(sys_uio, process_vm_writev) { |
Elliott Hughes | be57a40 | 2015-06-10 17:24:20 -0700 | [diff] [blame] | 134 | ASSERT_EQ(0, process_vm_writev(0, nullptr, 0, nullptr, 0, 0)); |
Pavel Labath | cc44100 | 2017-01-12 11:56:39 +0000 | [diff] [blame] | 135 | |
| 136 | // Test that we can read memory from our own process |
| 137 | char src[1024] = "This is the source buffer containing some data"; |
| 138 | char dst[1024] = ""; |
| 139 | iovec remote = { dst, sizeof dst }; |
| 140 | iovec local = { src, sizeof src }; |
| 141 | ASSERT_EQ(ssize_t(sizeof src), process_vm_writev(getpid(), &local, 1, &remote, 1, 0)); |
| 142 | // Check whether data was copied (in the correct direction) |
| 143 | ASSERT_EQ('T', dst[0]); |
| 144 | ASSERT_EQ(0, memcmp(src, dst, sizeof src)); |
| 145 | |
| 146 | // Writing to non-allocated memory should return an error |
| 147 | remote = { nullptr, sizeof dst }; |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 148 | errno = 0; |
Pavel Labath | cc44100 | 2017-01-12 11:56:39 +0000 | [diff] [blame] | 149 | ASSERT_EQ(-1, process_vm_writev(getpid(), &local, 1, &remote, 1, 0)); |
Elliott Hughes | 95646e6 | 2023-09-21 14:11:19 -0700 | [diff] [blame^] | 150 | ASSERT_ERRNO(EFAULT); |
Elliott Hughes | be57a40 | 2015-06-10 17:24:20 -0700 | [diff] [blame] | 151 | } |