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::FromSlotSpan(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::FromSlotSpan(slot_span);
25 PA_DCHECK(shift_from_slot_start >= root->settings.extras_offset);
26 // Use <= to allow an address immediately past the object.
27 PA_DCHECK(shift_from_slot_start <=
28 root->settings.extras_offset + root->GetSlotUsableSize(slot_span));
29 }
30
DCheckIsWithInSuperPagePayload(uintptr_t address)31 void DCheckIsWithInSuperPagePayload(uintptr_t address) {
32 uintptr_t super_page = address & kSuperPageBaseMask;
33 auto* extent = PartitionSuperPageToExtent(super_page);
34 PA_DCHECK(IsWithinSuperPagePayload(address,
35 IsManagedByNormalBuckets(address) &&
36 extent->root->IsQuarantineAllowed()));
37 }
38
DCheckIsValidObjectAddress(internal::SlotSpanMetadata * slot_span,uintptr_t object_addr)39 void DCheckIsValidObjectAddress(internal::SlotSpanMetadata* slot_span,
40 uintptr_t object_addr) {
41 uintptr_t slot_span_start = SlotSpanMetadata::ToSlotSpanStart(slot_span);
42 auto* root = PartitionRoot::FromSlotSpan(slot_span);
43 PA_DCHECK((object_addr - slot_span_start) % slot_span->bucket->slot_size ==
44 root->settings.extras_offset);
45 }
46
DCheckNumberOfPartitionPagesInSuperPagePayload(const PartitionSuperPageExtentEntry * entry,const PartitionRoot * root,size_t number_of_nonempty_slot_spans)47 void DCheckNumberOfPartitionPagesInSuperPagePayload(
48 const PartitionSuperPageExtentEntry* entry,
49 const PartitionRoot* root,
50 size_t number_of_nonempty_slot_spans) {
51 uintptr_t super_page = base::bits::AlignDown(
52 reinterpret_cast<uintptr_t>(entry), kSuperPageAlignment);
53 size_t number_of_partition_pages_in_superpage_payload =
54 SuperPagePayloadSize(super_page, root->IsQuarantineAllowed()) /
55 PartitionPageSize();
56 PA_DCHECK(number_of_partition_pages_in_superpage_payload >
57 number_of_nonempty_slot_spans);
58 }
59
DCheckRootLockIsAcquired(PartitionRoot * root)60 void DCheckRootLockIsAcquired(PartitionRoot* root) {
61 PartitionRootLock(root).AssertAcquired();
62 }
63
DCheckRootLockOfSlotSpanIsAcquired(internal::SlotSpanMetadata * slot_span)64 void DCheckRootLockOfSlotSpanIsAcquired(internal::SlotSpanMetadata* slot_span) {
65 DCheckRootLockIsAcquired(PartitionRoot::FromSlotSpan(slot_span));
66 }
67
68 #endif // BUILDFLAG(PA_DCHECK_IS_ON)
69
70 } // namespace partition_alloc::internal
71