Josh Gao | 682282e | 2017-04-17 18:31:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "safe_copy.h" |
| 18 | |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 19 | #ifdef __linux__ |
Josh Gao | 682282e | 2017-04-17 18:31:26 -0700 | [diff] [blame] | 20 | #include <sys/uio.h> |
Josh Gao | 9a2eea3 | 2017-04-19 15:44:20 -0700 | [diff] [blame] | 21 | #include <sys/user.h> |
David Sehr | 10db8fe | 2018-07-18 11:01:20 -0700 | [diff] [blame] | 22 | #endif |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Josh Gao | 9a2eea3 | 2017-04-19 15:44:20 -0700 | [diff] [blame] | 24 | |
| 25 | #include <algorithm> |
Josh Gao | 682282e | 2017-04-17 18:31:26 -0700 | [diff] [blame] | 26 | |
| 27 | #include <android-base/macros.h> |
| 28 | |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 29 | #include "bit_utils.h" |
Josh Gao | 9a2eea3 | 2017-04-19 15:44:20 -0700 | [diff] [blame] | 30 | |
Josh Gao | 682282e | 2017-04-17 18:31:26 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
| 33 | ssize_t SafeCopy(void *dst, const void *src, size_t len) { |
| 34 | #if defined(__linux__) |
| 35 | struct iovec dst_iov = { |
| 36 | .iov_base = dst, |
| 37 | .iov_len = len, |
| 38 | }; |
Josh Gao | 682282e | 2017-04-17 18:31:26 -0700 | [diff] [blame] | 39 | |
Josh Gao | 9a2eea3 | 2017-04-19 15:44:20 -0700 | [diff] [blame] | 40 | // Split up the remote read across page boundaries. |
| 41 | // From the manpage: |
| 42 | // A partial read/write may result if one of the remote_iov elements points to an invalid |
| 43 | // memory region in the remote process. |
| 44 | // |
| 45 | // Partial transfers apply at the granularity of iovec elements. These system calls won't |
| 46 | // perform a partial transfer that splits a single iovec element. |
| 47 | constexpr size_t kMaxIovecs = 64; |
| 48 | struct iovec src_iovs[kMaxIovecs]; |
| 49 | size_t iovecs_used = 0; |
| 50 | |
| 51 | const char* cur = static_cast<const char*>(src); |
| 52 | while (len > 0) { |
| 53 | if (iovecs_used == kMaxIovecs) { |
| 54 | errno = EINVAL; |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | src_iovs[iovecs_used].iov_base = const_cast<char*>(cur); |
| 59 | if (!IsAlignedParam(cur, PAGE_SIZE)) { |
| 60 | src_iovs[iovecs_used].iov_len = AlignUp(cur, PAGE_SIZE) - cur; |
| 61 | } else { |
| 62 | src_iovs[iovecs_used].iov_len = PAGE_SIZE; |
| 63 | } |
| 64 | |
| 65 | src_iovs[iovecs_used].iov_len = std::min(src_iovs[iovecs_used].iov_len, len); |
| 66 | |
| 67 | len -= src_iovs[iovecs_used].iov_len; |
| 68 | cur += src_iovs[iovecs_used].iov_len; |
| 69 | ++iovecs_used; |
| 70 | } |
| 71 | |
| 72 | ssize_t rc = process_vm_readv(getpid(), &dst_iov, 1, src_iovs, iovecs_used, 0); |
Josh Gao | 682282e | 2017-04-17 18:31:26 -0700 | [diff] [blame] | 73 | if (rc == -1) { |
| 74 | return 0; |
| 75 | } |
| 76 | return rc; |
| 77 | #else |
| 78 | UNUSED(dst, src, len); |
| 79 | return -1; |
| 80 | #endif |
| 81 | } |
| 82 | |
| 83 | } // namespace art |