• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_DANGLING_RAW_PTR_CHECKS_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_DANGLING_RAW_PTR_CHECKS_H_
7 
8 #include <cstdint>
9 
10 #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
11 
12 // When compiled with build flags `enable_dangling_raw_ptr_checks`, dangling
13 // raw_ptr are reported. Its behavior can be configured here.
14 //
15 // Purpose of this level of indirection:
16 // - Ease testing.
17 // - Keep partition_alloc/ independent from base/. In most cases, when a
18 //   dangling raw_ptr is detected/released, this involves recording a
19 //   base::debug::StackTrace, which isn't desirable inside partition_alloc/.
20 // - Be able (potentially) to turn this feature on/off at runtime based on
21 //   dependant's flags.
22 namespace partition_alloc {
23 
24 // DanglingRawPtrDetected is called when there exists a `raw_ptr` referencing a
25 // memory region and the allocator is asked to release it.
26 //
27 // It won't be called again with the same `id`, up until (potentially) a call to
28 // DanglingRawPtrReleased(`id`) is made.
29 //
30 // This function is called from within the allocator, and is not allowed to
31 // allocate memory.
32 using DanglingRawPtrDetectedFn = void(uintptr_t /*id*/);
33 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
34 DanglingRawPtrDetectedFn* GetDanglingRawPtrDetectedFn();
35 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
36 void SetDanglingRawPtrDetectedFn(DanglingRawPtrDetectedFn);
37 
38 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
39 DanglingRawPtrDetectedFn* GetUnretainedDanglingRawPtrDetectedFn();
40 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
41 void SetUnretainedDanglingRawPtrDetectedFn(DanglingRawPtrDetectedFn*);
42 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
43 bool SetUnretainedDanglingRawPtrCheckEnabled(bool enabled);
44 
45 // DanglingRawPtrReleased: Called after DanglingRawPtrDetected(id), once the
46 // last dangling raw_ptr stops referencing the memory region.
47 //
48 // This function is allowed to allocate memory.
49 using DanglingRawPtrReleasedFn = void(uintptr_t /*id*/);
50 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
51 DanglingRawPtrReleasedFn* GetDanglingRawPtrReleasedFn();
52 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
53 void SetDanglingRawPtrReleasedFn(DanglingRawPtrReleasedFn);
54 
55 namespace internal {
56 
57 PA_COMPONENT_EXPORT(PARTITION_ALLOC) void DanglingRawPtrDetected(uintptr_t id);
58 PA_COMPONENT_EXPORT(PARTITION_ALLOC) void DanglingRawPtrReleased(uintptr_t id);
59 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
60 void UnretainedDanglingRawPtrDetected(uintptr_t id);
61 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
62 bool IsUnretainedDanglingRawPtrCheckEnabled();
63 
64 }  // namespace internal
65 }  // namespace partition_alloc
66 
67 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_DANGLING_RAW_PTR_CHECKS_H_
68