asan_interface.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //===-- sanitizer/asan_interface.h ------------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file is a part of AddressSanitizer.
  11. //
  12. // Public interface header.
  13. //===----------------------------------------------------------------------===//
  14. #ifndef SANITIZER_ASAN_INTERFACE_H
  15. #define SANITIZER_ASAN_INTERFACE_H
  16. #include <sanitizer/common_interface_defs.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. // Marks memory region [addr, addr+size) as unaddressable.
  21. // This memory must be previously allocated by the user program. Accessing
  22. // addresses in this region from instrumented code is forbidden until
  23. // this region is unpoisoned. This function is not guaranteed to poison
  24. // the whole region - it may poison only subregion of [addr, addr+size) due
  25. // to ASan alignment restrictions.
  26. // Method is NOT thread-safe in the sense that no two threads can
  27. // (un)poison memory in the same memory region simultaneously.
  28. void __asan_poison_memory_region(void const volatile *addr, size_t size);
  29. // Marks memory region [addr, addr+size) as addressable.
  30. // This memory must be previously allocated by the user program. Accessing
  31. // addresses in this region is allowed until this region is poisoned again.
  32. // This function may unpoison a superregion of [addr, addr+size) due to
  33. // ASan alignment restrictions.
  34. // Method is NOT thread-safe in the sense that no two threads can
  35. // (un)poison memory in the same memory region simultaneously.
  36. void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
  37. // User code should use macros instead of functions.
  38. #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
  39. #define ASAN_POISON_MEMORY_REGION(addr, size) \
  40. __asan_poison_memory_region((addr), (size))
  41. #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
  42. __asan_unpoison_memory_region((addr), (size))
  43. #else
  44. #define ASAN_POISON_MEMORY_REGION(addr, size) \
  45. ((void)(addr), (void)(size))
  46. #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
  47. ((void)(addr), (void)(size))
  48. #endif
  49. // Returns 1 if addr is poisoned (i.e. 1-byte read/write access to this
  50. // address will result in error report from AddressSanitizer).
  51. // Otherwise returns 0.
  52. int __asan_address_is_poisoned(void const volatile *addr);
  53. // If at least one byte in [beg, beg+size) is poisoned, return the address
  54. // of the first such byte. Otherwise return 0.
  55. void *__asan_region_is_poisoned(void *beg, size_t size);
  56. // Print the description of addr (useful when debugging in gdb).
  57. void __asan_describe_address(void *addr);
  58. // Useful for calling from a debugger to get information about an ASan error.
  59. // Returns 1 if an error has been (or is being) reported, otherwise returns 0.
  60. int __asan_report_present(void);
  61. // Useful for calling from a debugger to get information about an ASan error.
  62. // If an error has been (or is being) reported, the following functions return
  63. // the pc, bp, sp, address, access type (0 = read, 1 = write), access size and
  64. // bug description (e.g. "heap-use-after-free"). Otherwise they return 0.
  65. void *__asan_get_report_pc(void);
  66. void *__asan_get_report_bp(void);
  67. void *__asan_get_report_sp(void);
  68. void *__asan_get_report_address(void);
  69. int __asan_get_report_access_type(void);
  70. size_t __asan_get_report_access_size(void);
  71. const char *__asan_get_report_description(void);
  72. // Useful for calling from the debugger to get information about a pointer.
  73. // Returns the category of the given pointer as a constant string.
  74. // Possible return values are "global", "stack", "stack-fake", "heap",
  75. // "heap-invalid", "shadow-low", "shadow-gap", "shadow-high", "unknown".
  76. // If global or stack, tries to also return the variable name, address and
  77. // size. If heap, tries to return the chunk address and size. 'name' should
  78. // point to an allocated buffer of size 'name_size'.
  79. const char *__asan_locate_address(void *addr, char *name, size_t name_size,
  80. void **region_address, size_t *region_size);
  81. // Useful for calling from the debugger to get the allocation stack trace
  82. // and thread ID for a heap address. Stores up to 'size' frames into 'trace',
  83. // returns the number of stored frames or 0 on error.
  84. size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size,
  85. int *thread_id);
  86. // Useful for calling from the debugger to get the free stack trace
  87. // and thread ID for a heap address. Stores up to 'size' frames into 'trace',
  88. // returns the number of stored frames or 0 on error.
  89. size_t __asan_get_free_stack(void *addr, void **trace, size_t size,
  90. int *thread_id);
  91. // Useful for calling from the debugger to get the current shadow memory
  92. // mapping.
  93. void __asan_get_shadow_mapping(size_t *shadow_scale, size_t *shadow_offset);
  94. // This is an internal function that is called to report an error.
  95. // However it is still a part of the interface because users may want to
  96. // set a breakpoint on this function in a debugger.
  97. void __asan_report_error(void *pc, void *bp, void *sp,
  98. void *addr, int is_write, size_t access_size);
  99. // Deprecated. Call __sanitizer_set_death_callback instead.
  100. void __asan_set_death_callback(void (*callback)(void));
  101. void __asan_set_error_report_callback(void (*callback)(const char*));
  102. // User may provide function that would be called right when ASan detects
  103. // an error. This can be used to notice cases when ASan detects an error, but
  104. // the program crashes before ASan report is printed.
  105. void __asan_on_error(void);
  106. // Prints accumulated stats to stderr. Used for debugging.
  107. void __asan_print_accumulated_stats(void);
  108. // This function may be optionally provided by user and should return
  109. // a string containing ASan runtime options. See asan_flags.h for details.
  110. const char* __asan_default_options(void);
  111. // The following 2 functions facilitate garbage collection in presence of
  112. // asan's fake stack.
  113. // Returns an opaque handler to be used later in __asan_addr_is_in_fake_stack.
  114. // Returns NULL if the current thread does not have a fake stack.
  115. void *__asan_get_current_fake_stack(void);
  116. // If fake_stack is non-NULL and addr belongs to a fake frame in
  117. // fake_stack, returns the address on real stack that corresponds to
  118. // the fake frame and sets beg/end to the boundaries of this fake frame.
  119. // Otherwise returns NULL and does not touch beg/end.
  120. // If beg/end are NULL, they are not touched.
  121. // This function may be called from a thread other than the owner of
  122. // fake_stack, but the owner thread need to be alive.
  123. void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
  124. void **end);
  125. // Performs cleanup before a [[noreturn]] function. Must be called
  126. // before things like _exit and execl to avoid false positives on stack.
  127. void __asan_handle_no_return(void);
  128. #ifdef __cplusplus
  129. } // extern "C"
  130. #endif
  131. #endif // SANITIZER_ASAN_INTERFACE_H