1 // Copyright (c) 2011, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // memory_mapped_file.cc: Implement google_breakpad::MemoryMappedFile.
31 // See memory_mapped_file.h for details.
33 #include "common/linux/memory_mapped_file.h"
37 #if defined(__ANDROID__)
42 #include "common/memory_range.h"
43 #include "third_party/lss/linux_syscall_support.h"
45 namespace google_breakpad {
47 MemoryMappedFile::MemoryMappedFile() {}
49 MemoryMappedFile::MemoryMappedFile(const char* path, size_t offset) {
53 MemoryMappedFile::~MemoryMappedFile() {
57 bool MemoryMappedFile::Map(const char* path, size_t offset) {
60 int fd = sys_open(path, O_RDONLY, 0);
65 #if defined(__x86_64__) || defined(__aarch64__)
66 struct kernel_stat st;
67 if (sys_fstat(fd, &st) == -1 || st.st_size < 0) {
69 struct kernel_stat64 st;
70 if (sys_fstat64(fd, &st) == -1 || st.st_size < 0) {
76 // Strangely file size can be negative, but we check above that it is not.
77 size_t file_len = static_cast<size_t>(st.st_size);
78 // If the file does not extend beyond the offset, simply use an empty
79 // MemoryRange and return true. Don't bother to call mmap()
80 // even though mmap() can handle an empty file on some platforms.
81 if (offset >= file_len) {
86 #if defined(__x86_64__) || defined(__aarch64__)
87 void* data = sys_mmap(NULL, file_len, PROT_READ, MAP_PRIVATE, fd, offset);
89 if ((offset & 4095) != 0) {
94 void* data = sys_mmap2(
95 NULL, file_len, PROT_READ, MAP_PRIVATE, fd, offset >> 12);
98 if (data == MAP_FAILED) {
102 content_.Set(data, file_len - offset);
106 void MemoryMappedFile::Unmap() {
107 if (content_.data()) {
108 sys_munmap(const_cast<uint8_t*>(content_.data()), content_.length());
109 content_.Set(NULL, 0);
113 } // namespace google_breakpad