Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdbsupport / new-op.cc
blob23ab0bb90aa98d4fb9ec6e5c460f0cb65063117b
1 /* Replace operator new/new[], for GDB, the GNU debugger.
3 Copyright (C) 2016-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 /* GCC does not understand __has_feature. */
21 #if !defined(__has_feature)
22 # define __has_feature(x) 0
23 #endif
25 #if !__has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__)
26 #include "host-defs.h"
27 #include <new>
29 /* These are declared in <new> starting C++14, but removing them
30 caused a build failure with clang. See PR build/31141. */
31 extern void operator delete (void *p, std::size_t) noexcept;
32 extern void operator delete[] (void *p, std::size_t) noexcept;
34 /* Override operator new / operator new[], in order to internal_error
35 on allocation failure and thus query the user for abort/core
36 dump/continue, just like xmalloc does. We don't do this from a
37 new-handler function instead (std::set_new_handler) because we want
38 to catch allocation errors from within global constructors too.
40 Skip overriding if building with -fsanitize=address though.
41 Address sanitizer wants to override operator new/delete too in
42 order to detect malloc+delete and new+free mismatches. Our
43 versions would mask out ASan's, with the result of losing that
44 useful mismatch detection.
46 Note that C++ implementations could either have their throw
47 versions call the nothrow versions (libstdc++), or the other way
48 around (clang/libc++). For that reason, we replace both throw and
49 nothrow variants and call malloc directly. */
51 void *
52 operator new (std::size_t sz)
54 /* malloc (0) is unpredictable; avoid it. */
55 if (sz == 0)
56 sz = 1;
58 void *p = malloc (sz); /* ARI: malloc */
59 if (p == NULL)
61 /* If the user decides to continue debugging, throw a
62 gdb_quit_bad_alloc exception instead of a regular QUIT
63 gdb_exception. The former extends both std::bad_alloc and a
64 QUIT gdb_exception. This is necessary because operator new
65 can only ever throw std::bad_alloc, or something that extends
66 it. */
67 try
69 malloc_failure (sz);
71 catch (gdb_exception &ex)
73 throw gdb_quit_bad_alloc (std::move (ex));
76 return p;
79 void *
80 operator new (std::size_t sz, const std::nothrow_t&) noexcept
82 /* malloc (0) is unpredictable; avoid it. */
83 if (sz == 0)
84 sz = 1;
85 return malloc (sz); /* ARI: malloc */
88 void *
89 operator new[] (std::size_t sz)
91 return ::operator new (sz);
94 void*
95 operator new[] (std::size_t sz, const std::nothrow_t&) noexcept
97 return ::operator new (sz, std::nothrow);
100 /* Define also operators delete as one can LD_PRELOAD=libasan.so.*
101 without recompiling the program with -fsanitize=address and then one would
102 get false positive alloc-dealloc-mismatch (malloc vs operator delete [])
103 errors from AddressSanitizers. */
105 void
106 operator delete (void *p) noexcept
108 free (p);
111 void
112 operator delete (void *p, const std::nothrow_t&) noexcept
114 return ::operator delete (p);
117 void
118 operator delete (void *p, std::size_t) noexcept
120 return ::operator delete (p, std::nothrow);
123 void
124 operator delete[] (void *p) noexcept
126 return ::operator delete (p);
129 void
130 operator delete[] (void *p, const std::nothrow_t&) noexcept
132 return ::operator delete (p, std::nothrow);
135 void
136 operator delete[] (void *p, std::size_t) noexcept
138 return ::operator delete[] (p, std::nothrow);
141 #endif