bp-checks.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Bounded-pointer checking macros for C.
  2. Copyright (C) 2000 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Greg McGary <greg@mcgary.org>
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #ifndef _bp_checks_h_
  18. #define _bp_checks_h_ 1
  19. #if __BOUNDED_POINTERS__
  20. # define BOUNDS_VIOLATED (__builtin_trap (), 0)
  21. /* Verify that pointer's value >= low. Return pointer value. */
  22. # define CHECK_BOUNDS_LOW(ARG) \
  23. (((__ptrvalue (ARG) < __ptrlow (ARG)) && BOUNDS_VIOLATED), \
  24. __ptrvalue (ARG))
  25. /* Verify that pointer's value < high. Return pointer value. */
  26. # define CHECK_BOUNDS_HIGH(ARG) \
  27. (((__ptrvalue (ARG) > __ptrhigh (ARG)) && BOUNDS_VIOLATED), \
  28. __ptrvalue (ARG))
  29. # define _CHECK_N(ARG, N, COND) \
  30. (((COND) \
  31. && (__ptrvalue (ARG) < __ptrlow (ARG) \
  32. || __ptrvalue (ARG) + (N) > __ptrhigh (ARG)) \
  33. && BOUNDS_VIOLATED), \
  34. __ptrvalue (ARG))
  35. extern void *__unbounded __ubp_memchr (const void *__unbounded, int, unsigned);
  36. # define _CHECK_STRING(ARG, COND) \
  37. (((COND) \
  38. && (__ptrvalue (ARG) < __ptrlow (ARG) \
  39. || !__ubp_memchr (__ptrvalue (ARG), '\0', \
  40. (__ptrhigh (ARG) - __ptrvalue (ARG)))) \
  41. && BOUNDS_VIOLATED), \
  42. __ptrvalue (ARG))
  43. /* Check bounds of a pointer seated to an array of N objects. */
  44. # define CHECK_N(ARG, N) _CHECK_N ((ARG), (N), 1)
  45. /* Same as CHECK_N, but tolerate ARG == NULL. */
  46. # define CHECK_N_NULL_OK(ARG, N) _CHECK_N ((ARG), (N), __ptrvalue (ARG))
  47. /* Check bounds of a pointer seated to a single object. */
  48. # define CHECK_1(ARG) CHECK_N ((ARG), 1)
  49. /* Same as CHECK_1, but tolerate ARG == NULL. */
  50. # define CHECK_1_NULL_OK(ARG) CHECK_N_NULL_OK ((ARG), 1)
  51. /* Check for NUL-terminator within string's bounds. */
  52. # define CHECK_STRING(ARG) _CHECK_STRING ((ARG), 1)
  53. /* Same as CHECK_STRING, but tolerate ARG == NULL. */
  54. # define CHECK_STRING_NULL_OK(ARG) _CHECK_STRING ((ARG), __ptrvalue (ARG))
  55. /* Check bounds of signal syscall args with type sigset_t. */
  56. # define CHECK_SIGSET(SET) CHECK_N ((SET), _NSIG / (8 * sizeof *(SET)))
  57. /* Same as CHECK_SIGSET, but tolerate SET == NULL. */
  58. # define CHECK_SIGSET_NULL_OK(SET) CHECK_N_NULL_OK ((SET), _NSIG / (8 * sizeof *(SET)))
  59. # if defined (_IOC_SIZESHIFT) && defined (_IOC_SIZEBITS)
  60. /* Extract the size of the ioctl data and check its bounds. */
  61. # define CHECK_IOCTL(ARG, CMD) \
  62. CHECK_N ((const char *) (ARG), \
  63. (((CMD) >> _IOC_SIZESHIFT) & ((1 << _IOC_SIZEBITS) - 1)))
  64. # else
  65. /* We don't know the size of the ioctl data, so the best we can do
  66. is check that the first byte is within bounds. */
  67. # define CHECK_IOCTL(ARG, CMD) CHECK_1 ((const char *) ARG)
  68. # endif
  69. /* Check bounds of `struct flock *' for the locking fcntl commands. */
  70. # define CHECK_FCNTL(ARG, CMD) \
  71. (((CMD) == F_GETLK || (CMD) == F_SETLK || (CMD) == F_SETLKW) \
  72. ? CHECK_1 ((struct flock *) ARG) : (unsigned long) (ARG))
  73. /* Check bounds of an array of mincore residency-status flags that
  74. cover a region of NBYTES. Such a vector occupies one byte per page
  75. of memory. */
  76. # define CHECK_N_PAGES(ARG, NBYTES) \
  77. ({ int _page_size_ = sysconf (_SC_PAGE_SIZE); \
  78. CHECK_N ((const char *) (ARG), \
  79. ((NBYTES) + _page_size_ - 1) / _page_size_); })
  80. /* Return a bounded pointer with value PTR that satisfies CHECK_N (PTR, N). */
  81. # define BOUNDED_N(PTR, N) \
  82. ({ __typeof (PTR) __bounded _p_; \
  83. __ptrvalue _p_ = __ptrlow _p_ = __ptrvalue (PTR); \
  84. __ptrhigh _p_ = __ptrvalue _p_ + (N); \
  85. _p_; })
  86. #else /* !__BOUNDED_POINTERS__ */
  87. /* Do nothing if not compiling with -fbounded-pointers. */
  88. # define BOUNDS_VIOLATED
  89. # define CHECK_BOUNDS_LOW(ARG) (ARG)
  90. # define CHECK_BOUNDS_HIGH(ARG) (ARG)
  91. # define CHECK_1(ARG) (ARG)
  92. # define CHECK_1_NULL_OK(ARG) (ARG)
  93. # define CHECK_N(ARG, N) (ARG)
  94. # define CHECK_N_NULL_OK(ARG, N) (ARG)
  95. # define CHECK_STRING(ARG) (ARG)
  96. # define CHECK_SIGSET(SET) (SET)
  97. # define CHECK_SIGSET_NULL_OK(SET) (SET)
  98. # define CHECK_IOCTL(ARG, CMD) (ARG)
  99. # define CHECK_FCNTL(ARG, CMD) (ARG)
  100. # define CHECK_N_PAGES(ARG, NBYTES) (ARG)
  101. # define BOUNDED_N(PTR, N) (PTR)
  102. #endif /* !__BOUNDED_POINTERS__ */
  103. #define BOUNDED_1(PTR) BOUNDED_N (PTR, 1)
  104. #endif /* _bp_checks_h_ */