Alexander Potapenko | 93b854f | 2022-02-07 19:22:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022, 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 | */ |
| 16 | |
| 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include "dmesg_parser.h" |
| 23 | |
| 24 | class DmesgParserTest : public ::testing::Test { |
| 25 | public: |
| 26 | void ReadLines(const std::vector<std::string>& lines); |
| 27 | bool CheckReport(const std::vector<std::string>& lines); |
| 28 | |
| 29 | dmesg_parser::DmesgParser parser; |
| 30 | std::string parsed_report; |
| 31 | }; |
| 32 | |
| 33 | void DmesgParserTest::ReadLines(const std::vector<std::string>& lines) { |
| 34 | for (auto line : lines) parser.ProcessLine(line); |
| 35 | } |
| 36 | |
| 37 | bool DmesgParserTest::CheckReport(const std::vector<std::string>& lines) { |
| 38 | if (!parser.ReportReady()) return false; |
| 39 | parsed_report = parser.FlushReport(); |
| 40 | |
| 41 | std::string report; |
| 42 | for (auto line : lines) { |
| 43 | report += line; |
| 44 | } |
| 45 | EXPECT_EQ(report, parsed_report); |
| 46 | return report == parsed_report; |
| 47 | } |
| 48 | |
| 49 | TEST_F(DmesgParserTest, SimpleKasanReport) { |
| 50 | std::vector<std::string> in = { |
| 51 | "[ 495.412333] [ T1] init: this line will be dropped\n", |
| 52 | "[ 495.412345] [ T9971] " |
| 53 | "==================================================================\n", |
| 54 | "[ 495.496694] [ T9971] BUG: KASAN: invalid-access in crash_write+0x134/0x140\n", |
| 55 | "[ 495.712345] [ T9971] " |
| 56 | "==================================================================\n", |
| 57 | "[ 495.767899] [ T9971] logs after the separator do not belong to report\n", |
| 58 | }; |
| 59 | |
| 60 | std::vector<std::string> report = { |
| 61 | "[ 495.496694] [ T9971] BUG: KASAN: invalid-access in crash_write+0x134/0x140\n", |
| 62 | }; |
| 63 | |
| 64 | ReadLines(in); |
| 65 | ASSERT_TRUE(parser.ReportReady()); |
| 66 | ASSERT_EQ("KASAN", parser.ReportType()); |
| 67 | ASSERT_EQ("BUG: KASAN: invalid-access in crash_write+0x134/0x140", parser.ReportTitle()); |
| 68 | ASSERT_TRUE(CheckReport(report)); |
| 69 | } |
| 70 | |
| 71 | TEST_F(DmesgParserTest, StrippedKasanReport) { |
| 72 | /* |
| 73 | * From the following report, only the lines from T9971 between the "=======" |
| 74 | * delimiters will be preserved, and only those that do not contain raw |
| 75 | * memory. |
| 76 | * Task name is also stripped off, because it may contain sensitive data. |
| 77 | */ |
| 78 | std::vector<std::string> in = { |
| 79 | "[ 495.412333] [ T1] init: this line will be dropped\n", |
| 80 | "[ 495.412345] [ T9971] " |
| 81 | "==================================================================\n", |
| 82 | "[ 495.496694] [ T9971] BUG: KASAN: invalid-access in crash_write+0x134/0x140\n", |
| 83 | "[ 495.501234] [ T333] random_process: interleaving output with our error report\n", |
| 84 | "[ 495.503671] [ T9971] Read at addr f0ffff87c23fdf7f by task sh/9971\n", |
| 85 | "[ 495.510025] [ T9971] Pointer tag: [f0], memory tag: [fe]\n", |
| 86 | "[ 495.515400] [ T9971] \n", |
| 87 | "[ 495.667603] [ T9971] raw: 4000000000010200 0000000000000000 0000000000000000 " |
| 88 | "0000000100200020\n", |
| 89 | "[ 495.667634] [ T9971] raw: dead000000000100 dead000000000200 ffffffc14900fc00 " |
| 90 | "0000000000000000\n", |
| 91 | "[ 495.712345] [ T9971] " |
| 92 | "==================================================================\n", |
| 93 | "[ 495.767899] [ T9971] logs after the separator do not belong to report\n", |
| 94 | }; |
| 95 | |
| 96 | std::vector<std::string> report = { |
| 97 | "[ 495.496694] [ T9971] BUG: KASAN: invalid-access in crash_write+0x134/0x140\n", |
| 98 | "[ 495.503671] [ T9971] Read at addr XXXXXXXXXXXXXXXX by task DELETED\n", |
| 99 | "[ 495.510025] [ T9971] Pointer tag: [f0], memory tag: [fe]\n", |
| 100 | "[ 495.515400] [ T9971] \n", |
| 101 | }; |
| 102 | |
| 103 | ReadLines(in); |
| 104 | ASSERT_TRUE(parser.ReportReady()); |
| 105 | ASSERT_EQ("KASAN", parser.ReportType()); |
| 106 | ASSERT_EQ("BUG: KASAN: invalid-access in crash_write+0x134/0x140", parser.ReportTitle()); |
| 107 | ASSERT_TRUE(CheckReport(report)); |
| 108 | } |
| 109 | |
| 110 | TEST_F(DmesgParserTest, SimpleKfenceReport) { |
| 111 | std::vector<std::string> in = { |
| 112 | "[ 495.412333] [ T1] init: this line will be dropped\n", |
| 113 | "[ 495.412345] [ T9971] " |
| 114 | "==================================================================\n", |
| 115 | "[ 495.496694] [ T9971] BUG: KFENCE: memory corruption in " |
| 116 | "test_corruption+0x98/0x19c\n", |
| 117 | "[ 495.712345] [ T9971] " |
| 118 | "==================================================================\n", |
| 119 | "[ 495.767899] [ T9971] logs after the separator do not belong to report\n", |
| 120 | }; |
| 121 | |
| 122 | std::vector<std::string> report = { |
| 123 | "[ 495.496694] [ T9971] BUG: KFENCE: memory corruption in " |
| 124 | "test_corruption+0x98/0x19c\n", |
| 125 | }; |
| 126 | |
| 127 | ReadLines(in); |
| 128 | ASSERT_TRUE(parser.ReportReady()); |
| 129 | ASSERT_EQ("KFENCE", parser.ReportType()); |
| 130 | ASSERT_EQ("BUG: KFENCE: memory corruption in test_corruption+0x98/0x19c", parser.ReportTitle()); |
| 131 | ASSERT_TRUE(CheckReport(report)); |
| 132 | } |
| 133 | |
| 134 | TEST_F(DmesgParserTest, StrippedKfenceReport) { |
| 135 | std::vector<std::string> in = { |
| 136 | "[ 200.412333] [ T1] init: this line will be dropped\n", |
| 137 | "[ 213.648234] [ T8752] " |
| 138 | "==================================================================\n", |
| 139 | "[ 213.648253] [ T8752] BUG: KFENCE: out-of-bounds write in crash_write+0x14c/0x174\n", |
| 140 | "[ 213.648262] [ T8752] Out-of-bounds write at 0xffffff8938a05000 (4096B left of " |
| 141 | "kfence-#2):\n", |
| 142 | "[ 213.648270] [ T8752] crash_write+0x14c/0x174\n", |
| 143 | "[ 213.648367] [ T8752] kfence-#2 [0xffffff8938a06000-0xffffff8938a0603f, size=64, " |
| 144 | "cache=kmalloc-128] allocated by task 1:\n", |
| 145 | "[ 213.648471] [ T8752] CPU: 1 PID: 8752 Comm: sh Tainted: G C O\n", |
| 146 | "[ 213.648478] [ T8752] Hardware name: Phone 1\n", |
| 147 | "[ 213.648498] [ T8752] " |
| 148 | "==================================================================\n", |
| 149 | "[ 495.767899] [ T8752] logs after the separator do not belong to report\n", |
| 150 | }; |
| 151 | |
| 152 | std::vector<std::string> report = { |
| 153 | "[ 213.648253] [ T8752] BUG: KFENCE: out-of-bounds write in crash_write+0x14c/0x174\n", |
| 154 | "[ 213.648262] [ T8752] Out-of-bounds write at XXXXXXXXXXXXXXXX (4096B left of " |
| 155 | "kfence-#2):\n", |
| 156 | "[ 213.648270] [ T8752] crash_write+0x14c/0x174\n", |
| 157 | "[ 213.648367] [ T8752] kfence-#2 [XXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX, size=64, " |
| 158 | "cache=kmalloc-128] allocated by task DELETED\n", |
| 159 | }; |
| 160 | |
| 161 | ReadLines(in); |
| 162 | ASSERT_TRUE(parser.ReportReady()); |
| 163 | ASSERT_EQ("KFENCE", parser.ReportType()); |
| 164 | ASSERT_EQ("BUG: KFENCE: out-of-bounds write in crash_write+0x14c/0x174", parser.ReportTitle()); |
| 165 | ASSERT_TRUE(CheckReport(report)); |
| 166 | } |
| 167 | |
| 168 | TEST_F(DmesgParserTest, PartialReport) { |
| 169 | std::vector<std::string> in = { |
| 170 | "[ 213.648234] [ T8752] " |
| 171 | "==================================================================\n", |
| 172 | "[ 213.648253] [ T8752] BUG: KFENCE: out-of-bounds write in crash_write+0x14c/0x174\n", |
| 173 | "[ 213.648262] [ T8752] Out-of-bounds write at 0xffffff8938a05000 (4096B left of " |
| 174 | "kfence-#2):\n", |
| 175 | "[ 213.648270] [ T8752] crash_write+0x14c/0x174\n", |
| 176 | }; |
| 177 | |
| 178 | ReadLines(in); |
| 179 | ASSERT_FALSE(parser.ReportReady()); |
| 180 | } |
| 181 | |
| 182 | TEST_F(DmesgParserTest, TwoReports) { |
| 183 | std::vector<std::string> in = { |
| 184 | "[ 200.412333] [ T1] init: this line will be dropped\n", |
| 185 | "[ 213.648234] [ T8752] " |
| 186 | "==================================================================\n", |
| 187 | "[ 213.648253] [ T8752] BUG: KFENCE: out-of-bounds write in crash_write+0x14c/0x174\n", |
| 188 | "[ 213.648262] [ T8752] Out-of-bounds write at 0xffffff8938a05000 (4096B left of " |
| 189 | "kfence-#2):\n", |
| 190 | "[ 214.648234] [ T9971] " |
| 191 | "==================================================================\n", |
| 192 | "[ 215.496694] [ T9971] BUG: KFENCE: memory corruption in " |
| 193 | "test_corruption+0x98/0x19c\n", |
| 194 | "[ 216.648270] [ T8752] crash_write+0x14c/0x174\n", |
| 195 | "[ 217.648234] [ T8752] " |
| 196 | "==================================================================\n", |
| 197 | }; |
| 198 | |
| 199 | std::vector<std::string> report = { |
| 200 | "[ 213.648253] [ T8752] BUG: KFENCE: out-of-bounds write in crash_write+0x14c/0x174\n", |
| 201 | "[ 213.648262] [ T8752] Out-of-bounds write at XXXXXXXXXXXXXXXX (4096B left of " |
| 202 | "kfence-#2):\n", |
| 203 | "[ 216.648270] [ T8752] crash_write+0x14c/0x174\n", |
| 204 | }; |
| 205 | |
| 206 | ReadLines(in); |
| 207 | ASSERT_TRUE(parser.ReportReady()); |
| 208 | ASSERT_EQ("KFENCE", parser.ReportType()); |
| 209 | ASSERT_EQ("BUG: KFENCE: out-of-bounds write in crash_write+0x14c/0x174", parser.ReportTitle()); |
| 210 | ASSERT_TRUE(CheckReport(report)); |
| 211 | } |