1 // Copyright (c) 2012, 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_core_dumper_unittest.cc:
31 // Unit tests for google_breakpad::LinuxCoreDumoer.
32 
33 #include <string>
34 
35 #include "breakpad_googletest_includes.h"
36 #include "client/linux/minidump_writer/linux_core_dumper.h"
37 #include "common/linux/tests/crash_generator.h"
38 #include "common/using_std_string.h"
39 
40 using namespace google_breakpad;
41 
TEST(LinuxCoreDumperTest,GetMappingAbsolutePath)42 TEST(LinuxCoreDumperTest, GetMappingAbsolutePath) {
43   const LinuxCoreDumper dumper(getpid(), "core", "/tmp", "/mnt/root");
44   const MappingInfo mapping = {0, 0, {0, 0}, 0, false, "/usr/lib/libc.so"};
45 
46   char path[PATH_MAX];
47   dumper.GetMappingAbsolutePath(mapping, path);
48 
49   EXPECT_STREQ("/mnt/root/usr/lib/libc.so", path);
50 }
51 
TEST(LinuxCoreDumperTest,BuildProcPath)52 TEST(LinuxCoreDumperTest, BuildProcPath) {
53   const pid_t pid = getpid();
54   const char procfs_path[] = "/procfs_copy";
55   LinuxCoreDumper dumper(getpid(), "core_file", procfs_path);
56 
57   char maps_path[NAME_MAX] = "";
58   char maps_path_expected[NAME_MAX];
59   snprintf(maps_path_expected, sizeof(maps_path_expected),
60            "%s/maps", procfs_path);
61   EXPECT_TRUE(dumper.BuildProcPath(maps_path, pid, "maps"));
62   EXPECT_STREQ(maps_path_expected, maps_path);
63 
64   EXPECT_FALSE(dumper.BuildProcPath(NULL, pid, "maps"));
65   EXPECT_FALSE(dumper.BuildProcPath(maps_path, pid, ""));
66   EXPECT_FALSE(dumper.BuildProcPath(maps_path, pid, NULL));
67 
68   char long_node[NAME_MAX];
69   size_t long_node_len = NAME_MAX - strlen(procfs_path) - 1;
70   memset(long_node, 'a', long_node_len);
71   long_node[long_node_len] = '\0';
72   EXPECT_FALSE(dumper.BuildProcPath(maps_path, pid, long_node));
73 }
74 
TEST(LinuxCoreDumperTest,VerifyDumpWithMultipleThreads)75 TEST(LinuxCoreDumperTest, VerifyDumpWithMultipleThreads) {
76   CrashGenerator crash_generator;
77   if (!crash_generator.HasDefaultCorePattern()) {
78     fprintf(stderr, "LinuxCoreDumperTest.VerifyDumpWithMultipleThreads test "
79             "is skipped due to non-default core pattern\n");
80     return;
81   }
82 
83   const unsigned kNumOfThreads = 3;
84   const unsigned kCrashThread = 1;
85   const int kCrashSignal = SIGABRT;
86   pid_t child_pid;
87   ASSERT_TRUE(crash_generator.CreateChildCrash(kNumOfThreads, kCrashThread,
88                                                kCrashSignal, &child_pid));
89 
90   const string core_file = crash_generator.GetCoreFilePath();
91   const string procfs_path = crash_generator.GetDirectoryOfProcFilesCopy();
92 
93 #if defined(__ANDROID__)
94   struct stat st;
95   if (stat(core_file.c_str(), &st) != 0) {
96     fprintf(stderr, "LinuxCoreDumperTest.VerifyDumpWithMultipleThreads test is "
97             "skipped due to no core file being generated\n");
98     return;
99   }
100 #endif
101 
102   LinuxCoreDumper dumper(child_pid, core_file.c_str(), procfs_path.c_str());
103 
104   EXPECT_TRUE(dumper.Init());
105 
106   EXPECT_TRUE(dumper.IsPostMortem());
107 
108   // These are no-ops and should always return true.
109   EXPECT_TRUE(dumper.ThreadsSuspend());
110   EXPECT_TRUE(dumper.ThreadsResume());
111 
112   // Linux does not set the crash address with SIGABRT, so make sure it always
113   // sets the crash address to 0.
114   EXPECT_EQ(0U, dumper.crash_address());
115   EXPECT_EQ(kCrashSignal, dumper.crash_signal());
116   EXPECT_EQ(crash_generator.GetThreadId(kCrashThread),
117             dumper.crash_thread());
118 
119 #if defined(THREAD_SANITIZER)
120   EXPECT_GE(dumper.threads().size(), kNumOfThreads);
121 #else
122   EXPECT_EQ(dumper.threads().size(), kNumOfThreads);
123 #endif
124   for (unsigned i = 0; i < kNumOfThreads; ++i) {
125     ThreadInfo info;
126     EXPECT_TRUE(dumper.GetThreadInfoByIndex(i, &info));
127     const void* stack;
128     size_t stack_len;
129     EXPECT_TRUE(dumper.GetStackInfo(&stack, &stack_len, info.stack_pointer));
130     EXPECT_EQ(getpid(), info.ppid);
131   }
132 }
133 
TEST(LinuxCoreDumperTest,VerifyExceptionDetails)134 TEST(LinuxCoreDumperTest, VerifyExceptionDetails) {
135   CrashGenerator crash_generator;
136   if (!crash_generator.HasDefaultCorePattern()) {
137     fprintf(stderr, "LinuxCoreDumperTest.VerifyDumpWithMultipleThreads test "
138             "is skipped due to non-default core pattern\n");
139     return;
140   }
141 
142 #ifndef si_syscall
143   fprintf(stderr, "LinuxCoreDumperTest.VerifyDumpWithMultipleThreads test is "
144           "skipped due to old kernel/C library headers\n");
145   return;
146 #endif
147 
148   const unsigned kNumOfThreads = 2;
149   const unsigned kCrashThread = 1;
150   const int kCrashSignal = SIGSYS;
151   pid_t child_pid;
152   ASSERT_TRUE(crash_generator.CreateChildCrash(kNumOfThreads, kCrashThread,
153                                                kCrashSignal, &child_pid));
154 
155   const string core_file = crash_generator.GetCoreFilePath();
156   const string procfs_path = crash_generator.GetDirectoryOfProcFilesCopy();
157 
158 #if defined(__ANDROID__)
159   struct stat st;
160   if (stat(core_file.c_str(), &st) != 0) {
161     fprintf(stderr, "LinuxCoreDumperTest.VerifyExceptionDetails test is "
162             "skipped due to no core file being generated\n");
163     return;
164   }
165 #endif
166 
167   LinuxCoreDumper dumper(child_pid, core_file.c_str(), procfs_path.c_str());
168 
169   EXPECT_TRUE(dumper.Init());
170 
171   EXPECT_TRUE(dumper.IsPostMortem());
172 
173 #if defined(__ANDROID__)
174   // TODO: For some reason, Android doesn't seem to pass this.
175   if (!dumper.crash_address()) {
176     fprintf(stderr, "LinuxCoreDumperTest.VerifyExceptionDetails test is "
177             "skipped due to missing signal details on Android\n");
178     return;
179   }
180 #endif
181 
182   // Check the exception details.
183   EXPECT_NE(0U, dumper.crash_address());
184   EXPECT_EQ(kCrashSignal, dumper.crash_signal());
185   EXPECT_EQ(crash_generator.GetThreadId(kCrashThread),
186             dumper.crash_thread());
187 
188   // We check the length, but not the actual fields.  We sent SIGSYS ourselves
189   // instead of the kernel, so the extended fields are garbage.
190   const std::vector<uint64_t> info(dumper.crash_exception_info());
191   EXPECT_EQ(2U, info.size());
192 }
193