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 
19 #include <utility>
20 
21 #include <errno.h>
22 
23 namespace android {
24 namespace base {
25 
26 static constexpr char kEmptyBuffer[] = {'0'};
27 
InitPageSize()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 
FromFd(borrowed_fd fd,off64_t offset,size_t length,int prot)38 std::unique_ptr<MappedFile> MappedFile::FromFd(borrowed_fd fd, off64_t offset, size_t length,
39                                                int prot) {
40 #if defined(_WIN32)
41   return FromOsHandle(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), offset, length, prot);
42 #else
43   return FromOsHandle(fd.get(), offset, length, prot);
44 #endif
45 }
46 
FromOsHandle(os_handle h,off64_t offset,size_t length,int prot)47 std::unique_ptr<MappedFile> MappedFile::FromOsHandle(os_handle h, off64_t offset, size_t length,
48                                                      int prot) {
49   static const off64_t page_size = InitPageSize();
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)
55   HANDLE handle = CreateFileMappingW(
56       h, nullptr, (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr);
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.
60     if (length == 0 && ::GetLastError() == ERROR_FILE_INVALID) {
61       return std::unique_ptr<MappedFile>(
62           new MappedFile(const_cast<char*>(kEmptyBuffer), 0, 0, nullptr));
63     }
64     return nullptr;
65   }
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);
70     return nullptr;
71   }
72   return std::unique_ptr<MappedFile>(
73       new MappedFile(static_cast<char*>(base), length, slop, handle));
74 #else
75   void* base = mmap(nullptr, file_length, prot, MAP_SHARED, h, file_offset);
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) {
80       return std::unique_ptr<MappedFile>(new MappedFile(const_cast<char*>(kEmptyBuffer), 0, 0));
81     }
82     return nullptr;
83   }
84   return std::unique_ptr<MappedFile>(new MappedFile(static_cast<char*>(base), length, slop));
85 #endif
86 }
87 
MappedFile(MappedFile && other)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 
operator =(MappedFile && other)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 
~MappedFile()110 MappedFile::~MappedFile() {
111   Close();
112 }
113 
Close()114 void MappedFile::Close() {
115 #if defined(_WIN32)
116   if (base_ != nullptr && size_ != 0) UnmapViewOfFile(base_);
117   if (handle_ != nullptr) CloseHandle(handle_);
118   handle_ = nullptr;
119 #else
120   if (base_ != nullptr && size_ != 0) munmap(base_, size_ + offset_);
121 #endif
122 
123   base_ = nullptr;
124   offset_ = size_ = 0;
125 }
126 
127 }  // namespace base
128 }  // namespace android
129