dangling_raw_ptr_checks.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2022 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_DANGLING_RAW_PTR_CHECKS_H_
  5. #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_DANGLING_RAW_PTR_CHECKS_H_
  6. #include <cstdint>
  7. #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
  8. // When compiled with build flags `enable_dangling_raw_ptr_checks`, dangling
  9. // raw_ptr are reported. Its behavior can be configured here.
  10. //
  11. // Purpose of this level of indirection:
  12. // - Ease testing.
  13. // - Keep partition_alloc/ independent from base/. In most cases, when a
  14. // dangling raw_ptr is detected/released, this involves recording a
  15. // base::debug::StackTrace, which isn't desirable inside partition_alloc/.
  16. // - Be able (potentially) to turn this feature on/off at runtime based on
  17. // dependant's flags.
  18. namespace partition_alloc {
  19. // DanglingRawPtrDetected is called when there exists a `raw_ptr` referencing a
  20. // memory region and the allocator is asked to release it.
  21. //
  22. // It won't be called again with the same `id`, up until (potentially) a call to
  23. // DanglingRawPtrReleased(`id`) is made.
  24. //
  25. // This function is called from within the allocator, and is not allowed to
  26. // allocate memory.
  27. using DanglingRawPtrDetectedFn = void(uintptr_t /*id*/);
  28. PA_COMPONENT_EXPORT(PARTITION_ALLOC)
  29. DanglingRawPtrDetectedFn* GetDanglingRawPtrDetectedFn();
  30. PA_COMPONENT_EXPORT(PARTITION_ALLOC)
  31. void SetDanglingRawPtrDetectedFn(DanglingRawPtrDetectedFn);
  32. // DanglingRawPtrReleased: Called after DanglingRawPtrDetected(id), once the
  33. // last dangling raw_ptr stops referencing the memory region.
  34. //
  35. // This function is allowed to allocate memory.
  36. using DanglingRawPtrReleasedFn = void(uintptr_t /*id*/);
  37. PA_COMPONENT_EXPORT(PARTITION_ALLOC)
  38. DanglingRawPtrReleasedFn* GetDanglingRawPtrReleasedFn();
  39. PA_COMPONENT_EXPORT(PARTITION_ALLOC)
  40. void SetDanglingRawPtrReleasedFn(DanglingRawPtrReleasedFn);
  41. namespace internal {
  42. PA_COMPONENT_EXPORT(PARTITION_ALLOC) void DanglingRawPtrDetected(uintptr_t id);
  43. PA_COMPONENT_EXPORT(PARTITION_ALLOC) void DanglingRawPtrReleased(uintptr_t id);
  44. } // namespace internal
  45. } // namespace partition_alloc
  46. #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_DANGLING_RAW_PTR_CHECKS_H_