Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / breakpad / src / common / linux / elf_core_dump_unittest.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 // elf_core_dump_unittest.cc: Unit tests for google_breakpad::ElfCoreDump.
31
32 #include <sys/procfs.h>
33
34 #include <set>
35 #include <string>
36
37 #include "breakpad_googletest_includes.h"
38 #include "common/linux/elf_core_dump.h"
39 #include "common/linux/memory_mapped_file.h"
40 #include "common/tests/file_utils.h"
41 #include "common/linux/tests/crash_generator.h"
42 #include "common/using_std_string.h"
43
44 using google_breakpad::AutoTempDir;
45 using google_breakpad::CrashGenerator;
46 using google_breakpad::ElfCoreDump;
47 using google_breakpad::MemoryMappedFile;
48 using google_breakpad::MemoryRange;
49 using google_breakpad::WriteFile;
50 using std::set;
51
52 TEST(ElfCoreDumpTest, DefaultConstructor) {
53   ElfCoreDump core;
54   EXPECT_FALSE(core.IsValid());
55   EXPECT_EQ(NULL, core.GetHeader());
56   EXPECT_EQ(0U, core.GetProgramHeaderCount());
57   EXPECT_EQ(NULL, core.GetProgramHeader(0));
58   EXPECT_EQ(NULL, core.GetFirstProgramHeaderOfType(PT_LOAD));
59   EXPECT_FALSE(core.GetFirstNote().IsValid());
60 }
61
62 TEST(ElfCoreDumpTest, TestElfHeader) {
63   ElfCoreDump::Ehdr header;
64   memset(&header, 0, sizeof(header));
65
66   AutoTempDir temp_dir;
67   string core_path = temp_dir.path() + "/core";
68   const char* core_file = core_path.c_str();
69   MemoryMappedFile mapped_core_file;
70   ElfCoreDump core;
71
72   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header) - 1));
73   ASSERT_TRUE(mapped_core_file.Map(core_file));
74   core.SetContent(mapped_core_file.content());
75   EXPECT_FALSE(core.IsValid());
76   EXPECT_EQ(NULL, core.GetHeader());
77   EXPECT_EQ(0U, core.GetProgramHeaderCount());
78   EXPECT_EQ(NULL, core.GetProgramHeader(0));
79   EXPECT_EQ(NULL, core.GetFirstProgramHeaderOfType(PT_LOAD));
80   EXPECT_FALSE(core.GetFirstNote().IsValid());
81
82   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
83   ASSERT_TRUE(mapped_core_file.Map(core_file));
84   core.SetContent(mapped_core_file.content());
85   EXPECT_FALSE(core.IsValid());
86
87   header.e_ident[0] = ELFMAG0;
88   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
89   ASSERT_TRUE(mapped_core_file.Map(core_file));
90   core.SetContent(mapped_core_file.content());
91   EXPECT_FALSE(core.IsValid());
92
93   header.e_ident[1] = ELFMAG1;
94   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
95   ASSERT_TRUE(mapped_core_file.Map(core_file));
96   core.SetContent(mapped_core_file.content());
97   EXPECT_FALSE(core.IsValid());
98
99   header.e_ident[2] = ELFMAG2;
100   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
101   ASSERT_TRUE(mapped_core_file.Map(core_file));
102   core.SetContent(mapped_core_file.content());
103   EXPECT_FALSE(core.IsValid());
104
105   header.e_ident[3] = ELFMAG3;
106   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
107   ASSERT_TRUE(mapped_core_file.Map(core_file));
108   core.SetContent(mapped_core_file.content());
109   EXPECT_FALSE(core.IsValid());
110
111   header.e_ident[4] = ElfCoreDump::kClass;
112   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
113   ASSERT_TRUE(mapped_core_file.Map(core_file));
114   core.SetContent(mapped_core_file.content());
115   EXPECT_FALSE(core.IsValid());
116
117   header.e_version = EV_CURRENT;
118   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
119   ASSERT_TRUE(mapped_core_file.Map(core_file));
120   core.SetContent(mapped_core_file.content());
121   EXPECT_FALSE(core.IsValid());
122
123   header.e_type = ET_CORE;
124   ASSERT_TRUE(WriteFile(core_file, &header, sizeof(header)));
125   ASSERT_TRUE(mapped_core_file.Map(core_file));
126   core.SetContent(mapped_core_file.content());
127   EXPECT_TRUE(core.IsValid());
128 }
129
130 TEST(ElfCoreDumpTest, ValidCoreFile) {
131   CrashGenerator crash_generator;
132   if (!crash_generator.HasDefaultCorePattern()) {
133     fprintf(stderr, "ElfCoreDumpTest.ValidCoreFile test is skipped "
134             "due to non-default core pattern");
135     return;
136   }
137
138   const unsigned kNumOfThreads = 3;
139   const unsigned kCrashThread = 1;
140   const int kCrashSignal = SIGABRT;
141   ASSERT_TRUE(crash_generator.CreateChildCrash(kNumOfThreads, kCrashThread,
142                                                kCrashSignal, NULL));
143   pid_t expected_crash_thread_id = crash_generator.GetThreadId(kCrashThread);
144   set<pid_t> expected_thread_ids;
145   for (unsigned i = 0; i < kNumOfThreads; ++i) {
146     expected_thread_ids.insert(crash_generator.GetThreadId(i));
147   }
148
149 #if defined(__ANDROID__)
150   struct stat st;
151   if (stat(crash_generator.GetCoreFilePath().c_str(), &st) != 0) {
152     fprintf(stderr, "ElfCoreDumpTest.ValidCoreFile test is skipped "
153             "due to no core file being generated");
154     return;
155   }
156 #endif
157
158   MemoryMappedFile mapped_core_file;
159   ASSERT_TRUE(mapped_core_file.Map(crash_generator.GetCoreFilePath().c_str()));
160
161   ElfCoreDump core;
162   core.SetContent(mapped_core_file.content());
163   EXPECT_TRUE(core.IsValid());
164
165   // Based on write_note_info() in linux/kernel/fs/binfmt_elf.c, notes are
166   // ordered as follows (NT_PRXFPREG and NT_386_TLS are i386 specific):
167   //   Thread           Name          Type
168   //   -------------------------------------------------------------------
169   //   1st thread       CORE          NT_PRSTATUS
170   //   process-wide     CORE          NT_PRPSINFO
171   //   process-wide     CORE          NT_AUXV
172   //   1st thread       CORE          NT_FPREGSET
173   //   1st thread       LINUX         NT_PRXFPREG
174   //   1st thread       LINUX         NT_386_TLS
175   //
176   //   2nd thread       CORE          NT_PRSTATUS
177   //   2nd thread       CORE          NT_FPREGSET
178   //   2nd thread       LINUX         NT_PRXFPREG
179   //   2nd thread       LINUX         NT_386_TLS
180   //
181   //   3rd thread       CORE          NT_PRSTATUS
182   //   3rd thread       CORE          NT_FPREGSET
183   //   3rd thread       LINUX         NT_PRXFPREG
184   //   3rd thread       LINUX         NT_386_TLS
185
186   size_t num_nt_prpsinfo = 0;
187   size_t num_nt_prstatus = 0;
188   size_t num_pr_fpvalid = 0;
189 #if defined(__i386__) || defined(__x86_64__)
190   size_t num_nt_fpregset = 0;
191 #endif
192 #if defined(__i386__)
193   size_t num_nt_prxfpreg = 0;
194 #endif
195   set<pid_t> actual_thread_ids;
196   ElfCoreDump::Note note = core.GetFirstNote();
197   while (note.IsValid()) {
198     MemoryRange name = note.GetName();
199     MemoryRange description = note.GetDescription();
200     EXPECT_FALSE(name.IsEmpty());
201     EXPECT_FALSE(description.IsEmpty());
202
203     switch (note.GetType()) {
204       case NT_PRPSINFO: {
205         EXPECT_TRUE(description.data() != NULL);
206         EXPECT_EQ(sizeof(elf_prpsinfo), description.length());
207         ++num_nt_prpsinfo;
208         break;
209       }
210       case NT_PRSTATUS: {
211         EXPECT_TRUE(description.data() != NULL);
212         EXPECT_EQ(sizeof(elf_prstatus), description.length());
213         const elf_prstatus* status = description.GetData<elf_prstatus>(0);
214         actual_thread_ids.insert(status->pr_pid);
215         if (num_nt_prstatus == 0) {
216           EXPECT_EQ(expected_crash_thread_id, status->pr_pid);
217           EXPECT_EQ(kCrashSignal, status->pr_info.si_signo);
218         }
219         ++num_nt_prstatus;
220         if (status->pr_fpvalid)
221           ++num_pr_fpvalid;
222         break;
223       }
224 #if defined(__i386__) || defined(__x86_64__)
225       case NT_FPREGSET: {
226         EXPECT_TRUE(description.data() != NULL);
227         EXPECT_EQ(sizeof(user_fpregs_struct), description.length());
228         ++num_nt_fpregset;
229         break;
230       }
231 #endif
232 #if defined(__i386__)
233       case NT_PRXFPREG: {
234         EXPECT_TRUE(description.data() != NULL);
235         EXPECT_EQ(sizeof(user_fpxregs_struct), description.length());
236         ++num_nt_prxfpreg;
237         break;
238       }
239 #endif
240       default:
241         break;
242     }
243     note = note.GetNextNote();
244   }
245
246   EXPECT_TRUE(expected_thread_ids == actual_thread_ids);
247   EXPECT_EQ(1U, num_nt_prpsinfo);
248   EXPECT_EQ(kNumOfThreads, num_nt_prstatus);
249 #if defined(__i386__) || defined(__x86_64__)
250   EXPECT_EQ(num_pr_fpvalid, num_nt_fpregset);
251 #endif
252 #if defined(__i386__)
253   EXPECT_EQ(num_pr_fpvalid, num_nt_prxfpreg);
254 #endif
255 }