1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker * Copyright (C) 2018 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker *
4*8f0ba417SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker *
8*8f0ba417SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker *
10*8f0ba417SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker */
16*8f0ba417SAndroid Build Coastguard Worker
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/mapped_file.h"
18*8f0ba417SAndroid Build Coastguard Worker
19*8f0ba417SAndroid Build Coastguard Worker #include <utility>
20*8f0ba417SAndroid Build Coastguard Worker
21*8f0ba417SAndroid Build Coastguard Worker #include <errno.h>
22*8f0ba417SAndroid Build Coastguard Worker
23*8f0ba417SAndroid Build Coastguard Worker namespace android {
24*8f0ba417SAndroid Build Coastguard Worker namespace base {
25*8f0ba417SAndroid Build Coastguard Worker
26*8f0ba417SAndroid Build Coastguard Worker static constexpr char kEmptyBuffer[] = {'0'};
27*8f0ba417SAndroid Build Coastguard Worker
InitPageSize()28*8f0ba417SAndroid Build Coastguard Worker static off64_t InitPageSize() {
29*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32)
30*8f0ba417SAndroid Build Coastguard Worker SYSTEM_INFO si;
31*8f0ba417SAndroid Build Coastguard Worker GetSystemInfo(&si);
32*8f0ba417SAndroid Build Coastguard Worker return si.dwAllocationGranularity;
33*8f0ba417SAndroid Build Coastguard Worker #else
34*8f0ba417SAndroid Build Coastguard Worker return sysconf(_SC_PAGE_SIZE);
35*8f0ba417SAndroid Build Coastguard Worker #endif
36*8f0ba417SAndroid Build Coastguard Worker }
37*8f0ba417SAndroid Build Coastguard Worker
FromFd(borrowed_fd fd,off64_t offset,size_t length,int prot)38*8f0ba417SAndroid Build Coastguard Worker std::unique_ptr<MappedFile> MappedFile::FromFd(borrowed_fd fd, off64_t offset, size_t length,
39*8f0ba417SAndroid Build Coastguard Worker int prot) {
40*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32)
41*8f0ba417SAndroid Build Coastguard Worker return FromOsHandle(reinterpret_cast<HANDLE>(_get_osfhandle(fd.get())), offset, length, prot);
42*8f0ba417SAndroid Build Coastguard Worker #else
43*8f0ba417SAndroid Build Coastguard Worker return FromOsHandle(fd.get(), offset, length, prot);
44*8f0ba417SAndroid Build Coastguard Worker #endif
45*8f0ba417SAndroid Build Coastguard Worker }
46*8f0ba417SAndroid Build Coastguard Worker
FromOsHandle(os_handle h,off64_t offset,size_t length,int prot)47*8f0ba417SAndroid Build Coastguard Worker std::unique_ptr<MappedFile> MappedFile::FromOsHandle(os_handle h, off64_t offset, size_t length,
48*8f0ba417SAndroid Build Coastguard Worker int prot) {
49*8f0ba417SAndroid Build Coastguard Worker static const off64_t page_size = InitPageSize();
50*8f0ba417SAndroid Build Coastguard Worker size_t slop = offset % page_size;
51*8f0ba417SAndroid Build Coastguard Worker off64_t file_offset = offset - slop;
52*8f0ba417SAndroid Build Coastguard Worker off64_t file_length = length + slop;
53*8f0ba417SAndroid Build Coastguard Worker
54*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32)
55*8f0ba417SAndroid Build Coastguard Worker HANDLE handle = CreateFileMappingW(
56*8f0ba417SAndroid Build Coastguard Worker h, nullptr, (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr);
57*8f0ba417SAndroid Build Coastguard Worker if (handle == nullptr) {
58*8f0ba417SAndroid Build Coastguard Worker // http://b/119818070 "app crashes when reading asset of zero length".
59*8f0ba417SAndroid Build Coastguard Worker // Return a MappedFile that's only valid for reading the size.
60*8f0ba417SAndroid Build Coastguard Worker if (length == 0 && ::GetLastError() == ERROR_FILE_INVALID) {
61*8f0ba417SAndroid Build Coastguard Worker return std::unique_ptr<MappedFile>(
62*8f0ba417SAndroid Build Coastguard Worker new MappedFile(const_cast<char*>(kEmptyBuffer), 0, 0, nullptr));
63*8f0ba417SAndroid Build Coastguard Worker }
64*8f0ba417SAndroid Build Coastguard Worker return nullptr;
65*8f0ba417SAndroid Build Coastguard Worker }
66*8f0ba417SAndroid Build Coastguard Worker void* base = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_ALL_ACCESS : FILE_MAP_READ,
67*8f0ba417SAndroid Build Coastguard Worker (file_offset >> 32), file_offset, file_length);
68*8f0ba417SAndroid Build Coastguard Worker if (base == nullptr) {
69*8f0ba417SAndroid Build Coastguard Worker CloseHandle(handle);
70*8f0ba417SAndroid Build Coastguard Worker return nullptr;
71*8f0ba417SAndroid Build Coastguard Worker }
72*8f0ba417SAndroid Build Coastguard Worker return std::unique_ptr<MappedFile>(
73*8f0ba417SAndroid Build Coastguard Worker new MappedFile(static_cast<char*>(base), length, slop, handle));
74*8f0ba417SAndroid Build Coastguard Worker #else
75*8f0ba417SAndroid Build Coastguard Worker void* base = mmap(nullptr, file_length, prot, MAP_SHARED, h, file_offset);
76*8f0ba417SAndroid Build Coastguard Worker if (base == MAP_FAILED) {
77*8f0ba417SAndroid Build Coastguard Worker // http://b/119818070 "app crashes when reading asset of zero length".
78*8f0ba417SAndroid Build Coastguard Worker // mmap fails with EINVAL for a zero length region.
79*8f0ba417SAndroid Build Coastguard Worker if (errno == EINVAL && length == 0) {
80*8f0ba417SAndroid Build Coastguard Worker return std::unique_ptr<MappedFile>(new MappedFile(const_cast<char*>(kEmptyBuffer), 0, 0));
81*8f0ba417SAndroid Build Coastguard Worker }
82*8f0ba417SAndroid Build Coastguard Worker return nullptr;
83*8f0ba417SAndroid Build Coastguard Worker }
84*8f0ba417SAndroid Build Coastguard Worker return std::unique_ptr<MappedFile>(new MappedFile(static_cast<char*>(base), length, slop));
85*8f0ba417SAndroid Build Coastguard Worker #endif
86*8f0ba417SAndroid Build Coastguard Worker }
87*8f0ba417SAndroid Build Coastguard Worker
MappedFile(MappedFile && other)88*8f0ba417SAndroid Build Coastguard Worker MappedFile::MappedFile(MappedFile&& other)
89*8f0ba417SAndroid Build Coastguard Worker : base_(std::exchange(other.base_, nullptr)),
90*8f0ba417SAndroid Build Coastguard Worker size_(std::exchange(other.size_, 0)),
91*8f0ba417SAndroid Build Coastguard Worker offset_(std::exchange(other.offset_, 0))
92*8f0ba417SAndroid Build Coastguard Worker #ifdef _WIN32
93*8f0ba417SAndroid Build Coastguard Worker ,
94*8f0ba417SAndroid Build Coastguard Worker handle_(std::exchange(other.handle_, nullptr))
95*8f0ba417SAndroid Build Coastguard Worker #endif
96*8f0ba417SAndroid Build Coastguard Worker {
97*8f0ba417SAndroid Build Coastguard Worker }
98*8f0ba417SAndroid Build Coastguard Worker
operator =(MappedFile && other)99*8f0ba417SAndroid Build Coastguard Worker MappedFile& MappedFile::operator=(MappedFile&& other) {
100*8f0ba417SAndroid Build Coastguard Worker Close();
101*8f0ba417SAndroid Build Coastguard Worker base_ = std::exchange(other.base_, nullptr);
102*8f0ba417SAndroid Build Coastguard Worker size_ = std::exchange(other.size_, 0);
103*8f0ba417SAndroid Build Coastguard Worker offset_ = std::exchange(other.offset_, 0);
104*8f0ba417SAndroid Build Coastguard Worker #ifdef _WIN32
105*8f0ba417SAndroid Build Coastguard Worker handle_ = std::exchange(other.handle_, nullptr);
106*8f0ba417SAndroid Build Coastguard Worker #endif
107*8f0ba417SAndroid Build Coastguard Worker return *this;
108*8f0ba417SAndroid Build Coastguard Worker }
109*8f0ba417SAndroid Build Coastguard Worker
~MappedFile()110*8f0ba417SAndroid Build Coastguard Worker MappedFile::~MappedFile() {
111*8f0ba417SAndroid Build Coastguard Worker Close();
112*8f0ba417SAndroid Build Coastguard Worker }
113*8f0ba417SAndroid Build Coastguard Worker
Close()114*8f0ba417SAndroid Build Coastguard Worker void MappedFile::Close() {
115*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32)
116*8f0ba417SAndroid Build Coastguard Worker if (base_ != nullptr && size_ != 0) UnmapViewOfFile(base_);
117*8f0ba417SAndroid Build Coastguard Worker if (handle_ != nullptr) CloseHandle(handle_);
118*8f0ba417SAndroid Build Coastguard Worker handle_ = nullptr;
119*8f0ba417SAndroid Build Coastguard Worker #else
120*8f0ba417SAndroid Build Coastguard Worker if (base_ != nullptr && size_ != 0) munmap(base_, size_ + offset_);
121*8f0ba417SAndroid Build Coastguard Worker #endif
122*8f0ba417SAndroid Build Coastguard Worker
123*8f0ba417SAndroid Build Coastguard Worker base_ = nullptr;
124*8f0ba417SAndroid Build Coastguard Worker offset_ = size_ = 0;
125*8f0ba417SAndroid Build Coastguard Worker }
126*8f0ba417SAndroid Build Coastguard Worker
127*8f0ba417SAndroid Build Coastguard Worker } // namespace base
128*8f0ba417SAndroid Build Coastguard Worker } // namespace android
129