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/base_export.h" 12 #include "base/memory/scoped_refptr.h" 13 #include "base/synchronization/lock.h" 14 #include "base/task/sequenced_task_runner.h" 15 #include "base/thread_annotations.h" 16 #include "partition_alloc/partition_alloc_buildflags.h" 17 #include "partition_alloc/partition_alloc_config.h" 18 #include "partition_alloc/thread_cache.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 process_affected_by_brp_flag = false; 49 }; 50 51 // Reconfigure* functions re-configure PartitionAlloc. It is impossible to 52 // configure PartitionAlloc before/at its initialization using information not 53 // known at compile-time (e.g. process type, Finch), because by the time this 54 // information is available memory allocations would have surely happened, 55 // that requiring a functioning allocator. 56 // 57 // *Earlyish() is called as early as it is reasonably possible. 58 // *AfterZygoteFork() is its complement to finish configuring process-specific 59 // stuff that had to be postponed due to *Earlyish() being called with 60 // |process_type==kZygoteProcess|. 61 // *AfterFeatureListInit() is called in addition to the above, once 62 // FeatureList has been initialized and ready to use. It is guaranteed to be 63 // called on non-zygote processes or after the zygote has been forked. 64 // *AfterTaskRunnerInit() is called once it is possible to post tasks, and 65 // after the previous steps. 66 // 67 // *Earlyish() must be called exactly once. *AfterZygoteFork() must be called 68 // once iff *Earlyish() was called before with |process_type==kZygoteProcess|. 69 // 70 // *AfterFeatureListInit() may be called more than once, but will perform its 71 // re-configuration steps exactly once. 72 // 73 // *AfterTaskRunnerInit() may be called more than once. 74 void ReconfigureForTests(); 75 void ReconfigureEarlyish(const std::string& process_type); 76 void ReconfigureAfterZygoteFork(const std::string& process_type); 77 void ReconfigureAfterFeatureListInit( 78 const std::string& process_type, 79 bool configure_dangling_pointer_detector = true); 80 void ReconfigureAfterTaskRunnerInit(const std::string& process_type); 81 82 // |has_main_frame| tells us if the renderer contains a main frame. 83 void OnForegrounded(bool has_main_frame); 84 void OnBackgrounded(); 85 86 #if BUILDFLAG(ENABLE_DANGLING_RAW_PTR_CHECKS) 87 static std::string ExtractDanglingPtrSignatureForTests( 88 std::string stacktrace); 89 #endif 90 91 static PartitionAllocSupport* Get(); 92 93 static BrpConfiguration GetBrpConfiguration(const std::string& process_type); 94 95 // Returns true if memory tagging should be enabled if available for the given 96 // process type. May be called multiple times per process. 97 static bool ShouldEnableMemoryTagging(const std::string& process_type); 98 99 // For calling from within third_party/blink/. 100 static bool ShouldEnableMemoryTaggingInRendererProcess(); 101 102 private: 103 PartitionAllocSupport(); 104 105 base::Lock lock_; 106 bool called_for_tests_ GUARDED_BY(lock_) = false; 107 bool called_earlyish_ GUARDED_BY(lock_) = false; 108 bool called_after_zygote_fork_ GUARDED_BY(lock_) = false; 109 bool called_after_feature_list_init_ GUARDED_BY(lock_) = false; 110 bool called_after_thread_pool_init_ GUARDED_BY(lock_) = false; 111 std::string established_process_type_ GUARDED_BY(lock_) = "INVALID"; 112 113 #if PA_CONFIG(THREAD_CACHE_SUPPORTED) && \ 114 BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) 115 size_t largest_cached_size_ = 116 ::partition_alloc::ThreadCacheLimits::kDefaultSizeThreshold; 117 #endif 118 }; 119 120 } // namespace base::allocator 121 122 #endif // BASE_ALLOCATOR_PARTITION_ALLOC_SUPPORT_H_ 123