forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PA: Move NonScannableAllocatorImpl into partition_allocator/
Moves base::internal::NonScannableAllocatorImpl to allocator_shim::internal::NonScannableAllocatorImpl, and provides allocator_shim::NonScannableAllocator and allocator_shim::NonQuarantinableAllocator as new public APIs. PS9 contains pure copies as: base/memory/nonscannable_memory.* -> b/a/p/shim/nonscannable_allocator.* (file names changed in order to avoid a conflict.) Bug: 1337681 Change-Id: I39c859855404737f7412c62cb7e6fc11d8da9c5f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4578558 Reviewed-by: Daniel Cheng <dcheng@chromium.org> Reviewed-by: Mikhail Khokhlov <khokhlov@google.com> Commit-Queue: Yuki Shiino <yukishiino@chromium.org> Reviewed-by: Bartek Nowierski <bartekn@chromium.org> Reviewed-by: Anton Bikineev <bikineev@chromium.org> Cr-Commit-Position: refs/heads/main@{#1153661}
- Loading branch information
Showing
10 changed files
with
193 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
base/allocator/partition_allocator/shim/nonscannable_allocator.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2021 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "base/allocator/partition_allocator/shim/nonscannable_allocator.h" | ||
|
||
#include "base/allocator/partition_allocator/partition_root.h" | ||
|
||
#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) | ||
#include "base/allocator/partition_alloc_features.h" | ||
#include "base/allocator/partition_allocator/shim/allocator_shim_default_dispatch_to_partition_alloc.h" | ||
|
||
#if BUILDFLAG(USE_STARSCAN) | ||
#include "base/allocator/partition_allocator/starscan/metadata_allocator.h" | ||
#include "base/allocator/partition_allocator/starscan/pcscan.h" | ||
#endif | ||
#endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) | ||
|
||
namespace allocator_shim::internal { | ||
|
||
#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) | ||
template <bool quarantinable> | ||
NonScannableAllocatorImpl<quarantinable>::NonScannableAllocatorImpl() = default; | ||
template <bool quarantinable> | ||
NonScannableAllocatorImpl<quarantinable>::~NonScannableAllocatorImpl() = | ||
default; | ||
|
||
template <bool quarantinable> | ||
NonScannableAllocatorImpl<quarantinable>& | ||
NonScannableAllocatorImpl<quarantinable>::Instance() { | ||
static partition_alloc::internal::base::NoDestructor< | ||
NonScannableAllocatorImpl> | ||
instance; | ||
return *instance; | ||
} | ||
|
||
template <bool quarantinable> | ||
void* NonScannableAllocatorImpl<quarantinable>::Alloc(size_t size) { | ||
#if BUILDFLAG(USE_STARSCAN) | ||
// TODO(bikineev): Change to LIKELY once PCScan is enabled by default. | ||
if (PA_UNLIKELY(pcscan_enabled_.load(std::memory_order_acquire))) { | ||
PA_DCHECK(allocator_.get()); | ||
return allocator_->root()->AllocWithFlagsNoHooks( | ||
0, size, partition_alloc::PartitionPageSize()); | ||
} | ||
#endif // BUILDFLAG(USE_STARSCAN) | ||
// Otherwise, dispatch to default partition. | ||
return allocator_shim::internal::PartitionAllocMalloc::Allocator() | ||
->AllocWithFlagsNoHooks(0, size, partition_alloc::PartitionPageSize()); | ||
} | ||
|
||
template <bool quarantinable> | ||
void NonScannableAllocatorImpl<quarantinable>::Free(void* ptr) { | ||
partition_alloc::ThreadSafePartitionRoot::FreeNoHooks(ptr); | ||
} | ||
|
||
template <bool quarantinable> | ||
void NonScannableAllocatorImpl<quarantinable>::NotifyPCScanEnabled() { | ||
#if BUILDFLAG(USE_STARSCAN) | ||
allocator_.reset(partition_alloc::internal::MakePCScanMetadata< | ||
partition_alloc::PartitionAllocator>()); | ||
allocator_->init(partition_alloc::PartitionOptions{ | ||
.quarantine = | ||
quarantinable | ||
? partition_alloc::PartitionOptions::Quarantine::kAllowed | ||
: partition_alloc::PartitionOptions::Quarantine::kDisallowed, | ||
.cookie = partition_alloc::PartitionOptions::Cookie::kAllowed, | ||
.backup_ref_ptr = | ||
partition_alloc::PartitionOptions::BackupRefPtr::kDisabled, | ||
}); | ||
if constexpr (quarantinable) { | ||
partition_alloc::internal::PCScan::RegisterNonScannableRoot( | ||
allocator_->root()); | ||
} | ||
pcscan_enabled_.store(true, std::memory_order_release); | ||
#endif // BUILDFLAG(USE_STARSCAN) | ||
} | ||
|
||
template class NonScannableAllocatorImpl<true>; | ||
template class NonScannableAllocatorImpl<false>; | ||
|
||
#endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) | ||
|
||
} // namespace allocator_shim::internal |
87 changes: 87 additions & 0 deletions
87
base/allocator/partition_allocator/shim/nonscannable_allocator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2021 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_NONSCANNABLE_ALLOCATOR_H_ | ||
#define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_NONSCANNABLE_ALLOCATOR_H_ | ||
|
||
#include <atomic> | ||
#include <cstddef> | ||
#include <memory> | ||
|
||
#include "base/allocator/partition_allocator/partition_alloc_base/component_export.h" | ||
#include "base/allocator/partition_allocator/partition_alloc_base/no_destructor.h" | ||
#include "base/allocator/partition_allocator/partition_alloc_buildflags.h" | ||
|
||
#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) | ||
#include "base/allocator/partition_allocator/partition_alloc.h" | ||
|
||
#if BUILDFLAG(USE_STARSCAN) | ||
#include "base/allocator/partition_allocator/starscan/metadata_allocator.h" | ||
#endif | ||
#endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) | ||
|
||
namespace allocator_shim { | ||
|
||
#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) | ||
namespace internal { | ||
|
||
// Represents allocator that contains memory for data-like objects (that don't | ||
// contain pointers/references) and therefore doesn't require scanning by | ||
// PCScan. An example would be strings or socket/IPC/file buffers. Use with | ||
// caution. | ||
template <bool quarantinable> | ||
class PA_COMPONENT_EXPORT(PARTITION_ALLOC) NonScannableAllocatorImpl final { | ||
public: | ||
static NonScannableAllocatorImpl& Instance(); | ||
|
||
NonScannableAllocatorImpl(const NonScannableAllocatorImpl&) = delete; | ||
NonScannableAllocatorImpl& operator=(const NonScannableAllocatorImpl&) = | ||
delete; | ||
|
||
void* Alloc(size_t size); | ||
static void Free(void*); | ||
|
||
// Returns PartitionRoot corresponding to the allocator, or nullptr if the | ||
// allocator is not enabled. | ||
partition_alloc::ThreadSafePartitionRoot* root() { | ||
#if BUILDFLAG(USE_STARSCAN) | ||
if (!allocator_.get()) { | ||
return nullptr; | ||
} | ||
return allocator_->root(); | ||
#else | ||
return nullptr; | ||
#endif // BUILDFLAG(USE_STARSCAN) | ||
} | ||
|
||
void NotifyPCScanEnabled(); | ||
|
||
private: | ||
template <typename> | ||
friend class partition_alloc::internal::base::NoDestructor; | ||
|
||
NonScannableAllocatorImpl(); | ||
~NonScannableAllocatorImpl(); | ||
|
||
#if BUILDFLAG(USE_STARSCAN) | ||
std::unique_ptr<partition_alloc::PartitionAllocator, | ||
partition_alloc::internal::PCScanMetadataDeleter> | ||
allocator_; | ||
std::atomic_bool pcscan_enabled_{false}; | ||
#endif // BUILDFLAG(USE_STARSCAN) | ||
}; | ||
|
||
extern template class NonScannableAllocatorImpl<true>; | ||
extern template class NonScannableAllocatorImpl<false>; | ||
|
||
} // namespace internal | ||
|
||
using NonScannableAllocator = internal::NonScannableAllocatorImpl<true>; | ||
using NonQuarantinableAllocator = internal::NonScannableAllocatorImpl<false>; | ||
|
||
#endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) | ||
|
||
} // namespace allocator_shim | ||
|
||
#endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_NONSCANNABLE_ALLOCATOR_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.