crash_handler.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // This function, provided the fault address from the signal handler, returns
  44. // the following values:
  45. // 1. If the crash was caused by an internally-detected error (invalid free,
  46. // double free), this function returns the pointer that was used for the
  47. // internally-detected bad operation (i.e. the pointer given to free()).
  48. // 2. For externally-detected crashes (use-after-free, buffer-overflow), this
  49. // function returns zero.
  50. // 3. If GWP-ASan wasn't responsible for the crash at all, this function also
  51. // returns zero.
  52. uintptr_t
  53. __gwp_asan_get_internal_crash_address(const gwp_asan::AllocatorState *State,
  54. uintptr_t ErrorPtr);
  55. // Returns a pointer to the metadata for the allocation that's responsible for
  56. // the crash. This metadata should not be dereferenced directly due to API
  57. // compatibility issues, but should be instead passed to functions below for
  58. // information retrieval. Returns nullptr if there is no metadata available for
  59. // this crash.
  60. const gwp_asan::AllocationMetadata *
  61. __gwp_asan_get_metadata(const gwp_asan::AllocatorState *State,
  62. const gwp_asan::AllocationMetadata *Metadata,
  63. uintptr_t ErrorPtr);
  64. // +---------------------------------------------------------------------------+
  65. // | Error Information Functions |
  66. // +---------------------------------------------------------------------------+
  67. // Functions below return information about the type of error that was caught by
  68. // GWP-ASan, or information about the allocation that caused the error. These
  69. // functions generally take an `AllocationMeta` argument, which should be
  70. // retrieved via. __gwp_asan_get_metadata.
  71. // Returns the start of the allocation whose metadata is in `AllocationMeta`.
  72. uintptr_t __gwp_asan_get_allocation_address(
  73. const gwp_asan::AllocationMetadata *AllocationMeta);
  74. // Returns the size of the allocation whose metadata is in `AllocationMeta`
  75. size_t __gwp_asan_get_allocation_size(
  76. const gwp_asan::AllocationMetadata *AllocationMeta);
  77. // Returns the Thread ID that allocated the memory that caused the error at
  78. // `ErrorPtr`. This function may not be called if __gwp_asan_has_metadata()
  79. // returns false.
  80. uint64_t __gwp_asan_get_allocation_thread_id(
  81. const gwp_asan::AllocationMetadata *AllocationMeta);
  82. // Retrieve the allocation trace for the allocation whose metadata is in
  83. // `AllocationMeta`, and place it into the provided `Buffer` that has at least
  84. // `BufferLen` elements. This function returns the number of frames that would
  85. // have been written into `Buffer` if the space was available (i.e. however many
  86. // frames were stored by GWP-ASan). A return value greater than `BufferLen`
  87. // indicates that the trace was truncated when storing to `Buffer`.
  88. size_t __gwp_asan_get_allocation_trace(
  89. const gwp_asan::AllocationMetadata *AllocationMeta, uintptr_t *Buffer,
  90. size_t BufferLen);
  91. // Returns whether the allocation whose metadata is in `AllocationMeta` has been
  92. // deallocated. This function may not be called if __gwp_asan_has_metadata()
  93. // returns false.
  94. bool __gwp_asan_is_deallocated(
  95. const gwp_asan::AllocationMetadata *AllocationMeta);
  96. // Returns the Thread ID that deallocated the memory whose metadata is in
  97. // `AllocationMeta`. This function may not be called if
  98. // __gwp_asan_is_deallocated() returns false.
  99. uint64_t __gwp_asan_get_deallocation_thread_id(
  100. const gwp_asan::AllocationMetadata *AllocationMeta);
  101. // Retrieve the deallocation trace for the allocation whose metadata is in
  102. // `AllocationMeta`, and place it into the provided `Buffer` that has at least
  103. // `BufferLen` elements. This function returns the number of frames that would
  104. // have been written into `Buffer` if the space was available (i.e. however many
  105. // frames were stored by GWP-ASan). A return value greater than `BufferLen`
  106. // indicates that the trace was truncated when storing to `Buffer`. This
  107. // function may not be called if __gwp_asan_is_deallocated() returns false.
  108. size_t __gwp_asan_get_deallocation_trace(
  109. const gwp_asan::AllocationMetadata *AllocationMeta, uintptr_t *Buffer,
  110. size_t BufferLen);
  111. #ifdef __cplusplus
  112. } // extern "C"
  113. #endif
  114. #endif // GWP_ASAN_INTERFACE_H_