1 /* Pointer bounds checking for GNU Emacs
3 Copyright 2017 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
20 /* Pointer bounds checking is a no-op unless running on hardware
21 supporting Intel MPX (Intel Skylake or better). Also, it requires
22 GCC 5 and Linux kernel 3.19, or later. Configure with
23 CFLAGS='-fcheck-pointer-bounds -mmpx', perhaps with
24 -fchkp-first-field-has-own-bounds thrown in.
26 Although pointer bounds checking can help during debugging, it is
27 disabled by default because it hurts performance significantly.
28 The checking does not detect all pointer errors. For example, a
29 dumped Emacs might not detect a bounds violation of a pointer that
30 was created before Emacs was dumped. */
37 /* When not checking pointer bounds, the following macros simply
38 return their first argument. These macros return either void *, or
39 the same type as their first argument. */
43 /* Return a copy of P, with bounds narrowed to [P, P + N). */
46 ptr_bounds_clip (void const *p
, size_t n
)
48 return __builtin___bnd_narrow_ptr_bounds (p
, p
, n
);
51 # define ptr_bounds_clip(p, n) ((void) (size_t) {n}, p)
54 /* Return a copy of P, but with the bounds of Q. */
56 # define ptr_bounds_copy(p, q) __builtin___bnd_copy_ptr_bounds (p, q)
58 # define ptr_bounds_copy(p, q) ((void) (void const *) {q}, p)
61 /* Return a copy of P, but with infinite bounds.
62 This is a loophole in pointer bounds checking. */
64 # define ptr_bounds_init(p) __builtin___bnd_init_ptr_bounds (p)
66 # define ptr_bounds_init(p) (p)
69 /* Return a copy of P, but with bounds [P, P + N).
70 This is a loophole in pointer bounds checking. */
72 # define ptr_bounds_set(p, n) __builtin___bnd_set_ptr_bounds (p, n)
74 # define ptr_bounds_set(p, n) ((void) (size_t) {n}, p)
79 #endif /* PTR_BOUNDS_H */