[llvm-dev] [ADT] Adding instrumentation for ASAN to SmallVector

Nathan James via llvm-dev llvm-dev at lists.llvm.org
Mon Sep 7 07:50:31 PDT 2020


Dear list,

I recently tried to add instrumentation to SmallVector for using
Address sanitizer to detect cases where references used after they are
invalidated. This basic implementation for this is here - 
https://reviews.llvm.org/D87237

However, in adding/testing this, I did uncover some questionable code.
Firstly `SmallString<unsigned>::c_str()` and
`Twine::toNullTerminatedStringRef(SmallVectorImpl<char>&)` both use
bytes outside the range of the SmallVectors storage. This isn't
inherently bad.
Secondly calling `SmallVectorImpl<T>::insert(iterator, const T&)`
results in a reference invalidation when the element to insert is
contained inside the SmallVector and the SmallVector needs to grow for
the insert. This has been fixed inside the aforementioned PR.

My main point here is how does everyone feel about using ASAN to catch
bugs like this not only inside SmallVector but also adding the
instrumentation to some other containers used by llvm. If people are
happy with this implementation for SmallVector I'd be happy for
feedback on the PR. It would likely need some specific asan test cases
however I'm not entirely sure how to go about adding those.

Thanks for reading,

~Nathan



More information about the llvm-dev mailing list