xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/common-defs.h (revision 889f3bb010ad20d396fb291b89f202288dac2c87)
1 /* Common definitions.
2 
3    Copyright (C) 1986-2024 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program 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
10    (at your option) any later version.
11 
12    This program 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.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef COMMON_COMMON_DEFS_H
21 #define COMMON_COMMON_DEFS_H
22 
23 #include <gdbsupport/config.h>
24 
25 #undef PACKAGE_NAME
26 #undef PACKAGE
27 #undef PACKAGE_VERSION
28 #undef PACKAGE_STRING
29 #undef PACKAGE_TARNAME
30 
31 #include "gnulib/config.h"
32 
33 /* From:
34     https://www.gnu.org/software/gnulib/manual/html_node/stdint_002eh.html
35 
36    "On some hosts that predate C++11, when using C++ one must define
37    __STDC_CONSTANT_MACROS to make visible the definitions of constant
38    macros such as INTMAX_C, and one must define __STDC_LIMIT_MACROS to
39    make visible the definitions of limit macros such as INTMAX_MAX.".
40 
41    And:
42     https://www.gnu.org/software/gnulib/manual/html_node/inttypes_002eh.html
43 
44    "On some hosts that predate C++11, when using C++ one must define
45    __STDC_FORMAT_MACROS to make visible the declarations of format
46    macros such as PRIdMAX."
47 
48    Must do this before including any system header, since other system
49    headers may include stdint.h/inttypes.h.  */
50 #define __STDC_CONSTANT_MACROS 1
51 #define __STDC_LIMIT_MACROS 1
52 #define __STDC_FORMAT_MACROS 1
53 
54 /* Some distros enable _FORTIFY_SOURCE by default, which on occasion
55    has caused build failures with -Wunused-result when a patch is
56    developed on a distro that does not enable _FORTIFY_SOURCE.  We
57    enable it here in order to try to catch these problems earlier;
58    plus this seems like a reasonable safety measure.  The check for
59    optimization is required because _FORTIFY_SOURCE only works when
60    optimization is enabled.  If _FORTIFY_SOURCE is already defined,
61    then we don't do anything.  Also, on MinGW, fortify requires
62    linking to -lssp, and to avoid the hassle of checking for
63    that and linking to it statically, we just don't define
64    _FORTIFY_SOURCE there.  */
65 
66 #if (!defined _FORTIFY_SOURCE && defined __OPTIMIZE__ && __OPTIMIZE__ > 0 \
67      && !defined(__MINGW32__))
68 #define _FORTIFY_SOURCE 2
69 #endif
70 
71 /* We don't support Windows versions before XP, so we define
72    _WIN32_WINNT correspondingly to ensure the Windows API headers
73    expose the required symbols.
74 
75    NOTE: this must be kept in sync with common.m4.  */
76 #if defined (__MINGW32__) || defined (__CYGWIN__)
77 # ifdef _WIN32_WINNT
78 #  if _WIN32_WINNT < 0x0501
79 #   undef _WIN32_WINNT
80 #   define _WIN32_WINNT 0x0501
81 #  endif
82 # else
83 #  define _WIN32_WINNT 0x0501
84 # endif
85 #endif	/* __MINGW32__ || __CYGWIN__ */
86 
87 #include <stdarg.h>
88 #include <stdio.h>
89 
90 /* Include both cstdlib and stdlib.h to ensure we have standard functions
91    defined both in the std:: namespace and in the global namespace.  */
92 #include <cstdlib>
93 #include <stdlib.h>
94 
95 #include <stddef.h>
96 #include <stdint.h>
97 #include <string.h>
98 #ifdef HAVE_STRINGS_H
99 #include <strings.h>
100 #endif
101 #include <errno.h>
102 #if HAVE_ALLOCA_H
103 #include <alloca.h>
104 #endif
105 
106 #include "ansidecl.h"
107 /* This is defined by ansidecl.h, but we prefer gnulib's version.  On
108    MinGW, gnulib might enable __USE_MINGW_ANSI_STDIO, which may or not
109    require use of attribute gnu_printf instead of printf.  gnulib
110    checks that at configure time.  Since _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD
111    is compatible with ATTRIBUTE_PRINTF, simply use it.  */
112 #undef ATTRIBUTE_PRINTF
113 #define ATTRIBUTE_PRINTF _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD
114 
115 /* This is defined by ansidecl.h, but we disable the attribute.
116 
117    Say a developer starts out with:
118    ...
119    extern void foo (void *ptr) __attribute__((nonnull (1)));
120    void foo (void *ptr) {}
121    ...
122    with the idea in mind to catch:
123    ...
124    foo (nullptr);
125    ...
126    at compile time with -Werror=nonnull, and then adds:
127    ...
128     void foo (void *ptr) {
129    +  gdb_assert (ptr != nullptr);
130     }
131    ...
132    to catch:
133    ...
134    foo (variable_with_nullptr_value);
135    ...
136    at runtime as well.
137 
138    Said developer then verifies that the assert works (using -O0), and commits
139    the code.
140 
141    Some other developer then checks out the code and accidentally writes some
142    variant of:
143    ...
144    foo (variable_with_nullptr_value);
145    ...
146    and builds with -O2, and ... the assert doesn't trigger, because it's
147    optimized away by gcc.
148 
149    There's no suppported recipe to prevent the assertion from being optimized
150    away (other than: build with -O0, or remove the nonnull attribute).  Note
151    that -fno-delete-null-pointer-checks does not help.  A patch was submitted
152    to improve gcc documentation to point this out more clearly (
153    https://gcc.gnu.org/pipermail/gcc-patches/2021-July/576218.html ).  The
154    patch also mentions a possible workaround that obfuscates the pointer
155    using:
156    ...
157     void foo (void *ptr) {
158    +  asm ("" : "+r"(ptr));
159       gdb_assert (ptr != nullptr);
160     }
161    ...
162    but that still requires the developer to manually add this in all cases
163    where that's necessary.
164 
165    A warning was added to detect the situation: -Wnonnull-compare, which does
166    help in detecting those cases, but each new gcc release may indicate a new
167    batch of locations that needs fixing, which means we've added a maintenance
168    burden.
169 
170    We could try to deal with the problem more proactively by introducing a
171    gdb_assert variant like:
172    ...
173    void gdb_assert_non_null (void *ptr) {
174       asm ("" : "+r"(ptr));
175       gdb_assert (ptr != nullptr);
176     }
177     void foo (void *ptr) {
178       gdb_assert_nonnull (ptr);
179     }
180    ...
181    and make it a coding style to use it everywhere, but again, maintenance
182    burden.
183 
184    With all these things considered, for now we go with the solution with the
185    least maintenance burden: disable the attribute, such that we reliably deal
186    with it everywhere.  */
187 #undef ATTRIBUTE_NONNULL
188 #define ATTRIBUTE_NONNULL(m)
189 
190 #define ATTRIBUTE_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
191 #define ATTRIBUTE_USED __attribute__ ((__used__))
192 
193 #include "libiberty.h"
194 #include "pathmax.h"
195 #include "gdb/signals.h"
196 #include "gdb_locale.h"
197 #include "ptid.h"
198 #include "common-types.h"
199 #include "common-utils.h"
200 #include "gdb_assert.h"
201 #include "errors.h"
202 #include "print-utils.h"
203 #include "common-debug.h"
204 #include "cleanups.h"
205 #include "common-exceptions.h"
206 #include "gdbsupport/poison.h"
207 
208 /* Pull in gdb::unique_xmalloc_ptr.  */
209 #include "gdbsupport/gdb_unique_ptr.h"
210 
211 /* sbrk on macOS is not useful for our purposes, since sbrk(0) always
212    returns the same value.  brk/sbrk on macOS is just an emulation
213    that always returns a pointer to a 4MB section reserved for
214    that.  */
215 
216 #if defined (HAVE_SBRK) && !__APPLE__
217 #define HAVE_USEFUL_SBRK 1
218 #endif
219 
220 #endif /* COMMON_COMMON_DEFS_H */
221