~linaro-toolchain-dev/cortex-strings/trunk

« back to all changes in this revision

Viewing changes to reference/glibc-c/bp-checks.h

  • Committer: Michael Hope
  • Date: 2012-06-12 03:19:48 UTC
  • Revision ID: michael.hope@linaro.org-20120612031948-4ii8jicywtzjprak
Added the C only routines from GLIBC 2.16+20120607~git24a6dbe

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
5
 
 
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.
 
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
 
14
   Lesser General Public License for more details.
 
15
 
 
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/>.  */
 
19
 
 
20
#ifndef _bp_checks_h_
 
21
#define _bp_checks_h_ 1
 
22
 
 
23
#if __BOUNDED_POINTERS__
 
24
 
 
25
# define BOUNDS_VIOLATED (__builtin_trap (), 0)
 
26
 
 
27
/* Verify that pointer's value >= low.  Return pointer value.  */
 
28
# define CHECK_BOUNDS_LOW(ARG)                                  \
 
29
  (((__ptrvalue (ARG) < __ptrlow (ARG)) && BOUNDS_VIOLATED),    \
 
30
   __ptrvalue (ARG))
 
31
 
 
32
/* Verify that pointer's value < high.  Return pointer value.  */
 
33
# define CHECK_BOUNDS_HIGH(ARG)                         \
 
34
  (((__ptrvalue (ARG) > __ptrhigh (ARG)) && BOUNDS_VIOLATED),   \
 
35
   __ptrvalue (ARG))
 
36
 
 
37
# define _CHECK_N(ARG, N, COND)                         \
 
38
  (((COND)                                              \
 
39
    && (__ptrvalue (ARG) < __ptrlow (ARG)               \
 
40
        || __ptrvalue (ARG) + (N) > __ptrhigh (ARG))    \
 
41
    && BOUNDS_VIOLATED),                                \
 
42
   __ptrvalue (ARG))
 
43
 
 
44
extern void *__unbounded __ubp_memchr (const void *__unbounded, int, unsigned);
 
45
 
 
46
# define _CHECK_STRING(ARG, COND)                               \
 
47
  (((COND)                                                      \
 
48
    && (__ptrvalue (ARG) < __ptrlow (ARG)                       \
 
49
        || !__ubp_memchr (__ptrvalue (ARG), '\0',                       \
 
50
                      (__ptrhigh (ARG) - __ptrvalue (ARG))))    \
 
51
    && BOUNDS_VIOLATED),                                        \
 
52
   __ptrvalue (ARG))
 
53
 
 
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))
 
58
 
 
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)
 
63
 
 
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))
 
68
 
 
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)))
 
73
 
 
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)))
 
79
# else
 
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)
 
83
# endif
 
84
 
 
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))
 
89
 
 
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
 
92
   of memory.  */
 
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_); })
 
97
 
 
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);              \
 
103
     _p_; })
 
104
 
 
105
#else /* !__BOUNDED_POINTERS__ */
 
106
 
 
107
/* Do nothing if not compiling with -fbounded-pointers.  */
 
108
 
 
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)
 
123
 
 
124
#endif /* !__BOUNDED_POINTERS__ */
 
125
 
 
126
#define BOUNDED_1(PTR) BOUNDED_N (PTR, 1)
 
127
 
 
128
#endif /* _bp_checks_h_ */