• 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_ALLOCATOR_SRC_PARTITION_ALLOC_SHIM_NONSCANNABLE_ALLOCATOR_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_SHIM_NONSCANNABLE_ALLOCATOR_H_
7 
8 #include <atomic>
9 #include <cstddef>
10 #include <memory>
11 
12 #include "partition_alloc/partition_alloc_base/component_export.h"
13 #include "partition_alloc/partition_alloc_base/no_destructor.h"
14 #include "partition_alloc/partition_alloc_buildflags.h"
15 
16 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
17 #include "partition_alloc/partition_alloc.h"
18 
19 #if BUILDFLAG(USE_STARSCAN)
20 #include "partition_alloc/starscan/metadata_allocator.h"
21 #endif
22 #endif  // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
23 
24 namespace allocator_shim {
25 
26 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
27 namespace internal {
28 
29 // Represents allocator that contains memory for data-like objects (that don't
30 // contain pointers/references) and therefore doesn't require scanning by
31 // PCScan. An example would be strings or socket/IPC/file buffers. Use with
32 // caution.
33 template <bool quarantinable>
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)34 class PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) NonScannableAllocatorImpl final {
35  public:
36   static NonScannableAllocatorImpl& Instance();
37 
38   NonScannableAllocatorImpl(const NonScannableAllocatorImpl&) = delete;
39   NonScannableAllocatorImpl& operator=(const NonScannableAllocatorImpl&) =
40       delete;
41 
42   void* Alloc(size_t size);
43   void Free(void*);
44 
45   // Returns PartitionRoot corresponding to the allocator, or nullptr if the
46   // allocator is not enabled.
47   partition_alloc::PartitionRoot* root() {
48 #if BUILDFLAG(USE_STARSCAN)
49     if (!allocator_.get()) {
50       return nullptr;
51     }
52     return allocator_->root();
53 #else
54     return nullptr;
55 #endif  // BUILDFLAG(USE_STARSCAN)
56   }
57 
58   void NotifyPCScanEnabled();
59 
60  private:
61   template <typename>
62   friend class partition_alloc::internal::base::NoDestructor;
63 
64   NonScannableAllocatorImpl();
65   ~NonScannableAllocatorImpl();
66 
67 #if BUILDFLAG(USE_STARSCAN)
68   std::unique_ptr<partition_alloc::PartitionAllocator,
69                   partition_alloc::internal::PCScanMetadataDeleter>
70       allocator_;
71   std::atomic_bool pcscan_enabled_{false};
72 #endif  // BUILDFLAG(USE_STARSCAN)
73 };
74 
75 extern template class NonScannableAllocatorImpl<true>;
76 extern template class NonScannableAllocatorImpl<false>;
77 
78 }  // namespace internal
79 
80 using NonScannableAllocator = internal::NonScannableAllocatorImpl<true>;
81 using NonQuarantinableAllocator = internal::NonScannableAllocatorImpl<false>;
82 
83 #endif  // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
84 
85 }  // namespace allocator_shim
86 
87 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_SHIM_NONSCANNABLE_ALLOCATOR_H_
88