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