| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2006 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 | // Shared file mapping class. | 
|  | 19 | // | 
|  | 20 |  | 
|  | 21 | #define LOG_TAG "filemap" | 
|  | 22 |  | 
|  | 23 | #include <utils/FileMap.h> | 
|  | 24 | #include <utils/Log.h> | 
|  | 25 |  | 
| Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 26 | #if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO) | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 27 | # define PRId32 "I32d" | 
|  | 28 | # define PRIx32 "I32x" | 
|  | 29 | # define PRId64 "I64d" | 
|  | 30 | #else | 
| Mark Salyzyn | 15085a6 | 2014-04-17 09:34:42 -0700 | [diff] [blame] | 31 | #include <inttypes.h> | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 32 | #endif | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | #include <stdio.h> | 
|  | 34 | #include <stdlib.h> | 
|  | 35 |  | 
| Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 36 | #if !defined(__MINGW32__) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <sys/mman.h> | 
|  | 38 | #endif | 
|  | 39 |  | 
|  | 40 | #include <string.h> | 
|  | 41 | #include <memory.h> | 
|  | 42 | #include <errno.h> | 
|  | 43 | #include <assert.h> | 
|  | 44 |  | 
|  | 45 | using namespace android; | 
|  | 46 |  | 
|  | 47 | /*static*/ long FileMap::mPageSize = -1; | 
|  | 48 |  | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 49 | // Constructor.  Create an empty object. | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | FileMap::FileMap(void) | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 51 | : mFileName(nullptr), | 
|  | 52 | mBasePtr(nullptr), | 
| Renaud Paquay | b7a4f0b | 2017-05-10 17:48:59 -0700 | [diff] [blame] | 53 | mBaseLength(0), | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 54 | mDataPtr(nullptr), | 
| Renaud Paquay | b7a4f0b | 2017-05-10 17:48:59 -0700 | [diff] [blame] | 55 | mDataLength(0) | 
|  | 56 | #if defined(__MINGW32__) | 
|  | 57 | , | 
|  | 58 | mFileHandle(INVALID_HANDLE_VALUE), | 
|  | 59 | mFileMapping(NULL) | 
|  | 60 | #endif | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | { | 
|  | 62 | } | 
|  | 63 |  | 
| Adam Lesinski | 6f8885b | 2015-09-22 18:42:19 -0700 | [diff] [blame] | 64 | // Move Constructor. | 
| Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 65 | FileMap::FileMap(FileMap&& other) noexcept | 
|  | 66 | : mFileName(other.mFileName), | 
|  | 67 | mBasePtr(other.mBasePtr), | 
|  | 68 | mBaseLength(other.mBaseLength), | 
|  | 69 | mDataOffset(other.mDataOffset), | 
|  | 70 | mDataPtr(other.mDataPtr), | 
|  | 71 | mDataLength(other.mDataLength) | 
| Adam Lesinski | 6f8885b | 2015-09-22 18:42:19 -0700 | [diff] [blame] | 72 | #if defined(__MINGW32__) | 
| Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 73 | , | 
|  | 74 | mFileHandle(other.mFileHandle), | 
|  | 75 | mFileMapping(other.mFileMapping) | 
| Adam Lesinski | 6f8885b | 2015-09-22 18:42:19 -0700 | [diff] [blame] | 76 | #endif | 
|  | 77 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 78 | other.mFileName = nullptr; | 
|  | 79 | other.mBasePtr = nullptr; | 
|  | 80 | other.mDataPtr = nullptr; | 
| Adam Lesinski | 6f8885b | 2015-09-22 18:42:19 -0700 | [diff] [blame] | 81 | #if defined(__MINGW32__) | 
| Renaud Paquay | b7a4f0b | 2017-05-10 17:48:59 -0700 | [diff] [blame] | 82 | other.mFileHandle = INVALID_HANDLE_VALUE; | 
|  | 83 | other.mFileMapping = NULL; | 
| Adam Lesinski | 6f8885b | 2015-09-22 18:42:19 -0700 | [diff] [blame] | 84 | #endif | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | // Move assign operator. | 
| Chih-Hung Hsieh | 747eb14 | 2018-09-25 11:16:22 -0700 | [diff] [blame] | 88 | FileMap& FileMap::operator=(FileMap&& other) noexcept { | 
| Adam Lesinski | 6f8885b | 2015-09-22 18:42:19 -0700 | [diff] [blame] | 89 | mFileName = other.mFileName; | 
|  | 90 | mBasePtr = other.mBasePtr; | 
|  | 91 | mBaseLength = other.mBaseLength; | 
|  | 92 | mDataOffset = other.mDataOffset; | 
|  | 93 | mDataPtr = other.mDataPtr; | 
|  | 94 | mDataLength = other.mDataLength; | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 95 | other.mFileName = nullptr; | 
|  | 96 | other.mBasePtr = nullptr; | 
|  | 97 | other.mDataPtr = nullptr; | 
| Adam Lesinski | 6f8885b | 2015-09-22 18:42:19 -0700 | [diff] [blame] | 98 | #if defined(__MINGW32__) | 
|  | 99 | mFileHandle = other.mFileHandle; | 
|  | 100 | mFileMapping = other.mFileMapping; | 
| Renaud Paquay | b7a4f0b | 2017-05-10 17:48:59 -0700 | [diff] [blame] | 101 | other.mFileHandle = INVALID_HANDLE_VALUE; | 
|  | 102 | other.mFileMapping = NULL; | 
| Adam Lesinski | 6f8885b | 2015-09-22 18:42:19 -0700 | [diff] [blame] | 103 | #endif | 
|  | 104 | return *this; | 
|  | 105 | } | 
|  | 106 |  | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 107 | // Destructor. | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | FileMap::~FileMap(void) | 
|  | 109 | { | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 110 | if (mFileName != nullptr) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | free(mFileName); | 
|  | 112 | } | 
| Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 113 | #if defined(__MINGW32__) | 
| Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 114 | if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) { | 
| Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 115 | ALOGD("UnmapViewOfFile(%p) failed, error = %lu\n", mBasePtr, | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | GetLastError() ); | 
|  | 117 | } | 
| Renaud Paquay | b7a4f0b | 2017-05-10 17:48:59 -0700 | [diff] [blame] | 118 | if (mFileMapping != NULL) { | 
| Jeff Brown | 1d618d6 | 2010-12-02 13:50:46 -0800 | [diff] [blame] | 119 | CloseHandle(mFileMapping); | 
|  | 120 | } | 
| Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 121 | #else | 
|  | 122 | if (mBasePtr && munmap(mBasePtr, mBaseLength) != 0) { | 
|  | 123 | ALOGD("munmap(%p, %zu) failed\n", mBasePtr, mBaseLength); | 
|  | 124 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | #endif | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 |  | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 129 | // Create a new mapping on an open file. | 
|  | 130 | // | 
|  | 131 | // Closing the file descriptor does not unmap the pages, so we don't | 
|  | 132 | // claim ownership of the fd. | 
|  | 133 | // | 
|  | 134 | // Returns "false" on failure. | 
| Kenny Root | e2fa7dc | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 135 | bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t length, | 
|  | 136 | bool readOnly) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | { | 
| Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 138 | #if defined(__MINGW32__) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | int     adjust; | 
| Kenny Root | e2fa7dc | 2010-11-24 12:56:06 -0800 | [diff] [blame] | 140 | off64_t adjOffset; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | size_t  adjLength; | 
|  | 142 |  | 
|  | 143 | if (mPageSize == -1) { | 
|  | 144 | SYSTEM_INFO  si; | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 145 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | GetSystemInfo( &si ); | 
|  | 147 | mPageSize = si.dwAllocationGranularity; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | DWORD  protect = readOnly ? PAGE_READONLY : PAGE_READWRITE; | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 151 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | mFileHandle  = (HANDLE) _get_osfhandle(fd); | 
|  | 153 | mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL); | 
|  | 154 | if (mFileMapping == NULL) { | 
| Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 155 | ALOGE("CreateFileMapping(%p, %lx) failed with error %lu\n", | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | mFileHandle, protect, GetLastError() ); | 
|  | 157 | return false; | 
|  | 158 | } | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 159 |  | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | adjust    = offset % mPageSize; | 
|  | 161 | adjOffset = offset - adjust; | 
|  | 162 | adjLength = length + adjust; | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 163 |  | 
|  | 164 | mBasePtr = MapViewOfFile( mFileMapping, | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | readOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, | 
|  | 166 | 0, | 
|  | 167 | (DWORD)(adjOffset), | 
|  | 168 | adjLength ); | 
|  | 169 | if (mBasePtr == NULL) { | 
| Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 170 | ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %lu\n", | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | adjOffset, adjLength, GetLastError() ); | 
|  | 172 | CloseHandle(mFileMapping); | 
| Renaud Paquay | b7a4f0b | 2017-05-10 17:48:59 -0700 | [diff] [blame] | 173 | mFileMapping = NULL; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | return false; | 
|  | 175 | } | 
| Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 176 | #else // !defined(__MINGW32__) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | assert(fd >= 0); | 
|  | 178 | assert(offset >= 0); | 
|  | 179 | assert(length > 0); | 
|  | 180 |  | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 181 | // init on first use | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | if (mPageSize == -1) { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | mPageSize = sysconf(_SC_PAGESIZE); | 
|  | 184 | if (mPageSize == -1) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 185 | ALOGE("could not get _SC_PAGESIZE\n"); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | return false; | 
|  | 187 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 190 | int adjust = offset % mPageSize; | 
|  | 191 | off64_t adjOffset = offset - adjust; | 
|  | 192 | size_t adjLength = length + adjust; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 |  | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 194 | int flags = MAP_SHARED; | 
|  | 195 | int prot = PROT_READ; | 
|  | 196 | if (!readOnly) prot |= PROT_WRITE; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 |  | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 198 | void* ptr = mmap(nullptr, adjLength, prot, flags, fd, adjOffset); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | if (ptr == MAP_FAILED) { | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 200 | if (errno == EINVAL && length == 0) { | 
|  | 201 | ptr = nullptr; | 
|  | 202 | adjust = 0; | 
|  | 203 | } else { | 
|  | 204 | ALOGE("mmap(%lld,%zu) failed: %s\n", (long long)adjOffset, adjLength, strerror(errno)); | 
|  | 205 | return false; | 
|  | 206 | } | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } | 
|  | 208 | mBasePtr = ptr; | 
| Yabin Cui | 266092c | 2014-11-11 10:31:30 -0800 | [diff] [blame] | 209 | #endif // !defined(__MINGW32__) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 |  | 
| Yi Kong | e1731a4 | 2018-07-16 18:11:34 -0700 | [diff] [blame] | 211 | mFileName = origFileName != nullptr ? strdup(origFileName) : nullptr; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | mBaseLength = adjLength; | 
|  | 213 | mDataOffset = offset; | 
|  | 214 | mDataPtr = (char*) mBasePtr + adjust; | 
|  | 215 | mDataLength = length; | 
|  | 216 |  | 
| Mark Salyzyn | 15085a6 | 2014-04-17 09:34:42 -0700 | [diff] [blame] | 217 | ALOGV("MAP: base %p/%zu data %p/%zu\n", | 
|  | 218 | mBasePtr, mBaseLength, mDataPtr, mDataLength); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 |  | 
|  | 220 | return true; | 
|  | 221 | } | 
|  | 222 |  | 
| Mark Salyzyn | b618576 | 2014-04-17 10:01:12 -0700 | [diff] [blame] | 223 | // Provide guidance to the system. | 
| Yabin Cui | 745c5f6 | 2014-11-18 20:00:41 -0800 | [diff] [blame] | 224 | #if !defined(_WIN32) | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | int FileMap::advise(MapAdvice advice) | 
|  | 226 | { | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | int cc, sysAdvice; | 
|  | 228 |  | 
|  | 229 | switch (advice) { | 
|  | 230 | case NORMAL:        sysAdvice = MADV_NORMAL;        break; | 
|  | 231 | case RANDOM:        sysAdvice = MADV_RANDOM;        break; | 
|  | 232 | case SEQUENTIAL:    sysAdvice = MADV_SEQUENTIAL;    break; | 
|  | 233 | case WILLNEED:      sysAdvice = MADV_WILLNEED;      break; | 
|  | 234 | case DONTNEED:      sysAdvice = MADV_DONTNEED;      break; | 
|  | 235 | default: | 
|  | 236 | assert(false); | 
|  | 237 | return -1; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | cc = madvise(mBasePtr, mBaseLength, sysAdvice); | 
|  | 241 | if (cc != 0) | 
| Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 242 | ALOGW("madvise(%d) failed: %s\n", sysAdvice, strerror(errno)); | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | return cc; | 
| The Android Open Source Project | cbb1011 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | } | 
| Yabin Cui | 745c5f6 | 2014-11-18 20:00:41 -0800 | [diff] [blame] | 245 |  | 
|  | 246 | #else | 
|  | 247 | int FileMap::advise(MapAdvice /* advice */) | 
|  | 248 | { | 
|  | 249 | return -1; | 
|  | 250 | } | 
|  | 251 | #endif |