asan.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* AddressSanitizer, a fast memory error detector.
  2. Copyright (C) 2011-2015 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 bool asan_protect_global (tree);
  23. extern void initialize_sanitizer_builtins (void);
  24. extern tree asan_dynamic_init_call (bool);
  25. extern bool asan_expand_check_ifn (gimple_stmt_iterator *, bool);
  26. extern gimple_stmt_iterator create_cond_insert_point
  27. (gimple_stmt_iterator *, bool, bool, bool, basic_block *, basic_block *);
  28. /* Alias set for accessing the shadow memory. */
  29. extern alias_set_type asan_shadow_set;
  30. /* Shadow memory is found at
  31. (address >> ASAN_SHADOW_SHIFT) + asan_shadow_offset (). */
  32. #define ASAN_SHADOW_SHIFT 3
  33. /* Red zone size, stack and global variables are padded by ASAN_RED_ZONE_SIZE
  34. up to 2 * ASAN_RED_ZONE_SIZE - 1 bytes. */
  35. #define ASAN_RED_ZONE_SIZE 32
  36. /* Shadow memory values for stack protection. Left is below protected vars,
  37. the first pointer in stack corresponding to that offset contains
  38. ASAN_STACK_FRAME_MAGIC word, the second pointer to a string describing
  39. the frame. Middle is for padding in between variables, right is
  40. above the last protected variable and partial immediately after variables
  41. up to ASAN_RED_ZONE_SIZE alignment. */
  42. #define ASAN_STACK_MAGIC_LEFT 0xf1
  43. #define ASAN_STACK_MAGIC_MIDDLE 0xf2
  44. #define ASAN_STACK_MAGIC_RIGHT 0xf3
  45. #define ASAN_STACK_MAGIC_PARTIAL 0xf4
  46. #define ASAN_STACK_MAGIC_USE_AFTER_RET 0xf5
  47. #define ASAN_STACK_FRAME_MAGIC 0x41b58ab3
  48. #define ASAN_STACK_RETIRED_MAGIC 0x45e0360e
  49. /* Return true if DECL should be guarded on the stack. */
  50. static inline bool
  51. asan_protect_stack_decl (tree decl)
  52. {
  53. return DECL_P (decl) && !DECL_ARTIFICIAL (decl);
  54. }
  55. /* Return the size of padding needed to insert after a protected
  56. decl of SIZE. */
  57. static inline unsigned int
  58. asan_red_zone_size (unsigned int size)
  59. {
  60. unsigned int c = size & (ASAN_RED_ZONE_SIZE - 1);
  61. return c ? 2 * ASAN_RED_ZONE_SIZE - c : ASAN_RED_ZONE_SIZE;
  62. }
  63. extern bool set_asan_shadow_offset (const char *);
  64. /* Return TRUE if builtin with given FCODE will be intercepted by
  65. libasan. */
  66. static inline bool
  67. asan_intercepted_p (enum built_in_function fcode)
  68. {
  69. return fcode == BUILT_IN_INDEX
  70. || fcode == BUILT_IN_MEMCHR
  71. || fcode == BUILT_IN_MEMCMP
  72. || fcode == BUILT_IN_MEMCPY
  73. || fcode == BUILT_IN_MEMMOVE
  74. || fcode == BUILT_IN_MEMSET
  75. || fcode == BUILT_IN_STRCASECMP
  76. || fcode == BUILT_IN_STRCAT
  77. || fcode == BUILT_IN_STRCHR
  78. || fcode == BUILT_IN_STRCMP
  79. || fcode == BUILT_IN_STRCPY
  80. || fcode == BUILT_IN_STRDUP
  81. || fcode == BUILT_IN_STRLEN
  82. || fcode == BUILT_IN_STRNCASECMP
  83. || fcode == BUILT_IN_STRNCAT
  84. || fcode == BUILT_IN_STRNCMP
  85. || fcode == BUILT_IN_STRNCPY;
  86. }
  87. #endif /* TREE_ASAN */