Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / breakpad / src / common / linux / memory_mapped_file.cc
1 // Copyright (c) 2011, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29
30 // memory_mapped_file.cc: Implement google_breakpad::MemoryMappedFile.
31 // See memory_mapped_file.h for details.
32
33 #include "common/linux/memory_mapped_file.h"
34
35 #include <fcntl.h>
36 #include <sys/mman.h>
37 #if defined(__ANDROID__)
38 #include <sys/stat.h>
39 #endif
40 #include <unistd.h>
41
42 #include "common/memory_range.h"
43 #include "third_party/lss/linux_syscall_support.h"
44
45 namespace google_breakpad {
46
47 MemoryMappedFile::MemoryMappedFile() {}
48
49 MemoryMappedFile::MemoryMappedFile(const char* path, size_t offset) {
50   Map(path, offset);
51 }
52
53 MemoryMappedFile::~MemoryMappedFile() {
54   Unmap();
55 }
56
57 bool MemoryMappedFile::Map(const char* path, size_t offset) {
58   Unmap();
59
60   int fd = sys_open(path, O_RDONLY, 0);
61   if (fd == -1) {
62     return false;
63   }
64
65 #if defined(__x86_64__) || defined(__aarch64__)
66   struct kernel_stat st;
67   if (sys_fstat(fd, &st) == -1 || st.st_size < 0) {
68 #else
69   struct kernel_stat64 st;
70   if (sys_fstat64(fd, &st) == -1 || st.st_size < 0) {
71 #endif
72     sys_close(fd);
73     return false;
74   }
75
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) {
82     sys_close(fd);
83     return true;
84   }
85
86 #if defined(__x86_64__) || defined(__aarch64__)
87   void* data = sys_mmap(NULL, file_len, PROT_READ, MAP_PRIVATE, fd, offset);
88 #else
89   if ((offset & 4095) != 0) {
90     // Not page aligned.
91     sys_close(fd);
92     return false;
93   }
94   void* data = sys_mmap2(
95       NULL, file_len, PROT_READ, MAP_PRIVATE, fd, offset >> 12);
96 #endif
97   sys_close(fd);
98   if (data == MAP_FAILED) {
99     return false;
100   }
101
102   content_.Set(data, file_len - offset);
103   return true;
104 }
105
106 void MemoryMappedFile::Unmap() {
107   if (content_.data()) {
108     sys_munmap(const_cast<uint8_t*>(content_.data()), content_.length());
109     content_.Set(NULL, 0);
110   }
111 }
112
113 }  // namespace google_breakpad