common_interface_defs.h 20 KB

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