1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "base/trace_event/process_memory_dump.h"
#include <stddef.h>
#include <array>
#include <memory>
#include <optional>
#include <string_view>
#include "base/memory/aligned_memory.h"
#include "base/memory/ptr_util.h"
#include "base/memory/shared_memory_tracker.h"
#include "base/memory/writable_shared_memory_region.h"
#include "base/process/process_metrics.h"
#include "base/trace_event/memory_allocator_dump_guid.h"
#include "base/trace_event/memory_infra_background_allowlist.h"
#include "base/trace_event/trace_log.h"
#include "base/trace_event/traced_value.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include "base/win/winbase_shim.h"
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include <sys/mman.h>
#endif
namespace base::trace_event {
namespace {
const MemoryDumpArgs kDetailedDumpArgs = {MemoryDumpLevelOfDetail::kDetailed};
constexpr std::string_view kTestDumpNameAllowlist[] = {
"Allowlisted/TestName", "Allowlisted/TestName_0x?",
"Allowlisted/0x?/TestName", "Allowlisted/0x?"};
void* Map(size_t size) {
#if BUILDFLAG(IS_WIN)
return ::VirtualAlloc(nullptr, size, MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
return ::mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
0, 0);
#endif
}
void Unmap(void* addr, size_t size) {
#if BUILDFLAG(IS_WIN)
::VirtualFree(addr, 0, MEM_DECOMMIT);
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
::munmap(addr, size);
#else
#error This architecture is not (yet) supported.
#endif
}
std::optional<size_t> CountResidentBytesInSharedMemory(
WritableSharedMemoryMapping& mapping) {
// SAFETY: We need the actual mapped memory size here. There's no public
// method to get this as a span, so we need to construct it unsafely. The
// mapped_size() is larger than `mem.size()` but represents the actual memory
// segment size in the SharedMemoryMapping.
auto mapped =
UNSAFE_BUFFERS(base::span(mapping.data(), mapping.mapped_size()));
return ProcessMemoryDump::CountResidentBytesInSharedMemory(mapped.data(),
mapped.size());
}
} // namespace
TEST(ProcessMemoryDumpTest, MoveConstructor) {
ProcessMemoryDump pmd1 = ProcessMemoryDump(kDetailedDumpArgs);
pmd1.CreateAllocatorDump("mad1");
pmd1.CreateAllocatorDump("mad2");
pmd1.AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
MemoryAllocatorDumpGuid(4242));
ProcessMemoryDump pmd2(std::move(pmd1));
EXPECT_EQ(1u, pmd2.allocator_dumps().count("mad1"));
EXPECT_EQ(1u, pmd2.allocator_dumps().count("mad2"));
EXPECT_EQ(MemoryDumpLevelOfDetail::kDetailed,
pmd2.dump_args().level_of_detail);
EXPECT_EQ(1u, pmd2.allocator_dumps_edges().size());
// Check that calling serialization routines doesn't cause a crash.
auto traced_value = std::make_unique<TracedValue>();
pmd2.SerializeAllocatorDumpsInto(traced_value.get());
}
TEST(ProcessMemoryDumpTest, MoveAssignment) {
ProcessMemoryDump pmd1 = ProcessMemoryDump(kDetailedDumpArgs);
pmd1.CreateAllocatorDump("mad1");
pmd1.CreateAllocatorDump("mad2");
pmd1.AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
MemoryAllocatorDumpGuid(4242));
ProcessMemoryDump pmd2({MemoryDumpLevelOfDetail::kBackground});
pmd2.CreateAllocatorDump("malloc");
pmd2 = std::move(pmd1);
EXPECT_EQ(1u, pmd2.allocator_dumps().count("mad1"));
EXPECT_EQ(1u, pmd2.allocator_dumps().count("mad2"));
EXPECT_EQ(0u, pmd2.allocator_dumps().count("mad3"));
EXPECT_EQ(MemoryDumpLevelOfDetail::kDetailed,
pmd2.dump_args().level_of_detail);
EXPECT_EQ(1u, pmd2.allocator_dumps_edges().size());
// Check that calling serialization routines doesn't cause a crash.
auto traced_value = std::make_unique<TracedValue>();
pmd2.SerializeAllocatorDumpsInto(traced_value.get());
}
TEST(ProcessMemoryDumpTest, Clear) {
std::unique_ptr<ProcessMemoryDump> pmd1(
new ProcessMemoryDump(kDetailedDumpArgs));
pmd1->CreateAllocatorDump("mad1");
pmd1->CreateAllocatorDump("mad2");
ASSERT_FALSE(pmd1->allocator_dumps().empty());
pmd1->AddOwnershipEdge(MemoryAllocatorDumpGuid(42),
MemoryAllocatorDumpGuid(4242));
MemoryAllocatorDumpGuid shared_mad_guid1(1);
MemoryAllocatorDumpGuid shared_mad_guid2(2);
pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1);
pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid2);
pmd1->Clear();
ASSERT_TRUE(pmd1->allocator_dumps().empty());
ASSERT_TRUE(pmd1->allocator_dumps_edges().empty());
ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad1"));
ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2"));
ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1));
ASSERT_EQ(nullptr, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2));
// Check that calling serialization routines doesn't cause a crash.
auto traced_value = std::make_unique<TracedValue>();
pmd1->SerializeAllocatorDumpsInto(traced_value.get());
// Check that the pmd can be reused and behaves as expected.
auto* mad1 = pmd1->CreateAllocatorDump("mad1");
auto* mad3 = pmd1->CreateAllocatorDump("mad3");
auto* shared_mad1 = pmd1->CreateSharedGlobalAllocatorDump(shared_mad_guid1);
auto* shared_mad2 =
pmd1->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid2);
ASSERT_EQ(4u, pmd1->allocator_dumps().size());
ASSERT_EQ(mad1, pmd1->GetAllocatorDump("mad1"));
ASSERT_EQ(nullptr, pmd1->GetAllocatorDump("mad2"));
ASSERT_EQ(mad3, pmd1->GetAllocatorDump("mad3"));
ASSERT_EQ(shared_mad1, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid1));
ASSERT_EQ(MemoryAllocatorDump::Flags::kDefault, shared_mad1->flags());
ASSERT_EQ(shared_mad2, pmd1->GetSharedGlobalAllocatorDump(shared_mad_guid2));
ASSERT_EQ(MemoryAllocatorDump::Flags::kWeak, shared_mad2->flags());
traced_value = std::make_unique<TracedValue>();
pmd1->SerializeAllocatorDumpsInto(traced_value.get());
pmd1.reset();
}
TEST(ProcessMemoryDumpTest, OverrideOwnershipEdge) {
std::unique_ptr<ProcessMemoryDump> pmd(
new ProcessMemoryDump(kDetailedDumpArgs));
auto* shm_dump1 = pmd->CreateAllocatorDump("shared_mem/seg1");
auto* shm_dump2 = pmd->CreateAllocatorDump("shared_mem/seg2");
auto* shm_dump3 = pmd->CreateAllocatorDump("shared_mem/seg3");
auto* shm_dump4 = pmd->CreateAllocatorDump("shared_mem/seg4");
// Create one allocation with an auto-assigned guid and mark it as a
// suballocation of "fakealloc/allocated_objects".
auto* child1_dump = pmd->CreateAllocatorDump("shared_mem/child/seg1");
pmd->AddOverridableOwnershipEdge(child1_dump->guid(), shm_dump1->guid(),
0 /* importance */);
auto* child2_dump = pmd->CreateAllocatorDump("shared_mem/child/seg2");
pmd->AddOwnershipEdge(child2_dump->guid(), shm_dump2->guid(),
3 /* importance */);
MemoryAllocatorDumpGuid shared_mad_guid(1);
pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid);
pmd->AddOverridableOwnershipEdge(shm_dump3->guid(), shared_mad_guid,
0 /* importance */);
auto* child4_dump = pmd->CreateAllocatorDump("shared_mem/child/seg4");
pmd->AddOverridableOwnershipEdge(child4_dump->guid(), shm_dump4->guid(),
4 /* importance */);
const ProcessMemoryDump::AllocatorDumpEdgesMap& edges =
pmd->allocator_dumps_edges();
EXPECT_EQ(4u, edges.size());
EXPECT_EQ(shm_dump1->guid(), edges.find(child1_dump->guid())->second.target);
EXPECT_EQ(0, edges.find(child1_dump->guid())->second.importance);
EXPECT_TRUE(edges.find(child1_dump->guid())->second.overridable);
EXPECT_EQ(shm_dump2->guid(), edges.find(child2_dump->guid())->second.target);
EXPECT_EQ(3, edges.find(child2_dump->guid())->second.importance);
EXPECT_FALSE(edges.find(child2_dump->guid())->second.overridable);
EXPECT_EQ(shared_mad_guid, edges.find(shm_dump3->guid())->second.target);
EXPECT_EQ(0, edges.find(shm_dump3->guid())->second.importance);
EXPECT_TRUE(edges.find(shm_dump3->guid())->second.overridable);
EXPECT_EQ(shm_dump4->guid(), edges.find(child4_dump->guid())->second.target);
EXPECT_EQ(4, edges.find(child4_dump->guid())->second.importance);
EXPECT_TRUE(edges.find(child4_dump->guid())->second.overridable);
// These should override old edges:
pmd->AddOwnershipEdge(child1_dump->guid(), shm_dump1->guid(),
1 /* importance */);
pmd->AddOwnershipEdge(shm_dump3->guid(), shared_mad_guid, 2 /* importance */);
// This should not change the old edges.
pmd->AddOverridableOwnershipEdge(child2_dump->guid(), shm_dump2->guid(),
0 /* importance */);
pmd->AddOwnershipEdge(child4_dump->guid(), shm_dump4->guid(),
0 /* importance */);
EXPECT_EQ(4u, edges.size());
EXPECT_EQ(shm_dump1->guid(), edges.find(child1_dump->guid())->second.target);
EXPECT_EQ(1, edges.find(child1_dump->guid())->second.importance);
EXPECT_FALSE(edges.find(child1_dump->guid())->second.overridable);
EXPECT_EQ(shm_dump2->guid(), edges.find(child2_dump->guid())->second.target);
EXPECT_EQ(3, edges.find(child2_dump->guid())->second.importance);
EXPECT_FALSE(edges.find(child2_dump->guid())->second.overridable);
EXPECT_EQ(shared_mad_guid, edges.find(shm_dump3->guid())->second.target);
EXPECT_EQ(2, edges.find(shm_dump3->guid())->second.importance);
EXPECT_FALSE(edges.find(shm_dump3->guid())->second.overridable);
EXPECT_EQ(shm_dump4->guid(), edges.find(child4_dump->guid())->second.target);
EXPECT_EQ(4, edges.find(child4_dump->guid())->second.importance);
EXPECT_FALSE(edges.find(child4_dump->guid())->second.overridable);
}
TEST(ProcessMemoryDumpTest, Suballocations) {
std::unique_ptr<ProcessMemoryDump> pmd(
new ProcessMemoryDump(kDetailedDumpArgs));
const std::string allocator_dump_name = "fakealloc/allocated_objects";
pmd->CreateAllocatorDump(allocator_dump_name);
// Create one allocation with an auto-assigned guid and mark it as a
// suballocation of "fakealloc/allocated_objects".
auto* pic1_dump = pmd->CreateAllocatorDump("picturemanager/picture1");
pmd->AddSuballocation(pic1_dump->guid(), allocator_dump_name);
// Same here, but this time create an allocation with an explicit guid.
auto* pic2_dump = pmd->CreateAllocatorDump("picturemanager/picture2",
MemoryAllocatorDumpGuid(0x42));
pmd->AddSuballocation(pic2_dump->guid(), allocator_dump_name);
// Now check that AddSuballocation() has created anonymous child dumps under
// "fakealloc/allocated_objects".
auto anon_node_1_it = pmd->allocator_dumps().find(
allocator_dump_name + "/__" + pic1_dump->guid().ToString());
ASSERT_NE(pmd->allocator_dumps().end(), anon_node_1_it);
auto anon_node_2_it =
pmd->allocator_dumps().find(allocator_dump_name + "/__42");
ASSERT_NE(pmd->allocator_dumps().end(), anon_node_2_it);
// Finally check that AddSuballocation() has created also the
// edges between the pictures and the anonymous allocator child dumps.
std::array<bool, 2> found_edge = {false, false};
for (const auto& e : pmd->allocator_dumps_edges()) {
found_edge[0] |= (e.first == pic1_dump->guid() &&
e.second.target == anon_node_1_it->second->guid());
found_edge[1] |= (e.first == pic2_dump->guid() &&
e.second.target == anon_node_2_it->second->guid());
}
ASSERT_TRUE(found_edge[0]);
ASSERT_TRUE(found_edge[1]);
// Check that calling serialization routines doesn't cause a crash.
std::unique_ptr<TracedValue> traced_value(new TracedValue);
pmd->SerializeAllocatorDumpsInto(traced_value.get());
pmd.reset();
}
TEST(ProcessMemoryDumpTest, GlobalAllocatorDumpTest) {
std::unique_ptr<ProcessMemoryDump> pmd(
new ProcessMemoryDump(kDetailedDumpArgs));
MemoryAllocatorDumpGuid shared_mad_guid(1);
auto* shared_mad1 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad_guid, shared_mad1->guid());
ASSERT_EQ(MemoryAllocatorDump::Flags::kWeak, shared_mad1->flags());
auto* shared_mad2 = pmd->GetSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad1, shared_mad2);
ASSERT_EQ(MemoryAllocatorDump::Flags::kWeak, shared_mad1->flags());
auto* shared_mad3 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad1, shared_mad3);
ASSERT_EQ(MemoryAllocatorDump::Flags::kWeak, shared_mad1->flags());
auto* shared_mad4 = pmd->CreateSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad1, shared_mad4);
ASSERT_EQ(MemoryAllocatorDump::Flags::kDefault, shared_mad1->flags());
auto* shared_mad5 = pmd->CreateWeakSharedGlobalAllocatorDump(shared_mad_guid);
ASSERT_EQ(shared_mad1, shared_mad5);
ASSERT_EQ(MemoryAllocatorDump::Flags::kDefault, shared_mad1->flags());
}
TEST(ProcessMemoryDumpTest, SharedMemoryOwnershipTest) {
std::unique_ptr<ProcessMemoryDump> pmd(
new ProcessMemoryDump(kDetailedDumpArgs));
const ProcessMemoryDump::AllocatorDumpEdgesMap& edges =
pmd->allocator_dumps_edges();
auto* client_dump2 = pmd->CreateAllocatorDump("discardable/segment2");
auto shm_token2 = UnguessableToken::Create();
MemoryAllocatorDumpGuid shm_local_guid2 =
pmd->GetDumpId(SharedMemoryTracker::GetDumpNameForTracing(shm_token2));
MemoryAllocatorDumpGuid shm_global_guid2 =
SharedMemoryTracker::GetGlobalDumpIdForTracing(shm_token2);
pmd->AddOverridableOwnershipEdge(shm_local_guid2, shm_global_guid2,
0 /* importance */);
pmd->CreateSharedMemoryOwnershipEdge(client_dump2->guid(), shm_token2,
1 /* importance */);
EXPECT_EQ(2u, edges.size());
EXPECT_EQ(shm_global_guid2, edges.find(shm_local_guid2)->second.target);
EXPECT_EQ(1, edges.find(shm_local_guid2)->second.importance);
EXPECT_FALSE(edges.find(shm_local_guid2)->second.overridable);
EXPECT_EQ(shm_local_guid2, edges.find(client_dump2->guid())->second.target);
EXPECT_EQ(1, edges.find(client_dump2->guid())->second.importance);
EXPECT_FALSE(edges.find(client_dump2->guid())->second.overridable);
}
TEST(ProcessMemoryDumpTest, BackgroundModeTest) {
MemoryDumpArgs background_args = {MemoryDumpLevelOfDetail::kBackground};
std::unique_ptr<ProcessMemoryDump> pmd(
new ProcessMemoryDump(background_args));
ProcessMemoryDump::is_black_hole_non_fatal_for_testing_ = true;
SetAllocatorDumpNameAllowlistForTesting(kTestDumpNameAllowlist);
MemoryAllocatorDump* black_hole_mad = pmd->GetBlackHoleMad(std::string());
// GetAllocatorDump works for uncreated dumps.
EXPECT_EQ(nullptr, pmd->GetAllocatorDump("NotAllowlisted/TestName"));
EXPECT_EQ(nullptr, pmd->GetAllocatorDump("Allowlisted/TestName"));
// Invalid dump names.
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("NotAllowlisted/TestName"));
EXPECT_EQ(black_hole_mad, pmd->CreateAllocatorDump("TestName"));
EXPECT_EQ(black_hole_mad, pmd->CreateAllocatorDump("Allowlisted/Test"));
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("Not/Allowlisted/TestName"));
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("Allowlisted/TestName/Google"));
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("Allowlisted/TestName/0x1a2Google"));
EXPECT_EQ(black_hole_mad,
pmd->CreateAllocatorDump("Allowlisted/TestName/__12/Google"));
// Suballocations.
MemoryAllocatorDumpGuid guid(1);
pmd->AddSuballocation(guid, "malloc/allocated_objects");
EXPECT_EQ(0u, pmd->allocator_dumps_edges_.size());
EXPECT_EQ(0u, pmd->allocator_dumps_.size());
// Global dumps.
EXPECT_NE(black_hole_mad, pmd->CreateSharedGlobalAllocatorDump(guid));
EXPECT_NE(black_hole_mad, pmd->CreateWeakSharedGlobalAllocatorDump(guid));
EXPECT_NE(black_hole_mad, pmd->GetSharedGlobalAllocatorDump(guid));
// Valid dump names.
EXPECT_NE(black_hole_mad, pmd->CreateAllocatorDump("Allowlisted/TestName"));
EXPECT_NE(black_hole_mad,
pmd->CreateAllocatorDump("Allowlisted/TestName_0xA1b2"));
EXPECT_NE(black_hole_mad,
pmd->CreateAllocatorDump("Allowlisted/0xaB/TestName"));
// GetAllocatorDump is consistent.
EXPECT_EQ(nullptr, pmd->GetAllocatorDump("NotAllowlisted/TestName"));
EXPECT_NE(black_hole_mad, pmd->GetAllocatorDump("Allowlisted/TestName"));
// Test allowed entries.
ASSERT_TRUE(IsMemoryAllocatorDumpNameInAllowlist("Allowlisted/TestName"));
// Global dumps should be allowed.
ASSERT_TRUE(IsMemoryAllocatorDumpNameInAllowlist("global/13456"));
// Global dumps with non-guids should not be.
ASSERT_FALSE(IsMemoryAllocatorDumpNameInAllowlist("global/random"));
// Random names should not.
ASSERT_FALSE(IsMemoryAllocatorDumpNameInAllowlist("NotAllowlisted/TestName"));
// Check hex processing.
ASSERT_TRUE(IsMemoryAllocatorDumpNameInAllowlist("Allowlisted/0xA1b2"));
}
TEST(ProcessMemoryDumpTest, GuidsTest) {
MemoryDumpArgs dump_args = {MemoryDumpLevelOfDetail::kDetailed};
const auto process_token_one = UnguessableToken::Create();
const auto process_token_two = UnguessableToken::Create();
ProcessMemoryDump pmd1(dump_args);
pmd1.set_process_token_for_testing(process_token_one);
MemoryAllocatorDump* mad1 = pmd1.CreateAllocatorDump("foo");
ProcessMemoryDump pmd2(dump_args);
pmd2.set_process_token_for_testing(process_token_one);
MemoryAllocatorDump* mad2 = pmd2.CreateAllocatorDump("foo");
// If we don't pass the argument we get a random PMD:
ProcessMemoryDump pmd3(dump_args);
MemoryAllocatorDump* mad3 = pmd3.CreateAllocatorDump("foo");
// PMD's for different processes produce different GUIDs even for the same
// names:
ProcessMemoryDump pmd4(dump_args);
pmd4.set_process_token_for_testing(process_token_two);
MemoryAllocatorDump* mad4 = pmd4.CreateAllocatorDump("foo");
ASSERT_EQ(mad1->guid(), mad2->guid());
ASSERT_NE(mad2->guid(), mad3->guid());
ASSERT_NE(mad3->guid(), mad4->guid());
ASSERT_NE(mad4->guid(), mad2->guid());
ASSERT_EQ(mad1->guid(), pmd1.GetDumpId("foo"));
}
#if defined(COUNT_RESIDENT_BYTES_SUPPORTED)
#if BUILDFLAG(IS_FUCHSIA)
// TODO(crbug.com/42050620): Counting resident bytes is not supported on
// Fuchsia.
#define MAYBE_CountResidentBytes DISABLED_CountResidentBytes
#else
#define MAYBE_CountResidentBytes CountResidentBytes
#endif
TEST(ProcessMemoryDumpTest, MAYBE_CountResidentBytes) {
const size_t page_size = ProcessMemoryDump::GetSystemPageSize();
// Allocate few page of dirty memory and check if it is resident.
const size_t size1 = 5 * page_size;
void* memory1 = Map(size1);
memset(memory1, 0, size1);
std::optional<size_t> res1 =
ProcessMemoryDump::CountResidentBytes(memory1, size1);
ASSERT_TRUE(res1.has_value());
ASSERT_EQ(res1.value(), size1);
Unmap(memory1, size1);
// Allocate a large memory segment (> 8Mib).
const size_t kVeryLargeMemorySize = 15 * 1024 * 1024;
void* memory2 = Map(kVeryLargeMemorySize);
memset(memory2, 0, kVeryLargeMemorySize);
std::optional<size_t> res2 =
ProcessMemoryDump::CountResidentBytes(memory2, kVeryLargeMemorySize);
ASSERT_TRUE(res2.has_value());
ASSERT_EQ(res2.value(), kVeryLargeMemorySize);
Unmap(memory2, kVeryLargeMemorySize);
}
#if BUILDFLAG(IS_FUCHSIA)
// TODO(crbug.com/42050620): Counting resident bytes is not supported on
// Fuchsia.
#define MAYBE_CountResidentBytesInSharedMemory \
DISABLED_CountResidentBytesInSharedMemory
#else
#define MAYBE_CountResidentBytesInSharedMemory CountResidentBytesInSharedMemory
#endif
TEST(ProcessMemoryDumpTest, MAYBE_CountResidentBytesInSharedMemory) {
const size_t page_size = ProcessMemoryDump::GetSystemPageSize();
// Allocate few page of dirty memory and check if it is resident.
{
const size_t kDirtyMemorySize = 5 * page_size;
auto region = base::WritableSharedMemoryRegion::Create(kDirtyMemorySize);
base::WritableSharedMemoryMapping mapping = region.Map();
base::span<uint8_t> mapping_mem(mapping);
std::ranges::fill(mapping_mem, 0u);
std::optional<size_t> res1 = CountResidentBytesInSharedMemory(mapping);
ASSERT_TRUE(res1.has_value());
ASSERT_EQ(res1.value(), kDirtyMemorySize);
}
// Allocate a shared memory segment but map at a non-page-aligned offset.
{
const size_t kDirtyMemorySize = 5 * page_size;
auto region =
base::WritableSharedMemoryRegion::Create(kDirtyMemorySize + page_size);
base::WritableSharedMemoryMapping mapping =
region.MapAt(page_size / 2, kDirtyMemorySize);
base::span<uint8_t> mapping_mem(mapping);
std::ranges::fill(mapping_mem, 0u);
std::optional<size_t> res1 = CountResidentBytesInSharedMemory(mapping);
ASSERT_TRUE(res1.has_value());
ASSERT_EQ(res1.value(), kDirtyMemorySize + page_size);
}
// Allocate a large memory segment (> 8Mib).
{
const size_t kVeryLargeMemorySize = 15 * 1024 * 1024;
auto region =
base::WritableSharedMemoryRegion::Create(kVeryLargeMemorySize);
base::WritableSharedMemoryMapping mapping = region.Map();
base::span<uint8_t> mapping_mem(mapping);
std::ranges::fill(mapping_mem, 0u);
std::optional<size_t> res2 = CountResidentBytesInSharedMemory(mapping);
ASSERT_TRUE(res2.has_value());
ASSERT_EQ(res2.value(), kVeryLargeMemorySize);
}
// Allocate a large memory segment, but touch about half of all pages.
{
const size_t kTouchedMemorySize = 7 * 1024 * 1024;
auto region = base::WritableSharedMemoryRegion::Create(kTouchedMemorySize);
base::WritableSharedMemoryMapping mapping = region.Map();
base::span<uint8_t> mapping_mem(mapping);
std::ranges::fill(mapping_mem, 0u);
std::optional<size_t> res3 = CountResidentBytesInSharedMemory(mapping);
ASSERT_TRUE(res3.has_value());
ASSERT_EQ(res3.value(), kTouchedMemorySize);
}
}
#endif // defined(COUNT_RESIDENT_BYTES_SUPPORTED)
} // namespace base::trace_event
|