Commit | Line | Data |
---|---|---|
503b1c39 PA |
1 | /* Replace operator new/new[], for GDB, the GNU debugger. |
2 | ||
b811d2c2 | 3 | Copyright (C) 2016-2020 Free Software Foundation, Inc. |
503b1c39 PA |
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 | ||
3ef9d661 YQ |
20 | /* GCC does not understand __has_feature. */ |
21 | #if !defined(__has_feature) | |
22 | # define __has_feature(x) 0 | |
23 | #endif | |
24 | ||
25 | #if !__has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__) | |
503b1c39 PA |
26 | #include "common-defs.h" |
27 | #include "host-defs.h" | |
28 | #include <new> | |
29 | ||
30 | /* Override operator new / operator new[], in order to internal_error | |
31 | on allocation failure and thus query the user for abort/core | |
32 | dump/continue, just like xmalloc does. We don't do this from a | |
33 | new-handler function instead (std::set_new_handler) because we want | |
34 | to catch allocation errors from within global constructors too. | |
35 | ||
e4426cb4 PA |
36 | Skip overriding if building with -fsanitize=address though. |
37 | Address sanitizer wants to override operator new/delete too in | |
38 | order to detect malloc+delete and new+free mismatches. Our | |
39 | versions would mask out ASan's, with the result of losing that | |
40 | useful mismatch detection. | |
41 | ||
503b1c39 PA |
42 | Note that C++ implementations could either have their throw |
43 | versions call the nothrow versions (libstdc++), or the other way | |
44 | around (clang/libc++). For that reason, we replace both throw and | |
45 | nothrow variants and call malloc directly. */ | |
46 | ||
47 | void * | |
48 | operator new (std::size_t sz) | |
49 | { | |
50 | /* malloc (0) is unpredictable; avoid it. */ | |
51 | if (sz == 0) | |
52 | sz = 1; | |
53 | ||
54 | void *p = malloc (sz); /* ARI: malloc */ | |
55 | if (p == NULL) | |
56 | { | |
57 | /* If the user decides to continue debugging, throw a | |
58 | gdb_quit_bad_alloc exception instead of a regular QUIT | |
59 | gdb_exception. The former extends both std::bad_alloc and a | |
60 | QUIT gdb_exception. This is necessary because operator new | |
61 | can only ever throw std::bad_alloc, or something that extends | |
62 | it. */ | |
a70b8144 | 63 | try |
503b1c39 PA |
64 | { |
65 | malloc_failure (sz); | |
66 | } | |
94aeb44b | 67 | catch (gdb_exception &ex) |
503b1c39 | 68 | { |
94aeb44b | 69 | throw gdb_quit_bad_alloc (std::move (ex)); |
503b1c39 | 70 | } |
503b1c39 PA |
71 | } |
72 | return p; | |
73 | } | |
74 | ||
75 | void * | |
bbe910e6 | 76 | operator new (std::size_t sz, const std::nothrow_t&) noexcept |
503b1c39 PA |
77 | { |
78 | /* malloc (0) is unpredictable; avoid it. */ | |
79 | if (sz == 0) | |
80 | sz = 1; | |
81 | return malloc (sz); /* ARI: malloc */ | |
82 | } | |
83 | ||
84 | void * | |
85 | operator new[] (std::size_t sz) | |
86 | { | |
87 | return ::operator new (sz); | |
88 | } | |
89 | ||
90 | void* | |
bbe910e6 | 91 | operator new[] (std::size_t sz, const std::nothrow_t&) noexcept |
503b1c39 PA |
92 | { |
93 | return ::operator new (sz, std::nothrow); | |
94 | } | |
3ef9d661 | 95 | #endif |