c1e77c966ce5e8b02b1ae084eeb3e5808474a497
[platform/framework/web/crosswalk.git] / src / breakpad / src / client / linux / minidump_writer / linux_dumper.cc
1 // Copyright (c) 2010, 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 // linux_dumper.cc: Implement google_breakpad::LinuxDumper.
31 // See linux_dumper.h for details.
32
33 // This code deals with the mechanics of getting information about a crashed
34 // process. Since this code may run in a compromised address space, the same
35 // rules apply as detailed at the top of minidump_writer.h: no libc calls and
36 // use the alternative allocator.
37
38 #include "client/linux/minidump_writer/linux_dumper.h"
39
40 #include <assert.h>
41 #include <elf.h>
42 #include <fcntl.h>
43 #include <limits.h>
44 #include <stddef.h>
45 #include <string.h>
46
47 #include "client/linux/minidump_writer/line_reader.h"
48 #include "common/linux/elfutils.h"
49 #include "common/linux/file_id.h"
50 #include "common/linux/linux_libc_support.h"
51 #include "common/linux/memory_mapped_file.h"
52 #include "common/linux/safe_readlink.h"
53 #include "third_party/lss/linux_syscall_support.h"
54
55 static const char kMappedFileUnsafePrefix[] = "/dev/";
56 static const char kDeletedSuffix[] = " (deleted)";
57
58 inline static bool IsMappedFileOpenUnsafe(
59     const google_breakpad::MappingInfo& mapping) {
60   // It is unsafe to attempt to open a mapped file that lives under /dev,
61   // because the semantics of the open may be driver-specific so we'd risk
62   // hanging the crash dumper. And a file in /dev/ almost certainly has no
63   // ELF file identifier anyways.
64   return my_strncmp(mapping.name,
65                     kMappedFileUnsafePrefix,
66                     sizeof(kMappedFileUnsafePrefix) - 1) == 0;
67 }
68
69 namespace google_breakpad {
70
71 // All interesting auvx entry types are below AT_SYSINFO_EHDR
72 #define AT_MAX AT_SYSINFO_EHDR
73
74 LinuxDumper::LinuxDumper(pid_t pid)
75     : pid_(pid),
76       crash_address_(0),
77       crash_signal_(0),
78       crash_thread_(pid),
79       threads_(&allocator_, 8),
80       mappings_(&allocator_),
81       auxv_(&allocator_, AT_MAX + 1) {
82   // The passed-in size to the constructor (above) is only a hint.
83   // Must call .resize() to do actual initialization of the elements.
84   auxv_.resize(AT_MAX + 1);
85 }
86
87 LinuxDumper::~LinuxDumper() {
88 }
89
90 bool LinuxDumper::Init() {
91   return ReadAuxv() && EnumerateThreads() && EnumerateMappings();
92 }
93
94 bool
95 LinuxDumper::ElfFileIdentifierForMapping(const MappingInfo& mapping,
96                                          bool member,
97                                          unsigned int mapping_id,
98                                          uint8_t identifier[sizeof(MDGUID)]) {
99   assert(!member || mapping_id < mappings_.size());
100   my_memset(identifier, 0, sizeof(MDGUID));
101   if (IsMappedFileOpenUnsafe(mapping))
102     return false;
103
104   // Special-case linux-gate because it's not a real file.
105   if (my_strcmp(mapping.name, kLinuxGateLibraryName) == 0) {
106     void* linux_gate = NULL;
107     if (pid_ == sys_getpid()) {
108       linux_gate = reinterpret_cast<void*>(mapping.start_addr);
109     } else {
110       linux_gate = allocator_.Alloc(mapping.size);
111       CopyFromProcess(linux_gate, pid_,
112                       reinterpret_cast<const void*>(mapping.start_addr),
113                       mapping.size);
114     }
115     return FileID::ElfFileIdentifierFromMappedFile(linux_gate, identifier);
116   }
117
118   char filename[NAME_MAX];
119   size_t filename_len = my_strlen(mapping.name);
120   if (filename_len >= NAME_MAX) {
121     assert(false);
122     return false;
123   }
124   my_memcpy(filename, mapping.name, filename_len);
125   filename[filename_len] = '\0';
126   bool filename_modified = HandleDeletedFileInMapping(filename);
127
128   MemoryMappedFile mapped_file(filename, mapping.offset);
129   if (!mapped_file.data() || mapped_file.size() < SELFMAG)
130     return false;
131
132   bool success =
133       FileID::ElfFileIdentifierFromMappedFile(mapped_file.data(), identifier);
134   if (success && member && filename_modified) {
135     mappings_[mapping_id]->name[filename_len -
136                                 sizeof(kDeletedSuffix) + 1] = '\0';
137   }
138
139   return success;
140 }
141
142 namespace {
143 bool ElfFileSoNameFromMappedFile(
144     const void* elf_base, char* soname, size_t soname_size) {
145   if (!IsValidElf(elf_base)) {
146     // Not ELF
147     return false;
148   }
149
150   const void* segment_start;
151   size_t segment_size;
152   int elf_class;
153   if (!FindElfSection(elf_base, ".dynamic", SHT_DYNAMIC,
154                       &segment_start, &segment_size, &elf_class)) {
155     // No dynamic section
156     return false;
157   }
158
159   const void* dynstr_start;
160   size_t dynstr_size;
161   if (!FindElfSection(elf_base, ".dynstr", SHT_STRTAB,
162                       &dynstr_start, &dynstr_size, &elf_class)) {
163     // No dynstr section
164     return false;
165   }
166
167   const ElfW(Dyn)* dynamic = static_cast<const ElfW(Dyn)*>(segment_start);
168   size_t dcount = segment_size / sizeof(ElfW(Dyn));
169   for (const ElfW(Dyn)* dyn = dynamic; dyn < dynamic + dcount; ++dyn) {
170     if (dyn->d_tag == DT_SONAME) {
171       const char* dynstr = static_cast<const char*>(dynstr_start);
172       if (dyn->d_un.d_val >= dynstr_size) {
173         // Beyond the end of the dynstr section
174         return false;
175       }
176       const char* str = dynstr + dyn->d_un.d_val;
177       const size_t maxsize = dynstr_size - dyn->d_un.d_val;
178       my_strlcpy(soname, str, maxsize < soname_size ? maxsize : soname_size);
179       return true;
180     }
181   }
182
183   // Did not find SONAME
184   return false;
185 }
186 }  // namespace
187
188 // static
189 bool LinuxDumper::ElfFileSoName(
190     const MappingInfo& mapping, char* soname, size_t soname_size) {
191   if (IsMappedFileOpenUnsafe(mapping)) {
192     // Not safe
193     return false;
194   }
195
196   char filename[NAME_MAX];
197   size_t filename_len = my_strlen(mapping.name);
198   if (filename_len >= NAME_MAX) {
199     assert(false);
200     // name too long
201     return false;
202   }
203
204   my_memcpy(filename, mapping.name, filename_len);
205   filename[filename_len] = '\0';
206
207   MemoryMappedFile mapped_file(filename, mapping.offset);
208   if (!mapped_file.data() || mapped_file.size() < SELFMAG) {
209     // mmap failed
210     return false;
211   }
212
213   return ElfFileSoNameFromMappedFile(mapped_file.data(), soname, soname_size);
214 }
215
216 bool LinuxDumper::ReadAuxv() {
217   char auxv_path[NAME_MAX];
218   if (!BuildProcPath(auxv_path, pid_, "auxv")) {
219     return false;
220   }
221
222   int fd = sys_open(auxv_path, O_RDONLY, 0);
223   if (fd < 0) {
224     return false;
225   }
226
227   elf_aux_entry one_aux_entry;
228   bool res = false;
229   while (sys_read(fd,
230                   &one_aux_entry,
231                   sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) &&
232          one_aux_entry.a_type != AT_NULL) {
233     if (one_aux_entry.a_type <= AT_MAX) {
234       auxv_[one_aux_entry.a_type] = one_aux_entry.a_un.a_val;
235       res = true;
236     }
237   }
238   sys_close(fd);
239   return res;
240 }
241
242 bool LinuxDumper::EnumerateMappings() {
243   char maps_path[NAME_MAX];
244   if (!BuildProcPath(maps_path, pid_, "maps"))
245     return false;
246
247   // linux_gate_loc is the beginning of the kernel's mapping of
248   // linux-gate.so in the process.  It doesn't actually show up in the
249   // maps list as a filename, but it can be found using the AT_SYSINFO_EHDR
250   // aux vector entry, which gives the information necessary to special
251   // case its entry when creating the list of mappings.
252   // See http://www.trilithium.com/johan/2005/08/linux-gate/ for more
253   // information.
254   const void* linux_gate_loc =
255       reinterpret_cast<void *>(auxv_[AT_SYSINFO_EHDR]);
256   // Although the initial executable is usually the first mapping, it's not
257   // guaranteed (see http://crosbug.com/25355); therefore, try to use the
258   // actual entry point to find the mapping.
259   const void* entry_point_loc = reinterpret_cast<void *>(auxv_[AT_ENTRY]);
260
261   const int fd = sys_open(maps_path, O_RDONLY, 0);
262   if (fd < 0)
263     return false;
264   LineReader* const line_reader = new(allocator_) LineReader(fd);
265
266   const char* line;
267   unsigned line_len;
268   while (line_reader->GetNextLine(&line, &line_len)) {
269     uintptr_t start_addr, end_addr, offset;
270
271     const char* i1 = my_read_hex_ptr(&start_addr, line);
272     if (*i1 == '-') {
273       const char* i2 = my_read_hex_ptr(&end_addr, i1 + 1);
274       if (*i2 == ' ') {
275         bool exec = (*(i2 + 3) == 'x');
276         const char* i3 = my_read_hex_ptr(&offset, i2 + 6 /* skip ' rwxp ' */);
277         if (*i3 == ' ') {
278           const char* name = NULL;
279           // Only copy name if the name is a valid path name, or if
280           // it's the VDSO image.
281           if (((name = my_strchr(line, '/')) == NULL) &&
282               linux_gate_loc &&
283               reinterpret_cast<void*>(start_addr) == linux_gate_loc) {
284             name = kLinuxGateLibraryName;
285             offset = 0;
286           }
287           // Merge adjacent mappings with the same name into one module,
288           // assuming they're a single library mapped by the dynamic linker
289           if (name && !mappings_.empty()) {
290             MappingInfo* module = mappings_.back();
291             if ((start_addr == module->start_addr + module->size) &&
292                 (my_strlen(name) == my_strlen(module->name)) &&
293                 (my_strncmp(name, module->name, my_strlen(name)) == 0)) {
294               module->size = end_addr - module->start_addr;
295               line_reader->PopLine(line_len);
296               continue;
297             }
298           }
299           MappingInfo* const module = new(allocator_) MappingInfo;
300           my_memset(module, 0, sizeof(MappingInfo));
301           module->start_addr = start_addr;
302           module->size = end_addr - start_addr;
303           module->offset = offset;
304           module->exec = exec;
305           if (name != NULL) {
306             const unsigned l = my_strlen(name);
307             if (l < sizeof(module->name))
308               my_memcpy(module->name, name, l);
309           }
310           // If this is the entry-point mapping, and it's not already the
311           // first one, then we need to make it be first.  This is because
312           // the minidump format assumes the first module is the one that
313           // corresponds to the main executable (as codified in
314           // processor/minidump.cc:MinidumpModuleList::GetMainModule()).
315           if (entry_point_loc &&
316               (entry_point_loc >=
317                   reinterpret_cast<void*>(module->start_addr)) &&
318               (entry_point_loc <
319                   reinterpret_cast<void*>(module->start_addr+module->size)) &&
320               !mappings_.empty()) {
321             // push the module onto the front of the list.
322             mappings_.resize(mappings_.size() + 1);
323             for (size_t idx = mappings_.size() - 1; idx > 0; idx--)
324               mappings_[idx] = mappings_[idx - 1];
325             mappings_[0] = module;
326           } else {
327             mappings_.push_back(module);
328           }
329         }
330       }
331     }
332     line_reader->PopLine(line_len);
333   }
334
335   sys_close(fd);
336
337   return !mappings_.empty();
338 }
339
340 // Get information about the stack, given the stack pointer. We don't try to
341 // walk the stack since we might not have all the information needed to do
342 // unwind. So we just grab, up to, 32k of stack.
343 bool LinuxDumper::GetStackInfo(const void** stack, size_t* stack_len,
344                                uintptr_t int_stack_pointer) {
345   // Move the stack pointer to the bottom of the page that it's in.
346   const uintptr_t page_size = getpagesize();
347
348   uint8_t* const stack_pointer =
349       reinterpret_cast<uint8_t*>(int_stack_pointer & ~(page_size - 1));
350
351   // The number of bytes of stack which we try to capture.
352   static const ptrdiff_t kStackToCapture = 32 * 1024;
353
354   const MappingInfo* mapping = FindMapping(stack_pointer);
355   if (!mapping)
356     return false;
357   const ptrdiff_t offset = stack_pointer -
358       reinterpret_cast<uint8_t*>(mapping->start_addr);
359   const ptrdiff_t distance_to_end =
360       static_cast<ptrdiff_t>(mapping->size) - offset;
361   *stack_len = distance_to_end > kStackToCapture ?
362       kStackToCapture : distance_to_end;
363   *stack = stack_pointer;
364   return true;
365 }
366
367 // Find the mapping which the given memory address falls in.
368 const MappingInfo* LinuxDumper::FindMapping(const void* address) const {
369   const uintptr_t addr = (uintptr_t) address;
370
371   for (size_t i = 0; i < mappings_.size(); ++i) {
372     const uintptr_t start = static_cast<uintptr_t>(mappings_[i]->start_addr);
373     if (addr >= start && addr - start < mappings_[i]->size)
374       return mappings_[i];
375   }
376
377   return NULL;
378 }
379
380 bool LinuxDumper::HandleDeletedFileInMapping(char* path) const {
381   static const size_t kDeletedSuffixLen = sizeof(kDeletedSuffix) - 1;
382
383   // Check for ' (deleted)' in |path|.
384   // |path| has to be at least as long as "/x (deleted)".
385   const size_t path_len = my_strlen(path);
386   if (path_len < kDeletedSuffixLen + 2)
387     return false;
388   if (my_strncmp(path + path_len - kDeletedSuffixLen, kDeletedSuffix,
389                  kDeletedSuffixLen) != 0) {
390     return false;
391   }
392
393   // Check |path| against the /proc/pid/exe 'symlink'.
394   char exe_link[NAME_MAX];
395   char new_path[NAME_MAX];
396   if (!BuildProcPath(exe_link, pid_, "exe"))
397     return false;
398   if (!SafeReadLink(exe_link, new_path))
399     return false;
400   if (my_strcmp(path, new_path) != 0)
401     return false;
402
403   // Check to see if someone actually named their executable 'foo (deleted)'.
404   struct kernel_stat exe_stat;
405   struct kernel_stat new_path_stat;
406   if (sys_stat(exe_link, &exe_stat) == 0 &&
407       sys_stat(new_path, &new_path_stat) == 0 &&
408       exe_stat.st_dev == new_path_stat.st_dev &&
409       exe_stat.st_ino == new_path_stat.st_ino) {
410     return false;
411   }
412
413   my_memcpy(path, exe_link, NAME_MAX);
414   return true;
415 }
416
417 }  // namespace google_breakpad