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