[PATCH] Add AddressSanitizer annotations to std::vector
Jonathan Wakely
jwakely@redhat.com
Wed Jul 5 20:24:00 GMT 2017
On 05/07/17 20:44 +0100, Yuri Gribov wrote:
>On Wed, Jul 5, 2017 at 8:00 PM, Jonathan Wakely <jwakely@redhat.com> wrote:
>> This patch adds AddressSanitizer annotations to std::vector, so that
>> ASan can detect out-of-bounds accesses to the unused capacity of a
>> vector. e.g.
>>
>> std::vector<int> v(2);
>> int* p = v.data();
>> v.pop_back();
>> return p[1]; // ERROR
>>
>> This cannot be detected by Debug Mode, but with these annotations ASan
>> knows that only v.data()[0] is valid and will give an error.
>>
>> The annotations are only enabled for vector<T, std::allocator<T>> and
>> only when std::allocator's base class is either malloc_allocator or
>> new_allocator. For other allocators the memory might not come from the
>> freestore and so isn't tracked by ASan.
>
>One important issue with enabling this by default is that it may
>(will?) break separate sanitization (which is extremely important
>feature in practice). If one part of application is sanitized but the
>other isn't and some poor std::vector is push_back'ed in latter and
>then accessed in former, we'll get a false positive because push_back
>wouldn't properly annotate memory.
Good point.
>Perhaps hide this under a compilation flag (disabled by default)?
If you define _GLIBCXX_SANITIZE_STD_ALLOCATOR to 0 the annotations are
disabled. To make them disabled by default would need some changes, to
use separate macros for "the std::allocator base class can be
sanitized" and "the user wants std::vector to be sanitized".
I'll do that before committing.
More information about the Libstdc++
mailing list