• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_COOKIE_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_COOKIE_H_
7 
8 #include "base/allocator/partition_allocator/partition_alloc_base/compiler_specific.h"
9 #include "base/allocator/partition_allocator/partition_alloc_base/debug/debugging_buildflags.h"
10 #include "base/allocator/partition_allocator/partition_alloc_check.h"
11 
12 namespace partition_alloc::internal {
13 
14 static constexpr size_t kCookieSize = 16;
15 
16 // Cookie is enabled for debug builds.
17 #if BUILDFLAG(PA_DCHECK_IS_ON)
18 
19 inline constexpr unsigned char kCookieValue[kCookieSize] = {
20     0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xD0, 0x0D,
21     0x13, 0x37, 0xF0, 0x05, 0xBA, 0x11, 0xAB, 0x1E};
22 
23 constexpr size_t kPartitionCookieSizeAdjustment = kCookieSize;
24 
PartitionCookieCheckValue(unsigned char * cookie_ptr)25 PA_ALWAYS_INLINE void PartitionCookieCheckValue(unsigned char* cookie_ptr) {
26   for (size_t i = 0; i < kCookieSize; ++i, ++cookie_ptr) {
27     PA_DCHECK(*cookie_ptr == kCookieValue[i]);
28   }
29 }
30 
PartitionCookieWriteValue(unsigned char * cookie_ptr)31 PA_ALWAYS_INLINE void PartitionCookieWriteValue(unsigned char* cookie_ptr) {
32   for (size_t i = 0; i < kCookieSize; ++i, ++cookie_ptr) {
33     *cookie_ptr = kCookieValue[i];
34   }
35 }
36 
37 #else
38 
39 constexpr size_t kPartitionCookieSizeAdjustment = 0;
40 
PartitionCookieCheckValue(unsigned char * address)41 PA_ALWAYS_INLINE void PartitionCookieCheckValue(unsigned char* address) {}
42 
PartitionCookieWriteValue(unsigned char * cookie_ptr)43 PA_ALWAYS_INLINE void PartitionCookieWriteValue(unsigned char* cookie_ptr) {}
44 
45 #endif  // BUILDFLAG(PA_DCHECK_IS_ON)
46 
47 }  // namespace partition_alloc::internal
48 
49 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_COOKIE_H_
50