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