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