1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "partition_alloc/pointers/instance_tracer.h"
6
7 #include <atomic>
8 #include <map>
9 #include <mutex>
10 #include <vector>
11
12 #include "partition_alloc/partition_alloc_base/check.h"
13 #include "partition_alloc/partition_alloc_base/debug/stack_trace.h"
14 #include "partition_alloc/partition_alloc_base/no_destructor.h"
15 #include "partition_alloc/partition_root.h"
16
17 namespace base::internal {
18
19 #if BUILDFLAG(ENABLE_BACKUP_REF_PTR_INSTANCE_TRACER)
20
21 static_assert(BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT),
22 "Instance tracing requires BackupRefPtr support.");
23
24 namespace {
25
26 struct Info {
Infobase::internal::__anon8617e69a0111::Info27 explicit Info(uintptr_t slot_count, bool may_dangle)
28 : slot_count(slot_count), may_dangle(may_dangle) {
29 partition_alloc::internal::base::debug::CollectStackTrace(
30 stack_trace.data(), stack_trace.size());
31 }
32
33 uintptr_t slot_count;
34 bool may_dangle;
35 std::array<const void*, 32> stack_trace = {};
36 };
37
GetStorage()38 auto& GetStorage() {
39 static partition_alloc::internal::base::NoDestructor<std::map<uint64_t, Info>>
40 storage;
41 return *storage;
42 }
43
GetStorageMutex()44 auto& GetStorageMutex() {
45 static partition_alloc::internal::base::NoDestructor<std::mutex>
46 storage_mutex;
47 return *storage_mutex;
48 }
49
50 } // namespace
51
52 std::atomic<uint64_t> InstanceTracer::counter_ = 0;
53
TraceImpl(uint64_t owner_id,bool may_dangle,uintptr_t address)54 void InstanceTracer::TraceImpl(uint64_t owner_id,
55 bool may_dangle,
56 uintptr_t address) {
57 PA_CHECK(owner_id);
58 const auto slot_and_size =
59 partition_alloc::PartitionAllocGetSlotStartAndSizeInBRPPool(address);
60 const uintptr_t slot_count = reinterpret_cast<uintptr_t>(
61 partition_alloc::PartitionRoot::InSlotMetadataPointerFromSlotStartAndSize(
62 slot_and_size.slot_start, slot_and_size.size));
63
64 const std::lock_guard guard(GetStorageMutex());
65 GetStorage().insert({owner_id, Info(slot_count, may_dangle)});
66 }
67
UntraceImpl(uint64_t owner_id)68 void InstanceTracer::UntraceImpl(uint64_t owner_id) {
69 PA_CHECK(owner_id);
70 const std::lock_guard guard(GetStorageMutex());
71 GetStorage().erase(owner_id);
72 }
73
74 std::vector<std::array<const void*, 32>>
GetStackTracesForDanglingRefs(uintptr_t allocation)75 InstanceTracer::GetStackTracesForDanglingRefs(uintptr_t allocation) {
76 std::vector<std::array<const void*, 32>> result;
77 const std::lock_guard guard(GetStorageMutex());
78 for (const auto& [id, info] : GetStorage()) {
79 if (info.slot_count == allocation && !info.may_dangle) {
80 result.push_back(info.stack_trace);
81 }
82 }
83 return result;
84 }
85
86 std::vector<std::array<const void*, 32>>
GetStackTracesForAddressForTest(const void * address)87 InstanceTracer::GetStackTracesForAddressForTest(const void* address) {
88 const auto slot_and_size =
89 partition_alloc::PartitionAllocGetSlotStartAndSizeInBRPPool(
90 reinterpret_cast<uintptr_t>(address));
91 const uintptr_t slot_count = reinterpret_cast<uintptr_t>(
92 partition_alloc::PartitionRoot::InSlotMetadataPointerFromSlotStartAndSize(
93 slot_and_size.slot_start, slot_and_size.size));
94 return GetStackTracesForDanglingRefs(slot_count);
95 }
96
97 #endif
98
99 } // namespace base::internal
100