* src/alloc.c: Avoid O(N²) complexity when unchaining markers (bug#24548).
[emacs.git] / src / ptr-bounds.h
blob8cbd58d72b04288e54aae6f788b8da18cd32a078
1 /* Pointer bounds checking for GNU Emacs
3 Copyright 2017-2018 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. */
32 #ifndef PTR_BOUNDS_H
33 #define PTR_BOUNDS_H
35 #include <stddef.h>
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. */
41 INLINE_HEADER_BEGIN
43 /* Return a copy of P, with bounds narrowed to [P, P + N). */
44 #ifdef __CHKP__
45 INLINE void *
46 ptr_bounds_clip (void const *p, size_t n)
48 return __builtin___bnd_narrow_ptr_bounds (p, p, n);
50 #else
51 # define ptr_bounds_clip(p, n) ((void) (size_t) {n}, p)
52 #endif
54 /* Return a copy of P, but with the bounds of Q. */
55 #ifdef __CHKP__
56 # define ptr_bounds_copy(p, q) __builtin___bnd_copy_ptr_bounds (p, q)
57 #else
58 # define ptr_bounds_copy(p, q) ((void) (void const *) {q}, p)
59 #endif
61 /* Return a copy of P, but with infinite bounds.
62 This is a loophole in pointer bounds checking. */
63 #ifdef __CHKP__
64 # define ptr_bounds_init(p) __builtin___bnd_init_ptr_bounds (p)
65 #else
66 # define ptr_bounds_init(p) (p)
67 #endif
69 /* Return a copy of P, but with bounds [P, P + N).
70 This is a loophole in pointer bounds checking. */
71 #ifdef __CHKP__
72 # define ptr_bounds_set(p, n) __builtin___bnd_set_ptr_bounds (p, n)
73 #else
74 # define ptr_bounds_set(p, n) ((void) (size_t) {n}, p)
75 #endif
77 INLINE_HEADER_END
79 #endif /* PTR_BOUNDS_H */