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