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