summaryrefslogtreecommitdiffstats
path: root/runtime/base/safe_copy.cc
blob: c76ea113d89b6efd2ed4ca67b706cf81d535653c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "safe_copy.h"

#include <sys/uio.h>
#include <sys/user.h>
#include <unistd.h>

#include <algorithm>

#include <android-base/macros.h>

#include "runtime/base/bit_utils.h"

namespace art {

ssize_t SafeCopy(void *dst, const void *src, size_t len) {
#if defined(__linux__)
  struct iovec dst_iov = {
    .iov_base = dst,
    .iov_len = len,
  };

  // Split up the remote read across page boundaries.
  // From the manpage:
  //   A partial read/write may result if one of the remote_iov elements points to an invalid
  //   memory region in the remote process.
  //
  //   Partial transfers apply at the granularity of iovec elements.  These system calls won't
  //   perform a partial transfer that splits a single iovec element.
  constexpr size_t kMaxIovecs = 64;
  struct iovec src_iovs[kMaxIovecs];
  size_t iovecs_used = 0;

  const char* cur = static_cast<const char*>(src);
  while (len > 0) {
    if (iovecs_used == kMaxIovecs) {
      errno = EINVAL;
      return -1;
    }

    src_iovs[iovecs_used].iov_base = const_cast<char*>(cur);
    if (!IsAlignedParam(cur, PAGE_SIZE)) {
      src_iovs[iovecs_used].iov_len = AlignUp(cur, PAGE_SIZE) - cur;
    } else {
      src_iovs[iovecs_used].iov_len = PAGE_SIZE;
    }

    src_iovs[iovecs_used].iov_len = std::min(src_iovs[iovecs_used].iov_len, len);

    len -= src_iovs[iovecs_used].iov_len;
    cur += src_iovs[iovecs_used].iov_len;
    ++iovecs_used;
  }

  ssize_t rc = process_vm_readv(getpid(), &dst_iov, 1, src_iovs, iovecs_used, 0);
  if (rc == -1) {
    return 0;
  }
  return rc;
#else
  UNUSED(dst, src, len);
  return -1;
#endif
}

}  // namespace art