Commit 2d07dc4f authored by Vlad Tsyrklevich's avatar Vlad Tsyrklevich Committed by Commit Bot

GWP-ASan: Change alloc_offset to alloc_ptr

Currently, the SlotMetadata records the allocation offset in the
returned page; however, it's only ever used to calculate the final
allocation address. Instead, just store the allocation address.

Bug: 896019
Change-Id: Id14a15e4a7c9afdeb4a517b85a90ddb9f30f7a9f
Reviewed-on: https://chromium-review.googlesource.com/c/1318404
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
Reviewed-by: default avatarAlbert J. Wong <ajwong@chromium.org>
Reviewed-by: default avatarVitaly Buka <vitalybuka@chromium.org>
Cr-Commit-Position: refs/heads/master@{#605548}
parent b655f45f
...@@ -82,10 +82,12 @@ void* GuardedPageAllocator::Allocate(size_t size, size_t align) { ...@@ -82,10 +82,12 @@ void* GuardedPageAllocator::Allocate(size_t size, size_t align) {
// Return left-aligned allocation to detect underflows. // Return left-aligned allocation to detect underflows.
offset = 0; offset = 0;
void* alloc = reinterpret_cast<void*>(free_page + offset);
// Initialize slot metadata. // Initialize slot metadata.
data_[free_slot].RecordAllocation(size, offset); data_[free_slot].RecordAllocation(size, alloc);
return reinterpret_cast<void*>(free_page + offset); return alloc;
} }
void GuardedPageAllocator::Deallocate(void* ptr) { void GuardedPageAllocator::Deallocate(void* ptr) {
...@@ -95,7 +97,7 @@ void GuardedPageAllocator::Deallocate(void* ptr) { ...@@ -95,7 +97,7 @@ void GuardedPageAllocator::Deallocate(void* ptr) {
MarkPageInaccessible(reinterpret_cast<void*>(GetPageAddr(addr))); MarkPageInaccessible(reinterpret_cast<void*>(GetPageAddr(addr)));
size_t slot = AddrToSlot(GetPageAddr(addr)); size_t slot = AddrToSlot(GetPageAddr(addr));
DCHECK_EQ(addr, GetPageAddr(addr) + data_[slot].alloc_offset); DCHECK_EQ(ptr, data_[slot].alloc_ptr);
// Check for double free. // Check for double free.
if (data_[slot].dealloc_trace_addr) { if (data_[slot].dealloc_trace_addr) {
double_free_detected_ = true; double_free_detected_ = true;
...@@ -113,7 +115,7 @@ size_t GuardedPageAllocator::GetRequestedSize(const void* ptr) const { ...@@ -113,7 +115,7 @@ size_t GuardedPageAllocator::GetRequestedSize(const void* ptr) const {
DCHECK(PointerIsMine(ptr)); DCHECK(PointerIsMine(ptr));
const uintptr_t addr = reinterpret_cast<uintptr_t>(ptr); const uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
size_t slot = AddrToSlot(GetPageAddr(addr)); size_t slot = AddrToSlot(GetPageAddr(addr));
DCHECK_EQ(addr, GetPageAddr(addr) + data_[slot].alloc_offset); DCHECK_EQ(ptr, data_[slot].alloc_ptr);
return data_[slot].alloc_size; return data_[slot].alloc_size;
} }
...@@ -257,11 +259,11 @@ void GuardedPageAllocator::SlotMetadata::Reset() { ...@@ -257,11 +259,11 @@ void GuardedPageAllocator::SlotMetadata::Reset() {
} }
void GuardedPageAllocator::SlotMetadata::RecordAllocation(size_t size, void GuardedPageAllocator::SlotMetadata::RecordAllocation(size_t size,
size_t offset) { void* ptr) {
Reset(); Reset();
alloc_size = size; alloc_size = size;
alloc_offset = offset; alloc_ptr = ptr;
alloc_tid = base::PlatformThread::CurrentId(); alloc_tid = base::PlatformThread::CurrentId();
new (stacktrace_alloc) StackTrace(); new (stacktrace_alloc) StackTrace();
......
...@@ -83,16 +83,16 @@ class GWP_ASAN_EXPORT GuardedPageAllocator { ...@@ -83,16 +83,16 @@ class GWP_ASAN_EXPORT GuardedPageAllocator {
// having them be statically allocated in the SlotMetadata itself.) // having them be statically allocated in the SlotMetadata itself.)
void Init(); void Init();
// Update slot metadata on an allocation with the given size and offset. // Update slot metadata on an allocation with the given size and pointer.
void RecordAllocation(size_t size, size_t offset); void RecordAllocation(size_t size, void* ptr);
// Update slot metadata on a deallocation. // Update slot metadata on a deallocation.
void RecordDeallocation(); void RecordDeallocation();
// Size of the allocation // Size of the allocation
size_t alloc_size = 0; size_t alloc_size = 0;
// How far into the page is the returned allocation. // The allocation address.
size_t alloc_offset = 0; void* alloc_ptr = nullptr;
// (De)allocation thread id or base::kInvalidThreadId if no (de)allocation // (De)allocation thread id or base::kInvalidThreadId if no (de)allocation
// occurred. // occurred.
......
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