]> sourceware.org Git - glibc.git/blame - sysdeps/generic/bp-checks.h
Update to LGPL v2.1.
[glibc.git] / sysdeps / generic / bp-checks.h
CommitLineData
34173b51
GM
1/* Bounded-pointer checking macros for C.
2 Copyright (C) 2000 Free Software Foundation, Inc.
7ca5d0dc 3 This file is part of the GNU C Library.
34173b51
GM
4 Contributed by Greg McGary <greg@mcgary.org>
5
34173b51 6 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
34173b51
GM
10
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
41bdb6e2 14 Lesser General Public License for more details.
34173b51 15
41bdb6e2
AJ
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. */
34173b51
GM
20
21#ifndef _bp_checks_h_
abf70633 22#define _bp_checks_h_ 1
34173b51 23
abf70633 24#if __BOUNDED_POINTERS__
34173b51 25
abf70633 26# define BOUNDS_VIOLATED (__builtin_trap (), 0)
34173b51
GM
27
28/* Verify that pointer's value >= low. Return pointer value. */
abf70633 29# define CHECK_BOUNDS_LOW(ARG) \
34173b51
GM
30 (((__ptrvalue (ARG) < __ptrlow (ARG)) && BOUNDS_VIOLATED), \
31 __ptrvalue (ARG))
32
33/* Verify that pointer's value < high. Return pointer value. */
abf70633 34# define CHECK_BOUNDS_HIGH(ARG) \
34173b51
GM
35 (((__ptrvalue (ARG) > __ptrhigh (ARG)) && BOUNDS_VIOLATED), \
36 __ptrvalue (ARG))
37
abf70633
GM
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))
34173b51 44
2ed5fd9a
GM
45extern void *__unbounded __ubp_memchr (const void *__unbounded, int, unsigned);
46
abf70633
GM
47# define _CHECK_STRING(ARG, COND) \
48 (((COND) \
49 && (__ptrvalue (ARG) < __ptrlow (ARG) \
2ed5fd9a 50 || !__ubp_memchr (__ptrvalue (ARG), '\0', \
abf70633
GM
51 (__ptrhigh (ARG) - __ptrvalue (ARG)))) \
52 && BOUNDS_VIOLATED), \
53 __ptrvalue (ARG))
34173b51
GM
54
55/* Check bounds of a pointer seated to an array of N objects. */
abf70633 56# define CHECK_N(ARG, N) _CHECK_N ((ARG), (N), 1)
34173b51 57/* Same as CHECK_N, but tolerate ARG == NULL. */
e64911d1 58# define CHECK_N_NULL_OK(ARG, N) _CHECK_N ((ARG), (N), __ptrvalue (ARG))
34173b51 59
abf70633
GM
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. */
e64911d1 63# define CHECK_1_NULL_OK(ARG) CHECK_N_NULL_OK ((ARG), 1)
4bbb61e4 64
abf70633
GM
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. */
e64911d1 68# define CHECK_STRING_NULL_OK(ARG) _CHECK_STRING ((ARG), __ptrvalue (ARG))
abf70633
GM
69
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. */
e64911d1 73# define CHECK_SIGSET_NULL_OK(SET) CHECK_N_NULL_OK ((SET), _NSIG / (8 * sizeof *(SET)))
abf70633
GM
74
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
85
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))
90
e694422d
GM
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_); })
98
abf70633
GM
99/* Return a bounded pointer with value PTR that satisfies CHECK_N (PTR, N). */
100# define BOUNDED_N(PTR, N) \
da8f38c5 101 ({ __typeof (PTR) __bounded _p_; \
abf70633
GM
102 __ptrvalue _p_ = __ptrlow _p_ = __ptrvalue (PTR); \
103 __ptrhigh _p_ = __ptrvalue _p_ + (N); \
104 _p_; })
105
106#else /* !__BOUNDED_POINTERS__ */
34173b51
GM
107
108/* Do nothing if not compiling with -fbounded-pointers. */
109
abf70633
GM
110# define BOUNDS_VIOLATED
111# define CHECK_BOUNDS_LOW(ARG) (ARG)
112# define CHECK_BOUNDS_HIGH(ARG) (ARG)
113# define CHECK_1(ARG) (ARG)
e64911d1 114# define CHECK_1_NULL_OK(ARG) (ARG)
abf70633 115# define CHECK_N(ARG, N) (ARG)
e64911d1 116# define CHECK_N_NULL_OK(ARG, N) (ARG)
abf70633
GM
117# define CHECK_STRING(ARG) (ARG)
118# define CHECK_SIGSET(SET) (SET)
e64911d1 119# define CHECK_SIGSET_NULL_OK(SET) (SET)
abf70633
GM
120# define CHECK_IOCTL(ARG, CMD) (ARG)
121# define CHECK_FCNTL(ARG, CMD) (ARG)
e694422d 122# define CHECK_N_PAGES(ARG, NBYTES) (ARG)
abf70633
GM
123# define BOUNDED_N(PTR, N) (PTR)
124
125#endif /* !__BOUNDED_POINTERS__ */
126
127#define BOUNDED_1(PTR) BOUNDED_N (PTR, 1)
34173b51
GM
128
129#endif /* _bp_checks_h_ */
This page took 0.09533 seconds and 5 git commands to generate.