• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 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_ALLOC_SUPPORT_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOC_SUPPORT_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_buildflags.h"
12 #include "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_config.h"
13 #include "base/allocator/partition_allocator/src/partition_alloc/thread_cache.h"
14 #include "base/base_export.h"
15 #include "base/memory/scoped_refptr.h"
16 #include "base/synchronization/lock.h"
17 #include "base/task/sequenced_task_runner.h"
18 #include "base/thread_annotations.h"
19 
20 namespace base::allocator {
21 
22 #if BUILDFLAG(USE_STARSCAN)
23 BASE_EXPORT void RegisterPCScanStatsReporter();
24 #endif
25 
26 // Starts a periodic timer on the current thread to purge all thread caches.
27 BASE_EXPORT void StartThreadCachePeriodicPurge();
28 
29 BASE_EXPORT void StartMemoryReclaimer(
30     scoped_refptr<SequencedTaskRunner> task_runner);
31 
32 BASE_EXPORT std::map<std::string, std::string> ProposeSyntheticFinchTrials();
33 
34 // Install handlers for when dangling raw_ptr(s) have been detected. This prints
35 // two StackTraces. One where the memory is freed, one where the last dangling
36 // raw_ptr stopped referencing it.
37 //
38 // This is currently effective, only when compiled with
39 // `enable_dangling_raw_ptr_checks` build flag.
40 BASE_EXPORT void InstallDanglingRawPtrChecks();
41 BASE_EXPORT void InstallUnretainedDanglingRawPtrChecks();
42 
43 // Allows to re-configure PartitionAlloc at run-time.
44 class BASE_EXPORT PartitionAllocSupport {
45  public:
46   struct BrpConfiguration {
47     bool enable_brp = false;
48     bool enable_brp_for_ash = false;
49     bool split_main_partition = false;
50     bool use_dedicated_aligned_partition = false;
51     bool process_affected_by_brp_flag = false;
52     size_t ref_count_size = 0;
53   };
54 
55   // Reconfigure* functions re-configure PartitionAlloc. It is impossible to
56   // configure PartitionAlloc before/at its initialization using information not
57   // known at compile-time (e.g. process type, Finch), because by the time this
58   // information is available memory allocations would have surely happened,
59   // that requiring a functioning allocator.
60   //
61   // *Earlyish() is called as early as it is reasonably possible.
62   // *AfterZygoteFork() is its complement to finish configuring process-specific
63   // stuff that had to be postponed due to *Earlyish() being called with
64   // |process_type==kZygoteProcess|.
65   // *AfterFeatureListInit() is called in addition to the above, once
66   // FeatureList has been initialized and ready to use. It is guaranteed to be
67   // called on non-zygote processes or after the zygote has been forked.
68   // *AfterTaskRunnerInit() is called once it is possible to post tasks, and
69   // after the previous steps.
70   //
71   // *Earlyish() must be called exactly once. *AfterZygoteFork() must be called
72   // once iff *Earlyish() was called before with |process_type==kZygoteProcess|.
73   //
74   // *AfterFeatureListInit() may be called more than once, but will perform its
75   // re-configuration steps exactly once.
76   //
77   // *AfterTaskRunnerInit() may be called more than once.
78   void ReconfigureForTests();
79   void ReconfigureEarlyish(const std::string& process_type);
80   void ReconfigureAfterZygoteFork(const std::string& process_type);
81   void ReconfigureAfterFeatureListInit(
82       const std::string& process_type,
83       bool configure_dangling_pointer_detector = true);
84   void ReconfigureAfterTaskRunnerInit(const std::string& process_type);
85 
86   // |has_main_frame| tells us if the renderer contains a main frame.
87   void OnForegrounded(bool has_main_frame);
88   void OnBackgrounded();
89 
90 #if BUILDFLAG(ENABLE_DANGLING_RAW_PTR_CHECKS)
91   static std::string ExtractDanglingPtrSignatureForTests(
92       std::string stacktrace);
93 #endif
94 
95   static PartitionAllocSupport* Get();
96 
97   static BrpConfiguration GetBrpConfiguration(const std::string& process_type);
98 
99   // Returns true if memory tagging should be enabled if available for the given
100   // process type. May be called multiple times per process.
101   static bool ShouldEnableMemoryTagging(const std::string& process_type);
102 
103   // For calling from within third_party/blink/.
104   static bool ShouldEnableMemoryTaggingInRendererProcess();
105 
106  private:
107   PartitionAllocSupport();
108 
109   base::Lock lock_;
110   bool called_for_tests_ GUARDED_BY(lock_) = false;
111   bool called_earlyish_ GUARDED_BY(lock_) = false;
112   bool called_after_zygote_fork_ GUARDED_BY(lock_) = false;
113   bool called_after_feature_list_init_ GUARDED_BY(lock_) = false;
114   bool called_after_thread_pool_init_ GUARDED_BY(lock_) = false;
115   std::string established_process_type_ GUARDED_BY(lock_) = "INVALID";
116 
117 #if PA_CONFIG(THREAD_CACHE_SUPPORTED) && \
118     BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
119   size_t largest_cached_size_ =
120       ::partition_alloc::ThreadCacheLimits::kDefaultSizeThreshold;
121 #endif
122 };
123 
124 }  // namespace base::allocator
125 
126 #endif  // BASE_ALLOCATOR_PARTITION_ALLOC_SUPPORT_H_
127