Back to index

glibc  2.9
bp-checks.h
Go to the documentation of this file.
00001 /* Bounded-pointer checking macros for C.
00002    Copyright (C) 2000 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Greg McGary <greg@mcgary.org>
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef _bp_checks_h_
00022 #define _bp_checks_h_ 1
00023 
00024 #if __BOUNDED_POINTERS__
00025 
00026 # define BOUNDS_VIOLATED (__builtin_trap (), 0)
00027 
00028 /* Verify that pointer's value >= low.  Return pointer value.  */
00029 # define CHECK_BOUNDS_LOW(ARG)                                 \
00030   (((__ptrvalue (ARG) < __ptrlow (ARG)) && BOUNDS_VIOLATED),   \
00031    __ptrvalue (ARG))
00032 
00033 /* Verify that pointer's value < high.  Return pointer value.  */
00034 # define CHECK_BOUNDS_HIGH(ARG)                         \
00035   (((__ptrvalue (ARG) > __ptrhigh (ARG)) && BOUNDS_VIOLATED),  \
00036    __ptrvalue (ARG))
00037 
00038 # define _CHECK_N(ARG, N, COND)                         \
00039   (((COND)                                       \
00040     && (__ptrvalue (ARG) < __ptrlow (ARG)        \
00041        || __ptrvalue (ARG) + (N) > __ptrhigh (ARG))     \
00042     && BOUNDS_VIOLATED),                         \
00043    __ptrvalue (ARG))
00044 
00045 extern void *__unbounded __ubp_memchr (const void *__unbounded, int, unsigned);
00046 
00047 # define _CHECK_STRING(ARG, COND)                       \
00048   (((COND)                                              \
00049     && (__ptrvalue (ARG) < __ptrlow (ARG)               \
00050        || !__ubp_memchr (__ptrvalue (ARG), '\0',               \
00051                     (__ptrhigh (ARG) - __ptrvalue (ARG))))     \
00052     && BOUNDS_VIOLATED),                                \
00053    __ptrvalue (ARG))
00054 
00055 /* Check bounds of a pointer seated to an array of N objects.  */
00056 # define CHECK_N(ARG, N) _CHECK_N ((ARG), (N), 1)
00057 /* Same as CHECK_N, but tolerate ARG == NULL.  */
00058 # define CHECK_N_NULL_OK(ARG, N) _CHECK_N ((ARG), (N), __ptrvalue (ARG))
00059 
00060 /* Check bounds of a pointer seated to a single object.  */
00061 # define CHECK_1(ARG) CHECK_N ((ARG), 1)
00062 /* Same as CHECK_1, but tolerate ARG == NULL.  */
00063 # define CHECK_1_NULL_OK(ARG) CHECK_N_NULL_OK ((ARG), 1)
00064 
00065 /* Check for NUL-terminator within string's bounds.  */
00066 # define CHECK_STRING(ARG) _CHECK_STRING ((ARG), 1)
00067 /* Same as CHECK_STRING, but tolerate ARG == NULL.  */
00068 # define CHECK_STRING_NULL_OK(ARG) _CHECK_STRING ((ARG), __ptrvalue (ARG))
00069 
00070 /* Check bounds of signal syscall args with type sigset_t.  */
00071 # define CHECK_SIGSET(SET) CHECK_N ((SET), _NSIG / (8 * sizeof *(SET)))
00072 /* Same as CHECK_SIGSET, but tolerate SET == NULL.  */
00073 # define CHECK_SIGSET_NULL_OK(SET) CHECK_N_NULL_OK ((SET), _NSIG / (8 * sizeof *(SET)))
00074 
00075 # if defined (_IOC_SIZESHIFT) && defined (_IOC_SIZEBITS)
00076 /* Extract the size of the ioctl data and check its bounds.  */
00077 #  define CHECK_IOCTL(ARG, CMD)                                       \
00078   CHECK_N ((const char *) (ARG),                               \
00079           (((CMD) >> _IOC_SIZESHIFT) & ((1 << _IOC_SIZEBITS) - 1)))
00080 # else
00081 /* We don't know the size of the ioctl data, so the best we can do
00082    is check that the first byte is within bounds.  */
00083 #  define CHECK_IOCTL(ARG, CMD) CHECK_1 ((const char *) ARG)
00084 # endif
00085 
00086 /* Check bounds of `struct flock *' for the locking fcntl commands.  */
00087 # define CHECK_FCNTL(ARG, CMD)                                 \
00088   (((CMD) == F_GETLK || (CMD) == F_SETLK || (CMD) == F_SETLKW) \
00089    ? CHECK_1 ((struct flock *) ARG) : (unsigned long) (ARG))
00090 
00091 /* Check bounds of an array of mincore residency-status flags that
00092    cover a region of NBYTES.  Such a vector occupies one byte per page
00093    of memory.  */
00094 # define CHECK_N_PAGES(ARG, NBYTES)                            \
00095   ({ int _page_size_ = sysconf (_SC_PAGE_SIZE);                \
00096      CHECK_N ((const char *) (ARG),                            \
00097              ((NBYTES) + _page_size_ - 1) / _page_size_); })
00098 
00099 /* Return a bounded pointer with value PTR that satisfies CHECK_N (PTR, N).  */
00100 # define BOUNDED_N(PTR, N)                       \
00101   ({ __typeof (PTR) __bounded _p_;               \
00102      __ptrvalue _p_ = __ptrlow _p_ = __ptrvalue (PTR);  \
00103      __ptrhigh _p_ = __ptrvalue _p_ + (N);              \
00104      _p_; })
00105 
00106 #else /* !__BOUNDED_POINTERS__ */
00107 
00108 /* Do nothing if not compiling with -fbounded-pointers.  */
00109 
00110 # define BOUNDS_VIOLATED
00111 # define CHECK_BOUNDS_LOW(ARG) (ARG)
00112 # define CHECK_BOUNDS_HIGH(ARG) (ARG)
00113 # define CHECK_1(ARG) (ARG)
00114 # define CHECK_1_NULL_OK(ARG) (ARG)
00115 # define CHECK_N(ARG, N) (ARG)
00116 # define CHECK_N_NULL_OK(ARG, N) (ARG)
00117 # define CHECK_STRING(ARG) (ARG)
00118 # define CHECK_SIGSET(SET) (SET)
00119 # define CHECK_SIGSET_NULL_OK(SET) (SET)
00120 # define CHECK_IOCTL(ARG, CMD) (ARG)
00121 # define CHECK_FCNTL(ARG, CMD) (ARG)
00122 # define CHECK_N_PAGES(ARG, NBYTES) (ARG)
00123 # define BOUNDED_N(PTR, N) (PTR)
00124 
00125 #endif /* !__BOUNDED_POINTERS__ */
00126 
00127 #define BOUNDED_1(PTR) BOUNDED_N (PTR, 1)
00128 
00129 #endif /* _bp_checks_h_ */