summaryrefslogtreecommitdiff
path: root/sysdeps/generic/bp-checks.h
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps/generic/bp-checks.h')
-rw-r--r--sysdeps/generic/bp-checks.h134
1 files changed, 76 insertions, 58 deletions
diff --git a/sysdeps/generic/bp-checks.h b/sysdeps/generic/bp-checks.h
index ea71cf875f..349bd81792 100644
--- a/sysdeps/generic/bp-checks.h
+++ b/sysdeps/generic/bp-checks.h
@@ -21,83 +21,101 @@
Boston, MA 02111-1307, USA. */
#ifndef _bp_checks_h_
-# define _bp_checks_h_ 1
+#define _bp_checks_h_ 1
-# if !__ASSEMBLER__
+#if __BOUNDED_POINTERS__
-# if __BOUNDED_POINTERS__
-
-/* GKM FIXME: when gcc is ready, add real bounds checks */
-# define BOUNDS_VIOLATED (__builtin_trap (), 0)
-extern int __ubp_memchr (const char *__unbounded, int, unsigned);
+# define BOUNDS_VIOLATED (__builtin_trap (), 0)
+extern int __memchr (const char *__unbounded, int, unsigned);
/* Verify that pointer's value >= low. Return pointer value. */
-# define CHECK_BOUNDS_LOW(ARG) \
+# define CHECK_BOUNDS_LOW(ARG) \
(((__ptrvalue (ARG) < __ptrlow (ARG)) && BOUNDS_VIOLATED), \
__ptrvalue (ARG))
/* Verify that pointer's value < high. Return pointer value. */
-# define CHECK_BOUNDS_HIGH(ARG) \
+# define CHECK_BOUNDS_HIGH(ARG) \
(((__ptrvalue (ARG) > __ptrhigh (ARG)) && BOUNDS_VIOLATED), \
__ptrvalue (ARG))
-/* Check bounds of a pointer seated to a single object. */
-# define CHECK_1(ARG) CHECK_N ((ARG), 1)
+# define _CHECK_N(ARG, N, COND) \
+ (((COND) \
+ && (__ptrvalue (ARG) < __ptrlow (ARG) \
+ || __ptrvalue (ARG) + (N) > __ptrhigh (ARG)) \
+ && BOUNDS_VIOLATED), \
+ __ptrvalue (ARG))
-/* Same as CHECK_1, but tolerate ARG == NULL. */
-# define CHECK_1opt(ARG) CHECK_Nopt ((ARG), 1)
+# define _CHECK_STRING(ARG, COND) \
+ (((COND) \
+ && (__ptrvalue (ARG) < __ptrlow (ARG) \
+ || !__memchr (__ptrvalue (ARG), '\0', \
+ (__ptrhigh (ARG) - __ptrvalue (ARG)))) \
+ && BOUNDS_VIOLATED), \
+ __ptrvalue (ARG))
/* Check bounds of a pointer seated to an array of N objects. */
-# define CHECK_N(ARG, N) \
- (((__ptrvalue (ARG) < __ptrlow (ARG) \
- || __ptrvalue (ARG) + (N) > __ptrhigh (ARG)) \
- && BOUNDS_VIOLATED), __ptrvalue (ARG))
-
+# define CHECK_N(ARG, N) _CHECK_N ((ARG), (N), 1)
/* Same as CHECK_N, but tolerate ARG == NULL. */
-# define CHECK_Nopt(ARG, N) \
- (((__ptrvalue (ARG) \
- && (__ptrvalue (ARG) < __ptrlow (ARG) \
- || __ptrvalue (ARG) + (N) > __ptrhigh (ARG))) \
- && BOUNDS_VIOLATED), __ptrvalue (ARG))
-
-/* Check for NUL-terminator within string's bounds. */
-# define CHECK_STRING(ARG) \
- (((__ptrvalue (ARG) < __ptrlow (ARG) \
- || !__ubp_memchr (__ptrvalue (ARG), '\0', \
- (__ptrhigh (ARG) - __ptrvalue (ARG)))) \
- && BOUNDS_VIOLATED), \
- __ptrvalue (ARG))
+# define CHECK_Nopt(ARG, N) _CHECK_N ((ARG), (N), __ptrvalue (ARG))
-# define CHECK_SIGSET(SET) CHECK_N ((SET), _NSIG / (8 * sizeof *(SET)))
-# define CHECK_SIGSETopt(SET) CHECK_Nopt ((SET), _NSIG / (8 * sizeof *(SET)))
+/* Check bounds of a pointer seated to a single object. */
+# define CHECK_1(ARG) CHECK_N ((ARG), 1)
+/* Same as CHECK_1, but tolerate ARG == NULL. */
+# define CHECK_1opt(ARG) CHECK_Nopt ((ARG), 1)
-# else /* !__BOUNDED_POINTERS__ */
+/* Check for NUL-terminator within string's bounds. */
+# define CHECK_STRING(ARG) _CHECK_STRING ((ARG), 1)
+/* Same as CHECK_STRING, but tolerate ARG == NULL. */
+# define CHECK_STRINGopt(ARG) _CHECK_STRING ((ARG), __ptrvalue (ARG))
+
+/* Check bounds of signal syscall args with type sigset_t. */
+# define CHECK_SIGSET(SET) CHECK_N ((SET), _NSIG / (8 * sizeof *(SET)))
+/* Same as CHECK_SIGSET, but tolerate SET == NULL. */
+# define CHECK_SIGSETopt(SET) CHECK_Nopt ((SET), _NSIG / (8 * sizeof *(SET)))
+
+# if defined (_IOC_SIZESHIFT) && defined (_IOC_SIZEBITS)
+/* Extract the size of the ioctl data and check its bounds. */
+# define CHECK_IOCTL(ARG, CMD) \
+ CHECK_N ((const char *) (ARG), \
+ (((CMD) >> _IOC_SIZESHIFT) & ((1 << _IOC_SIZEBITS) - 1)))
+# else
+/* We don't know the size of the ioctl data, so the best we can do
+ is check that the first byte is within bounds. */
+# define CHECK_IOCTL(ARG, CMD) CHECK_1 ((const char *) ARG)
+# endif
+
+/* Check bounds of `struct flock *' for the locking fcntl commands. */
+# define CHECK_FCNTL(ARG, CMD) \
+ (((CMD) == F_GETLK || (CMD) == F_SETLK || (CMD) == F_SETLKW) \
+ ? CHECK_1 ((struct flock *) ARG) : (unsigned long) (ARG))
+
+/* Return a bounded pointer with value PTR that satisfies CHECK_N (PTR, N). */
+# define BOUNDED_N(PTR, N) \
+ ({ __typeof (*(PTR)) *__bounded _p_; \
+ __ptrvalue _p_ = __ptrlow _p_ = __ptrvalue (PTR); \
+ __ptrhigh _p_ = __ptrvalue _p_ + (N); \
+ _p_; })
+
+#else /* !__BOUNDED_POINTERS__ */
/* Do nothing if not compiling with -fbounded-pointers. */
-# define BOUNDS_VIOLATED
-# define CHECK_BOUNDS_LOW(ARG) (ARG)
-# define CHECK_BOUNDS_HIGH(ARG) (ARG)
-# define CHECK_1(ARG) (ARG)
-# define CHECK_1opt(ARG) (ARG)
-# define CHECK_N(ARG, N) (ARG)
-# define CHECK_Nopt(ARG, N) (ARG)
-# define CHECK_STRING(ARG) (ARG)
-# define CHECK_SIGSET(SET) (SET)
-# define CHECK_SIGSETopt(SET) (SET)
-
-# endif /* !__BOUNDED_POINTERS__ */
-
-# if defined (_IOC_SIZESHIFT) && defined (_IOC_SIZEBITS)
-
-/* Extract the size of the ioctl parameter argument and check its bounds. */
-# define CHECK_IOCTL(ARG, CMD) \
- CHECK_N ((ARG), (((CMD) >> _IOC_SIZESHIFT) & ((1 << _IOC_SIZEBITS) - 1)))
-
-# else
-# define CHECK_IOCTL(ARG, CMD) __ptrvalue (ARG)
-# endif
-
-# endif /* !__ASSEMBLER__ */
+# define BOUNDS_VIOLATED
+# define CHECK_BOUNDS_LOW(ARG) (ARG)
+# define CHECK_BOUNDS_HIGH(ARG) (ARG)
+# define CHECK_1(ARG) (ARG)
+# define CHECK_1opt(ARG) (ARG)
+# define CHECK_N(ARG, N) (ARG)
+# define CHECK_Nopt(ARG, N) (ARG)
+# define CHECK_STRING(ARG) (ARG)
+# define CHECK_SIGSET(SET) (SET)
+# define CHECK_SIGSETopt(SET) (SET)
+# define CHECK_IOCTL(ARG, CMD) (ARG)
+# define CHECK_FCNTL(ARG, CMD) (ARG)
+# define BOUNDED_N(PTR, N) (PTR)
+
+#endif /* !__BOUNDED_POINTERS__ */
+
+#define BOUNDED_1(PTR) BOUNDED_N (PTR, 1)
#endif /* _bp_checks_h_ */