• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #ifdef __linux__
20 #include <sys/uio.h>
21 #include <sys/user.h>
22 #endif
23 #include <unistd.h>
24 
25 #include <algorithm>
26 
27 #include <android-base/macros.h>
28 
29 #include "bit_utils.h"
30 
31 namespace art {
32 
SafeCopy(void * dst,const void * src,size_t len)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   };
39 
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);
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
84