blob: ad75aa7b2812faceac28e77b0de8e6d88570c235 [file] [log] [blame]
Josh Gao682282e2017-04-17 18:31:26 -07001/*
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 Sehr10db8fe2018-07-18 11:01:20 -070019#ifdef __linux__
Josh Gao682282e2017-04-17 18:31:26 -070020#include <sys/uio.h>
Josh Gao9a2eea32017-04-19 15:44:20 -070021#include <sys/user.h>
David Sehr10db8fe2018-07-18 11:01:20 -070022#endif
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include <unistd.h>
Josh Gao9a2eea32017-04-19 15:44:20 -070024
25#include <algorithm>
Josh Gao682282e2017-04-17 18:31:26 -070026
27#include <android-base/macros.h>
28
David Sehr1979c642018-04-26 14:41:18 -070029#include "bit_utils.h"
Josh Gao9a2eea32017-04-19 15:44:20 -070030
Josh Gao682282e2017-04-17 18:31:26 -070031namespace art {
32
33ssize_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 Gao682282e2017-04-17 18:31:26 -070039
Josh Gao9a2eea32017-04-19 15:44:20 -070040 // 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 Gao682282e2017-04-17 18:31:26 -070073 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