| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2018 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 "android-base/mapped_file.h" | 
 | 18 |  | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 19 | #include <utility> | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 20 |  | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 21 | #include <errno.h> | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 22 |  | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 23 | namespace android { | 
 | 24 | namespace base { | 
 | 25 |  | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 26 | static constexpr char kEmptyBuffer[] = {'0'}; | 
 | 27 |  | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 28 | static off64_t InitPageSize() { | 
 | 29 | #if defined(_WIN32) | 
 | 30 |   SYSTEM_INFO si; | 
 | 31 |   GetSystemInfo(&si); | 
 | 32 |   return si.dwAllocationGranularity; | 
 | 33 | #else | 
 | 34 |   return sysconf(_SC_PAGE_SIZE); | 
 | 35 | #endif | 
 | 36 | } | 
 | 37 |  | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 38 | std::unique_ptr<MappedFile> MappedFile::FromFd(borrowed_fd fd, off64_t offset, size_t length, | 
 | 39 |                                                int prot) { | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 40 | #if defined(_WIN32) | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 41 |   return FromOsHandle(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), offset, length, prot); | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 42 | #else | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 43 |   return FromOsHandle(fd.get(), offset, length, prot); | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 44 | #endif | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 45 | } | 
 | 46 |  | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 47 | std::unique_ptr<MappedFile> MappedFile::FromOsHandle(os_handle h, off64_t offset, size_t length, | 
 | 48 |                                                      int prot) { | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 49 |   static const off64_t page_size = InitPageSize(); | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 50 |   size_t slop = offset % page_size; | 
 | 51 |   off64_t file_offset = offset - slop; | 
 | 52 |   off64_t file_length = length + slop; | 
 | 53 |  | 
 | 54 | #if defined(_WIN32) | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 55 |   HANDLE handle = CreateFileMappingW( | 
 | 56 |       h, nullptr, (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr); | 
| Pirama Arumuga Nainar | 5d7f841 | 2019-03-07 16:12:55 -0800 | [diff] [blame] | 57 |   if (handle == nullptr) { | 
 | 58 |     // http://b/119818070 "app crashes when reading asset of zero length". | 
 | 59 |     // Return a MappedFile that's only valid for reading the size. | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 60 |     if (length == 0 && ::GetLastError() == ERROR_FILE_INVALID) { | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 61 |       return std::unique_ptr<MappedFile>( | 
 | 62 |           new MappedFile(const_cast<char*>(kEmptyBuffer), 0, 0, nullptr)); | 
| Pirama Arumuga Nainar | 5d7f841 | 2019-03-07 16:12:55 -0800 | [diff] [blame] | 63 |     } | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 64 |     return nullptr; | 
| Pirama Arumuga Nainar | 5d7f841 | 2019-03-07 16:12:55 -0800 | [diff] [blame] | 65 |   } | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 66 |   void* base = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ, 0, | 
 | 67 |                              file_offset, file_length); | 
 | 68 |   if (base == nullptr) { | 
 | 69 |     CloseHandle(handle); | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 70 |     return nullptr; | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 71 |   } | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 72 |   return std::unique_ptr<MappedFile>( | 
 | 73 |       new MappedFile(static_cast<char*>(base), length, slop, handle)); | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 74 | #else | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 75 |   void* base = mmap(nullptr, file_length, prot, MAP_SHARED, h, file_offset); | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 76 |   if (base == MAP_FAILED) { | 
 | 77 |     // http://b/119818070 "app crashes when reading asset of zero length". | 
 | 78 |     // mmap fails with EINVAL for a zero length region. | 
 | 79 |     if (errno == EINVAL && length == 0) { | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 80 |       return std::unique_ptr<MappedFile>(new MappedFile(const_cast<char*>(kEmptyBuffer), 0, 0)); | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 81 |     } | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 82 |     return nullptr; | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 83 |   } | 
| Elliott Hughes | fba2a1a | 2019-12-16 16:16:16 -0800 | [diff] [blame] | 84 |   return std::unique_ptr<MappedFile>(new MappedFile(static_cast<char*>(base), length, slop)); | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 85 | #endif | 
 | 86 | } | 
 | 87 |  | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 88 | MappedFile::MappedFile(MappedFile&& other) | 
 | 89 |     : base_(std::exchange(other.base_, nullptr)), | 
 | 90 |       size_(std::exchange(other.size_, 0)), | 
 | 91 |       offset_(std::exchange(other.offset_, 0)) | 
 | 92 | #ifdef _WIN32 | 
 | 93 |       , | 
 | 94 |       handle_(std::exchange(other.handle_, nullptr)) | 
 | 95 | #endif | 
 | 96 | { | 
 | 97 | } | 
 | 98 |  | 
 | 99 | MappedFile& MappedFile::operator=(MappedFile&& other) { | 
 | 100 |   Close(); | 
 | 101 |   base_ = std::exchange(other.base_, nullptr); | 
 | 102 |   size_ = std::exchange(other.size_, 0); | 
 | 103 |   offset_ = std::exchange(other.offset_, 0); | 
 | 104 | #ifdef _WIN32 | 
 | 105 |   handle_ = std::exchange(other.handle_, nullptr); | 
 | 106 | #endif | 
 | 107 |   return *this; | 
 | 108 | } | 
 | 109 |  | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 110 | MappedFile::~MappedFile() { | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 111 |   Close(); | 
 | 112 | } | 
 | 113 |  | 
 | 114 | void MappedFile::Close() { | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 115 | #if defined(_WIN32) | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 116 |   if (base_ != nullptr && size_ != 0) UnmapViewOfFile(base_); | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 117 |   if (handle_ != nullptr) CloseHandle(handle_); | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 118 |   handle_ = nullptr; | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 119 | #else | 
| Yurii Zubrytskyi | d77c99e | 2019-08-09 15:22:55 -0700 | [diff] [blame] | 120 |   if (base_ != nullptr && size_ != 0) munmap(base_, size_ + offset_); | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 121 | #endif | 
 | 122 |  | 
 | 123 |   base_ = nullptr; | 
 | 124 |   offset_ = size_ = 0; | 
 | 125 | } | 
 | 126 |  | 
 | 127 | }  // namespace base | 
 | 128 | }  // namespace android |