asan.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* AddressSanitizer, a fast memory error detector.
  2. Copyright (C) 2011-2018 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. /* Shadow memory values for stack protection. Left is below protected vars,
  45. the first pointer in stack corresponding to that offset contains
  46. ASAN_STACK_FRAME_MAGIC word, the second pointer to a string describing
  47. the frame. Middle is for padding in between variables, right is
  48. above the last protected variable and partial immediately after variables
  49. up to ASAN_RED_ZONE_SIZE alignment. */
  50. #define ASAN_STACK_MAGIC_LEFT 0xf1
  51. #define ASAN_STACK_MAGIC_MIDDLE 0xf2
  52. #define ASAN_STACK_MAGIC_RIGHT 0xf3
  53. #define ASAN_STACK_MAGIC_USE_AFTER_RET 0xf5
  54. #define ASAN_STACK_MAGIC_USE_AFTER_SCOPE 0xf8
  55. #define ASAN_STACK_FRAME_MAGIC 0x41b58ab3
  56. #define ASAN_STACK_RETIRED_MAGIC 0x45e0360e
  57. #define ASAN_USE_AFTER_SCOPE_ATTRIBUTE "use after scope memory"
  58. /* Various flags for Asan builtins. */
  59. enum asan_check_flags
  60. {
  61. ASAN_CHECK_STORE = 1 << 0,
  62. ASAN_CHECK_SCALAR_ACCESS = 1 << 1,
  63. ASAN_CHECK_NON_ZERO_LEN = 1 << 2,
  64. ASAN_CHECK_LAST = 1 << 3
  65. };
  66. /* Flags for Asan check builtins. */
  67. #define IFN_ASAN_MARK_FLAGS DEF(POISON), DEF(UNPOISON)
  68. enum asan_mark_flags
  69. {
  70. #define DEF(X) ASAN_MARK_##X
  71. IFN_ASAN_MARK_FLAGS
  72. #undef DEF
  73. };
  74. /* Return true if STMT is ASAN_MARK with FLAG as first argument. */
  75. extern bool asan_mark_p (gimple *stmt, enum asan_mark_flags flag);
  76. /* Return the size of padding needed to insert after a protected
  77. decl of SIZE. */
  78. static inline unsigned int
  79. asan_red_zone_size (unsigned int size)
  80. {
  81. unsigned int c = size & (ASAN_RED_ZONE_SIZE - 1);
  82. return c ? 2 * ASAN_RED_ZONE_SIZE - c : ASAN_RED_ZONE_SIZE;
  83. }
  84. extern bool set_asan_shadow_offset (const char *);
  85. extern void set_sanitized_sections (const char *);
  86. extern bool asan_sanitize_stack_p (void);
  87. extern bool asan_sanitize_allocas_p (void);
  88. extern hash_set<tree> *asan_handled_variables;
  89. /* Return TRUE if builtin with given FCODE will be intercepted by
  90. libasan. */
  91. static inline bool
  92. asan_intercepted_p (enum built_in_function fcode)
  93. {
  94. return fcode == BUILT_IN_INDEX
  95. || fcode == BUILT_IN_MEMCHR
  96. || fcode == BUILT_IN_MEMCMP
  97. || fcode == BUILT_IN_MEMCPY
  98. || fcode == BUILT_IN_MEMMOVE
  99. || fcode == BUILT_IN_MEMSET
  100. || fcode == BUILT_IN_STRCASECMP
  101. || fcode == BUILT_IN_STRCAT
  102. || fcode == BUILT_IN_STRCHR
  103. || fcode == BUILT_IN_STRCMP
  104. || fcode == BUILT_IN_STRCPY
  105. || fcode == BUILT_IN_STRDUP
  106. || fcode == BUILT_IN_STRLEN
  107. || fcode == BUILT_IN_STRNCASECMP
  108. || fcode == BUILT_IN_STRNCAT
  109. || fcode == BUILT_IN_STRNCMP
  110. || fcode == BUILT_IN_STRCSPN
  111. || fcode == BUILT_IN_STRPBRK
  112. || fcode == BUILT_IN_STRSPN
  113. || fcode == BUILT_IN_STRSTR
  114. || fcode == BUILT_IN_STRNCPY;
  115. }
  116. /* Return TRUE if we should instrument for use-after-scope sanity checking. */
  117. static inline bool
  118. asan_sanitize_use_after_scope (void)
  119. {
  120. return (flag_sanitize_address_use_after_scope && asan_sanitize_stack_p ());
  121. }
  122. /* Return true if DECL should be guarded on the stack. */
  123. static inline bool
  124. asan_protect_stack_decl (tree decl)
  125. {
  126. return DECL_P (decl)
  127. && (!DECL_ARTIFICIAL (decl)
  128. || (asan_sanitize_use_after_scope () && TREE_ADDRESSABLE (decl)));
  129. }
  130. /* Return true when flag_sanitize & FLAG is non-zero. If FN is non-null,
  131. remove all flags mentioned in "no_sanitize" of DECL_ATTRIBUTES. */
  132. static inline bool
  133. sanitize_flags_p (unsigned int flag, const_tree fn = current_function_decl)
  134. {
  135. unsigned int result_flags = flag_sanitize & flag;
  136. if (result_flags == 0)
  137. return false;
  138. if (fn != NULL_TREE)
  139. {
  140. tree value = lookup_attribute ("no_sanitize", DECL_ATTRIBUTES (fn));
  141. if (value)
  142. result_flags &= ~tree_to_uhwi (TREE_VALUE (value));
  143. }
  144. return result_flags;
  145. }
  146. #endif /* TREE_ASAN */