common_interface_defs.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //===-- sanitizer/common_interface_defs.h -----------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Common part of the public sanitizer interface.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_COMMON_INTERFACE_DEFS_H
  13. #define SANITIZER_COMMON_INTERFACE_DEFS_H
  14. #include <stddef.h>
  15. #include <stdint.h>
  16. // GCC does not understand __has_feature.
  17. #if !defined(__has_feature)
  18. # define __has_feature(x) 0
  19. #endif
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. // Arguments for __sanitizer_sandbox_on_notify() below.
  24. typedef struct {
  25. // Enable sandbox support in sanitizer coverage.
  26. int coverage_sandboxed;
  27. // File descriptor to write coverage data to. If -1 is passed, a file will
  28. // be pre-opened by __sanitizer_sandobx_on_notify(). This field has no
  29. // effect if coverage_sandboxed == 0.
  30. intptr_t coverage_fd;
  31. // If non-zero, split the coverage data into well-formed blocks. This is
  32. // useful when coverage_fd is a socket descriptor. Each block will contain
  33. // a header, allowing data from multiple processes to be sent over the same
  34. // socket.
  35. unsigned int coverage_max_block_size;
  36. } __sanitizer_sandbox_arguments;
  37. // Tell the tools to write their reports to "path.<pid>" instead of stderr.
  38. void __sanitizer_set_report_path(const char *path);
  39. // Tell the tools to write their reports to the provided file descriptor
  40. // (casted to void *).
  41. void __sanitizer_set_report_fd(void *fd);
  42. // Notify the tools that the sandbox is going to be turned on. The reserved
  43. // parameter will be used in the future to hold a structure with functions
  44. // that the tools may call to bypass the sandbox.
  45. void __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments *args);
  46. // This function is called by the tool when it has just finished reporting
  47. // an error. 'error_summary' is a one-line string that summarizes
  48. // the error message. This function can be overridden by the client.
  49. void __sanitizer_report_error_summary(const char *error_summary);
  50. // Some of the sanitizers (e.g. asan/tsan) may miss bugs that happen
  51. // in unaligned loads/stores. In order to find such bugs reliably one needs
  52. // to replace plain unaligned loads/stores with these calls.
  53. uint16_t __sanitizer_unaligned_load16(const void *p);
  54. uint32_t __sanitizer_unaligned_load32(const void *p);
  55. uint64_t __sanitizer_unaligned_load64(const void *p);
  56. void __sanitizer_unaligned_store16(void *p, uint16_t x);
  57. void __sanitizer_unaligned_store32(void *p, uint32_t x);
  58. void __sanitizer_unaligned_store64(void *p, uint64_t x);
  59. // Annotate the current state of a contiguous container, such as
  60. // std::vector, std::string or similar.
  61. // A contiguous container is a container that keeps all of its elements
  62. // in a contiguous region of memory. The container owns the region of memory
  63. // [beg, end); the memory [beg, mid) is used to store the current elements
  64. // and the memory [mid, end) is reserved for future elements;
  65. // beg <= mid <= end. For example, in "std::vector<> v"
  66. // beg = &v[0];
  67. // end = beg + v.capacity() * sizeof(v[0]);
  68. // mid = beg + v.size() * sizeof(v[0]);
  69. //
  70. // This annotation tells the Sanitizer tool about the current state of the
  71. // container so that the tool can report errors when memory from [mid, end)
  72. // is accessed. Insert this annotation into methods like push_back/pop_back.
  73. // Supply the old and the new values of mid (old_mid/new_mid).
  74. // In the initial state mid == end and so should be the final
  75. // state when the container is destroyed or when it reallocates the storage.
  76. //
  77. // Use with caution and don't use for anything other than vector-like classes.
  78. //
  79. // For AddressSanitizer, 'beg' should be 8-aligned and 'end' should
  80. // be either 8-aligned or it should point to the end of a separate heap-,
  81. // stack-, or global- allocated buffer. I.e. the following will not work:
  82. // int64_t x[2]; // 16 bytes, 8-aligned.
  83. // char *beg = (char *)&x[0];
  84. // char *end = beg + 12; // Not 8 aligned, not the end of the buffer.
  85. // This however will work fine:
  86. // int32_t x[3]; // 12 bytes, but 8-aligned under AddressSanitizer.
  87. // char *beg = (char*)&x[0];
  88. // char *end = beg + 12; // Not 8-aligned, but is the end of the buffer.
  89. void __sanitizer_annotate_contiguous_container(const void *beg,
  90. const void *end,
  91. const void *old_mid,
  92. const void *new_mid);
  93. // Returns true if the contiguous container [beg, end) is properly poisoned
  94. // (e.g. with __sanitizer_annotate_contiguous_container), i.e. if
  95. // - [beg, mid) is addressable,
  96. // - [mid, end) is unaddressable.
  97. // Full verification requires O(end-beg) time; this function tries to avoid
  98. // such complexity by touching only parts of the container around beg/mid/end.
  99. int __sanitizer_verify_contiguous_container(const void *beg, const void *mid,
  100. const void *end);
  101. // Similar to __sanitizer_verify_contiguous_container but returns the address
  102. // of the first improperly poisoned byte otherwise. Returns null if the area
  103. // is poisoned properly.
  104. const void *__sanitizer_contiguous_container_find_bad_address(
  105. const void *beg, const void *mid, const void *end);
  106. // Print the stack trace leading to this call. Useful for debugging user code.
  107. void __sanitizer_print_stack_trace(void);
  108. // Symbolizes the supplied 'pc' using the format string 'fmt'.
  109. // Outputs at most 'out_buf_size' bytes into 'out_buf'.
  110. // The format syntax is described in
  111. // lib/sanitizer_common/sanitizer_stacktrace_printer.h.
  112. void __sanitizer_symbolize_pc(void *pc, const char *fmt, char *out_buf,
  113. size_t out_buf_size);
  114. // Same as __sanitizer_symbolize_pc, but for data section (i.e. globals).
  115. void __sanitizer_symbolize_global(void *data_ptr, const char *fmt,
  116. char *out_buf, size_t out_buf_size);
  117. // Sets the callback to be called right before death on error.
  118. // Passing 0 will unset the callback.
  119. void __sanitizer_set_death_callback(void (*callback)(void));
  120. // Interceptor hooks.
  121. // Whenever a libc function interceptor is called it checks if the
  122. // corresponding weak hook is defined, and it so -- calls it.
  123. // The primary use case is data-flow-guided fuzzing, where the fuzzer needs
  124. // to know what is being passed to libc functions, e.g. memcmp.
  125. // FIXME: implement more hooks.
  126. void __sanitizer_weak_hook_memcmp(void *called_pc, const void *s1,
  127. const void *s2, size_t n, int result);
  128. void __sanitizer_weak_hook_strncmp(void *called_pc, const char *s1,
  129. const char *s2, size_t n, int result);
  130. void __sanitizer_weak_hook_strncasecmp(void *called_pc, const char *s1,
  131. const char *s2, size_t n, int result);
  132. void __sanitizer_weak_hook_strcmp(void *called_pc, const char *s1,
  133. const char *s2, int result);
  134. void __sanitizer_weak_hook_strcasecmp(void *called_pc, const char *s1,
  135. const char *s2, int result);
  136. void __sanitizer_weak_hook_strstr(void *called_pc, const char *s1,
  137. const char *s2, char *result);
  138. void __sanitizer_weak_hook_strcasestr(void *called_pc, const char *s1,
  139. const char *s2, char *result);
  140. void __sanitizer_weak_hook_memmem(void *called_pc,
  141. const void *s1, size_t len1,
  142. const void *s2, size_t len2, void *result);
  143. // Prints stack traces for all live heap allocations ordered by total
  144. // allocation size until `top_percent` of total live heap is shown.
  145. // `top_percent` should be between 1 and 100.
  146. // At most `max_number_of_contexts` contexts (stack traces) is printed.
  147. // Experimental feature currently available only with asan on Linux/x86_64.
  148. void __sanitizer_print_memory_profile(size_t top_percent,
  149. size_t max_number_of_contexts);
  150. // Fiber annotation interface.
  151. // Before switching to a different stack, one must call
  152. // __sanitizer_start_switch_fiber with a pointer to the bottom of the
  153. // destination stack and its size. When code starts running on the new stack,
  154. // it must call __sanitizer_finish_switch_fiber to finalize the switch.
  155. // The start_switch function takes a void** to store the current fake stack if
  156. // there is one (it is needed when detect_stack_use_after_return is enabled).
  157. // When restoring a stack, this pointer must be given to the finish_switch
  158. // function. In most cases, this void* can be stored on the stack just before
  159. // switching. When leaving a fiber definitely, null must be passed as first
  160. // argument to the start_switch function so that the fake stack is destroyed.
  161. // If you do not want support for stack use-after-return detection, you can
  162. // always pass null to these two functions.
  163. // Note that the fake stack mechanism is disabled during fiber switch, so if a
  164. // signal callback runs during the switch, it will not benefit from the stack
  165. // use-after-return detection.
  166. void __sanitizer_start_switch_fiber(void **fake_stack_save,
  167. const void *bottom, size_t size);
  168. void __sanitizer_finish_switch_fiber(void *fake_stack_save,
  169. const void **bottom_old,
  170. size_t *size_old);
  171. // Get full module name and calculate pc offset within it.
  172. // Returns 1 if pc belongs to some module, 0 if module was not found.
  173. int __sanitizer_get_module_and_offset_for_pc(void *pc, char *module_path,
  174. size_t module_path_len,
  175. void **pc_offset);
  176. #ifdef __cplusplus
  177. } // extern "C"
  178. #endif
  179. #endif // SANITIZER_COMMON_INTERFACE_DEFS_H