blob: 60fd83243b6705fccf5324417bb0f4eed9b545a5 [file] [log] [blame]
Steve Fung6c34c252015-08-20 00:27:30 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Ken Mixter03403162010-08-18 15:23:16 -070016
Steve Fung129bea52015-07-23 13:11:15 -070017#include "kernel_collector_test.h"
Ben Chan7e776902014-06-18 13:19:51 -070018
Ken Mixter03403162010-08-18 15:23:16 -070019#include <unistd.h>
20
Ben Chanab6cc902014-09-05 08:21:06 -070021#include <base/files/file_util.h>
Ben Chan7e776902014-06-18 13:19:51 -070022#include <base/files/scoped_temp_dir.h>
23#include <base/strings/string_util.h>
24#include <base/strings/stringprintf.h>
Alex Vakulenko74dc6242015-10-13 09:23:34 -070025#include <brillo/syslog_logging.h>
Ben Chan7e776902014-06-18 13:19:51 -070026#include <gtest/gtest.h>
Ken Mixter03403162010-08-18 15:23:16 -070027
Simon Que9f90aca2013-02-19 17:19:52 -080028using base::FilePath;
Mike Frysingera557c112014-02-05 22:55:39 -050029using base::StringPrintf;
Alex Vakulenko74dc6242015-10-13 09:23:34 -070030using brillo::FindLog;
31using brillo::GetLog;
Ken Mixtera3249322011-03-03 08:47:38 -080032
Daniel Eratd257ea12015-01-28 10:23:28 -070033namespace {
34
35int s_crashes = 0;
36bool s_metrics = false;
37
Ken Mixter03403162010-08-18 15:23:16 -070038void CountCrash() {
39 ++s_crashes;
40}
41
42bool IsMetrics() {
43 return s_metrics;
44}
45
Daniel Eratd257ea12015-01-28 10:23:28 -070046} // namespace
47
Ken Mixter03403162010-08-18 15:23:16 -070048class KernelCollectorTest : public ::testing::Test {
Ken Mixter03403162010-08-18 15:23:16 -070049 protected:
50 void WriteStringToFile(const FilePath &file_path,
51 const char *data) {
James Hawkinsa28317d2016-02-26 11:24:38 -080052 unsigned int numBytesWritten =
53 base::WriteFile(file_path, data, strlen(data));
54 ASSERT_EQ(strlen(data), numBytesWritten);
Ken Mixter03403162010-08-18 15:23:16 -070055 }
56
57 void SetUpSuccessfulCollect();
Simon Glassd74cc092011-04-06 10:47:01 -070058 void ComputeKernelStackSignatureCommon();
Ken Mixter03403162010-08-18 15:23:16 -070059
Lei Zhang9b1f3002014-04-24 02:10:57 -070060 const FilePath &kcrash_file() const { return test_kcrash_; }
61 const FilePath &test_crash_directory() const { return test_crash_directory_; }
62
Mike Frysinger6f891c52014-09-24 15:42:11 -040063 KernelCollectorMock collector_;
Lei Zhang9b1f3002014-04-24 02:10:57 -070064
65 private:
Alex Vakulenko0dfc9ce2014-08-14 12:55:41 -070066 void SetUp() override {
Lei Zhang9b1f3002014-04-24 02:10:57 -070067 s_crashes = 0;
68 s_metrics = true;
Steve Fung6e139522015-02-05 14:54:16 -080069
70 EXPECT_CALL(collector_, SetUpDBus()).WillRepeatedly(testing::Return());
71
Lei Zhang9b1f3002014-04-24 02:10:57 -070072 collector_.Initialize(CountCrash, IsMetrics);
73 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
74 test_kcrash_ = scoped_temp_dir_.path().Append("kcrash");
75 ASSERT_TRUE(base::CreateDirectory(test_kcrash_));
76 collector_.OverridePreservedDumpPath(test_kcrash_);
77
78 test_kcrash_ = test_kcrash_.Append("dmesg-ramoops-0");
79 ASSERT_FALSE(base::PathExists(test_kcrash_));
80
81 test_crash_directory_ = scoped_temp_dir_.path().Append("crash_directory");
82 ASSERT_TRUE(base::CreateDirectory(test_crash_directory_));
Alex Vakulenko74dc6242015-10-13 09:23:34 -070083 brillo::ClearLog();
Lei Zhang9b1f3002014-04-24 02:10:57 -070084 }
85
Ken Mixter03403162010-08-18 15:23:16 -070086 FilePath test_kcrash_;
Lei Zhang9b1f3002014-04-24 02:10:57 -070087 FilePath test_crash_directory_;
88 base::ScopedTempDir scoped_temp_dir_;
Ken Mixter03403162010-08-18 15:23:16 -070089};
90
Simon Glassd74cc092011-04-06 10:47:01 -070091TEST_F(KernelCollectorTest, ComputeKernelStackSignatureBase) {
92 // Make sure the normal build architecture is detected
Ben Chan3c6b82c2014-07-23 14:52:14 -070093 EXPECT_NE(KernelCollector::kArchUnknown, collector_.arch());
Simon Glassd74cc092011-04-06 10:47:01 -070094}
95
Ken Mixter03403162010-08-18 15:23:16 -070096TEST_F(KernelCollectorTest, LoadPreservedDump) {
Lei Zhang9b1f3002014-04-24 02:10:57 -070097 ASSERT_FALSE(base::PathExists(kcrash_file()));
Ken Mixter03403162010-08-18 15:23:16 -070098 std::string dump;
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070099 dump.clear();
100
Ben Zhang1c5533d2015-01-20 17:26:31 -0800101 WriteStringToFile(kcrash_file(),
102 "CrashRecordWithoutRamoopsHeader\n<6>[ 0.078852]");
Ben Chan2076b902012-02-29 22:26:54 -0800103 ASSERT_TRUE(collector_.LoadParameters());
Ben Zhang8e5340a2014-07-07 17:39:47 -0700104 ASSERT_TRUE(collector_.LoadPreservedDump(&dump));
Ben Zhang1c5533d2015-01-20 17:26:31 -0800105 ASSERT_EQ("CrashRecordWithoutRamoopsHeader\n<6>[ 0.078852]", dump);
Kees Cookce9556e2011-11-04 20:49:09 +0000106
Lei Zhang9b1f3002014-04-24 02:10:57 -0700107 WriteStringToFile(kcrash_file(), "====1.1\nsomething");
Ben Chan2076b902012-02-29 22:26:54 -0800108 ASSERT_TRUE(collector_.LoadParameters());
Ken Mixter03403162010-08-18 15:23:16 -0700109 ASSERT_TRUE(collector_.LoadPreservedDump(&dump));
110 ASSERT_EQ("something", dump);
Ben Zhang1c5533d2015-01-20 17:26:31 -0800111
112 WriteStringToFile(kcrash_file(), "\x01\x02\xfe\xff random blob");
113 ASSERT_TRUE(collector_.LoadParameters());
114 ASSERT_FALSE(collector_.LoadPreservedDump(&dump));
115 ASSERT_EQ("", dump);
Ken Mixter03403162010-08-18 15:23:16 -0700116}
117
118TEST_F(KernelCollectorTest, EnableMissingKernel) {
119 ASSERT_FALSE(collector_.Enable());
Ben Chan3c6b82c2014-07-23 14:52:14 -0700120 ASSERT_FALSE(collector_.is_enabled());
Ken Mixtera3249322011-03-03 08:47:38 -0800121 ASSERT_TRUE(FindLog(
122 "Kernel does not support crash dumping"));
Ken Mixter03403162010-08-18 15:23:16 -0700123 ASSERT_EQ(s_crashes, 0);
124}
125
126TEST_F(KernelCollectorTest, EnableOK) {
Lei Zhang9b1f3002014-04-24 02:10:57 -0700127 WriteStringToFile(kcrash_file(), "");
Mike Frysinger6f891c52014-09-24 15:42:11 -0400128 EXPECT_CALL(collector_, DumpDirMounted()).WillOnce(::testing::Return(true));
Ken Mixter03403162010-08-18 15:23:16 -0700129 ASSERT_TRUE(collector_.Enable());
Ben Chan3c6b82c2014-07-23 14:52:14 -0700130 ASSERT_TRUE(collector_.is_enabled());
Ken Mixtera3249322011-03-03 08:47:38 -0800131 ASSERT_TRUE(FindLog("Enabling kernel crash handling"));
Ken Mixter03403162010-08-18 15:23:16 -0700132 ASSERT_EQ(s_crashes, 0);
133}
134
Doug Anderson1e6b8bd2011-04-07 09:40:05 -0700135TEST_F(KernelCollectorTest, StripSensitiveDataBasic) {
136 // Basic tests of StripSensitiveData...
137
138 // Make sure we work OK with a string w/ no MAC addresses.
139 const std::string kCrashWithNoMacsOrig =
140 "<7>[111566.131728] PM: Entering mem sleep\n";
141 std::string crash_with_no_macs(kCrashWithNoMacsOrig);
142 collector_.StripSensitiveData(&crash_with_no_macs);
143 EXPECT_EQ(kCrashWithNoMacsOrig, crash_with_no_macs);
144
145 // Make sure that we handle the case where there's nothing before/after the
146 // MAC address.
147 const std::string kJustAMacOrig =
148 "11:22:33:44:55:66";
149 const std::string kJustAMacStripped =
150 "00:00:00:00:00:01";
151 std::string just_a_mac(kJustAMacOrig);
152 collector_.StripSensitiveData(&just_a_mac);
153 EXPECT_EQ(kJustAMacStripped, just_a_mac);
154
155 // Test MAC addresses crammed together to make sure it gets both of them.
156 //
157 // I'm not sure that the code does ideal on these two test cases (they don't
158 // look like two MAC addresses to me), but since we don't see them I think
159 // it's OK to behave as shown here.
160 const std::string kCrammedMacs1Orig =
161 "11:22:33:44:55:66:11:22:33:44:55:66";
162 const std::string kCrammedMacs1Stripped =
163 "00:00:00:00:00:01:00:00:00:00:00:01";
164 std::string crammed_macs_1(kCrammedMacs1Orig);
165 collector_.StripSensitiveData(&crammed_macs_1);
166 EXPECT_EQ(kCrammedMacs1Stripped, crammed_macs_1);
167
168 const std::string kCrammedMacs2Orig =
169 "11:22:33:44:55:6611:22:33:44:55:66";
170 const std::string kCrammedMacs2Stripped =
171 "00:00:00:00:00:0100:00:00:00:00:01";
172 std::string crammed_macs_2(kCrammedMacs2Orig);
173 collector_.StripSensitiveData(&crammed_macs_2);
174 EXPECT_EQ(kCrammedMacs2Stripped, crammed_macs_2);
175
176 // Test case-sensitiveness (we shouldn't be case-senstive).
177 const std::string kCapsMacOrig =
178 "AA:BB:CC:DD:EE:FF";
179 const std::string kCapsMacStripped =
180 "00:00:00:00:00:01";
181 std::string caps_mac(kCapsMacOrig);
182 collector_.StripSensitiveData(&caps_mac);
183 EXPECT_EQ(kCapsMacStripped, caps_mac);
184
185 const std::string kLowerMacOrig =
186 "aa:bb:cc:dd:ee:ff";
187 const std::string kLowerMacStripped =
188 "00:00:00:00:00:01";
189 std::string lower_mac(kLowerMacOrig);
190 collector_.StripSensitiveData(&lower_mac);
191 EXPECT_EQ(kLowerMacStripped, lower_mac);
192}
193
194TEST_F(KernelCollectorTest, StripSensitiveDataBulk) {
195 // Test calling StripSensitiveData w/ lots of MAC addresses in the "log".
196
197 // Test that stripping code handles more than 256 unique MAC addresses, since
198 // that overflows past the last byte...
199 // We'll write up some code that generates 258 unique MAC addresses. Sorta
200 // cheating since the code is very similar to the current code in
201 // StripSensitiveData(), but would catch if someone changed that later.
202 std::string lotsa_macs_orig;
203 std::string lotsa_macs_stripped;
204 int i;
205 for (i = 0; i < 258; i++) {
206 lotsa_macs_orig += StringPrintf(" 11:11:11:11:%02X:%02x",
207 (i & 0xff00) >> 8, i & 0x00ff);
208 lotsa_macs_stripped += StringPrintf(" 00:00:00:00:%02X:%02x",
209 ((i+1) & 0xff00) >> 8, (i+1) & 0x00ff);
210 }
211 std::string lotsa_macs(lotsa_macs_orig);
212 collector_.StripSensitiveData(&lotsa_macs);
213 EXPECT_EQ(lotsa_macs_stripped, lotsa_macs);
214}
215
216TEST_F(KernelCollectorTest, StripSensitiveDataSample) {
217 // Test calling StripSensitiveData w/ some actual lines from a real crash;
218 // included two MAC addresses (though replaced them with some bogusness).
219 const std::string kCrashWithMacsOrig =
220 "<6>[111567.195339] ata1.00: ACPI cmd ef/10:03:00:00:00:a0 (SET FEATURES)"
221 " filtered out\n"
222 "<7>[108539.540144] wlan0: authenticate with 11:22:33:44:55:66 (try 1)\n"
223 "<7>[108539.554973] wlan0: associate with 11:22:33:44:55:66 (try 1)\n"
224 "<6>[110136.587583] usb0: register 'QCUSBNet2k' at usb-0000:00:1d.7-2,"
225 " QCUSBNet Ethernet Device, 99:88:77:66:55:44\n"
226 "<7>[110964.314648] wlan0: deauthenticated from 11:22:33:44:55:66"
227 " (Reason: 6)\n"
228 "<7>[110964.325057] phy0: Removed STA 11:22:33:44:55:66\n"
229 "<7>[110964.325115] phy0: Destroyed STA 11:22:33:44:55:66\n"
230 "<6>[110969.219172] usb0: register 'QCUSBNet2k' at usb-0000:00:1d.7-2,"
231 " QCUSBNet Ethernet Device, 99:88:77:66:55:44\n"
232 "<7>[111566.131728] PM: Entering mem sleep\n";
233 const std::string kCrashWithMacsStripped =
234 "<6>[111567.195339] ata1.00: ACPI cmd ef/10:03:00:00:00:a0 (SET FEATURES)"
235 " filtered out\n"
236 "<7>[108539.540144] wlan0: authenticate with 00:00:00:00:00:01 (try 1)\n"
237 "<7>[108539.554973] wlan0: associate with 00:00:00:00:00:01 (try 1)\n"
238 "<6>[110136.587583] usb0: register 'QCUSBNet2k' at usb-0000:00:1d.7-2,"
239 " QCUSBNet Ethernet Device, 00:00:00:00:00:02\n"
240 "<7>[110964.314648] wlan0: deauthenticated from 00:00:00:00:00:01"
241 " (Reason: 6)\n"
242 "<7>[110964.325057] phy0: Removed STA 00:00:00:00:00:01\n"
243 "<7>[110964.325115] phy0: Destroyed STA 00:00:00:00:00:01\n"
244 "<6>[110969.219172] usb0: register 'QCUSBNet2k' at usb-0000:00:1d.7-2,"
245 " QCUSBNet Ethernet Device, 00:00:00:00:00:02\n"
246 "<7>[111566.131728] PM: Entering mem sleep\n";
247 std::string crash_with_macs(kCrashWithMacsOrig);
248 collector_.StripSensitiveData(&crash_with_macs);
249 EXPECT_EQ(kCrashWithMacsStripped, crash_with_macs);
250}
251
Ken Mixter03403162010-08-18 15:23:16 -0700252TEST_F(KernelCollectorTest, CollectPreservedFileMissing) {
253 ASSERT_FALSE(collector_.Collect());
Ben Chan2076b902012-02-29 22:26:54 -0800254 ASSERT_FALSE(FindLog("Stored kcrash to "));
Ken Mixter03403162010-08-18 15:23:16 -0700255 ASSERT_EQ(0, s_crashes);
256}
257
Ken Mixter03403162010-08-18 15:23:16 -0700258void KernelCollectorTest::SetUpSuccessfulCollect() {
Lei Zhang9b1f3002014-04-24 02:10:57 -0700259 collector_.ForceCrashDirectory(test_crash_directory());
260 WriteStringToFile(kcrash_file(), "====1.1\nsomething");
Ken Mixter03403162010-08-18 15:23:16 -0700261 ASSERT_EQ(0, s_crashes);
262}
263
Ken Mixter03403162010-08-18 15:23:16 -0700264TEST_F(KernelCollectorTest, CollectOptedOut) {
265 SetUpSuccessfulCollect();
266 s_metrics = false;
267 ASSERT_TRUE(collector_.Collect());
Ken Mixtera3249322011-03-03 08:47:38 -0800268 ASSERT_TRUE(FindLog("(ignoring - no consent)"));
Ken Mixter03403162010-08-18 15:23:16 -0700269 ASSERT_EQ(0, s_crashes);
Ken Mixter03403162010-08-18 15:23:16 -0700270}
271
Ken Mixter03403162010-08-18 15:23:16 -0700272TEST_F(KernelCollectorTest, CollectOK) {
273 SetUpSuccessfulCollect();
274 ASSERT_TRUE(collector_.Collect());
275 ASSERT_EQ(1, s_crashes);
Ken Mixtera3249322011-03-03 08:47:38 -0800276 ASSERT_TRUE(FindLog("(handling)"));
Ken Mixterafcf8082010-10-26 14:45:01 -0700277 static const char kNamePrefix[] = "Stored kcrash to ";
Alex Vakulenko74dc6242015-10-13 09:23:34 -0700278 std::string log = brillo::GetLog();
Ken Mixtera3249322011-03-03 08:47:38 -0800279 size_t pos = log.find(kNamePrefix);
Michael Krebs87a49502012-04-12 15:24:22 -0700280 ASSERT_NE(std::string::npos, pos)
281 << "Did not find string \"" << kNamePrefix << "\" in log: {\n"
282 << log << "}";
Ken Mixter03403162010-08-18 15:23:16 -0700283 pos += strlen(kNamePrefix);
Ken Mixtera3249322011-03-03 08:47:38 -0800284 std::string filename = log.substr(pos, std::string::npos);
Ken Mixter03403162010-08-18 15:23:16 -0700285 // Take the name up until \n
286 size_t end_pos = filename.find_first_of("\n");
287 ASSERT_NE(std::string::npos, end_pos);
288 filename = filename.substr(0, end_pos);
James Hawkinsa28317d2016-02-26 11:24:38 -0800289 ASSERT_EQ(0U, filename.find(test_crash_directory().value()));
Mike Frysingera557c112014-02-05 22:55:39 -0500290 ASSERT_TRUE(base::PathExists(FilePath(filename)));
Ken Mixter03403162010-08-18 15:23:16 -0700291 std::string contents;
Mike Frysingera557c112014-02-05 22:55:39 -0500292 ASSERT_TRUE(base::ReadFileToString(FilePath(filename), &contents));
Ken Mixter03403162010-08-18 15:23:16 -0700293 ASSERT_EQ("something", contents);
Ken Mixter03403162010-08-18 15:23:16 -0700294}
295
Simon Glassd74cc092011-04-06 10:47:01 -0700296// Perform tests which are common across architectures
297void KernelCollectorTest::ComputeKernelStackSignatureCommon() {
298 std::string signature;
299
300 const char kStackButNoPC[] =
301 "<4>[ 6066.829029] [<790340af>] __do_softirq+0xa6/0x143\n";
302 EXPECT_TRUE(
303 collector_.ComputeKernelStackSignature(kStackButNoPC, &signature, false));
304 EXPECT_EQ("kernel--83615F0A", signature);
305
306 const char kMissingEverything[] =
307 "<4>[ 6066.829029] [<790340af>] ? __do_softirq+0xa6/0x143\n";
308 EXPECT_FALSE(
309 collector_.ComputeKernelStackSignature(kMissingEverything,
310 &signature,
311 false));
312
313 // Long message.
314 const char kTruncatedMessage[] =
315 "<0>[ 87.485611] Kernel panic - not syncing: 01234567890123456789"
316 "01234567890123456789X\n";
317 EXPECT_TRUE(
318 collector_.ComputeKernelStackSignature(kTruncatedMessage,
319 &signature,
320 false));
321 EXPECT_EQ("kernel-0123456789012345678901234567890123456789-00000000",
322 signature);
323}
324
325TEST_F(KernelCollectorTest, ComputeKernelStackSignatureARM) {
326 const char kBugToPanic[] =
327 "<5>[ 123.412524] Modules linked in:\n"
328 "<5>[ 123.412534] CPU: 0 Tainted: G W "
329 "(2.6.37-01030-g51cee64 #153)\n"
330 "<5>[ 123.412552] PC is at write_breakme+0xd0/0x1b4\n"
331 "<5>[ 123.412560] LR is at write_breakme+0xc8/0x1b4\n"
332 "<5>[ 123.412569] pc : [<c0058220>] lr : [<c005821c>] "
333 "psr: 60000013\n"
334 "<5>[ 123.412574] sp : f4e0ded8 ip : c04d104c fp : 000e45e0\n"
335 "<5>[ 123.412581] r10: 400ff000 r9 : f4e0c000 r8 : 00000004\n"
336 "<5>[ 123.412589] r7 : f4e0df80 r6 : f4820c80 r5 : 00000004 "
337 "r4 : f4e0dee8\n"
338 "<5>[ 123.412598] r3 : 00000000 r2 : f4e0decc r1 : c05f88a9 "
339 "r0 : 00000039\n"
340 "<5>[ 123.412608] Flags: nZCv IRQs on FIQs on Mode SVC_32 ISA "
341 "ARM Segment user\n"
342 "<5>[ 123.412617] Control: 10c53c7d Table: 34dcc04a DAC: 00000015\n"
343 "<0>[ 123.412626] Process bash (pid: 1014, stack limit = 0xf4e0c2f8)\n"
344 "<0>[ 123.412634] Stack: (0xf4e0ded8 to 0xf4e0e000)\n"
345 "<0>[ 123.412641] dec0: "
346 " f4e0dee8 c0183678\n"
347 "<0>[ 123.412654] dee0: 00000000 00000000 00677562 0000081f c06a6a78 "
348 "400ff000 f4e0dfb0 00000000\n"
349 "<0>[ 123.412666] df00: bec7ab44 000b1719 bec7ab0c c004f498 bec7a314 "
350 "c024acc8 00000001 c018359c\n"
351 "<0>[ 123.412679] df20: f4e0df34 c04d10fc f5803c80 271beb39 000e45e0 "
352 "f5803c80 c018359c c017bfe0\n"
353 "<0>[ 123.412691] df40: 00000004 f4820c80 400ff000 f4e0df80 00000004 "
354 "f4e0c000 00000000 c01383e4\n"
355 "<0>[ 123.412703] df60: f4820c80 400ff000 f4820c80 400ff000 00000000 "
356 "00000000 00000004 c0138578\n"
357 "<0>[ 123.412715] df80: 00000000 00000000 00000004 00000000 00000004 "
358 "402f95d0 00000004 00000004\n"
359 "<0>[ 123.412727] dfa0: c0054984 c00547c0 00000004 402f95d0 00000001 "
360 "400ff000 00000004 00000000\n"
361 "<0>[ 123.412739] dfc0: 00000004 402f95d0 00000004 00000004 400ff000 "
362 "000c194c bec7ab58 000e45e0\n"
363 "<0>[ 123.412751] dfe0: 00000000 bec7aad8 40232520 40284e9c 60000010 "
364 "00000001 00000000 00000000\n"
365 "<5>[ 39.496577] Backtrace:\n"
366 "<5>[ 123.412782] [<c0058220>] (__bug+0x20/0x2c) from [<c0183678>] "
367 "(write_breakme+0xdc/0x1bc)\n"
368 "<5>[ 123.412798] [<c0183678>] (write_breakme+0xdc/0x1bc) from "
369 "[<c017bfe0>] (proc_reg_write+0x88/0x9c)\n";
370 std::string signature;
371
Ben Chan3c6b82c2014-07-23 14:52:14 -0700372 collector_.set_arch(KernelCollector::kArchArm);
Simon Glassd74cc092011-04-06 10:47:01 -0700373 EXPECT_TRUE(
374 collector_.ComputeKernelStackSignature(kBugToPanic, &signature, false));
375 EXPECT_EQ("kernel-write_breakme-97D3E92F", signature);
376
377 ComputeKernelStackSignatureCommon();
378}
379
Ben Chan120c6752014-07-22 21:06:09 -0700380TEST_F(KernelCollectorTest, ComputeKernelStackSignatureMIPS) {
381 const char kBugToPanic[] =
382 "<5>[ 3378.472000] lkdtm: Performing direct entry BUG\n"
383 "<5>[ 3378.476000] Kernel bug detected[#1]:\n"
384 "<5>[ 3378.484000] CPU: 0 PID: 185 Comm: dash Not tainted 3.14.0 #1\n"
385 "<5>[ 3378.488000] task: 8fed5220 ti: 8ec4a000 task.ti: 8ec4a000\n"
386 "<5>[ 3378.496000] $ 0 : 00000000 804018b8 804010f0 7785b507\n"
387 "<5>[ 3378.500000] $ 4 : 8061ab64 81204478 81205b20 00000000\n"
388 "<5>[ 3378.508000] $ 8 : 80830000 20746365 72746e65 55422079\n"
389 "<5>[ 3378.512000] $12 : 8ec4be94 000000fc 00000000 00000048\n"
390 "<5>[ 3378.520000] $16 : 00000004 8ef54000 80710000 00000002\n"
391 "<5>[ 3378.528000] $20 : 7765b6d4 00000004 7fffffff 00000002\n"
392 "<5>[ 3378.532000] $24 : 00000001 803dc0dc \n"
393 "<5>[ 3378.540000] $28 : 8ec4a000 8ec4be20 7775438d 804018b8\n"
394 "<5>[ 3378.544000] Hi : 00000000\n"
395 "<5>[ 3378.548000] Lo : 49bf8080\n"
396 "<5>[ 3378.552000] epc : 804010f0 lkdtm_do_action+0x68/0x3f8\n"
397 "<5>[ 3378.560000] Not tainted\n"
398 "<5>[ 3378.564000] ra : 804018b8 direct_entry+0x110/0x154\n"
399 "<5>[ 3378.568000] Status: 3100dc03 KERNEL EXL IE \n"
400 "<5>[ 3378.572000] Cause : 10800024\n"
401 "<5>[ 3378.576000] PrId : 0001a120 (MIPS interAptiv (multi))\n"
402 "<5>[ 3378.580000] Modules linked in: uinput cfg80211 nf_conntrack_ipv6 "
403 "nf_defrag_ipv6 ip6table_filter ip6_tables pcnet32 mii fuse "
404 "ppp_async ppp_generic slhc tun\n"
405 "<5>[ 3378.600000] Process dash (pid: 185, threadinfo=8ec4a000, "
406 "task=8fed5220, tls=77632490)\n"
407 "<5>[ 3378.608000] Stack : 00000006 ffffff9c 00000000 00000000 00000000 "
408 "00000000 8083454a 00000022\n"
409 "<5> 7765baa1 00001fee 80710000 8ef54000 8ec4bf08 00000002 "
410 "7765b6d4 00000004\n"
411 "<5> 7fffffff 00000002 7775438d 805e5158 7fffffff 00000002 "
412 "00000000 7785b507\n"
413 "<5> 806a96bc 00000004 8ef54000 8ec4bf08 00000002 804018b8 "
414 "80710000 806a98bc\n"
415 "<5> 00000002 00000020 00000004 8d515600 77756450 00000004 "
416 "8ec4bf08 802377e4\n"
417 "<5> ...\n"
418 "<5>[ 3378.652000] Call Trace:\n"
419 "<5>[ 3378.656000] [<804010f0>] lkdtm_do_action+0x68/0x3f8\n"
420 "<5>[ 3378.660000] [<804018b8>] direct_entry+0x110/0x154\n"
421 "<5>[ 3378.664000] [<802377e4>] vfs_write+0xe0/0x1bc\n"
422 "<5>[ 3378.672000] [<80237f90>] SyS_write+0x78/0xf8\n"
423 "<5>[ 3378.676000] [<80111888>] handle_sys+0x128/0x14c\n"
424 "<5>[ 3378.680000] \n"
425 "<5>[ 3378.684000] \n"
426 "<5>Code: 3c04806b 0c1793aa 248494f0 <000c000d> 3c04806b 248494fc "
427 "0c04cc7f 2405017a 08100514 \n"
428 "<5>[ 3378.696000] ---[ end trace 75067432f24bbc93 ]---\n";
429 std::string signature;
430
Ben Chan3c6b82c2014-07-23 14:52:14 -0700431 collector_.set_arch(KernelCollector::kArchMips);
Ben Chan120c6752014-07-22 21:06:09 -0700432 EXPECT_TRUE(
433 collector_.ComputeKernelStackSignature(kBugToPanic, &signature, false));
434 EXPECT_EQ("kernel-lkdtm_do_action-5E600A6B", signature);
435
436 ComputeKernelStackSignatureCommon();
437}
Simon Glassd74cc092011-04-06 10:47:01 -0700438
439TEST_F(KernelCollectorTest, ComputeKernelStackSignatureX86) {
Ken Mixterafcf8082010-10-26 14:45:01 -0700440 const char kBugToPanic[] =
441 "<4>[ 6066.829029] [<79039d16>] ? run_timer_softirq+0x165/0x1e6\n"
442 "<4>[ 6066.829029] [<790340af>] ignore_old_stack+0xa6/0x143\n"
443 "<0>[ 6066.829029] EIP: [<b82d7c15>] ieee80211_stop_tx_ba_session+"
444 "0xa3/0xb5 [mac80211] SS:ESP 0068:7951febc\n"
445 "<0>[ 6066.829029] CR2: 00000000323038a7\n"
446 "<4>[ 6066.845422] ---[ end trace 12b058bb46c43500 ]---\n"
447 "<0>[ 6066.845747] Kernel panic - not syncing: Fatal exception "
448 "in interrupt\n"
449 "<0>[ 6066.846902] Call Trace:\n"
450 "<4>[ 6066.846902] [<7937a07b>] ? printk+0x14/0x19\n"
451 "<4>[ 6066.949779] [<79379fc1>] panic+0x3e/0xe4\n"
452 "<4>[ 6066.949971] [<7937c5c5>] oops_end+0x73/0x81\n"
453 "<4>[ 6066.950208] [<7901b260>] no_context+0x10d/0x117\n";
454 std::string signature;
Simon Glassd74cc092011-04-06 10:47:01 -0700455
Ben Chan3c6b82c2014-07-23 14:52:14 -0700456 collector_.set_arch(KernelCollector::kArchX86);
Ken Mixterafcf8082010-10-26 14:45:01 -0700457 EXPECT_TRUE(
458 collector_.ComputeKernelStackSignature(kBugToPanic, &signature, false));
459 EXPECT_EQ("kernel-ieee80211_stop_tx_ba_session-DE253569", signature);
460
461 const char kPCButNoStack[] =
462 "<0>[ 6066.829029] EIP: [<b82d7c15>] ieee80211_stop_tx_ba_session+";
463 EXPECT_TRUE(
464 collector_.ComputeKernelStackSignature(kPCButNoStack, &signature, false));
465 EXPECT_EQ("kernel-ieee80211_stop_tx_ba_session-00000000", signature);
466
Ken Mixterafcf8082010-10-26 14:45:01 -0700467 const char kBreakmeBug[] =
468 "<4>[ 180.492137] [<790970c6>] ? handle_mm_fault+0x67f/0x96d\n"
469 "<4>[ 180.492137] [<790dcdfe>] ? proc_reg_write+0x5f/0x73\n"
470 "<4>[ 180.492137] [<790e2224>] ? write_breakme+0x0/0x108\n"
471 "<4>[ 180.492137] [<790dcd9f>] ? proc_reg_write+0x0/0x73\n"
472 "<4>[ 180.492137] [<790ac0aa>] vfs_write+0x85/0xe4\n"
473 "<0>[ 180.492137] Code: c6 44 05 b2 00 89 d8 e8 0c ef 09 00 85 c0 75 "
474 "0b c7 00 00 00 00 00 e9 8e 00 00 00 ba e6 75 4b 79 89 d8 e8 f1 ee 09 "
475 "00 85 c0 75 04 <0f> 0b eb fe ba 58 47 49 79 89 d8 e8 dd ee 09 00 85 "
476 "c0 75 0a 68\n"
477 "<0>[ 180.492137] EIP: [<790e22a4>] write_breakme+0x80/0x108 SS:ESP "
478 "0068:aa3e9efc\n"
479 "<4>[ 180.501800] ---[ end trace 2a6b72965e1b1523 ]---\n"
480 "<0>[ 180.502026] Kernel panic - not syncing: Fatal exception\n"
481 "<4>[ 180.502026] Call Trace:\n"
482 "<4>[ 180.502806] [<79379aba>] ? printk+0x14/0x1a\n"
483 "<4>[ 180.503033] [<79379a00>] panic+0x3e/0xe4\n"
484 "<4>[ 180.503287] [<7937c005>] oops_end+0x73/0x81\n"
485 "<4>[ 180.503520] [<790055dd>] die+0x58/0x5e\n"
486 "<4>[ 180.503538] [<7937b96c>] do_trap+0x8e/0xa7\n"
487 "<4>[ 180.503555] [<79003d70>] ? do_invalid_op+0x0/0x80\n";
488
489 EXPECT_TRUE(
490 collector_.ComputeKernelStackSignature(kBreakmeBug, &signature, false));
491 EXPECT_EQ("kernel-write_breakme-122AB3CD", signature);
492
493 const char kPCLineTooOld[] =
494 "<4>[ 174.492137] [<790970c6>] ignored_function+0x67f/0x96d\n"
495 "<4>[ 175.492137] [<790970c6>] ignored_function2+0x67f/0x96d\n"
496 "<0>[ 174.492137] EIP: [<790e22a4>] write_breakme+0x80/0x108 SS:ESP "
497 "0068:aa3e9efc\n"
498 "<4>[ 180.501800] ---[ end trace 2a6b72965e1b1523 ]---\n"
499 "<4>[ 180.502026] Call Trace:\n"
500 "<0>[ 180.502026] Kernel panic - not syncing: Fatal exception\n"
501 "<4>[ 180.502806] [<79379aba>] printk+0x14/0x1a\n";
502
503 EXPECT_TRUE(
504 collector_.ComputeKernelStackSignature(kPCLineTooOld, &signature, false));
505 EXPECT_EQ("kernel-Fatal exception-ED4C84FE", signature);
506
507 // Panic without EIP line.
508 const char kExamplePanicOnly[] =
509 "<0>[ 87.485611] Kernel panic - not syncing: Testing panic\n"
510 "<4>[ 87.485630] Pid: 2825, comm: bash Tainted: G "
511 "C 2.6.32.23+drm33.10 #1\n"
512 "<4>[ 87.485639] Call Trace:\n"
513 "<4>[ 87.485660] [<8133f71d>] ? printk+0x14/0x17\n"
514 "<4>[ 87.485674] [<8133f663>] panic+0x3e/0xe4\n"
515 "<4>[ 87.485689] [<810d062e>] write_breakme+0xaa/0x124\n";
516 EXPECT_TRUE(
517 collector_.ComputeKernelStackSignature(kExamplePanicOnly,
518 &signature,
519 false));
520 EXPECT_EQ("kernel-Testing panic-E0FC3552", signature);
Luigi Semenzatof6400992011-12-29 13:18:35 -0800521
522 // Panic from hung task.
523 const char kHungTaskBreakMe[] =
524 "<3>[ 720.459157] INFO: task bash:2287 blocked blah blah\n"
525 "<5>[ 720.459282] Call Trace:\n"
526 "<5>[ 720.459307] [<810a457b>] ? __dentry_open+0x186/0x23e\n"
527 "<5>[ 720.459323] [<810b9c71>] ? mntput_no_expire+0x29/0xe2\n"
528 "<5>[ 720.459336] [<810b9d48>] ? mntput+0x1e/0x20\n"
529 "<5>[ 720.459350] [<810ad135>] ? path_put+0x1a/0x1d\n"
530 "<5>[ 720.459366] [<8137cacc>] schedule+0x4d/0x4f\n"
531 "<5>[ 720.459379] [<8137ccfb>] schedule_timeout+0x26/0xaf\n"
532 "<5>[ 720.459394] [<8102127e>] ? should_resched+0xd/0x27\n"
533 "<5>[ 720.459409] [<81174d1f>] ? _copy_from_user+0x3c/0x50\n"
534 "<5>[ 720.459423] [<8137cd9e>] "
535 "schedule_timeout_uninterruptible+0x1a/0x1c\n"
536 "<5>[ 720.459438] [<810dee63>] write_breakme+0xb3/0x178\n"
537 "<5>[ 720.459453] [<810dedb0>] ? meminfo_proc_show+0x2f2/0x2f2\n"
538 "<5>[ 720.459467] [<810d94ae>] proc_reg_write+0x6d/0x87\n"
539 "<5>[ 720.459481] [<810d9441>] ? proc_reg_poll+0x76/0x76\n"
540 "<5>[ 720.459493] [<810a5e9e>] vfs_write+0x79/0xa5\n"
541 "<5>[ 720.459505] [<810a6011>] sys_write+0x40/0x65\n"
542 "<5>[ 720.459519] [<8137e677>] sysenter_do_call+0x12/0x26\n"
543 "<0>[ 720.459530] Kernel panic - not syncing: hung_task: blocked tasks\n"
544 "<5>[ 720.459768] Pid: 31, comm: khungtaskd Tainted: "
545 "G C 3.0.8 #1\n"
546 "<5>[ 720.459998] Call Trace:\n"
547 "<5>[ 720.460140] [<81378a35>] panic+0x53/0x14a\n"
548 "<5>[ 720.460312] [<8105f875>] watchdog+0x15b/0x1a0\n"
549 "<5>[ 720.460495] [<8105f71a>] ? hung_task_panic+0x16/0x16\n"
550 "<5>[ 720.460693] [<81043af3>] kthread+0x67/0x6c\n"
551 "<5>[ 720.460862] [<81043a8c>] ? __init_kthread_worker+0x2d/0x2d\n"
552 "<5>[ 720.461106] [<8137eb9e>] kernel_thread_helper+0x6/0x10\n";
553
554 EXPECT_TRUE(
555 collector_.ComputeKernelStackSignature(kHungTaskBreakMe,
556 &signature,
557 false));
558
559 EXPECT_EQ("kernel-(HANG)-hung_task: blocked tasks-600B37EA", signature);
560
561 // Panic with all question marks in the last stack trace.
562 const char kUncertainStackTrace[] =
563 "<0>[56279.689669] ------------[ cut here ]------------\n"
564 "<2>[56279.689677] kernel BUG at /build/x86-alex/tmp/portage/"
565 "sys-kernel/chromeos-kernel-0.0.1-r516/work/chromeos-kernel-0.0.1/"
566 "kernel/timer.c:844!\n"
567 "<0>[56279.689683] invalid opcode: 0000 [#1] SMP \n"
568 "<0>[56279.689688] last sysfs file: /sys/power/state\n"
569 "<5>[56279.689692] Modules linked in: nls_iso8859_1 nls_cp437 vfat fat "
570 "gobi usbnet tsl2583(C) industrialio(C) snd_hda_codec_realtek "
571 "snd_hda_intel i2c_dev snd_hda_codec snd_hwdep qcserial snd_pcm usb_wwan "
572 "i2c_i801 snd_timer nm10_gpio snd_page_alloc rtc_cmos fuse "
573 "nf_conntrack_ipv6 nf_defrag_ipv6 uvcvideo videodev ip6table_filter "
574 "ath9k ip6_tables ipv6 mac80211 ath9k_common ath9k_hw ath cfg80211 "
575 "xt_mark\n"
576 "<5>[56279.689731] \n"
577 "<5>[56279.689738] Pid: 24607, comm: powerd_suspend Tainted: G "
578 "WC 2.6.38.3+ #1 SAMSUNG ELECTRONICS CO., LTD. Alex/G100 \n"
579 "<5>[56279.689748] EIP: 0060:[<8103e3ea>] EFLAGS: 00210286 CPU: 3\n"
580 "<5>[56279.689758] EIP is at add_timer+0xd/0x1b\n"
581 "<5>[56279.689762] EAX: f5e00684 EBX: f5e003c0 ECX: 00000002 EDX: "
582 "00200246\n"
583 "<5>[56279.689767] ESI: f5e003c0 EDI: d28bc03c EBP: d2be5e40 ESP: "
584 "d2be5e40\n"
585 "<5>[56279.689772] DS: 007b ES: 007b FS: 00d8 GS: 00e0 SS: 0068\n"
586 "<0>[56279.689778] Process powerd_suspend (pid: 24607, ti=d2be4000 "
587 "task=f5dc9b60 task.ti=d2be4000)\n"
588 "<0>[56279.689782] Stack:\n"
589 "<5>[56279.689785] d2be5e4c f8dccced f4ac02c0 d2be5e70 f8ddc752 "
590 "f5e003c0 f4ac0458 f4ac092c\n"
591 "<5>[56279.689797] f4ac043c f4ac02c0 f4ac0000 f4ac007c d2be5e7c "
592 "f8dd4a33 f4ac0164 d2be5e94\n"
593 "<5>[56279.689809] f87e0304 f69ff0cc f4ac0164 f87e02a4 f4ac0164 "
594 "d2be5eb0 81248968 00000000\n"
595 "<0>[56279.689821] Call Trace:\n"
596 "<5>[56279.689840] [<f8dccced>] ieee80211_sta_restart+0x25/0x8c "
597 "[mac80211]\n"
598 "<5>[56279.689854] [<f8ddc752>] ieee80211_reconfig+0x2e9/0x339 "
599 "[mac80211]\n"
600 "<5>[56279.689869] [<f8dd4a33>] ieee80211_aes_cmac+0x182d/0x184e "
601 "[mac80211]\n"
602 "<5>[56279.689883] [<f87e0304>] cfg80211_get_dev_from_info+0x29b/0x2c0 "
603 "[cfg80211]\n"
604 "<5>[56279.689895] [<f87e02a4>] ? "
605 "cfg80211_get_dev_from_info+0x23b/0x2c0 [cfg80211]\n"
606 "<5>[56279.689904] [<81248968>] legacy_resume+0x25/0x5d\n"
607 "<5>[56279.689910] [<812490ae>] device_resume+0xdd/0x110\n"
608 "<5>[56279.689917] [<812491c2>] dpm_resume_end+0xe1/0x271\n"
609 "<5>[56279.689925] [<81060481>] suspend_devices_and_enter+0x18b/0x1de\n"
610 "<5>[56279.689932] [<810605ba>] enter_state+0xe6/0x132\n"
611 "<5>[56279.689939] [<8105fd4b>] state_store+0x91/0x9d\n"
612 "<5>[56279.689945] [<8105fcba>] ? state_store+0x0/0x9d\n"
613 "<5>[56279.689953] [<81178fb1>] kobj_attr_store+0x16/0x22\n"
614 "<5>[56279.689961] [<810eea5e>] sysfs_write_file+0xc1/0xec\n"
615 "<5>[56279.689969] [<810af443>] vfs_write+0x8f/0x101\n"
616 "<5>[56279.689975] [<810ee99d>] ? sysfs_write_file+0x0/0xec\n"
617 "<5>[56279.689982] [<810af556>] sys_write+0x40/0x65\n"
618 "<5>[56279.689989] [<81002d57>] sysenter_do_call+0x12/0x26\n"
619 "<0>[56279.689993] Code: c1 d3 e2 4a 89 55 f4 f7 d2 21 f2 6a 00 31 c9 89 "
620 "d8 e8 6e fd ff ff 5a 8d 65 f8 5b 5e 5d c3 55 89 e5 3e 8d 74 26 00 83 38 "
621 "00 74 04 <0f> 0b eb fe 8b 50 08 e8 6f ff ff ff 5d c3 55 89 e5 3e 8d 74 "
622 "26 \n"
623 "<0>[56279.690009] EIP: [<8103e3ea>] add_timer+0xd/0x1b SS:ESP "
624 "0068:d2be5e40\n"
625 "<4>[56279.690113] ---[ end trace b71141bb67c6032a ]---\n"
626 "<7>[56279.694069] wlan0: deauthenticated from 00:00:00:00:00:01 "
627 "(Reason: 6)\n"
628 "<0>[56279.703465] Kernel panic - not syncing: Fatal exception\n"
629 "<5>[56279.703471] Pid: 24607, comm: powerd_suspend Tainted: G D "
630 "WC 2.6.38.3+ #1\n"
631 "<5>[56279.703475] Call Trace:\n"
632 "<5>[56279.703483] [<8136648c>] ? panic+0x55/0x152\n"
633 "<5>[56279.703491] [<810057fa>] ? oops_end+0x73/0x81\n"
634 "<5>[56279.703497] [<81005a44>] ? die+0xed/0xf5\n"
635 "<5>[56279.703503] [<810033cb>] ? do_trap+0x7a/0x80\n"
636 "<5>[56279.703509] [<8100369b>] ? do_invalid_op+0x0/0x80\n"
637 "<5>[56279.703515] [<81003711>] ? do_invalid_op+0x76/0x80\n"
638 "<5>[56279.703522] [<8103e3ea>] ? add_timer+0xd/0x1b\n"
639 "<5>[56279.703529] [<81025e23>] ? check_preempt_curr+0x2e/0x69\n"
640 "<5>[56279.703536] [<8102ef28>] ? ttwu_post_activation+0x5a/0x11b\n"
641 "<5>[56279.703543] [<8102fa8d>] ? try_to_wake_up+0x213/0x21d\n"
642 "<5>[56279.703550] [<81368b7f>] ? error_code+0x67/0x6c\n"
643 "<5>[56279.703557] [<8103e3ea>] ? add_timer+0xd/0x1b\n"
644 "<5>[56279.703577] [<f8dccced>] ? ieee80211_sta_restart+0x25/0x8c "
645 "[mac80211]\n"
646 "<5>[56279.703591] [<f8ddc752>] ? ieee80211_reconfig+0x2e9/0x339 "
647 "[mac80211]\n"
648 "<5>[56279.703605] [<f8dd4a33>] ? ieee80211_aes_cmac+0x182d/0x184e "
649 "[mac80211]\n"
650 "<5>[56279.703618] [<f87e0304>] ? "
651 "cfg80211_get_dev_from_info+0x29b/0x2c0 [cfg80211]\n"
652 "<5>[56279.703630] [<f87e02a4>] ? "
653 "cfg80211_get_dev_from_info+0x23b/0x2c0 [cfg80211]\n"
654 "<5>[56279.703637] [<81248968>] ? legacy_resume+0x25/0x5d\n"
655 "<5>[56279.703643] [<812490ae>] ? device_resume+0xdd/0x110\n"
656 "<5>[56279.703649] [<812491c2>] ? dpm_resume_end+0xe1/0x271\n"
657 "<5>[56279.703657] [<81060481>] ? "
658 "suspend_devices_and_enter+0x18b/0x1de\n"
659 "<5>[56279.703663] [<810605ba>] ? enter_state+0xe6/0x132\n"
660 "<5>[56279.703670] [<8105fd4b>] ? state_store+0x91/0x9d\n"
661 "<5>[56279.703676] [<8105fcba>] ? state_store+0x0/0x9d\n"
662 "<5>[56279.703683] [<81178fb1>] ? kobj_attr_store+0x16/0x22\n"
663 "<5>[56279.703690] [<810eea5e>] ? sysfs_write_file+0xc1/0xec\n"
664 "<5>[56279.703697] [<810af443>] ? vfs_write+0x8f/0x101\n"
665 "<5>[56279.703703] [<810ee99d>] ? sysfs_write_file+0x0/0xec\n"
666 "<5>[56279.703709] [<810af556>] ? sys_write+0x40/0x65\n"
667 "<5>[56279.703716] [<81002d57>] ? sysenter_do_call+0x12/0x26\n";
668
669 EXPECT_TRUE(
670 collector_.ComputeKernelStackSignature(kUncertainStackTrace,
671 &signature,
672 false));
673 // The first trace contains only uncertain entries and its hash is 00000000,
674 // so, if we used that, the signature would be kernel-add_timer-00000000.
675 // Instead we use the second-to-last trace for the hash.
676 EXPECT_EQ("kernel-add_timer-B5178878", signature);
677
Simon Glassd74cc092011-04-06 10:47:01 -0700678 ComputeKernelStackSignatureCommon();
Ken Mixterafcf8082010-10-26 14:45:01 -0700679}