asan_interface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //===-- sanitizer/asan_interface.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. //
  9. // This file is a part of AddressSanitizer (ASan).
  10. //
  11. // Public interface header.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_ASAN_INTERFACE_H
  14. #define SANITIZER_ASAN_INTERFACE_H
  15. #include <sanitizer/common_interface_defs.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /// Marks a memory region (<c>[addr, addr+size)</c>) as unaddressable.
  20. ///
  21. /// This memory must be previously allocated by your program. Instrumented
  22. /// code is forbidden from accessing addresses in this region until it is
  23. /// unpoisoned. This function is not guaranteed to poison the entire region -
  24. /// it could poison only a subregion of <c>[addr, addr+size)</c> due to ASan
  25. /// alignment restrictions.
  26. ///
  27. /// \note This function is not thread-safe because no two threads can poison or
  28. /// unpoison memory in the same memory region simultaneously.
  29. ///
  30. /// \param addr Start of memory region.
  31. /// \param size Size of memory region.
  32. void __asan_poison_memory_region(void const volatile *addr, size_t size);
  33. /// Marks a memory region (<c>[addr, addr+size)</c>) as addressable.
  34. ///
  35. /// This memory must be previously allocated by your program. Accessing
  36. /// addresses in this region is allowed until this region is poisoned again.
  37. /// This function could unpoison a super-region of <c>[addr, addr+size)</c> due
  38. /// to ASan alignment restrictions.
  39. ///
  40. /// \note This function is not thread-safe because no two threads can
  41. /// poison or unpoison memory in the same memory region simultaneously.
  42. ///
  43. /// \param addr Start of memory region.
  44. /// \param size Size of memory region.
  45. void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
  46. // Macros provided for convenience.
  47. #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
  48. /// Marks a memory region as unaddressable.
  49. ///
  50. /// \note Macro provided for convenience; defined as a no-op if ASan is not
  51. /// enabled.
  52. ///
  53. /// \param addr Start of memory region.
  54. /// \param size Size of memory region.
  55. #define ASAN_POISON_MEMORY_REGION(addr, size) \
  56. __asan_poison_memory_region((addr), (size))
  57. /// Marks a memory region as addressable.
  58. ///
  59. /// \note Macro provided for convenience; defined as a no-op if ASan is not
  60. /// enabled.
  61. ///
  62. /// \param addr Start of memory region.
  63. /// \param size Size of memory region.
  64. #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
  65. __asan_unpoison_memory_region((addr), (size))
  66. #else
  67. #define ASAN_POISON_MEMORY_REGION(addr, size) \
  68. ((void)(addr), (void)(size))
  69. #define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
  70. ((void)(addr), (void)(size))
  71. #endif
  72. /// Checks if an address is poisoned.
  73. ///
  74. /// Returns 1 if <c><i>addr</i></c> is poisoned (that is, 1-byte read/write
  75. /// access to this address would result in an error report from ASan).
  76. /// Otherwise returns 0.
  77. ///
  78. /// \param addr Address to check.
  79. ///
  80. /// \retval 1 Address is poisoned.
  81. /// \retval 0 Address is not poisoned.
  82. int __asan_address_is_poisoned(void const volatile *addr);
  83. /// Checks if a region is poisoned.
  84. ///
  85. /// If at least one byte in <c>[beg, beg+size)</c> is poisoned, returns the
  86. /// address of the first such byte. Otherwise returns 0.
  87. ///
  88. /// \param beg Start of memory region.
  89. /// \param size Start of memory region.
  90. /// \returns Address of first poisoned byte.
  91. void *__asan_region_is_poisoned(void *beg, size_t size);
  92. /// Describes an address (useful for calling from the debugger).
  93. ///
  94. /// Prints the description of <c><i>addr</i></c>.
  95. ///
  96. /// \param addr Address to describe.
  97. void __asan_describe_address(void *addr);
  98. /// Checks if an error has been or is being reported (useful for calling from
  99. /// the debugger to get information about an ASan error).
  100. ///
  101. /// Returns 1 if an error has been (or is being) reported. Otherwise returns 0.
  102. ///
  103. /// \returns 1 if an error has been (or is being) reported. Otherwise returns
  104. /// 0.
  105. int __asan_report_present(void);
  106. /// Gets the PC (program counter) register value of an ASan error (useful for
  107. /// calling from the debugger).
  108. ///
  109. /// Returns PC if an error has been (or is being) reported.
  110. /// Otherwise returns 0.
  111. ///
  112. /// \returns PC value.
  113. void *__asan_get_report_pc(void);
  114. /// Gets the BP (base pointer) register value of an ASan error (useful for
  115. /// calling from the debugger).
  116. ///
  117. /// Returns BP if an error has been (or is being) reported.
  118. /// Otherwise returns 0.
  119. ///
  120. /// \returns BP value.
  121. void *__asan_get_report_bp(void);
  122. /// Gets the SP (stack pointer) register value of an ASan error (useful for
  123. /// calling from the debugger).
  124. ///
  125. /// If an error has been (or is being) reported, returns SP.
  126. /// Otherwise returns 0.
  127. ///
  128. /// \returns SP value.
  129. void *__asan_get_report_sp(void);
  130. /// Gets the address of the report buffer of an ASan error (useful for calling
  131. /// from the debugger).
  132. ///
  133. /// Returns the address of the report buffer if an error has been (or is being)
  134. /// reported. Otherwise returns 0.
  135. ///
  136. /// \returns Address of report buffer.
  137. void *__asan_get_report_address(void);
  138. /// Gets access type of an ASan error (useful for calling from the debugger).
  139. ///
  140. /// Returns access type (read or write) if an error has been (or is being)
  141. /// reported. Otherwise returns 0.
  142. ///
  143. /// \returns Access type (0 = read, 1 = write).
  144. int __asan_get_report_access_type(void);
  145. /// Gets access size of an ASan error (useful for calling from the debugger).
  146. ///
  147. /// Returns access size if an error has been (or is being) reported. Otherwise
  148. /// returns 0.
  149. ///
  150. /// \returns Access size in bytes.
  151. size_t __asan_get_report_access_size(void);
  152. /// Gets the bug description of an ASan error (useful for calling from a
  153. /// debugger).
  154. ///
  155. /// \returns Returns a bug description if an error has been (or is being)
  156. /// reported - for example, "heap-use-after-free". Otherwise returns an empty
  157. /// string.
  158. const char *__asan_get_report_description(void);
  159. /// Gets information about a pointer (useful for calling from the debugger).
  160. ///
  161. /// Returns the category of the given pointer as a constant string.
  162. /// Possible return values are <c>global</c>, <c>stack</c>, <c>stack-fake</c>,
  163. /// <c>heap</c>, <c>heap-invalid</c>, <c>shadow-low</c>, <c>shadow-gap</c>,
  164. /// <c>shadow-high</c>, and <c>unknown</c>.
  165. ///
  166. /// If the return value is <c>global</c> or <c>stack</c>, tries to also return
  167. /// the variable name, address, and size. If the return value is <c>heap</c>,
  168. /// tries to return the chunk address and size. <c><i>name</i></c> should point
  169. /// to an allocated buffer of size <c><i>name_size</i></c>.
  170. ///
  171. /// \param addr Address to locate.
  172. /// \param name Buffer to store the variable's name.
  173. /// \param name_size Size in bytes of the variable's name buffer.
  174. /// \param[out] region_address Address of the region.
  175. /// \param[out] region_size Size of the region in bytes.
  176. ///
  177. /// \returns Returns the category of the given pointer as a constant string.
  178. const char *__asan_locate_address(void *addr, char *name, size_t name_size,
  179. void **region_address, size_t *region_size);
  180. /// Gets the allocation stack trace and thread ID for a heap address (useful
  181. /// for calling from the debugger).
  182. ///
  183. /// Stores up to <c><i>size</i></c> frames in <c><i>trace</i></c>. Returns
  184. /// the number of stored frames or 0 on error.
  185. ///
  186. /// \param addr A heap address.
  187. /// \param trace A buffer to store the stack trace.
  188. /// \param size Size in bytes of the trace buffer.
  189. /// \param[out] thread_id The thread ID of the address.
  190. ///
  191. /// \returns Returns the number of stored frames or 0 on error.
  192. size_t __asan_get_alloc_stack(void *addr, void **trace, size_t size,
  193. int *thread_id);
  194. /// Gets the free stack trace and thread ID for a heap address (useful for
  195. /// calling from the debugger).
  196. ///
  197. /// Stores up to <c><i>size</i></c> frames in <c><i>trace</i></c>. Returns
  198. /// the number of stored frames or 0 on error.
  199. ///
  200. /// \param addr A heap address.
  201. /// \param trace A buffer to store the stack trace.
  202. /// \param size Size in bytes of the trace buffer.
  203. /// \param[out] thread_id The thread ID of the address.
  204. ///
  205. /// \returns Returns the number of stored frames or 0 on error.
  206. size_t __asan_get_free_stack(void *addr, void **trace, size_t size,
  207. int *thread_id);
  208. /// Gets the current shadow memory mapping (useful for calling from the
  209. /// debugger).
  210. ///
  211. /// \param[out] shadow_scale Shadow scale value.
  212. /// \param[out] shadow_offset Offset value.
  213. void __asan_get_shadow_mapping(size_t *shadow_scale, size_t *shadow_offset);
  214. /// This is an internal function that is called to report an error. However,
  215. /// it is still a part of the interface because you might want to set a
  216. /// breakpoint on this function in the debugger.
  217. ///
  218. /// \param pc <c><i>pc</i></c> value of the ASan error.
  219. /// \param bp <c><i>bp</i></c> value of the ASan error.
  220. /// \param sp <c><i>sp</i></c> value of the ASan error.
  221. /// \param addr Address of the ASan error.
  222. /// \param is_write True if the error is a write error; false otherwise.
  223. /// \param access_size Size of the memory access of the ASan error.
  224. void __asan_report_error(void *pc, void *bp, void *sp,
  225. void *addr, int is_write, size_t access_size);
  226. // Deprecated. Call __sanitizer_set_death_callback instead.
  227. void __asan_set_death_callback(void (*callback)(void));
  228. /// Sets the callback function to be called during ASan error reporting.
  229. ///
  230. /// The callback provides a string pointer to the report.
  231. ///
  232. /// \param callback User-provided function.
  233. void __asan_set_error_report_callback(void (*callback)(const char *));
  234. /// User-provided callback on ASan errors.
  235. ///
  236. /// You can provide a function that would be called immediately when ASan
  237. /// detects an error. This is useful in cases when ASan detects an error but
  238. /// your program crashes before the ASan report is printed.
  239. void __asan_on_error(void);
  240. /// Prints accumulated statistics to <c>stderr</c> (useful for calling from the
  241. /// debugger).
  242. void __asan_print_accumulated_stats(void);
  243. /// User-provided default option settings.
  244. ///
  245. /// You can provide your own implementation of this function to return a string
  246. /// containing ASan runtime options (for example,
  247. /// <c>verbosity=1:halt_on_error=0</c>).
  248. ///
  249. /// \returns Default options string.
  250. const char* __asan_default_options(void);
  251. // The following two functions facilitate garbage collection in presence of
  252. // ASan's fake stack.
  253. /// Gets an opaque handler to the current thread's fake stack.
  254. ///
  255. /// Returns an opaque handler to be used by
  256. /// <c>__asan_addr_is_in_fake_stack()</c>. Returns NULL if the current thread
  257. /// does not have a fake stack.
  258. ///
  259. /// \returns An opaque handler to the fake stack or NULL.
  260. void *__asan_get_current_fake_stack(void);
  261. /// Checks if an address belongs to a given fake stack.
  262. ///
  263. /// If <c><i>fake_stack</i></c> is non-NULL and <c><i>addr</i></c> belongs to a
  264. /// fake frame in <c><i>fake_stack</i></c>, returns the address of the real
  265. /// stack that corresponds to the fake frame and sets <c><i>beg</i></c> and
  266. /// <c><i>end</i></c> to the boundaries of this fake frame. Otherwise returns
  267. /// NULL and does not touch <c><i>beg</i></c> and <c><i>end</i></c>.
  268. ///
  269. /// If <c><i>beg</i></c> or <c><i>end</i></c> are NULL, they are not touched.
  270. ///
  271. /// \note This function can be called from a thread other than the owner of
  272. /// <c><i>fake_stack</i></c>, but the owner thread needs to be alive.
  273. ///
  274. /// \param fake_stack An opaque handler to a fake stack.
  275. /// \param addr Address to test.
  276. /// \param[out] beg Beginning of fake frame.
  277. /// \param[out] end End of fake frame.
  278. /// \returns Stack address or NULL.
  279. void *__asan_addr_is_in_fake_stack(void *fake_stack, void *addr, void **beg,
  280. void **end);
  281. /// Performs shadow memory cleanup of the current thread's stack before a
  282. /// function marked with the <c>[[noreturn]]</c> attribute is called.
  283. ///
  284. /// To avoid false positives on the stack, must be called before no-return
  285. /// functions like <c>_exit()</c> and <c>execl()</c>.
  286. void __asan_handle_no_return(void);
  287. /// Update allocation stack trace for the given allocation to the current stack
  288. /// trace. Returns 1 if successful, 0 if not.
  289. int __asan_update_allocation_context(void* addr);
  290. #ifdef __cplusplus
  291. } // extern "C"
  292. #endif
  293. #endif // SANITIZER_ASAN_INTERFACE_H