| 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 |  | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 19 | #include <errno.h> | 
|  | 20 |  | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 21 | #include "android-base/unique_fd.h" | 
|  | 22 |  | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 23 | namespace android { | 
|  | 24 | namespace base { | 
|  | 25 |  | 
|  | 26 | static off64_t InitPageSize() { | 
|  | 27 | #if defined(_WIN32) | 
|  | 28 | SYSTEM_INFO si; | 
|  | 29 | GetSystemInfo(&si); | 
|  | 30 | return si.dwAllocationGranularity; | 
|  | 31 | #else | 
|  | 32 | return sysconf(_SC_PAGE_SIZE); | 
|  | 33 | #endif | 
|  | 34 | } | 
|  | 35 |  | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 36 | std::unique_ptr<MappedFile> MappedFile::FromFd(borrowed_fd fd, off64_t offset, size_t length, | 
|  | 37 | int prot) { | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 38 | static off64_t page_size = InitPageSize(); | 
|  | 39 | size_t slop = offset % page_size; | 
|  | 40 | off64_t file_offset = offset - slop; | 
|  | 41 | off64_t file_length = length + slop; | 
|  | 42 |  | 
|  | 43 | #if defined(_WIN32) | 
|  | 44 | HANDLE handle = | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 45 | CreateFileMapping(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), nullptr, | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 46 | (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr); | 
| Pirama Arumuga Nainar | 5d7f841 | 2019-03-07 16:12:55 -0800 | [diff] [blame] | 47 | if (handle == nullptr) { | 
|  | 48 | // http://b/119818070 "app crashes when reading asset of zero length". | 
|  | 49 | // Return a MappedFile that's only valid for reading the size. | 
|  | 50 | if (length == 0) { | 
|  | 51 | return std::unique_ptr<MappedFile>(new MappedFile{nullptr, 0, 0, nullptr}); | 
|  | 52 | } | 
|  | 53 | return nullptr; | 
|  | 54 | } | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 55 | void* base = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ, 0, | 
|  | 56 | file_offset, file_length); | 
|  | 57 | if (base == nullptr) { | 
|  | 58 | CloseHandle(handle); | 
|  | 59 | return nullptr; | 
|  | 60 | } | 
|  | 61 | return std::unique_ptr<MappedFile>( | 
|  | 62 | new MappedFile{static_cast<char*>(base), length, slop, handle}); | 
|  | 63 | #else | 
| Josh Gao | 27241a7 | 2019-04-25 14:04:57 -0700 | [diff] [blame] | 64 | void* base = mmap(nullptr, file_length, prot, MAP_SHARED, fd.get(), file_offset); | 
| Elliott Hughes | eb0ef14 | 2019-02-06 14:28:32 -0800 | [diff] [blame] | 65 | if (base == MAP_FAILED) { | 
|  | 66 | // http://b/119818070 "app crashes when reading asset of zero length". | 
|  | 67 | // mmap fails with EINVAL for a zero length region. | 
|  | 68 | if (errno == EINVAL && length == 0) { | 
|  | 69 | return std::unique_ptr<MappedFile>(new MappedFile{nullptr, 0, 0}); | 
|  | 70 | } | 
|  | 71 | return nullptr; | 
|  | 72 | } | 
| Elliott Hughes | e8f4b14 | 2018-10-19 16:09:39 -0700 | [diff] [blame] | 73 | return std::unique_ptr<MappedFile>(new MappedFile{static_cast<char*>(base), length, slop}); | 
|  | 74 | #endif | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | MappedFile::~MappedFile() { | 
|  | 78 | #if defined(_WIN32) | 
|  | 79 | if (base_ != nullptr) UnmapViewOfFile(base_); | 
|  | 80 | if (handle_ != nullptr) CloseHandle(handle_); | 
|  | 81 | #else | 
|  | 82 | if (base_ != nullptr) munmap(base_, size_); | 
|  | 83 | #endif | 
|  | 84 |  | 
|  | 85 | base_ = nullptr; | 
|  | 86 | offset_ = size_ = 0; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | }  // namespace base | 
|  | 90 | }  // namespace android |