(struct _IO_streambuf): Use correct type and name of VTable element.
[glibc/history.git] / sysdeps / generic / bp-checks.h
blobefbb8471627f3efccac8d4b3b1744f9f2c382f22
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>
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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef _bp_checks_h_
22 #define _bp_checks_h_ 1
24 #if __BOUNDED_POINTERS__
26 # define BOUNDS_VIOLATED (__builtin_trap (), 0)
28 /* Verify that pointer's value >= low. Return pointer value. */
29 # define CHECK_BOUNDS_LOW(ARG) \
30 (((__ptrvalue (ARG) < __ptrlow (ARG)) && BOUNDS_VIOLATED), \
31 __ptrvalue (ARG))
33 /* Verify that pointer's value < high. Return pointer value. */
34 # define CHECK_BOUNDS_HIGH(ARG) \
35 (((__ptrvalue (ARG) > __ptrhigh (ARG)) && BOUNDS_VIOLATED), \
36 __ptrvalue (ARG))
38 # define _CHECK_N(ARG, N, COND) \
39 (((COND) \
40 && (__ptrvalue (ARG) < __ptrlow (ARG) \
41 || __ptrvalue (ARG) + (N) > __ptrhigh (ARG)) \
42 && BOUNDS_VIOLATED), \
43 __ptrvalue (ARG))
45 extern void *__unbounded __ubp_memchr (const void *__unbounded, int, unsigned);
47 # define _CHECK_STRING(ARG, COND) \
48 (((COND) \
49 && (__ptrvalue (ARG) < __ptrlow (ARG) \
50 || !__ubp_memchr (__ptrvalue (ARG), '\0', \
51 (__ptrhigh (ARG) - __ptrvalue (ARG)))) \
52 && BOUNDS_VIOLATED), \
53 __ptrvalue (ARG))
55 /* Check bounds of a pointer seated to an array of N objects. */
56 # define CHECK_N(ARG, N) _CHECK_N ((ARG), (N), 1)
57 /* Same as CHECK_N, but tolerate ARG == NULL. */
58 # define CHECK_N_NULL_OK(ARG, N) _CHECK_N ((ARG), (N), __ptrvalue (ARG))
60 /* Check bounds of a pointer seated to a single object. */
61 # define CHECK_1(ARG) CHECK_N ((ARG), 1)
62 /* Same as CHECK_1, but tolerate ARG == NULL. */
63 # define CHECK_1_NULL_OK(ARG) CHECK_N_NULL_OK ((ARG), 1)
65 /* Check for NUL-terminator within string's bounds. */
66 # define CHECK_STRING(ARG) _CHECK_STRING ((ARG), 1)
67 /* Same as CHECK_STRING, but tolerate ARG == NULL. */
68 # define CHECK_STRING_NULL_OK(ARG) _CHECK_STRING ((ARG), __ptrvalue (ARG))
70 /* Check bounds of signal syscall args with type sigset_t. */
71 # define CHECK_SIGSET(SET) CHECK_N ((SET), _NSIG / (8 * sizeof *(SET)))
72 /* Same as CHECK_SIGSET, but tolerate SET == NULL. */
73 # define CHECK_SIGSET_NULL_OK(SET) CHECK_N_NULL_OK ((SET), _NSIG / (8 * sizeof *(SET)))
75 # if defined (_IOC_SIZESHIFT) && defined (_IOC_SIZEBITS)
76 /* Extract the size of the ioctl data and check its bounds. */
77 # define CHECK_IOCTL(ARG, CMD) \
78 CHECK_N ((const char *) (ARG), \
79 (((CMD) >> _IOC_SIZESHIFT) & ((1 << _IOC_SIZEBITS) - 1)))
80 # else
81 /* We don't know the size of the ioctl data, so the best we can do
82 is check that the first byte is within bounds. */
83 # define CHECK_IOCTL(ARG, CMD) CHECK_1 ((const char *) ARG)
84 # endif
86 /* Check bounds of `struct flock *' for the locking fcntl commands. */
87 # define CHECK_FCNTL(ARG, CMD) \
88 (((CMD) == F_GETLK || (CMD) == F_SETLK || (CMD) == F_SETLKW) \
89 ? CHECK_1 ((struct flock *) ARG) : (unsigned long) (ARG))
91 /* Check bounds of an array of mincore residency-status flags that
92 cover a region of NBYTES. Such a vector occupies one byte per page
93 of memory. */
94 # define CHECK_N_PAGES(ARG, NBYTES) \
95 ({ int _page_size_ = sysconf (_SC_PAGE_SIZE); \
96 CHECK_N ((const char *) (ARG), \
97 ((NBYTES) + _page_size_ - 1) / _page_size_); })
99 /* Return a bounded pointer with value PTR that satisfies CHECK_N (PTR, N). */
100 # define BOUNDED_N(PTR, N) \
101 ({ __typeof (PTR) __bounded _p_; \
102 __ptrvalue _p_ = __ptrlow _p_ = __ptrvalue (PTR); \
103 __ptrhigh _p_ = __ptrvalue _p_ + (N); \
104 _p_; })
106 #else /* !__BOUNDED_POINTERS__ */
108 /* Do nothing if not compiling with -fbounded-pointers. */
110 # define BOUNDS_VIOLATED
111 # define CHECK_BOUNDS_LOW(ARG) (ARG)
112 # define CHECK_BOUNDS_HIGH(ARG) (ARG)
113 # define CHECK_1(ARG) (ARG)
114 # define CHECK_1_NULL_OK(ARG) (ARG)
115 # define CHECK_N(ARG, N) (ARG)
116 # define CHECK_N_NULL_OK(ARG, N) (ARG)
117 # define CHECK_STRING(ARG) (ARG)
118 # define CHECK_SIGSET(SET) (SET)
119 # define CHECK_SIGSET_NULL_OK(SET) (SET)
120 # define CHECK_IOCTL(ARG, CMD) (ARG)
121 # define CHECK_FCNTL(ARG, CMD) (ARG)
122 # define CHECK_N_PAGES(ARG, NBYTES) (ARG)
123 # define BOUNDED_N(PTR, N) (PTR)
125 #endif /* !__BOUNDED_POINTERS__ */
127 #define BOUNDED_1(PTR) BOUNDED_N (PTR, 1)
129 #endif /* _bp_checks_h_ */