crash_handler.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //===-- crash_handler.h -----------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // This file contains interface functions that can be called by an in-process or
  9. // out-of-process crash handler after the process has terminated. Functions in
  10. // this interface are never thread safe. For an in-process crash handler, the
  11. // handler should call GuardedPoolAllocator::disable() to stop any other threads
  12. // from retrieving new GWP-ASan allocations, which may corrupt the metadata.
  13. #ifndef GWP_ASAN_INTERFACE_H_
  14. #define GWP_ASAN_INTERFACE_H_
  15. #include "gwp_asan/common.h"
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. // When a process crashes, there are three possible outcomes:
  20. // 1. The crash is unrelated to GWP-ASan - in which case this function returns
  21. // false.
  22. // 2. The crash is internally detected within GWP-ASan itself (e.g. a
  23. // double-free bug is caught in GuardedPoolAllocator::deallocate(), and
  24. // GWP-ASan will terminate the process). In this case - this function
  25. // returns true.
  26. // 3. The crash is caused by a memory error at `AccessPtr` that's caught by the
  27. // system, but GWP-ASan is responsible for the allocation. In this case -
  28. // the function also returns true.
  29. // This function takes an optional `AccessPtr` parameter. If the pointer that
  30. // was attempted to be accessed is available, you should provide it here. In the
  31. // case of some internally-detected errors, the crash may manifest as an abort
  32. // or trap may or may not have an associated pointer. In these cases, the
  33. // pointer can be obtained by a call to __gwp_asan_get_internal_crash_address.
  34. bool __gwp_asan_error_is_mine(const gwp_asan::AllocatorState *State,
  35. uintptr_t ErrorPtr = 0u);
  36. // Diagnose and return the type of error that occurred at `ErrorPtr`. If
  37. // `ErrorPtr` is unrelated to GWP-ASan, or if the error type cannot be deduced,
  38. // this function returns Error::UNKNOWN.
  39. gwp_asan::Error
  40. __gwp_asan_diagnose_error(const gwp_asan::AllocatorState *State,
  41. const gwp_asan::AllocationMetadata *Metadata,
  42. uintptr_t ErrorPtr);
  43. // For internally-detected errors (double free, invalid free), this function
  44. // returns the pointer that the error occurred at. If the error is unrelated to
  45. // GWP-ASan, or if the error was caused by a non-internally detected failure,
  46. // this function returns zero.
  47. uintptr_t
  48. __gwp_asan_get_internal_crash_address(const gwp_asan::AllocatorState *State);
  49. // Returns a pointer to the metadata for the allocation that's responsible for
  50. // the crash. This metadata should not be dereferenced directly due to API
  51. // compatibility issues, but should be instead passed to functions below for
  52. // information retrieval. Returns nullptr if there is no metadata available for
  53. // this crash.
  54. const gwp_asan::AllocationMetadata *
  55. __gwp_asan_get_metadata(const gwp_asan::AllocatorState *State,
  56. const gwp_asan::AllocationMetadata *Metadata,
  57. uintptr_t ErrorPtr);
  58. // +---------------------------------------------------------------------------+
  59. // | Error Information Functions |
  60. // +---------------------------------------------------------------------------+
  61. // Functions below return information about the type of error that was caught by
  62. // GWP-ASan, or information about the allocation that caused the error. These
  63. // functions generally take an `AllocationMeta` argument, which should be
  64. // retrieved via. __gwp_asan_get_metadata.
  65. // Returns the start of the allocation whose metadata is in `AllocationMeta`.
  66. uintptr_t __gwp_asan_get_allocation_address(
  67. const gwp_asan::AllocationMetadata *AllocationMeta);
  68. // Returns the size of the allocation whose metadata is in `AllocationMeta`
  69. size_t __gwp_asan_get_allocation_size(
  70. const gwp_asan::AllocationMetadata *AllocationMeta);
  71. // Returns the Thread ID that allocated the memory that caused the error at
  72. // `ErrorPtr`. This function may not be called if __gwp_asan_has_metadata()
  73. // returns false.
  74. uint64_t __gwp_asan_get_allocation_thread_id(
  75. const gwp_asan::AllocationMetadata *AllocationMeta);
  76. // Retrieve the allocation trace for the allocation whose metadata is in
  77. // `AllocationMeta`, and place it into the provided `Buffer` that has at least
  78. // `BufferLen` elements. This function returns the number of frames that would
  79. // have been written into `Buffer` if the space was available (i.e. however many
  80. // frames were stored by GWP-ASan). A return value greater than `BufferLen`
  81. // indicates that the trace was truncated when storing to `Buffer`.
  82. size_t __gwp_asan_get_allocation_trace(
  83. const gwp_asan::AllocationMetadata *AllocationMeta, uintptr_t *Buffer,
  84. size_t BufferLen);
  85. // Returns whether the allocation whose metadata is in `AllocationMeta` has been
  86. // deallocated. This function may not be called if __gwp_asan_has_metadata()
  87. // returns false.
  88. bool __gwp_asan_is_deallocated(
  89. const gwp_asan::AllocationMetadata *AllocationMeta);
  90. // Returns the Thread ID that deallocated the memory whose metadata is in
  91. // `AllocationMeta`. This function may not be called if
  92. // __gwp_asan_is_deallocated() returns false.
  93. uint64_t __gwp_asan_get_deallocation_thread_id(
  94. const gwp_asan::AllocationMetadata *AllocationMeta);
  95. // Retrieve the deallocation trace for the allocation whose metadata is in
  96. // `AllocationMeta`, and place it into the provided `Buffer` that has at least
  97. // `BufferLen` elements. This function returns the number of frames that would
  98. // have been written into `Buffer` if the space was available (i.e. however many
  99. // frames were stored by GWP-ASan). A return value greater than `BufferLen`
  100. // indicates that the trace was truncated when storing to `Buffer`. This
  101. // function may not be called if __gwp_asan_is_deallocated() returns false.
  102. size_t __gwp_asan_get_deallocation_trace(
  103. const gwp_asan::AllocationMetadata *AllocationMeta, uintptr_t *Buffer,
  104. size_t BufferLen);
  105. #ifdef __cplusplus
  106. } // extern "C"
  107. #endif
  108. #endif // GWP_ASAN_INTERFACE_H_