1 // Copyright 2023 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 #include "partition_alloc/partition_dcheck_helper.h"
6 
7 #include <cstdint>
8 
9 #include "partition_alloc/partition_bucket.h"
10 #include "partition_alloc/partition_page.h"
11 #include "partition_alloc/partition_root.h"
12 
13 namespace partition_alloc::internal {
14 
15 #if BUILDFLAG(PA_DCHECK_IS_ON)
16 
DCheckIsValidSlotSpan(internal::SlotSpanMetadata * slot_span)17 void DCheckIsValidSlotSpan(internal::SlotSpanMetadata* slot_span) {
18   PartitionRoot* root = PartitionRoot::FromSlotSpanMetadata(slot_span);
19   PA_DCHECK(root->inverted_self == ~reinterpret_cast<uintptr_t>(root));
20 }
21 
DCheckIsValidShiftFromSlotStart(internal::SlotSpanMetadata * slot_span,uintptr_t shift_from_slot_start)22 void DCheckIsValidShiftFromSlotStart(internal::SlotSpanMetadata* slot_span,
23                                      uintptr_t shift_from_slot_start) {
24   PartitionRoot* root = PartitionRoot::FromSlotSpanMetadata(slot_span);
25   // Use <= to allow an address immediately past the object.
26   PA_DCHECK(shift_from_slot_start <= root->GetSlotUsableSize(slot_span));
27 }
28 
DCheckIsWithInSuperPagePayload(uintptr_t address)29 void DCheckIsWithInSuperPagePayload(uintptr_t address) {
30   uintptr_t super_page = address & kSuperPageBaseMask;
31   auto* extent = PartitionSuperPageToExtent(super_page);
32   PA_DCHECK(IsWithinSuperPagePayload(address,
33                                      IsManagedByNormalBuckets(address) &&
34                                          extent->root->IsQuarantineAllowed()));
35 }
36 
DCheckIsValidObjectAddress(internal::SlotSpanMetadata * slot_span,uintptr_t object_addr)37 void DCheckIsValidObjectAddress(internal::SlotSpanMetadata* slot_span,
38                                 uintptr_t object_addr) {
39   uintptr_t slot_span_start = SlotSpanMetadata::ToSlotSpanStart(slot_span);
40   PA_DCHECK((object_addr - slot_span_start) % slot_span->bucket->slot_size ==
41             0);
42 }
43 
DCheckNumberOfPartitionPagesInSuperPagePayload(const PartitionSuperPageExtentEntry * entry,const PartitionRoot * root,size_t number_of_nonempty_slot_spans)44 void DCheckNumberOfPartitionPagesInSuperPagePayload(
45     const PartitionSuperPageExtentEntry* entry,
46     const PartitionRoot* root,
47     size_t number_of_nonempty_slot_spans) {
48   uintptr_t super_page = base::bits::AlignDown(
49       reinterpret_cast<uintptr_t>(entry), kSuperPageAlignment);
50   size_t number_of_partition_pages_in_superpage_payload =
51       SuperPagePayloadSize(super_page, root->IsQuarantineAllowed()) /
52       PartitionPageSize();
53   PA_DCHECK(number_of_partition_pages_in_superpage_payload >
54             number_of_nonempty_slot_spans);
55 }
56 
DCheckRootLockIsAcquired(PartitionRoot * root)57 void DCheckRootLockIsAcquired(PartitionRoot* root) {
58   PartitionRootLock(root).AssertAcquired();
59 }
60 
DCheckRootLockOfSlotSpanIsAcquired(internal::SlotSpanMetadata * slot_span)61 void DCheckRootLockOfSlotSpanIsAcquired(internal::SlotSpanMetadata* slot_span) {
62   DCheckRootLockIsAcquired(PartitionRoot::FromSlotSpanMetadata(slot_span));
63 }
64 
65 #endif  // BUILDFLAG(PA_DCHECK_IS_ON)
66 
67 }  // namespace partition_alloc::internal
68