new-op.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Replace operator new/new[], for GDB, the GNU debugger.
  2. Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /* GCC does not understand __has_feature. */
  15. #if !defined(__has_feature)
  16. # define __has_feature(x) 0
  17. #endif
  18. #if !__has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__)
  19. #include "common-defs.h"
  20. #include "host-defs.h"
  21. #include <new>
  22. /* These are declared in <new> starting C++14. Add these here to enable
  23. compilation using C++11. */
  24. extern void operator delete (void *p, std::size_t) noexcept;
  25. extern void operator delete[] (void *p, std::size_t) noexcept;
  26. /* Override operator new / operator new[], in order to internal_error
  27. on allocation failure and thus query the user for abort/core
  28. dump/continue, just like xmalloc does. We don't do this from a
  29. new-handler function instead (std::set_new_handler) because we want
  30. to catch allocation errors from within global constructors too.
  31. Skip overriding if building with -fsanitize=address though.
  32. Address sanitizer wants to override operator new/delete too in
  33. order to detect malloc+delete and new+free mismatches. Our
  34. versions would mask out ASan's, with the result of losing that
  35. useful mismatch detection.
  36. Note that C++ implementations could either have their throw
  37. versions call the nothrow versions (libstdc++), or the other way
  38. around (clang/libc++). For that reason, we replace both throw and
  39. nothrow variants and call malloc directly. */
  40. void *
  41. operator new (std::size_t sz)
  42. {
  43. /* malloc (0) is unpredictable; avoid it. */
  44. if (sz == 0)
  45. sz = 1;
  46. void *p = malloc (sz); /* ARI: malloc */
  47. if (p == NULL)
  48. {
  49. /* If the user decides to continue debugging, throw a
  50. gdb_quit_bad_alloc exception instead of a regular QUIT
  51. gdb_exception. The former extends both std::bad_alloc and a
  52. QUIT gdb_exception. This is necessary because operator new
  53. can only ever throw std::bad_alloc, or something that extends
  54. it. */
  55. try
  56. {
  57. malloc_failure (sz);
  58. }
  59. catch (gdb_exception &ex)
  60. {
  61. throw gdb_quit_bad_alloc (std::move (ex));
  62. }
  63. }
  64. return p;
  65. }
  66. void *
  67. operator new (std::size_t sz, const std::nothrow_t&) noexcept
  68. {
  69. /* malloc (0) is unpredictable; avoid it. */
  70. if (sz == 0)
  71. sz = 1;
  72. return malloc (sz); /* ARI: malloc */
  73. }
  74. void *
  75. operator new[] (std::size_t sz)
  76. {
  77. return ::operator new (sz);
  78. }
  79. void*
  80. operator new[] (std::size_t sz, const std::nothrow_t&) noexcept
  81. {
  82. return ::operator new (sz, std::nothrow);
  83. }
  84. /* Define also operators delete as one can LD_PRELOAD=libasan.so.*
  85. without recompiling the program with -fsanitize=address and then one would
  86. get false positive alloc-dealloc-mismatch (malloc vs operator delete [])
  87. errors from AddressSanitizers. */
  88. void
  89. operator delete (void *p) noexcept
  90. {
  91. free (p);
  92. }
  93. void
  94. operator delete (void *p, const std::nothrow_t&) noexcept
  95. {
  96. return ::operator delete (p);
  97. }
  98. void
  99. operator delete (void *p, std::size_t) noexcept
  100. {
  101. return ::operator delete (p, std::nothrow);
  102. }
  103. void
  104. operator delete[] (void *p) noexcept
  105. {
  106. return ::operator delete (p);
  107. }
  108. void
  109. operator delete[] (void *p, const std::nothrow_t&) noexcept
  110. {
  111. return ::operator delete (p, std::nothrow);
  112. }
  113. void
  114. operator delete[] (void *p, std::size_t) noexcept
  115. {
  116. return ::operator delete[] (p, std::nothrow);
  117. }
  118. #endif