memory_mapped_file.cc
1 // Copyright 2011 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 // memory_mapped_file.cc: Implement google_breakpad::MemoryMappedFile. 30 // See memory_mapped_file.h for details. 31 32 #ifdef HAVE_CONFIG_H 33 #include <config.h> // Must come first 34 #endif 35 36 #include "common/linux/memory_mapped_file.h" 37 38 #include <fcntl.h> 39 #include <sys/mman.h> 40 #if defined(__ANDROID__) 41 #include <sys/stat.h> 42 #endif 43 #include <unistd.h> 44 45 #include "common/memory_range.h" 46 #include "third_party/lss/linux_syscall_support.h" 47 48 namespace google_breakpad { 49 50 MemoryMappedFile::MemoryMappedFile() {} 51 52 MemoryMappedFile::MemoryMappedFile(const char* path, size_t offset) { 53 Map(path, offset); 54 } 55 56 MemoryMappedFile::~MemoryMappedFile() { 57 Unmap(); 58 } 59 60 #include <unistd.h> 61 62 bool MemoryMappedFile::Map(const char* path, size_t offset) { 63 Unmap(); 64 // Based on https://pubs.opengroup.org/onlinepubs/7908799/xsh/open.html 65 // If O_NONBLOCK is set: The open() function will return without blocking 66 // for the device to be ready or available. Setting this value will provent 67 // hanging if file is not avilable. 68 int fd = sys_open(path, O_RDONLY | O_NONBLOCK, 0); 69 if (fd == -1) { 70 return false; 71 } 72 73 #if defined(__x86_64__) || defined(__aarch64__) || \ 74 (defined(__mips__) && _MIPS_SIM == _ABI64) || \ 75 (defined(__riscv) && __riscv_xlen == 64) 76 77 struct kernel_stat st; 78 if (sys_fstat(fd, &st) == -1 || st.st_size < 0) { 79 #else 80 struct kernel_stat64 st; 81 if (sys_fstat64(fd, &st) == -1 || st.st_size < 0) { 82 #endif 83 sys_close(fd); 84 return false; 85 } 86 87 // Strangely file size can be negative, but we check above that it is not. 88 size_t file_len = static_cast<size_t>(st.st_size); 89 // If the file does not extend beyond the offset, simply use an empty 90 // MemoryRange and return true. Don't bother to call mmap() 91 // even though mmap() can handle an empty file on some platforms. 92 if (offset >= file_len) { 93 sys_close(fd); 94 return true; 95 } 96 97 size_t content_len = file_len - offset; 98 void* data = sys_mmap(NULL, content_len, PROT_READ, MAP_PRIVATE, fd, offset); 99 sys_close(fd); 100 if (data == MAP_FAILED) { 101 return false; 102 } 103 104 content_.Set(data, content_len); 105 return true; 106 } 107 108 void MemoryMappedFile::Unmap() { 109 if (content_.data()) { 110 sys_munmap(const_cast<uint8_t*>(content_.data()), content_.length()); 111 content_.Set(NULL, 0); 112 } 113 } 114 115 } // namespace google_breakpad