blob: 91d0b0fc1b26b8128c7340db7c2e484674ad464b [file] [log] [blame]
Elliott Hughes51140082018-10-19 16:09:39 -07001/*
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 Zubrytskyi667a8cf2019-08-09 15:22:55 -070019#include <utility>
Elliott Hughesf5b28ff2019-02-06 14:28:32 -080020
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070021#include <errno.h>
Josh Gaoe4401992019-04-25 14:04:57 -070022
Elliott Hughes51140082018-10-19 16:09:39 -070023namespace android {
24namespace base {
25
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070026static constexpr char kEmptyBuffer[] = {'0'};
27
Elliott Hughes51140082018-10-19 16:09:39 -070028static 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 Gaoe4401992019-04-25 14:04:57 -070038std::unique_ptr<MappedFile> MappedFile::FromFd(borrowed_fd fd, off64_t offset, size_t length,
39 int prot) {
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070040#if defined(_WIN32)
Elliott Hughes1be1f912019-12-16 16:16:16 -080041 return FromOsHandle(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), offset, length, prot);
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070042#else
Elliott Hughes1be1f912019-12-16 16:16:16 -080043 return FromOsHandle(fd.get(), offset, length, prot);
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070044#endif
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070045}
46
Elliott Hughes1be1f912019-12-16 16:16:16 -080047std::unique_ptr<MappedFile> MappedFile::FromOsHandle(os_handle h, off64_t offset, size_t length,
48 int prot) {
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070049 static const off64_t page_size = InitPageSize();
Elliott Hughes51140082018-10-19 16:09:39 -070050 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 Zubrytskyi667a8cf2019-08-09 15:22:55 -070055 HANDLE handle = CreateFileMappingW(
56 h, nullptr, (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr);
Pirama Arumuga Nainar5b589662019-03-07 16:12:55 -080057 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 Zubrytskyi667a8cf2019-08-09 15:22:55 -070060 if (length == 0 && ::GetLastError() == ERROR_FILE_INVALID) {
Elliott Hughes1be1f912019-12-16 16:16:16 -080061 return std::unique_ptr<MappedFile>(
62 new MappedFile(const_cast<char*>(kEmptyBuffer), 0, 0, nullptr));
Pirama Arumuga Nainar5b589662019-03-07 16:12:55 -080063 }
Elliott Hughes1be1f912019-12-16 16:16:16 -080064 return nullptr;
Pirama Arumuga Nainar5b589662019-03-07 16:12:55 -080065 }
mtk8090584882082022-01-04 15:47:22 +080066 void* base = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ,
67 (file_offset >> 32), file_offset, file_length);
Elliott Hughes51140082018-10-19 16:09:39 -070068 if (base == nullptr) {
69 CloseHandle(handle);
Elliott Hughes1be1f912019-12-16 16:16:16 -080070 return nullptr;
Elliott Hughes51140082018-10-19 16:09:39 -070071 }
Elliott Hughes1be1f912019-12-16 16:16:16 -080072 return std::unique_ptr<MappedFile>(
73 new MappedFile(static_cast<char*>(base), length, slop, handle));
Elliott Hughes51140082018-10-19 16:09:39 -070074#else
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070075 void* base = mmap(nullptr, file_length, prot, MAP_SHARED, h, file_offset);
Elliott Hughesf5b28ff2019-02-06 14:28:32 -080076 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 Hughes1be1f912019-12-16 16:16:16 -080080 return std::unique_ptr<MappedFile>(new MappedFile(const_cast<char*>(kEmptyBuffer), 0, 0));
Elliott Hughesf5b28ff2019-02-06 14:28:32 -080081 }
Elliott Hughes1be1f912019-12-16 16:16:16 -080082 return nullptr;
Elliott Hughesf5b28ff2019-02-06 14:28:32 -080083 }
Elliott Hughes1be1f912019-12-16 16:16:16 -080084 return std::unique_ptr<MappedFile>(new MappedFile(static_cast<char*>(base), length, slop));
Elliott Hughes51140082018-10-19 16:09:39 -070085#endif
86}
87
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -070088MappedFile::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
99MappedFile& 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 Hughes51140082018-10-19 16:09:39 -0700110MappedFile::~MappedFile() {
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -0700111 Close();
112}
113
114void MappedFile::Close() {
Elliott Hughes51140082018-10-19 16:09:39 -0700115#if defined(_WIN32)
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -0700116 if (base_ != nullptr && size_ != 0) UnmapViewOfFile(base_);
Elliott Hughes51140082018-10-19 16:09:39 -0700117 if (handle_ != nullptr) CloseHandle(handle_);
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -0700118 handle_ = nullptr;
Elliott Hughes51140082018-10-19 16:09:39 -0700119#else
Yurii Zubrytskyi667a8cf2019-08-09 15:22:55 -0700120 if (base_ != nullptr && size_ != 0) munmap(base_, size_ + offset_);
Elliott Hughes51140082018-10-19 16:09:39 -0700121#endif
122
123 base_ = nullptr;
124 offset_ = size_ = 0;
125}
126
127} // namespace base
128} // namespace android