Aart Bik | 1a65052 | 2015-07-08 21:20:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | /* |
| 18 | * System utilities. |
| 19 | */ |
| 20 | #include "DexFile.h" |
| 21 | #include "SysUtil.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <unistd.h> |
| 26 | #include <string.h> |
| 27 | #if !defined(__MINGW32__) |
| 28 | # include <sys/mman.h> |
| 29 | #endif |
| 30 | #include <limits.h> |
| 31 | #include <errno.h> |
| 32 | |
Orion Hodson | 9912893 | 2019-01-27 10:51:50 +0000 | [diff] [blame] | 33 | /* |
| 34 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of |
| 35 | * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's |
| 36 | * not already defined, then define it here. |
| 37 | */ |
| 38 | #ifndef TEMP_FAILURE_RETRY |
| 39 | /* Used to retry syscalls that can return EINTR. */ |
| 40 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 41 | typeof (exp) _rc; \ |
| 42 | do { \ |
| 43 | _rc = (exp); \ |
| 44 | } while (_rc == -1 && errno == EINTR); \ |
| 45 | _rc; }) |
| 46 | #endif |
Aart Bik | 1a65052 | 2015-07-08 21:20:41 +0000 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Create an anonymous shared memory segment large enough to hold "length" |
| 50 | * bytes. The actual segment may be larger because mmap() operates on |
| 51 | * page boundaries (usually 4K). |
| 52 | */ |
| 53 | static void* sysCreateAnonShmem(size_t length) |
| 54 | { |
| 55 | #if !defined(__MINGW32__) |
| 56 | void* ptr; |
| 57 | |
| 58 | ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, |
| 59 | MAP_SHARED | MAP_ANON, -1, 0); |
| 60 | if (ptr == MAP_FAILED) { |
| 61 | ALOGW("mmap(%d, RW, SHARED|ANON) failed: %s", (int) length, |
| 62 | strerror(errno)); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | return ptr; |
| 67 | #else |
| 68 | ALOGE("sysCreateAnonShmem not implemented."); |
| 69 | return NULL; |
| 70 | #endif |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Create a private anonymous storage area. |
| 75 | */ |
| 76 | int sysCreatePrivateMap(size_t length, MemMapping* pMap) |
| 77 | { |
| 78 | void* memPtr; |
| 79 | |
| 80 | memPtr = sysCreateAnonShmem(length); |
| 81 | if (memPtr == NULL) |
| 82 | return -1; |
| 83 | |
| 84 | pMap->addr = pMap->baseAddr = memPtr; |
| 85 | pMap->length = pMap->baseLength = length; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Determine the current offset and remaining length of the open file. |
| 91 | */ |
| 92 | static int getFileStartAndLength(int fd, off_t *start_, size_t *length_) |
| 93 | { |
| 94 | off_t start, end; |
| 95 | size_t length; |
| 96 | |
| 97 | assert(start_ != NULL); |
| 98 | assert(length_ != NULL); |
| 99 | |
| 100 | start = lseek(fd, 0L, SEEK_CUR); |
| 101 | end = lseek(fd, 0L, SEEK_END); |
| 102 | (void) lseek(fd, start, SEEK_SET); |
| 103 | |
| 104 | if (start == (off_t) -1 || end == (off_t) -1) { |
| 105 | ALOGE("could not determine length of file"); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | length = end - start; |
| 110 | if (length == 0) { |
| 111 | ALOGE("file is empty"); |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | *start_ = start; |
| 116 | *length_ = length; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | #if defined(__MINGW32__) |
| 122 | int sysFakeMapFile(int fd, MemMapping* pMap) |
| 123 | { |
| 124 | /* No MMAP, just fake it by copying the bits. |
| 125 | For Win32 we could use MapViewOfFile if really necessary |
| 126 | (see libs/utils/FileMap.cpp). |
| 127 | */ |
| 128 | off_t start; |
| 129 | size_t length; |
| 130 | void* memPtr; |
| 131 | |
| 132 | assert(pMap != NULL); |
| 133 | |
| 134 | if (getFileStartAndLength(fd, &start, &length) < 0) |
| 135 | return -1; |
| 136 | |
| 137 | memPtr = malloc(length); |
| 138 | if (read(fd, memPtr, length) < 0) { |
| 139 | ALOGW("read(fd=%d, start=%d, length=%d) failed: %s", (int) length, |
| 140 | fd, (int) start, strerror(errno)); |
Aart Bik | f23d879 | 2016-05-25 13:43:59 -0700 | [diff] [blame] | 141 | free(memPtr); |
Aart Bik | 1a65052 | 2015-07-08 21:20:41 +0000 | [diff] [blame] | 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | pMap->baseAddr = pMap->addr = memPtr; |
| 146 | pMap->baseLength = pMap->length = length; |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | /* |
| 153 | * Map a file (from fd's current offset) into a private, read-write memory |
| 154 | * segment that will be marked read-only (a/k/a "writable read-only"). The |
| 155 | * file offset must be a multiple of the system page size. |
| 156 | * |
| 157 | * In some cases the mapping will be fully writable (e.g. for files on |
| 158 | * FAT filesystems). |
| 159 | * |
| 160 | * On success, returns 0 and fills out "pMap". On failure, returns a nonzero |
| 161 | * value and does not disturb "pMap". |
| 162 | */ |
| 163 | int sysMapFileInShmemWritableReadOnly(int fd, MemMapping* pMap) |
| 164 | { |
| 165 | #if !defined(__MINGW32__) |
| 166 | off_t start; |
| 167 | size_t length; |
| 168 | void* memPtr; |
| 169 | |
| 170 | assert(pMap != NULL); |
| 171 | |
| 172 | if (getFileStartAndLength(fd, &start, &length) < 0) |
| 173 | return -1; |
| 174 | |
| 175 | memPtr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, |
| 176 | fd, start); |
| 177 | if (memPtr == MAP_FAILED) { |
| 178 | ALOGW("mmap(%d, R/W, FILE|PRIVATE, %d, %d) failed: %s", (int) length, |
| 179 | fd, (int) start, strerror(errno)); |
| 180 | return -1; |
| 181 | } |
| 182 | if (mprotect(memPtr, length, PROT_READ) < 0) { |
| 183 | /* this fails with EACCESS on FAT filesystems, e.g. /sdcard */ |
| 184 | int err = errno; |
| 185 | ALOGV("mprotect(%p, %zd, PROT_READ) failed: %s", |
| 186 | memPtr, length, strerror(err)); |
| 187 | ALOGD("mprotect(RO) failed (%d), file will remain read-write", err); |
| 188 | } |
| 189 | |
| 190 | pMap->baseAddr = pMap->addr = memPtr; |
| 191 | pMap->baseLength = pMap->length = length; |
| 192 | |
| 193 | return 0; |
| 194 | #else |
| 195 | return sysFakeMapFile(fd, pMap); |
| 196 | #endif |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Map part of a file into a shared, read-only memory segment. The "start" |
| 201 | * offset is absolute, not relative. |
| 202 | * |
| 203 | * On success, returns 0 and fills out "pMap". On failure, returns a nonzero |
| 204 | * value and does not disturb "pMap". |
| 205 | */ |
| 206 | int sysMapFileSegmentInShmem(int fd, off_t start, size_t length, |
| 207 | MemMapping* pMap) |
| 208 | { |
| 209 | #if !defined(__MINGW32__) |
| 210 | size_t actualLength; |
| 211 | off_t actualStart; |
| 212 | int adjust; |
| 213 | void* memPtr; |
| 214 | |
| 215 | assert(pMap != NULL); |
| 216 | |
| 217 | /* adjust to be page-aligned */ |
| 218 | adjust = start % SYSTEM_PAGE_SIZE; |
| 219 | actualStart = start - adjust; |
| 220 | actualLength = length + adjust; |
| 221 | |
| 222 | memPtr = mmap(NULL, actualLength, PROT_READ, MAP_FILE | MAP_SHARED, |
| 223 | fd, actualStart); |
| 224 | if (memPtr == MAP_FAILED) { |
| 225 | ALOGW("mmap(%d, R, FILE|SHARED, %d, %d) failed: %s", |
| 226 | (int) actualLength, fd, (int) actualStart, strerror(errno)); |
| 227 | return -1; |
| 228 | } |
| 229 | |
| 230 | pMap->baseAddr = memPtr; |
| 231 | pMap->baseLength = actualLength; |
| 232 | pMap->addr = (char*)memPtr + adjust; |
| 233 | pMap->length = length; |
| 234 | |
| 235 | LOGVV("mmap seg (st=%d ln=%d): bp=%p bl=%d ad=%p ln=%d", |
| 236 | (int) start, (int) length, |
| 237 | pMap->baseAddr, (int) pMap->baseLength, |
| 238 | pMap->addr, (int) pMap->length); |
| 239 | |
| 240 | return 0; |
| 241 | #else |
| 242 | ALOGE("sysMapFileSegmentInShmem not implemented."); |
| 243 | return -1; |
| 244 | #endif |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Change the access rights on one or more pages to read-only or read-write. |
| 249 | * |
| 250 | * Returns 0 on success. |
| 251 | */ |
| 252 | int sysChangeMapAccess(void* addr, size_t length, int wantReadWrite, |
| 253 | MemMapping* pMap) |
| 254 | { |
| 255 | #if !defined(__MINGW32__) |
| 256 | /* |
| 257 | * Verify that "addr" is part of this mapping file. |
| 258 | */ |
| 259 | if (addr < pMap->baseAddr || |
| 260 | (u1*)addr >= (u1*)pMap->baseAddr + pMap->baseLength) |
| 261 | { |
| 262 | ALOGE("Attempted to change %p; map is %p - %p", |
| 263 | addr, pMap->baseAddr, (u1*)pMap->baseAddr + pMap->baseLength); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Align "addr" to a page boundary and adjust "length" appropriately. |
| 269 | * (The address must be page-aligned, the length doesn't need to be, |
| 270 | * but we do need to ensure we cover the same range.) |
| 271 | */ |
| 272 | u1* alignAddr = (u1*) ((uintptr_t) addr & ~(SYSTEM_PAGE_SIZE-1)); |
| 273 | size_t alignLength = length + ((u1*) addr - alignAddr); |
| 274 | |
| 275 | //ALOGI("%p/%zd --> %p/%zd", addr, length, alignAddr, alignLength); |
| 276 | int prot = wantReadWrite ? (PROT_READ|PROT_WRITE) : (PROT_READ); |
| 277 | if (mprotect(alignAddr, alignLength, prot) != 0) { |
Aart Bik | 1a65052 | 2015-07-08 21:20:41 +0000 | [diff] [blame] | 278 | ALOGV("mprotect (%p,%zd,%d) failed: %s", |
| 279 | alignAddr, alignLength, prot, strerror(errno)); |
| 280 | return (errno != 0) ? errno : -1; |
| 281 | } |
| 282 | #endif |
| 283 | |
| 284 | /* for "fake" mapping, no need to do anything */ |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Release a memory mapping. |
| 290 | */ |
| 291 | void sysReleaseShmem(MemMapping* pMap) |
| 292 | { |
| 293 | #if !defined(__MINGW32__) |
| 294 | if (pMap->baseAddr == NULL && pMap->baseLength == 0) |
| 295 | return; |
| 296 | |
| 297 | if (munmap(pMap->baseAddr, pMap->baseLength) < 0) { |
| 298 | ALOGW("munmap(%p, %zd) failed: %s", |
| 299 | pMap->baseAddr, pMap->baseLength, strerror(errno)); |
| 300 | } else { |
| 301 | ALOGV("munmap(%p, %zd) succeeded", pMap->baseAddr, pMap->baseLength); |
| 302 | pMap->baseAddr = NULL; |
| 303 | pMap->baseLength = 0; |
| 304 | } |
| 305 | #else |
| 306 | /* Free the bits allocated by sysMapFileInShmem. */ |
| 307 | if (pMap->baseAddr != NULL) { |
| 308 | free(pMap->baseAddr); |
| 309 | pMap->baseAddr = NULL; |
| 310 | } |
| 311 | pMap->baseLength = 0; |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Make a copy of a MemMapping. |
| 317 | */ |
| 318 | void sysCopyMap(MemMapping* dst, const MemMapping* src) |
| 319 | { |
| 320 | memcpy(dst, src, sizeof(MemMapping)); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * Write until all bytes have been written. |
| 325 | * |
| 326 | * Returns 0 on success, or an errno value on failure. |
| 327 | */ |
| 328 | int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg) |
| 329 | { |
| 330 | while (count != 0) { |
| 331 | ssize_t actual = TEMP_FAILURE_RETRY(write(fd, buf, count)); |
| 332 | if (actual < 0) { |
| 333 | int err = errno; |
| 334 | ALOGE("%s: write failed: %s", logMsg, strerror(err)); |
| 335 | return err; |
| 336 | } else if (actual != (ssize_t) count) { |
| 337 | ALOGD("%s: partial write (will retry): (%d of %zd)", |
| 338 | logMsg, (int) actual, count); |
| 339 | buf = (const void*) (((const u1*) buf) + actual); |
| 340 | } |
| 341 | count -= actual; |
| 342 | } |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* See documentation comment in header file. */ |
| 348 | int sysCopyFileToFile(int outFd, int inFd, size_t count) |
| 349 | { |
| 350 | const size_t kBufSize = 32768; |
| 351 | unsigned char buf[kBufSize]; |
| 352 | |
| 353 | while (count != 0) { |
| 354 | size_t getSize = (count > kBufSize) ? kBufSize : count; |
| 355 | |
| 356 | ssize_t actual = TEMP_FAILURE_RETRY(read(inFd, buf, getSize)); |
| 357 | if (actual != (ssize_t) getSize) { |
| 358 | ALOGW("sysCopyFileToFile: copy read failed (%d vs %zd)", |
| 359 | (int) actual, getSize); |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | if (sysWriteFully(outFd, buf, getSize, "sysCopyFileToFile") != 0) |
| 364 | return -1; |
| 365 | |
| 366 | count -= getSize; |
| 367 | } |
| 368 | |
| 369 | return 0; |
| 370 | } |