partition_cookie.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (c) 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_COOKIE_H_
  5. #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_COOKIE_H_
  6. #include "base/allocator/partition_allocator/partition_alloc_base/compiler_specific.h"
  7. #include "base/allocator/partition_allocator/partition_alloc_base/debug/debugging_buildflags.h"
  8. #include "base/allocator/partition_allocator/partition_alloc_check.h"
  9. namespace partition_alloc::internal {
  10. static constexpr size_t kCookieSize = 16;
  11. // Cookie is enabled for debug builds.
  12. #if BUILDFLAG(PA_DCHECK_IS_ON)
  13. static constexpr unsigned char kCookieValue[kCookieSize] = {
  14. 0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xD0, 0x0D,
  15. 0x13, 0x37, 0xF0, 0x05, 0xBA, 0x11, 0xAB, 0x1E};
  16. constexpr size_t kPartitionCookieSizeAdjustment = kCookieSize;
  17. PA_ALWAYS_INLINE void PartitionCookieCheckValue(unsigned char* cookie_ptr) {
  18. for (size_t i = 0; i < kCookieSize; ++i, ++cookie_ptr)
  19. PA_DCHECK(*cookie_ptr == kCookieValue[i]);
  20. }
  21. PA_ALWAYS_INLINE void PartitionCookieWriteValue(unsigned char* cookie_ptr) {
  22. for (size_t i = 0; i < kCookieSize; ++i, ++cookie_ptr)
  23. *cookie_ptr = kCookieValue[i];
  24. }
  25. #else
  26. constexpr size_t kPartitionCookieSizeAdjustment = 0;
  27. PA_ALWAYS_INLINE void PartitionCookieCheckValue(unsigned char* address) {}
  28. PA_ALWAYS_INLINE void PartitionCookieWriteValue(unsigned char* cookie_ptr) {}
  29. #endif // BUILDFLAG(PA_DCHECK_IS_ON)
  30. } // namespace partition_alloc::internal
  31. #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_COOKIE_H_