asan_interface.h 13 KB

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