1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_allocator/block/contiguous.h"
16
17 #include "pw_assert/check.h"
18
19 namespace pw::allocator::internal {
20
21 // TODO: b/234875269 - Add stack tracing to locate which call to the heap
22 // operation caused the corruption in the methods below.
23
CheckNextMisaligned(const void * block,const void * next,bool next_is_aligned)24 void CheckNextMisaligned(const void* block,
25 const void* next,
26 bool next_is_aligned) {
27 if constexpr (Hardening::kIncludesDebugChecks) {
28 PW_CHECK(
29 next_is_aligned,
30 "A block (%p) is corrupted: it has a 'next' field (%p) that is not "
31 "properly aligned.",
32 block,
33 next);
34 }
35 }
36
CheckNextPrevMismatched(const void * block,const void * next,const void * next_prev,bool next_prev_matches)37 void CheckNextPrevMismatched(const void* block,
38 const void* next,
39 const void* next_prev,
40 bool next_prev_matches) {
41 if constexpr (Hardening::kIncludesDebugChecks) {
42 PW_CHECK(next_prev_matches,
43 "A block (%p) is corrupted: its 'next' field (%p) has a 'prev' "
44 "field (%p) that does not match the block.",
45 block,
46 next,
47 next_prev);
48 }
49 }
50
CheckPrevMisaligned(const void * block,const void * prev,bool prev_is_aligned)51 void CheckPrevMisaligned(const void* block,
52 const void* prev,
53 bool prev_is_aligned) {
54 if constexpr (Hardening::kIncludesDebugChecks) {
55 PW_CHECK(
56 prev_is_aligned,
57 "A block (%p) is corrupted: it has a 'prev' field (%p) that is not "
58 "properly aligned.",
59 block,
60 prev);
61 }
62 }
63
CheckPrevNextMismatched(const void * block,const void * prev,const void * prev_next,bool prev_next_matches)64 void CheckPrevNextMismatched(const void* block,
65 const void* prev,
66 const void* prev_next,
67 bool prev_next_matches) {
68 if constexpr (Hardening::kIncludesDebugChecks) {
69 PW_CHECK(prev_next_matches,
70 "A block (%p) is corrupted: its 'prev' field (%p) has a 'next' "
71 "field (%p) that does not match the block.",
72 block,
73 prev,
74 prev_next);
75 }
76 }
77
78 } // namespace pw::allocator::internal
79