common_interface_defs.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //===-- sanitizer/common_interface_defs.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. // Common part of the public sanitizer interface.
  10. //===----------------------------------------------------------------------===//
  11. #ifndef SANITIZER_COMMON_INTERFACE_DEFS_H
  12. #define SANITIZER_COMMON_INTERFACE_DEFS_H
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. // GCC does not understand __has_feature.
  16. #if !defined(__has_feature)
  17. #define __has_feature(x) 0
  18. #endif
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. // Arguments for __sanitizer_sandbox_on_notify() below.
  23. typedef struct {
  24. // Enable sandbox support in sanitizer coverage.
  25. int coverage_sandboxed;
  26. // File descriptor to write coverage data to. If -1 is passed, a file will
  27. // be pre-opened by __sanitizer_sandbox_on_notify(). This field has no
  28. // effect if coverage_sandboxed == 0.
  29. intptr_t coverage_fd;
  30. // If non-zero, split the coverage data into well-formed blocks. This is
  31. // useful when coverage_fd is a socket descriptor. Each block will contain
  32. // a header, allowing data from multiple processes to be sent over the same
  33. // socket.
  34. unsigned int coverage_max_block_size;
  35. } __sanitizer_sandbox_arguments;
  36. // Tell the tools to write their reports to "path.<pid>" instead of stderr.
  37. void __sanitizer_set_report_path(const char *path);
  38. // Tell the tools to write their reports to the provided file descriptor
  39. // (casted to void *).
  40. void __sanitizer_set_report_fd(void *fd);
  41. // Get the current full report file path, if a path was specified by
  42. // an earlier call to __sanitizer_set_report_path. Returns null otherwise.
  43. const char *__sanitizer_get_report_path();
  44. // Notify the tools that the sandbox is going to be turned on. The reserved
  45. // parameter will be used in the future to hold a structure with functions
  46. // that the tools may call to bypass the sandbox.
  47. void __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args);
  48. // This function is called by the tool when it has just finished reporting
  49. // an error. 'error_summary' is a one-line string that summarizes
  50. // the error message. This function can be overridden by the client.
  51. void __sanitizer_report_error_summary(const char *error_summary);
  52. // Some of the sanitizers (for example ASan/TSan) could miss bugs that happen
  53. // in unaligned loads/stores. To find such bugs reliably, you need to replace
  54. // plain unaligned loads/stores with these calls.
  55. /// Loads a 16-bit unaligned value.
  56. ///
  57. /// \param p Pointer to unaligned memory.
  58. ///
  59. /// \returns Loaded value.
  60. uint16_t __sanitizer_unaligned_load16(const void *p);
  61. /// Loads a 32-bit unaligned value.
  62. ///
  63. /// \param p Pointer to unaligned memory.
  64. ///
  65. /// \returns Loaded value.
  66. uint32_t __sanitizer_unaligned_load32(const void *p);
  67. /// Loads a 64-bit unaligned value.
  68. ///
  69. /// \param p Pointer to unaligned memory.
  70. ///
  71. /// \returns Loaded value.
  72. uint64_t __sanitizer_unaligned_load64(const void *p);
  73. /// Stores a 16-bit unaligned value.
  74. ///
  75. /// \param p Pointer to unaligned memory.
  76. /// \param x 16-bit value to store.
  77. void __sanitizer_unaligned_store16(void *p, uint16_t x);
  78. /// Stores a 32-bit unaligned value.
  79. ///
  80. /// \param p Pointer to unaligned memory.
  81. /// \param x 32-bit value to store.
  82. void __sanitizer_unaligned_store32(void *p, uint32_t x);
  83. /// Stores a 64-bit unaligned value.
  84. ///
  85. /// \param p Pointer to unaligned memory.
  86. /// \param x 64-bit value to store.
  87. void __sanitizer_unaligned_store64(void *p, uint64_t x);
  88. // Returns 1 on the first call, then returns 0 thereafter. Called by the tool
  89. // to ensure only one report is printed when multiple errors occur
  90. // simultaneously.
  91. int __sanitizer_acquire_crash_state();
  92. /// Annotates the current state of a contiguous container, such as
  93. /// <c>std::vector</c>, <c>std::string</c>, or similar.
  94. ///
  95. /// A contiguous container is a container that keeps all of its elements
  96. /// in a contiguous region of memory. The container owns the region of memory
  97. /// <c>[beg, end)</c>; the memory <c>[beg, mid)</c> is used to store the
  98. /// current elements, and the memory <c>[mid, end)</c> is reserved for future
  99. /// elements (<c>beg <= mid <= end</c>). For example, in
  100. /// <c>std::vector<> v</c>:
  101. ///
  102. /// \code
  103. /// beg = &v[0];
  104. /// end = beg + v.capacity() * sizeof(v[0]);
  105. /// mid = beg + v.size() * sizeof(v[0]);
  106. /// \endcode
  107. ///
  108. /// This annotation tells the Sanitizer tool about the current state of the
  109. /// container so that the tool can report errors when memory from
  110. /// <c>[mid, end)</c> is accessed. Insert this annotation into methods like
  111. /// <c>push_back()</c> or <c>pop_back()</c>. Supply the old and new values of
  112. /// <c>mid</c>(<c><i>old_mid</i></c> and <c><i>new_mid</i></c>). In the initial
  113. /// state <c>mid == end</c>, so that should be the final state when the
  114. /// container is destroyed or when the container reallocates the storage.
  115. ///
  116. /// For ASan, <c><i>beg</i></c> should be 8-aligned and <c><i>end</i></c>
  117. /// should be either 8-aligned or it should point to the end of a separate
  118. /// heap-, stack-, or global-allocated buffer. So the following example will
  119. /// not work:
  120. ///
  121. /// \code
  122. /// int64_t x[2]; // 16 bytes, 8-aligned
  123. /// char *beg = (char *)&x[0];
  124. /// char *end = beg + 12; // Not 8-aligned, not the end of the buffer
  125. /// \endcode
  126. ///
  127. /// The following, however, will work:
  128. /// \code
  129. /// int32_t x[3]; // 12 bytes, but 8-aligned under ASan.
  130. /// char *beg = (char*)&x[0];
  131. /// char *end = beg + 12; // Not 8-aligned, but is the end of the buffer
  132. /// \endcode
  133. ///
  134. /// \note Use this function with caution and do not use for anything other
  135. /// than vector-like classes.
  136. ///
  137. /// \param beg Beginning of memory region.
  138. /// \param end End of memory region.
  139. /// \param old_mid Old middle of memory region.
  140. /// \param new_mid New middle of memory region.
  141. void __sanitizer_annotate_contiguous_container(const void *beg,
  142. const void *end,
  143. const void *old_mid,
  144. const void *new_mid);
  145. /// Returns true if the contiguous container <c>[beg, end)</c> is properly
  146. /// poisoned.
  147. ///
  148. /// Proper poisoning could occur, for example, with
  149. /// <c>__sanitizer_annotate_contiguous_container</c>), that is, if
  150. /// <c>[beg, mid)</c> is addressable and <c>[mid, end)</c> is unaddressable.
  151. /// Full verification requires O (<c>end - beg</c>) time; this function tries
  152. /// to avoid such complexity by touching only parts of the container around
  153. /// <c><i>beg</i></c>, <c><i>mid</i></c>, and <c><i>end</i></c>.
  154. ///
  155. /// \param beg Beginning of memory region.
  156. /// \param mid Middle of memory region.
  157. /// \param end Old end of memory region.
  158. ///
  159. /// \returns True if the contiguous container <c>[beg, end)</c> is properly
  160. /// poisoned.
  161. int __sanitizer_verify_contiguous_container(const void *beg, const void *mid,
  162. const void *end);
  163. /// Similar to <c>__sanitizer_verify_contiguous_container()</c> but also
  164. /// returns the address of the first improperly poisoned byte.
  165. ///
  166. /// Returns NULL if the area is poisoned properly.
  167. ///
  168. /// \param beg Beginning of memory region.
  169. /// \param mid Middle of memory region.
  170. /// \param end Old end of memory region.
  171. ///
  172. /// \returns The bad address or NULL.
  173. const void *__sanitizer_contiguous_container_find_bad_address(const void *beg,
  174. const void *mid,
  175. const void *end);
  176. /// Prints the stack trace leading to this call (useful for calling from the
  177. /// debugger).
  178. void __sanitizer_print_stack_trace(void);
  179. // Symbolizes the supplied 'pc' using the format string 'fmt'.
  180. // Outputs at most 'out_buf_size' bytes into 'out_buf'.
  181. // If 'out_buf' is not empty then output is zero or more non empty C strings
  182. // followed by single empty C string. Multiple strings can be returned if PC
  183. // corresponds to inlined function. Inlined frames are printed in the order
  184. // from "most-inlined" to the "least-inlined", so the last frame should be the
  185. // not inlined function.
  186. // Inlined frames can be removed with 'symbolize_inline_frames=0'.
  187. // The format syntax is described in
  188. // lib/sanitizer_common/sanitizer_stacktrace_printer.h.
  189. void __sanitizer_symbolize_pc(void *pc, const char *fmt, char *out_buf,
  190. size_t out_buf_size);
  191. // Same as __sanitizer_symbolize_pc, but for data section (i.e. globals).
  192. void __sanitizer_symbolize_global(void *data_ptr, const char *fmt,
  193. char *out_buf, size_t out_buf_size);
  194. // Determine the return address.
  195. #if !defined(_MSC_VER) || defined(__clang__)
  196. #define __sanitizer_return_address() \
  197. __builtin_extract_return_addr(__builtin_return_address(0))
  198. #else
  199. extern "C" void *_ReturnAddress(void);
  200. #pragma intrinsic(_ReturnAddress)
  201. #define __sanitizer_return_address() _ReturnAddress()
  202. #endif
  203. /// Sets the callback to be called immediately before death on error.
  204. ///
  205. /// Passing 0 will unset the callback.
  206. ///
  207. /// \param callback User-provided callback.
  208. void __sanitizer_set_death_callback(void (*callback)(void));
  209. // Interceptor hooks.
  210. // Whenever a libc function interceptor is called, it checks if the
  211. // corresponding weak hook is defined, and calls it if it is indeed defined.
  212. // The primary use-case is data-flow-guided fuzzing, where the fuzzer needs
  213. // to know what is being passed to libc functions (for example memcmp).
  214. // FIXME: implement more hooks.
  215. /// Interceptor hook for <c>memcmp()</c>.
  216. ///
  217. /// \param called_pc PC (program counter) address of the original call.
  218. /// \param s1 Pointer to block of memory.
  219. /// \param s2 Pointer to block of memory.
  220. /// \param n Number of bytes to compare.
  221. /// \param result Value returned by the intercepted function.
  222. void __sanitizer_weak_hook_memcmp(void *called_pc, const void *s1,
  223. const void *s2, size_t n, int result);
  224. /// Interceptor hook for <c>strncmp()</c>.
  225. ///
  226. /// \param called_pc PC (program counter) address of the original call.
  227. /// \param s1 Pointer to block of memory.
  228. /// \param s2 Pointer to block of memory.
  229. /// \param n Number of bytes to compare.
  230. /// \param result Value returned by the intercepted function.
  231. void __sanitizer_weak_hook_strncmp(void *called_pc, const char *s1,
  232. const char *s2, size_t n, int result);
  233. /// Interceptor hook for <c>strncasecmp()</c>.
  234. ///
  235. /// \param called_pc PC (program counter) address of the original call.
  236. /// \param s1 Pointer to block of memory.
  237. /// \param s2 Pointer to block of memory.
  238. /// \param n Number of bytes to compare.
  239. /// \param result Value returned by the intercepted function.
  240. void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
  241. const char *s2, size_t n, int result);
  242. /// Interceptor hook for <c>strcmp()</c>.
  243. ///
  244. /// \param called_pc PC (program counter) address of the original call.
  245. /// \param s1 Pointer to block of memory.
  246. /// \param s2 Pointer to block of memory.
  247. /// \param result Value returned by the intercepted function.
  248. void __sanitizer_weak_hook_strcmp(void *called_pc, const char *s1,
  249. const char *s2, int result);
  250. /// Interceptor hook for <c>strcasecmp()</c>.
  251. ///
  252. /// \param called_pc PC (program counter) address of the original call.
  253. /// \param s1 Pointer to block of memory.
  254. /// \param s2 Pointer to block of memory.
  255. /// \param result Value returned by the intercepted function.
  256. void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
  257. const char *s2, int result);
  258. /// Interceptor hook for <c>strstr()</c>.
  259. ///
  260. /// \param called_pc PC (program counter) address of the original call.
  261. /// \param s1 Pointer to block of memory.
  262. /// \param s2 Pointer to block of memory.
  263. /// \param result Value returned by the intercepted function.
  264. void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
  265. const char *s2, char *result);
  266. void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
  267. const char *s2, char *result);
  268. void __sanitizer_weak_hook_memmem(void *called_pc,
  269. const void *s1, size_t len1,
  270. const void *s2, size_t len2, void *result);
  271. // Prints stack traces for all live heap allocations ordered by total
  272. // allocation size until top_percent of total live heap is shown. top_percent
  273. // should be between 1 and 100. At most max_number_of_contexts contexts
  274. // (stack traces) are printed.
  275. // Experimental feature currently available only with ASan on Linux/x86_64.
  276. void __sanitizer_print_memory_profile(size_t top_percent,
  277. size_t max_number_of_contexts);
  278. /// Notify ASan that a fiber switch has started (required only if implementing
  279. /// your own fiber library).
  280. ///
  281. /// Before switching to a different stack, you must call
  282. /// <c>__sanitizer_start_switch_fiber()</c> with a pointer to the bottom of the
  283. /// destination stack and with its size. When code starts running on the new
  284. /// stack, it must call <c>__sanitizer_finish_switch_fiber()</c> to finalize
  285. /// the switch. The <c>__sanitizer_start_switch_fiber()</c> function takes a
  286. /// <c>void**</c> pointer argument to store the current fake stack if there is
  287. /// one (it is necessary when the runtime option
  288. /// <c>detect_stack_use_after_return</c> is enabled).
  289. ///
  290. /// When restoring a stack, this <c>void**</c> pointer must be given to the
  291. /// <c>__sanitizer_finish_switch_fiber()</c> function. In most cases, this
  292. /// pointer can be stored on the stack immediately before switching. When
  293. /// leaving a fiber definitely, NULL must be passed as the first argument to
  294. /// the <c>__sanitizer_start_switch_fiber()</c> function so that the fake stack
  295. /// is destroyed. If your program does not need stack use-after-return
  296. /// detection, you can always pass NULL to these two functions.
  297. ///
  298. /// \note The fake stack mechanism is disabled during fiber switch, so if a
  299. /// signal callback runs during the switch, it will not benefit from stack
  300. /// use-after-return detection.
  301. ///
  302. /// \param[out] fake_stack_save Fake stack save location.
  303. /// \param bottom Bottom address of stack.
  304. /// \param size Size of stack in bytes.
  305. void __sanitizer_start_switch_fiber(void **fake_stack_save,
  306. const void *bottom, size_t size);
  307. /// Notify ASan that a fiber switch has completed (required only if
  308. /// implementing your own fiber library).
  309. ///
  310. /// When code starts running on the new stack, it must call
  311. /// <c>__sanitizer_finish_switch_fiber()</c> to finalize
  312. /// the switch. For usage details, see the description of
  313. /// <c>__sanitizer_start_switch_fiber()</c>.
  314. ///
  315. /// \param fake_stack_save Fake stack save location.
  316. /// \param[out] bottom_old Bottom address of old stack.
  317. /// \param[out] size_old Size of old stack in bytes.
  318. void __sanitizer_finish_switch_fiber(void *fake_stack_save,
  319. const void **bottom_old,
  320. size_t *size_old);
  321. // Get full module name and calculate pc offset within it.
  322. // Returns 1 if pc belongs to some module, 0 if module was not found.
  323. int __sanitizer_get_module_and_offset_for_pc(void *pc, char *module_path,
  324. size_t module_path_len,
  325. void **pc_offset);
  326. #ifdef __cplusplus
  327. } // extern "C"
  328. #endif
  329. #endif // SANITIZER_COMMON_INTERFACE_DEFS_H