Commit 3f565126 authored by Vlad Tsyrklevich's avatar Vlad Tsyrklevich Committed by Commit Bot

GWP-ASan: Get rid of kFreePagesNumBits

This change is preparing for an upcoming refactor. kFreePagesNumBits
and kGpaMaxPages are equivalent. Get rid of the latter with a
static_assert() that kGpaMaxPages is always equal to the size of the
BitMap.

Bug: 896019
Change-Id: I7e99766d2e6fffa0e3d3eb3a47a2e76038b522ab
Reviewed-on: https://chromium-review.googlesource.com/c/1340526Reviewed-by: default avatarVitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608936}
parent fe2499e4
...@@ -20,13 +20,12 @@ namespace internal { ...@@ -20,13 +20,12 @@ namespace internal {
// TODO: Delete out-of-line constexpr defininitons once C++17 is in use. // TODO: Delete out-of-line constexpr defininitons once C++17 is in use.
constexpr size_t GuardedPageAllocator::kGpaMaxPages; constexpr size_t GuardedPageAllocator::kGpaMaxPages;
constexpr size_t GuardedPageAllocator::kGpaAllocAlignment; constexpr size_t GuardedPageAllocator::kGpaAllocAlignment;
constexpr size_t GuardedPageAllocator::kFreePagesNumBits;
GuardedPageAllocator::GuardedPageAllocator() {} GuardedPageAllocator::GuardedPageAllocator() {}
void GuardedPageAllocator::Init(size_t num_pages) { void GuardedPageAllocator::Init(size_t num_pages) {
CHECK_GT(num_pages, 0U); CHECK_GT(num_pages, 0U);
CHECK_LE(num_pages, kFreePagesNumBits); CHECK_LE(num_pages, kGpaMaxPages);
num_pages_ = num_pages; num_pages_ = num_pages;
page_size_ = base::GetPageSize(); page_size_ = base::GetPageSize();
...@@ -37,7 +36,7 @@ void GuardedPageAllocator::Init(size_t num_pages) { ...@@ -37,7 +36,7 @@ void GuardedPageAllocator::Init(size_t num_pages) {
// there should be no risk of a race here. // there should be no risk of a race here.
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
free_pages_ = free_pages_ =
(num_pages_ == kFreePagesNumBits) ? ~0ULL : (1ULL << num_pages_) - 1; (num_pages_ == kGpaMaxPages) ? ~0ULL : (1ULL << num_pages_) - 1;
} }
for (size_t i = 0; i < num_pages_; i++) for (size_t i = 0; i < num_pages_; i++)
...@@ -134,20 +133,19 @@ size_t GuardedPageAllocator::ReserveSlot() { ...@@ -134,20 +133,19 @@ size_t GuardedPageAllocator::ReserveSlot() {
if (double_free_detected_) if (double_free_detected_)
return SIZE_MAX; return SIZE_MAX;
uint64_t rot = base::RandGenerator(kFreePagesNumBits); uint64_t rot = base::RandGenerator(kGpaMaxPages);
BitMap rotated_bitmap = BitMap rotated_bitmap =
(free_pages_ << rot) | (free_pages_ >> (kFreePagesNumBits - rot)); (free_pages_ << rot) | (free_pages_ >> (kGpaMaxPages - rot));
int rotated_selection = CountTrailingZeroBits64(rotated_bitmap); int rotated_selection = CountTrailingZeroBits64(rotated_bitmap);
size_t selection = size_t selection = (rotated_selection - rot + kGpaMaxPages) % kGpaMaxPages;
(rotated_selection - rot + kFreePagesNumBits) % kFreePagesNumBits; DCHECK_LT(selection, kGpaMaxPages);
DCHECK_LT(selection, kFreePagesNumBits);
DCHECK(free_pages_ & (1ULL << selection)); DCHECK(free_pages_ & (1ULL << selection));
free_pages_ &= ~(1ULL << selection); free_pages_ &= ~(1ULL << selection);
return selection; return selection;
} }
void GuardedPageAllocator::FreeSlot(size_t slot) { void GuardedPageAllocator::FreeSlot(size_t slot) {
DCHECK_LT(slot, kFreePagesNumBits); DCHECK_LT(slot, kGpaMaxPages);
BitMap bit = 1ULL << slot; BitMap bit = 1ULL << slot;
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
...@@ -207,7 +205,7 @@ GuardedPageAllocator::ErrorType GuardedPageAllocator::GetErrorType( ...@@ -207,7 +205,7 @@ GuardedPageAllocator::ErrorType GuardedPageAllocator::GetErrorType(
} }
uintptr_t GuardedPageAllocator::SlotToAddr(size_t slot) const { uintptr_t GuardedPageAllocator::SlotToAddr(size_t slot) const {
DCHECK_LT(slot, kFreePagesNumBits); DCHECK_LT(slot, kGpaMaxPages);
return first_page_addr_ + 2 * slot * page_size_; return first_page_addr_ + 2 * slot * page_size_;
} }
...@@ -216,7 +214,7 @@ size_t GuardedPageAllocator::AddrToSlot(uintptr_t addr) const { ...@@ -216,7 +214,7 @@ size_t GuardedPageAllocator::AddrToSlot(uintptr_t addr) const {
uintptr_t offset = addr - first_page_addr_; uintptr_t offset = addr - first_page_addr_;
DCHECK_EQ((offset / page_size_) % 2, 0ULL); DCHECK_EQ((offset / page_size_) % 2, 0ULL);
size_t slot = offset / page_size_ / 2; size_t slot = offset / page_size_ / 2;
DCHECK_LT(slot, kFreePagesNumBits); DCHECK_LT(slot, kGpaMaxPages);
return slot; return slot;
} }
......
...@@ -72,6 +72,8 @@ class GuardedPageAllocator { ...@@ -72,6 +72,8 @@ class GuardedPageAllocator {
private: private:
using BitMap = uint64_t; using BitMap = uint64_t;
static_assert(kGpaMaxPages == sizeof(BitMap) * 8,
"Maximum number of pages is the size of free_pages_ bitmap");
// Structure for storing data about a slot. // Structure for storing data about a slot.
struct SlotMetadata { struct SlotMetadata {
...@@ -124,9 +126,6 @@ class GuardedPageAllocator { ...@@ -124,9 +126,6 @@ class GuardedPageAllocator {
void Reset(); void Reset();
}; };
// Number of bits in the free_pages_ bitmap.
static constexpr size_t kFreePagesNumBits = sizeof(BitMap) * 8;
// Does not allocate any memory for the allocator, to finish initializing call // Does not allocate any memory for the allocator, to finish initializing call
// Init(). // Init().
GuardedPageAllocator(); GuardedPageAllocator();
...@@ -179,7 +178,7 @@ class GuardedPageAllocator { ...@@ -179,7 +178,7 @@ class GuardedPageAllocator {
// Information about every allocation, including its size, offset, and // Information about every allocation, including its size, offset, and
// pointers to the allocation/deallocation stack traces (if present.) // pointers to the allocation/deallocation stack traces (if present.)
SlotMetadata data_[kFreePagesNumBits] = {}; SlotMetadata data_[kGpaMaxPages] = {};
uintptr_t pages_base_addr_ = 0; // Points to start of mapped region. uintptr_t pages_base_addr_ = 0; // Points to start of mapped region.
uintptr_t pages_end_addr_ = 0; // Points to the end of mapped region. uintptr_t pages_end_addr_ = 0; // Points to the end of mapped region.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment