asan.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* AddressSanitizer, a fast memory error detector.
  2. Copyright (C) 2011-2020 Free Software Foundation, Inc.
  3. Contributed by Kostya Serebryany <kcc@google.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef TREE_ASAN
  17. #define TREE_ASAN
  18. extern void asan_function_start (void);
  19. extern void asan_finish_file (void);
  20. extern rtx_insn *asan_emit_stack_protection (rtx, rtx, unsigned int,
  21. HOST_WIDE_INT *, tree *, int);
  22. extern rtx_insn *asan_emit_allocas_unpoison (rtx, rtx, rtx_insn *);
  23. extern bool asan_protect_global (tree, bool ignore_decl_rtl_set_p = false);
  24. extern void initialize_sanitizer_builtins (void);
  25. extern tree asan_dynamic_init_call (bool);
  26. extern bool asan_expand_check_ifn (gimple_stmt_iterator *, bool);
  27. extern bool asan_expand_mark_ifn (gimple_stmt_iterator *);
  28. extern bool asan_expand_poison_ifn (gimple_stmt_iterator *, bool *,
  29. hash_map<tree, tree> &);
  30. extern gimple_stmt_iterator create_cond_insert_point
  31. (gimple_stmt_iterator *, bool, bool, bool, basic_block *, basic_block *);
  32. /* Alias set for accessing the shadow memory. */
  33. extern alias_set_type asan_shadow_set;
  34. /* Hash set of labels that are either used in a goto, or their address
  35. has been taken. */
  36. extern hash_set <tree> *asan_used_labels;
  37. /* Shadow memory is found at
  38. (address >> ASAN_SHADOW_SHIFT) + asan_shadow_offset (). */
  39. #define ASAN_SHADOW_SHIFT 3
  40. #define ASAN_SHADOW_GRANULARITY (1UL << ASAN_SHADOW_SHIFT)
  41. /* Red zone size, stack and global variables are padded by ASAN_RED_ZONE_SIZE
  42. up to 2 * ASAN_RED_ZONE_SIZE - 1 bytes. */
  43. #define ASAN_RED_ZONE_SIZE 32
  44. /* Stack variable use more compact red zones. The size includes also
  45. size of variable itself. */
  46. #define ASAN_MIN_RED_ZONE_SIZE 16
  47. /* Shadow memory values for stack protection. Left is below protected vars,
  48. the first pointer in stack corresponding to that offset contains
  49. ASAN_STACK_FRAME_MAGIC word, the second pointer to a string describing
  50. the frame. Middle is for padding in between variables, right is
  51. above the last protected variable and partial immediately after variables
  52. up to ASAN_RED_ZONE_SIZE alignment. */
  53. #define ASAN_STACK_MAGIC_LEFT 0xf1
  54. #define ASAN_STACK_MAGIC_MIDDLE 0xf2
  55. #define ASAN_STACK_MAGIC_RIGHT 0xf3
  56. #define ASAN_STACK_MAGIC_USE_AFTER_RET 0xf5
  57. #define ASAN_STACK_MAGIC_USE_AFTER_SCOPE 0xf8
  58. #define ASAN_STACK_FRAME_MAGIC 0x41b58ab3
  59. #define ASAN_STACK_RETIRED_MAGIC 0x45e0360e
  60. #define ASAN_USE_AFTER_SCOPE_ATTRIBUTE "use after scope memory"
  61. /* Various flags for Asan builtins. */
  62. enum asan_check_flags
  63. {
  64. ASAN_CHECK_STORE = 1 << 0,
  65. ASAN_CHECK_SCALAR_ACCESS = 1 << 1,
  66. ASAN_CHECK_NON_ZERO_LEN = 1 << 2,
  67. ASAN_CHECK_LAST = 1 << 3
  68. };
  69. /* Flags for Asan check builtins. */
  70. #define IFN_ASAN_MARK_FLAGS DEF(POISON), DEF(UNPOISON)
  71. enum asan_mark_flags
  72. {
  73. #define DEF(X) ASAN_MARK_##X
  74. IFN_ASAN_MARK_FLAGS
  75. #undef DEF
  76. };
  77. /* Return true if STMT is ASAN_MARK with FLAG as first argument. */
  78. extern bool asan_mark_p (gimple *stmt, enum asan_mark_flags flag);
  79. /* Return the size of padding needed to insert after a protected
  80. decl of SIZE. */
  81. static inline unsigned int
  82. asan_red_zone_size (unsigned int size)
  83. {
  84. unsigned int c = size & (ASAN_RED_ZONE_SIZE - 1);
  85. return c ? 2 * ASAN_RED_ZONE_SIZE - c : ASAN_RED_ZONE_SIZE;
  86. }
  87. /* Return how much a stack variable occupis on a stack
  88. including a space for red zone. */
  89. static inline unsigned HOST_WIDE_INT
  90. asan_var_and_redzone_size (unsigned HOST_WIDE_INT size)
  91. {
  92. if (size <= 4)
  93. return 16;
  94. else if (size <= 16)
  95. return 32;
  96. else if (size <= 128)
  97. return size + 32;
  98. else if (size <= 512)
  99. return size + 64;
  100. else if (size <= 4096)
  101. return size + 128;
  102. else
  103. return size + 256;
  104. }
  105. extern bool set_asan_shadow_offset (const char *);
  106. extern bool asan_shadow_offset_set_p ();
  107. extern void set_sanitized_sections (const char *);
  108. extern bool asan_sanitize_stack_p (void);
  109. extern bool asan_sanitize_allocas_p (void);
  110. extern hash_set<tree> *asan_handled_variables;
  111. /* Return TRUE if builtin with given FCODE will be intercepted by
  112. libasan. */
  113. static inline bool
  114. asan_intercepted_p (enum built_in_function fcode)
  115. {
  116. return fcode == BUILT_IN_INDEX
  117. || fcode == BUILT_IN_MEMCHR
  118. || fcode == BUILT_IN_MEMCMP
  119. || fcode == BUILT_IN_MEMCPY
  120. || fcode == BUILT_IN_MEMMOVE
  121. || fcode == BUILT_IN_MEMSET
  122. || fcode == BUILT_IN_STRCASECMP
  123. || fcode == BUILT_IN_STRCAT
  124. || fcode == BUILT_IN_STRCHR
  125. || fcode == BUILT_IN_STRCMP
  126. || fcode == BUILT_IN_STRCPY
  127. || fcode == BUILT_IN_STRDUP
  128. || fcode == BUILT_IN_STRLEN
  129. || fcode == BUILT_IN_STRNCASECMP
  130. || fcode == BUILT_IN_STRNCAT
  131. || fcode == BUILT_IN_STRNCMP
  132. || fcode == BUILT_IN_STRCSPN
  133. || fcode == BUILT_IN_STRPBRK
  134. || fcode == BUILT_IN_STRSPN
  135. || fcode == BUILT_IN_STRSTR
  136. || fcode == BUILT_IN_STRNCPY;
  137. }
  138. /* Return TRUE if we should instrument for use-after-scope sanity checking. */
  139. static inline bool
  140. asan_sanitize_use_after_scope (void)
  141. {
  142. return (flag_sanitize_address_use_after_scope && asan_sanitize_stack_p ());
  143. }
  144. /* Return true if DECL should be guarded on the stack. */
  145. static inline bool
  146. asan_protect_stack_decl (tree decl)
  147. {
  148. return DECL_P (decl)
  149. && (!DECL_ARTIFICIAL (decl)
  150. || (asan_sanitize_use_after_scope () && TREE_ADDRESSABLE (decl)));
  151. }
  152. /* Return true when flag_sanitize & FLAG is non-zero. If FN is non-null,
  153. remove all flags mentioned in "no_sanitize" of DECL_ATTRIBUTES. */
  154. static inline bool
  155. sanitize_flags_p (unsigned int flag, const_tree fn = current_function_decl)
  156. {
  157. unsigned int result_flags = flag_sanitize & flag;
  158. if (result_flags == 0)
  159. return false;
  160. if (fn != NULL_TREE)
  161. {
  162. tree value = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (fn));
  163. if (value)
  164. result_flags &= ~tree_to_uhwi (TREE_VALUE (value));
  165. }
  166. return result_flags;
  167. }
  168. #endif /* TREE_ASAN */