Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tom Sepez | 8726d30e | 2025-01-29 02:11:08 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/390223051): Remove C-library calls to fix the errors. |
| 7 | #pragma allow_unsafe_libc_calls |
| 8 | #endif |
| 9 | |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 10 | #include "base/debug/proc_maps_linux.h" |
| 11 | |
avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame] | 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
Thiabaud Engelbrecht | 309fb1ea | 2024-10-29 15:35:19 | [diff] [blame] | 14 | #include <sys/utsname.h> |
avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame] | 15 | |
Arthur Sonzogni | 0844a99 | 2024-12-12 11:36:20 | [diff] [blame] | 16 | #include <array> |
| 17 | |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 18 | #include "base/files/file_path.h" |
| 19 | #include "base/path_service.h" |
| 20 | #include "base/strings/stringprintf.h" |
Thiabaud Engelbrecht | 309fb1ea | 2024-10-29 15:35:19 | [diff] [blame] | 21 | #include "base/system/sys_info.h" |
scherkus | aff972d | 2014-11-21 01:36:04 | [diff] [blame] | 22 | #include "base/threading/platform_thread.h" |
avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame] | 23 | #include "build/build_config.h" |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
Thiabaud Engelbrecht | 309fb1ea | 2024-10-29 15:35:19 | [diff] [blame] | 26 | namespace base::debug { |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | // SmapsRollup was added in Linux 4.14. |
| 31 | bool IsSmapsRollupSupported() { |
| 32 | struct utsname info; |
| 33 | if (uname(&info) < 0) { |
| 34 | NOTREACHED(); |
| 35 | } |
| 36 | |
| 37 | int major, minor, patch; |
| 38 | if (sscanf(info.release, "%d.%d.%d", &major, &minor, &patch) < 3) { |
| 39 | NOTREACHED(); |
| 40 | } |
| 41 | |
| 42 | if (major > 4) { |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | if (major < 4 || minor < 14) { |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | } // namespace |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 54 | |
| 55 | TEST(ProcMapsTest, Empty) { |
| 56 | std::vector<MappedMemoryRegion> regions; |
| 57 | EXPECT_TRUE(ParseProcMaps("", ®ions)); |
| 58 | EXPECT_EQ(0u, regions.size()); |
| 59 | } |
| 60 | |
| 61 | TEST(ProcMapsTest, NoSpaces) { |
| 62 | static const char kNoSpaces[] = |
| 63 | "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat\n"; |
| 64 | |
| 65 | std::vector<MappedMemoryRegion> regions; |
| 66 | ASSERT_TRUE(ParseProcMaps(kNoSpaces, ®ions)); |
| 67 | ASSERT_EQ(1u, regions.size()); |
| 68 | |
| 69 | EXPECT_EQ(0x00400000u, regions[0].start); |
| 70 | EXPECT_EQ(0x0040b000u, regions[0].end); |
| 71 | EXPECT_EQ(0x00002200u, regions[0].offset); |
| 72 | EXPECT_EQ("/bin/cat", regions[0].path); |
| 73 | } |
| 74 | |
| 75 | TEST(ProcMapsTest, Spaces) { |
| 76 | static const char kSpaces[] = |
| 77 | "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/space cat\n"; |
| 78 | |
| 79 | std::vector<MappedMemoryRegion> regions; |
| 80 | ASSERT_TRUE(ParseProcMaps(kSpaces, ®ions)); |
| 81 | ASSERT_EQ(1u, regions.size()); |
| 82 | |
| 83 | EXPECT_EQ(0x00400000u, regions[0].start); |
| 84 | EXPECT_EQ(0x0040b000u, regions[0].end); |
| 85 | EXPECT_EQ(0x00002200u, regions[0].offset); |
| 86 | EXPECT_EQ("/bin/space cat", regions[0].path); |
| 87 | } |
| 88 | |
| 89 | TEST(ProcMapsTest, NoNewline) { |
| 90 | static const char kNoSpaces[] = |
| 91 | "00400000-0040b000 r-xp 00002200 fc:00 794418 /bin/cat"; |
| 92 | |
| 93 | std::vector<MappedMemoryRegion> regions; |
| 94 | ASSERT_FALSE(ParseProcMaps(kNoSpaces, ®ions)); |
| 95 | } |
| 96 | |
| 97 | TEST(ProcMapsTest, NoPath) { |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 98 | static const char kNoPath[] = "00400000-0040b000 rw-p 00000000 00:00 0 \n"; |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 99 | |
| 100 | std::vector<MappedMemoryRegion> regions; |
| 101 | ASSERT_TRUE(ParseProcMaps(kNoPath, ®ions)); |
| 102 | ASSERT_EQ(1u, regions.size()); |
| 103 | |
| 104 | EXPECT_EQ(0x00400000u, regions[0].start); |
| 105 | EXPECT_EQ(0x0040b000u, regions[0].end); |
| 106 | EXPECT_EQ(0x00000000u, regions[0].offset); |
| 107 | EXPECT_EQ("", regions[0].path); |
| 108 | } |
| 109 | |
| 110 | TEST(ProcMapsTest, Heap) { |
| 111 | static const char kHeap[] = |
| 112 | "022ac000-022cd000 rw-p 00000000 00:00 0 [heap]\n"; |
| 113 | |
| 114 | std::vector<MappedMemoryRegion> regions; |
| 115 | ASSERT_TRUE(ParseProcMaps(kHeap, ®ions)); |
| 116 | ASSERT_EQ(1u, regions.size()); |
| 117 | |
| 118 | EXPECT_EQ(0x022ac000u, regions[0].start); |
| 119 | EXPECT_EQ(0x022cd000u, regions[0].end); |
| 120 | EXPECT_EQ(0x00000000u, regions[0].offset); |
| 121 | EXPECT_EQ("[heap]", regions[0].path); |
| 122 | } |
| 123 | |
| 124 | #if defined(ARCH_CPU_32_BITS) |
| 125 | TEST(ProcMapsTest, Stack32) { |
| 126 | static const char kStack[] = |
| 127 | "beb04000-beb25000 rw-p 00000000 00:00 0 [stack]\n"; |
| 128 | |
| 129 | std::vector<MappedMemoryRegion> regions; |
| 130 | ASSERT_TRUE(ParseProcMaps(kStack, ®ions)); |
| 131 | ASSERT_EQ(1u, regions.size()); |
| 132 | |
| 133 | EXPECT_EQ(0xbeb04000u, regions[0].start); |
| 134 | EXPECT_EQ(0xbeb25000u, regions[0].end); |
| 135 | EXPECT_EQ(0x00000000u, regions[0].offset); |
| 136 | EXPECT_EQ("[stack]", regions[0].path); |
| 137 | } |
| 138 | #elif defined(ARCH_CPU_64_BITS) |
| 139 | TEST(ProcMapsTest, Stack64) { |
| 140 | static const char kStack[] = |
| 141 | "7fff69c5b000-7fff69c7d000 rw-p 00000000 00:00 0 [stack]\n"; |
| 142 | |
| 143 | std::vector<MappedMemoryRegion> regions; |
| 144 | ASSERT_TRUE(ParseProcMaps(kStack, ®ions)); |
| 145 | ASSERT_EQ(1u, regions.size()); |
| 146 | |
| 147 | EXPECT_EQ(0x7fff69c5b000u, regions[0].start); |
| 148 | EXPECT_EQ(0x7fff69c7d000u, regions[0].end); |
| 149 | EXPECT_EQ(0x00000000u, regions[0].offset); |
| 150 | EXPECT_EQ("[stack]", regions[0].path); |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | TEST(ProcMapsTest, Multiple) { |
| 155 | static const char kMultiple[] = |
| 156 | "00400000-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n" |
| 157 | "0060a000-0060b000 r--p 0000a000 fc:00 794418 /bin/cat\n" |
| 158 | "0060b000-0060c000 rw-p 0000b000 fc:00 794418 /bin/cat\n"; |
| 159 | |
| 160 | std::vector<MappedMemoryRegion> regions; |
| 161 | ASSERT_TRUE(ParseProcMaps(kMultiple, ®ions)); |
| 162 | ASSERT_EQ(3u, regions.size()); |
| 163 | |
| 164 | EXPECT_EQ(0x00400000u, regions[0].start); |
| 165 | EXPECT_EQ(0x0040b000u, regions[0].end); |
| 166 | EXPECT_EQ(0x00000000u, regions[0].offset); |
| 167 | EXPECT_EQ("/bin/cat", regions[0].path); |
| 168 | |
| 169 | EXPECT_EQ(0x0060a000u, regions[1].start); |
| 170 | EXPECT_EQ(0x0060b000u, regions[1].end); |
| 171 | EXPECT_EQ(0x0000a000u, regions[1].offset); |
| 172 | EXPECT_EQ("/bin/cat", regions[1].path); |
| 173 | |
| 174 | EXPECT_EQ(0x0060b000u, regions[2].start); |
| 175 | EXPECT_EQ(0x0060c000u, regions[2].end); |
| 176 | EXPECT_EQ(0x0000b000u, regions[2].offset); |
| 177 | EXPECT_EQ("/bin/cat", regions[2].path); |
| 178 | } |
| 179 | |
| 180 | TEST(ProcMapsTest, Permissions) { |
Arthur Sonzogni | 0844a99 | 2024-12-12 11:36:20 | [diff] [blame] | 181 | struct TestCases { |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 182 | const char* input; |
avi | ebe805c | 2015-12-24 08:20:28 | [diff] [blame] | 183 | uint8_t permissions; |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 184 | }; |
Arthur Sonzogni | 0844a99 | 2024-12-12 11:36:20 | [diff] [blame] | 185 | static auto kTestCases = std::to_array<TestCases>({ |
| 186 | {"00400000-0040b000 ---s 00000000 fc:00 794418 /bin/cat\n", 0}, |
| 187 | {"00400000-0040b000 ---S 00000000 fc:00 794418 /bin/cat\n", 0}, |
| 188 | {"00400000-0040b000 r--s 00000000 fc:00 794418 /bin/cat\n", |
| 189 | MappedMemoryRegion::READ}, |
| 190 | {"00400000-0040b000 -w-s 00000000 fc:00 794418 /bin/cat\n", |
| 191 | MappedMemoryRegion::WRITE}, |
| 192 | {"00400000-0040b000 --xs 00000000 fc:00 794418 /bin/cat\n", |
| 193 | MappedMemoryRegion::EXECUTE}, |
| 194 | {"00400000-0040b000 rwxs 00000000 fc:00 794418 /bin/cat\n", |
| 195 | MappedMemoryRegion::READ | MappedMemoryRegion::WRITE | |
| 196 | MappedMemoryRegion::EXECUTE}, |
| 197 | {"00400000-0040b000 ---p 00000000 fc:00 794418 /bin/cat\n", |
| 198 | MappedMemoryRegion::PRIVATE}, |
| 199 | {"00400000-0040b000 r--p 00000000 fc:00 794418 /bin/cat\n", |
| 200 | MappedMemoryRegion::READ | MappedMemoryRegion::PRIVATE}, |
| 201 | {"00400000-0040b000 -w-p 00000000 fc:00 794418 /bin/cat\n", |
| 202 | MappedMemoryRegion::WRITE | MappedMemoryRegion::PRIVATE}, |
| 203 | {"00400000-0040b000 --xp 00000000 fc:00 794418 /bin/cat\n", |
| 204 | MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE}, |
| 205 | {"00400000-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 206 | MappedMemoryRegion::READ | MappedMemoryRegion::WRITE | |
| 207 | MappedMemoryRegion::EXECUTE | MappedMemoryRegion::PRIVATE}, |
| 208 | }); |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 209 | |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 210 | for (size_t i = 0; i < std::size(kTestCases); ++i) { |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 211 | SCOPED_TRACE( |
| 212 | base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i].input)); |
| 213 | |
| 214 | std::vector<MappedMemoryRegion> regions; |
| 215 | EXPECT_TRUE(ParseProcMaps(kTestCases[i].input, ®ions)); |
| 216 | EXPECT_EQ(1u, regions.size()); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 217 | if (regions.empty()) { |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 218 | continue; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 219 | } |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 220 | EXPECT_EQ(kTestCases[i].permissions, regions[0].permissions); |
| 221 | } |
| 222 | } |
| 223 | |
glider | fb699a7e | 2014-12-05 12:20:28 | [diff] [blame] | 224 | // AddressSanitizer may move local variables to a dedicated "fake stack" which |
| 225 | // is outside the stack region listed in /proc/self/maps. We disable ASan |
| 226 | // instrumentation for this function to force the variable to be local. |
Peter Collingbourne | 5a35305d | 2019-02-06 02:51:43 | [diff] [blame] | 227 | // |
| 228 | // Similarly, HWAddressSanitizer may add a tag to all stack pointers which may |
| 229 | // move it outside of the stack regions in /proc/self/maps. |
| 230 | __attribute__((no_sanitize("address", "hwaddress"))) void CheckProcMapsRegions( |
| 231 | const std::vector<MappedMemoryRegion>& regions) { |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 232 | // We should be able to find both the current executable as well as the stack |
glider | fb699a7e | 2014-12-05 12:20:28 | [diff] [blame] | 233 | // mapped into memory. Use the address of |exe_path| as a way of finding the |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 234 | // stack. |
| 235 | FilePath exe_path; |
| 236 | EXPECT_TRUE(PathService::Get(FILE_EXE, &exe_path)); |
glider | fb699a7e | 2014-12-05 12:20:28 | [diff] [blame] | 237 | uintptr_t address = reinterpret_cast<uintptr_t>(&exe_path); |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 238 | bool found_exe = false; |
| 239 | bool found_stack = false; |
[email protected] | fe7bc8df | 2013-07-04 06:06:09 | [diff] [blame] | 240 | bool found_address = false; |
scherkus | aff972d | 2014-11-21 01:36:04 | [diff] [blame] | 241 | |
jdoerrie | 6c622935 | 2018-10-22 15:55:43 | [diff] [blame] | 242 | for (const auto& i : regions) { |
| 243 | if (i.path == exe_path.value()) { |
[email protected] | fe7bc8df | 2013-07-04 06:06:09 | [diff] [blame] | 244 | // It's OK to find the executable mapped multiple times as there'll be |
| 245 | // multiple sections (e.g., text, data). |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 246 | found_exe = true; |
| 247 | } |
| 248 | |
jdoerrie | 6c622935 | 2018-10-22 15:55:43 | [diff] [blame] | 249 | if (i.path == "[stack]") { |
jcivelli | f4462a35 | 2017-01-10 04:45:59 | [diff] [blame] | 250 | // On Android the test is run on a background thread, since [stack] is for |
| 251 | // the main thread, we cannot test this. |
Xiaohan Wang | 131aa4d | 2022-01-15 19:39:41 | [diff] [blame] | 252 | #if !BUILDFLAG(IS_ANDROID) |
jdoerrie | 6c622935 | 2018-10-22 15:55:43 | [diff] [blame] | 253 | EXPECT_GE(address, i.start); |
| 254 | EXPECT_LT(address, i.end); |
jcivelli | f4462a35 | 2017-01-10 04:45:59 | [diff] [blame] | 255 | #endif |
jdoerrie | 6c622935 | 2018-10-22 15:55:43 | [diff] [blame] | 256 | EXPECT_TRUE(i.permissions & MappedMemoryRegion::READ); |
| 257 | EXPECT_TRUE(i.permissions & MappedMemoryRegion::WRITE); |
| 258 | EXPECT_FALSE(i.permissions & MappedMemoryRegion::EXECUTE); |
| 259 | EXPECT_TRUE(i.permissions & MappedMemoryRegion::PRIVATE); |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 260 | EXPECT_FALSE(found_stack) << "Found duplicate stacks"; |
| 261 | found_stack = true; |
| 262 | } |
[email protected] | fe7bc8df | 2013-07-04 06:06:09 | [diff] [blame] | 263 | |
jdoerrie | 6c622935 | 2018-10-22 15:55:43 | [diff] [blame] | 264 | if (address >= i.start && address < i.end) { |
[email protected] | fe7bc8df | 2013-07-04 06:06:09 | [diff] [blame] | 265 | EXPECT_FALSE(found_address) << "Found same address in multiple regions"; |
| 266 | found_address = true; |
| 267 | } |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | EXPECT_TRUE(found_exe); |
Mostyn Bramley-Moore | d0ecd6a | 2017-12-06 19:13:21 | [diff] [blame] | 271 | EXPECT_TRUE(found_stack); |
| 272 | EXPECT_TRUE(found_address); |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 273 | } |
| 274 | |
glider | fb699a7e | 2014-12-05 12:20:28 | [diff] [blame] | 275 | TEST(ProcMapsTest, ReadProcMaps) { |
| 276 | std::string proc_maps; |
| 277 | ASSERT_TRUE(ReadProcMaps(&proc_maps)); |
| 278 | |
| 279 | std::vector<MappedMemoryRegion> regions; |
| 280 | ASSERT_TRUE(ParseProcMaps(proc_maps, ®ions)); |
| 281 | ASSERT_FALSE(regions.empty()); |
| 282 | |
| 283 | CheckProcMapsRegions(regions); |
| 284 | } |
| 285 | |
[email protected] | 35c8083 | 2013-09-06 05:07:50 | [diff] [blame] | 286 | TEST(ProcMapsTest, ReadProcMapsNonEmptyString) { |
| 287 | std::string old_string("I forgot to clear the string"); |
| 288 | std::string proc_maps(old_string); |
| 289 | ASSERT_TRUE(ReadProcMaps(&proc_maps)); |
| 290 | EXPECT_EQ(std::string::npos, proc_maps.find(old_string)); |
| 291 | } |
| 292 | |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 293 | TEST(ProcMapsTest, MissingFields) { |
Arthur Sonzogni | 0844a99 | 2024-12-12 11:36:20 | [diff] [blame] | 294 | static const auto kTestCases = std::to_array<const char*>({ |
| 295 | "00400000\n", // Missing end + beyond. |
| 296 | "00400000-0040b000\n", // Missing perms + beyond. |
| 297 | "00400000-0040b000 r-xp\n", // Missing offset + beyond. |
| 298 | "00400000-0040b000 r-xp 00000000\n", // Missing device + beyond. |
| 299 | "00400000-0040b000 r-xp 00000000 fc:00\n", // Missing inode + beyond. |
| 300 | "00400000-0040b000 00000000 fc:00 794418 /bin/cat\n", // Missing perms. |
| 301 | "00400000-0040b000 r-xp fc:00 794418 /bin/cat\n", // Missing offset. |
| 302 | "00400000-0040b000 r-xp 00000000 fc:00 /bin/cat\n", // Missing inode. |
| 303 | "00400000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing end. |
| 304 | "-0040b000 r-xp 00000000 fc:00 794418 /bin/cat\n", // Missing start. |
| 305 | "00400000-0040b000 r-xp 00000000 794418 /bin/cat\n", // Missing device. |
| 306 | }); |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 307 | |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 308 | for (size_t i = 0; i < std::size(kTestCases); ++i) { |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 309 | SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i])); |
| 310 | std::vector<MappedMemoryRegion> regions; |
| 311 | EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | TEST(ProcMapsTest, InvalidInput) { |
Arthur Sonzogni | 0844a99 | 2024-12-12 11:36:20 | [diff] [blame] | 316 | static const auto kTestCases = std::to_array<const char*>({ |
| 317 | "thisisal-0040b000 rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 318 | "0040000d-linvalid rwxp 00000000 fc:00 794418 /bin/cat\n", |
| 319 | "00400000-0040b000 inpu 00000000 fc:00 794418 /bin/cat\n", |
| 320 | "00400000-0040b000 rwxp tforproc fc:00 794418 /bin/cat\n", |
| 321 | "00400000-0040b000 rwxp 00000000 ma:ps 794418 /bin/cat\n", |
| 322 | "00400000-0040b000 rwxp 00000000 fc:00 parse! /bin/cat\n", |
| 323 | }); |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 324 | |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 325 | for (size_t i = 0; i < std::size(kTestCases); ++i) { |
[email protected] | 959a8bf | 2013-07-03 02:02:23 | [diff] [blame] | 326 | SCOPED_TRACE(base::StringPrintf("kTestCases[%zu] = %s", i, kTestCases[i])); |
| 327 | std::vector<MappedMemoryRegion> regions; |
| 328 | EXPECT_FALSE(ParseProcMaps(kTestCases[i], ®ions)); |
| 329 | } |
| 330 | } |
| 331 | |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 332 | TEST(ProcMapsTest, ParseProcMapsEmptyString) { |
| 333 | std::vector<MappedMemoryRegion> regions; |
| 334 | EXPECT_TRUE(ParseProcMaps("", ®ions)); |
| 335 | EXPECT_EQ(0ULL, regions.size()); |
| 336 | } |
| 337 | |
| 338 | // Testing a couple of remotely possible weird things in the input: |
| 339 | // - Line ending with \r\n or \n\r. |
| 340 | // - File name contains quotes. |
| 341 | // - File name has whitespaces. |
| 342 | TEST(ProcMapsTest, ParseProcMapsWeirdCorrectInput) { |
| 343 | std::vector<MappedMemoryRegion> regions; |
| 344 | const std::string kContents = |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 345 | "00400000-0040b000 r-xp 00000000 fc:00 2106562 " |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 346 | " /bin/cat\r\n" |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 347 | "7f53b7dad000-7f53b7f62000 r-xp 00000000 fc:00 263011 " |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 348 | " /lib/x86_64-linux-gnu/libc-2.15.so\n\r" |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 349 | "7f53b816d000-7f53b818f000 r-xp 00000000 fc:00 264284 " |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 350 | " /lib/x86_64-linux-gnu/ld-2.15.so\n" |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 351 | "7fff9c7ff000-7fff9c800000 r-xp 00000000 00:00 0 " |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 352 | " \"vd so\"\n" |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 353 | "ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 " |
[email protected] | ad9a012 | 2014-03-22 00:34:52 | [diff] [blame] | 354 | " [vsys call]\n"; |
| 355 | EXPECT_TRUE(ParseProcMaps(kContents, ®ions)); |
| 356 | EXPECT_EQ(5ULL, regions.size()); |
| 357 | EXPECT_EQ("/bin/cat", regions[0].path); |
| 358 | EXPECT_EQ("/lib/x86_64-linux-gnu/libc-2.15.so", regions[1].path); |
| 359 | EXPECT_EQ("/lib/x86_64-linux-gnu/ld-2.15.so", regions[2].path); |
| 360 | EXPECT_EQ("\"vd so\"", regions[3].path); |
| 361 | EXPECT_EQ("[vsys call]", regions[4].path); |
| 362 | } |
| 363 | |
Thiabaud Engelbrecht | 0ccd4da | 2024-11-28 04:31:17 | [diff] [blame] | 364 | TEST(SmapsRollupTest, ReadAndParse) { |
Thiabaud Engelbrecht | 309fb1ea | 2024-10-29 15:35:19 | [diff] [blame] | 365 | if (!IsSmapsRollupSupported()) { |
| 366 | GTEST_SKIP() << "smaps_rollup not supported"; |
| 367 | } |
| 368 | |
| 369 | const auto result = ReadAndParseSmapsRollup(); |
| 370 | |
| 371 | EXPECT_TRUE(result.has_value()); |
| 372 | |
| 373 | SmapsRollup smaps_rollup = result.value(); |
| 374 | |
| 375 | EXPECT_GT(smaps_rollup.rss, 0u); |
| 376 | EXPECT_GT(smaps_rollup.pss, 0u); |
Thiabaud Engelbrecht | 309fb1ea | 2024-10-29 15:35:19 | [diff] [blame] | 377 | EXPECT_GT(smaps_rollup.private_dirty, 0u); |
| 378 | } |
| 379 | |
| 380 | TEST(SmapsRollupTest, Valid) { |
| 381 | const auto result = ParseSmapsRollupForTesting( |
| 382 | // This input is based on a real one captured locally, but with some |
| 383 | // values changed in order to make them unique (to test that the correct |
| 384 | // values are being parsed). |
| 385 | R"(55f4d118e000-7ffff6e62000 ---p 00000000 00:00 0 [rollup] |
| 386 | Rss: 1908 kB |
| 387 | Pss: 573 kB |
| 388 | Pss_Dirty: 144 kB |
| 389 | Pss_Anon: 100 kB |
| 390 | Pss_File: 469 kB |
| 391 | Pss_Shmem: 12 kB |
| 392 | Shared_Clean: 1356 kB |
| 393 | Shared_Dirty: 0 kB |
| 394 | Private_Clean: 448 kB |
| 395 | Private_Dirty: 104 kB |
| 396 | Referenced: 1900 kB |
| 397 | Anonymous: 105 kB |
| 398 | KSM: 0 kB |
| 399 | LazyFree: 0 kB |
| 400 | AnonHugePages: 0 kB |
| 401 | ShmemPmdMapped: 0 kB |
| 402 | FilePmdMapped: 0 kB |
| 403 | Shared_Hugetlb: 0 kB |
| 404 | Private_Hugetlb: 0 kB |
| 405 | Swap: 10 kB |
| 406 | SwapPss: 20 kB |
| 407 | Locked: 0 kB |
| 408 | )"); |
| 409 | |
| 410 | EXPECT_TRUE(result.has_value()); |
| 411 | |
| 412 | SmapsRollup smaps_rollup = result.value(); |
| 413 | |
| 414 | EXPECT_EQ(smaps_rollup.rss, 1024 * 1908u); |
| 415 | EXPECT_EQ(smaps_rollup.pss, 1024 * 573u); |
| 416 | EXPECT_EQ(smaps_rollup.private_dirty, 1024 * 104u); |
| 417 | EXPECT_EQ(smaps_rollup.pss_anon, 1024 * 100u); |
| 418 | EXPECT_EQ(smaps_rollup.pss_file, 1024 * 469u); |
| 419 | EXPECT_EQ(smaps_rollup.pss_shmem, 1024 * 12u); |
| 420 | EXPECT_EQ(smaps_rollup.swap, 1024 * 10u); |
| 421 | EXPECT_EQ(smaps_rollup.swap_pss, 1024 * 20u); |
| 422 | } |
| 423 | |
| 424 | } // namespace base::debug |