asan_interface.h 7.0 KB

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