blob: 862b73bc2263ace7d820b43482681e1b445b046c [file] [log] [blame]
Elliott Hughese8f4b142018-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 Zubrytskyid77c99e2019-08-09 15:22:55 -070019#include <utility>
Elliott Hugheseb0ef142019-02-06 14:28:32 -080020
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070021#include <errno.h>
Josh Gao27241a72019-04-25 14:04:57 -070022
Elliott Hughese8f4b142018-10-19 16:09:39 -070023namespace android {
24namespace base {
25
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070026static constexpr char kEmptyBuffer[] = {'0'};
27
Elliott Hughese8f4b142018-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 Gao27241a72019-04-25 14:04:57 -070038std::unique_ptr<MappedFile> MappedFile::FromFd(borrowed_fd fd, off64_t offset, size_t length,
39 int prot) {
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070040#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
49MappedFile MappedFile::FromOsHandle(os_handle h, off64_t offset, size_t length, int prot) {
50 static const off64_t page_size = InitPageSize();
Elliott Hughese8f4b142018-10-19 16:09:39 -070051 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 Zubrytskyid77c99e2019-08-09 15:22:55 -070056 HANDLE handle = CreateFileMappingW(
57 h, nullptr, (prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY, 0, 0, nullptr);
Pirama Arumuga Nainar5d7f8412019-03-07 16:12:55 -080058 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 Zubrytskyid77c99e2019-08-09 15:22:55 -070061 if (length == 0 && ::GetLastError() == ERROR_FILE_INVALID) {
62 return MappedFile{const_cast<char*>(kEmptyBuffer), 0, 0, nullptr};
Pirama Arumuga Nainar5d7f8412019-03-07 16:12:55 -080063 }
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070064 return MappedFile(nullptr, 0, 0, nullptr);
Pirama Arumuga Nainar5d7f8412019-03-07 16:12:55 -080065 }
Elliott Hughese8f4b142018-10-19 16:09:39 -070066 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 Zubrytskyid77c99e2019-08-09 15:22:55 -070070 return MappedFile(nullptr, 0, 0, nullptr);
Elliott Hughese8f4b142018-10-19 16:09:39 -070071 }
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070072 return MappedFile{static_cast<char*>(base), length, slop, handle};
Elliott Hughese8f4b142018-10-19 16:09:39 -070073#else
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070074 void* base = mmap(nullptr, file_length, prot, MAP_SHARED, h, file_offset);
Elliott Hugheseb0ef142019-02-06 14:28:32 -080075 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 Zubrytskyid77c99e2019-08-09 15:22:55 -070079 return MappedFile{const_cast<char*>(kEmptyBuffer), 0, 0};
Elliott Hugheseb0ef142019-02-06 14:28:32 -080080 }
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070081 return MappedFile(nullptr, 0, 0);
Elliott Hugheseb0ef142019-02-06 14:28:32 -080082 }
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070083 return MappedFile{static_cast<char*>(base), length, slop};
Elliott Hughese8f4b142018-10-19 16:09:39 -070084#endif
85}
86
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -070087MappedFile::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
98MappedFile& 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 Hughese8f4b142018-10-19 16:09:39 -0700109MappedFile::~MappedFile() {
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -0700110 Close();
111}
112
113void MappedFile::Close() {
Elliott Hughese8f4b142018-10-19 16:09:39 -0700114#if defined(_WIN32)
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -0700115 if (base_ != nullptr && size_ != 0) UnmapViewOfFile(base_);
Elliott Hughese8f4b142018-10-19 16:09:39 -0700116 if (handle_ != nullptr) CloseHandle(handle_);
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -0700117 handle_ = nullptr;
Elliott Hughese8f4b142018-10-19 16:09:39 -0700118#else
Yurii Zubrytskyid77c99e2019-08-09 15:22:55 -0700119 if (base_ != nullptr && size_ != 0) munmap(base_, size_ + offset_);
Elliott Hughese8f4b142018-10-19 16:09:39 -0700120#endif
121
122 base_ = nullptr;
123 offset_ = size_ = 0;
124}
125
126} // namespace base
127} // namespace android