Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit 5fff611

Browse files
jankratochvilvries
authored andcommitted
Fix LD_PRELOAD=/usr/lib64/libasan.so.6 gdb
Currently for a binary compiled normally (without -fsanitize=address) but with LD_PRELOAD of ASAN one gets: $ ASAN_OPTIONS=detect_leaks=0:alloc_dealloc_mismatch=1:abort_on_error=1:fast_unwind_on_malloc=0 LD_PRELOAD=/usr/lib64/libasan.so.6 gdb ================================================================= ==1909567==ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete []) on 0x602000001570 #0 0x7f1c98e5efa7 in operator delete[](void*) (/usr/lib64/libasan.so.6+0xb0fa7) ... 0x602000001570 is located 0 bytes inside of 2-byte region [0x602000001570,0x602000001572) allocated by thread T0 here: #0 0x7f1c98e5cd1f in __interceptor_malloc (/usr/lib64/libasan.so.6+0xaed1f) #1 0x557ee4a42e81 in operator new(unsigned long) (/usr/libexec/gdb+0x74ce81) SUMMARY: AddressSanitizer: alloc-dealloc-mismatch (/usr/lib64/libasan.so.6+0xb0fa7) in operator delete[](void*) ==1909567==HINT: if you don't care about these errors you may set ASAN_OPTIONS=alloc_dealloc_mismatch=0 ==1909567==ABORTING Despite the code called properly operator new[] and operator delete[]. But GDB's new-op.cc provides its own operator new[] which gets translated into malloc() (which gets recogized as operatore new(size_t)) but as it does not translate also operators delete[] Address Sanitizer gets confused. The question is how many variants of the delete operator need to be provided. There could be 14 operators new but there are only 4, GDB uses 3 of them. There could be 16 operators delete but there are only 6, GDB uses 2 of them. It depends on libraries and compiler which of the operators will get used. Currently being used: U operator new[](unsigned long) U operator new(unsigned long) U operator new(unsigned long, std::nothrow_t const&) U operator delete[](void*) U operator delete(void*, unsigned long) Tested on x86_64-linux.
1 parent 6ef4fa0 commit 5fff611

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

gdbsupport/new-op.cc

+42
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,46 @@ operator new[] (std::size_t sz, const std::nothrow_t&) noexcept
9292
{
9393
return ::operator new (sz, std::nothrow);
9494
}
95+
96+
/* Define also operators delete as one can LD_PRELOAD=libasan.so.*
97+
without recompiling the program with -fsanitize=address and then one would
98+
get false positive alloc-dealloc-mismatch (malloc vs operator delete [])
99+
errors from AddressSanitizers. */
100+
101+
void
102+
operator delete (void *p)
103+
{
104+
free (p);
105+
}
106+
107+
void
108+
operator delete (void *p, const std::nothrow_t&) noexcept
109+
{
110+
return ::operator delete (p);
111+
}
112+
113+
void
114+
operator delete (void *p, std::size_t) noexcept
115+
{
116+
return ::operator delete (p, std::nothrow);
117+
}
118+
119+
void
120+
operator delete[] (void *p)
121+
{
122+
return ::operator delete (p);
123+
}
124+
125+
void
126+
operator delete[] (void *p, const std::nothrow_t&) noexcept
127+
{
128+
return ::operator delete (p, std::nothrow);
129+
}
130+
131+
void
132+
operator delete[] (void *p, std::size_t) noexcept
133+
{
134+
return ::operator delete[] (p, std::nothrow);
135+
}
136+
95137
#endif

0 commit comments

Comments
 (0)