1111#define PHASAR_UTILS_STABLEVECTOR_H_
1212
1313#include " llvm/ADT/SmallVector.h"
14+ #include " llvm/Support/Compiler.h"
1415#include " llvm/Support/MathExtras.h"
1516#include " llvm/Support/raw_ostream.h"
1617
@@ -197,6 +198,8 @@ class StableVector {
197198 Start = Blck;
198199 End = Blck + Cap;
199200 Pos = Blck + (Other.Pos - Other.Start );
201+
202+ __asan_poison_memory_region (Pos, (End - Pos) * sizeof (T));
200203 }
201204
202205 void swap (StableVector &Other) noexcept {
@@ -244,6 +247,7 @@ class StableVector {
244247 std::destroy (Start, Pos);
245248
246249 for (size_t I = BlockIdx; I < Blocks.size (); ++I) {
250+ __asan_unpoison_memory_region (Blocks[I], Cap * sizeof (T));
247251 std::allocator_traits<allocator_type>::deallocate (Alloc, Blocks[I], Cap);
248252
249253 Cap = TotalSize;
@@ -263,6 +267,7 @@ class StableVector {
263267 }
264268
265269 auto Ret = Pos;
270+ __asan_unpoison_memory_region (Ret, sizeof (T));
266271 std::allocator_traits<allocator_type>::construct (
267272 Alloc, Ret, std::forward<ArgTys>(Args)...);
268273 ++Pos;
@@ -343,6 +348,8 @@ class StableVector {
343348 assert (!empty () && " Do not call pop_back() on an empty StableVector!" );
344349
345350 std::destroy_at (--Pos);
351+ __asan_poison_memory_region (Pos, sizeof (T));
352+
346353 --Size;
347354 if (Pos != Start) {
348355 return ;
@@ -374,11 +381,13 @@ class StableVector {
374381
375382 for (size_t I = 0 ; I < BlockIdx; ++I) {
376383 std::destroy_n (Blocks[I], Cap);
384+ __asan_poison_memory_region (Blocks[I], Cap * sizeof (T));
377385 Cap = TotalSize;
378386 TotalSize += Cap;
379387 }
380388
381389 std::destroy (Start, Pos);
390+ __asan_poison_memory_region (Start, (Pos - Start) * sizeof (T));
382391 BlockIdx = 0 ;
383392 Size = 0 ;
384393 if (!Blocks.empty ()) {
@@ -399,10 +408,12 @@ class StableVector {
399408 Pos -= N;
400409 Size -= N;
401410 std::destroy_n (Pos, N);
411+ __asan_poison_memory_region (Pos, N * sizeof (T));
402412 return ;
403413 }
404414
405415 std::destroy (Start, Pos);
416+ __asan_poison_memory_region (Start, (Pos - Start) * sizeof (T));
406417 Size -= NumElementsInCurrBlock;
407418 N -= NumElementsInCurrBlock;
408419
@@ -429,6 +440,7 @@ class StableVector {
429440
430441 if (Size == 0 ) {
431442 assert (BlockIdx == 0 );
443+ __asan_unpoison_memory_region (Blocks[0 ], InitialCapacity * sizeof (T));
432444 std::allocator_traits<allocator_type>::deallocate (Alloc, Blocks[0 ],
433445 InitialCapacity);
434446 }
@@ -437,6 +449,7 @@ class StableVector {
437449
438450 for (size_t I = BlockIdx + 1 , BlocksEnd = Blocks.size (); I < BlocksEnd;
439451 ++I) {
452+ __asan_unpoison_memory_region (Blocks[I], Cap * sizeof (T));
440453 std::allocator_traits<allocator_type>::deallocate (Alloc, Blocks[I], Cap);
441454 Cap <<= 1 ;
442455 }
@@ -491,7 +504,9 @@ class StableVector {
491504 template <typename ... ArgTys>
492505 [[nodiscard]] T &growAndEmplace (ArgTys &&...Args) {
493506 auto makeBlock = [this ](size_t N) {
494- return std::allocator_traits<allocator_type>::allocate (Alloc, N);
507+ auto *Ret = std::allocator_traits<allocator_type>::allocate (Alloc, N);
508+ __asan_poison_memory_region (std::next (Ret), (N - 1 ) * sizeof (T));
509+ return Ret;
495510 };
496511
497512 if (Blocks.empty ()) {
@@ -501,6 +516,7 @@ class StableVector {
501516 assert (llvm::isPowerOf2_64 (Size));
502517 BlockIdx++;
503518 End = Blocks[BlockIdx] + Size;
519+ __asan_unpoison_memory_region (Blocks[BlockIdx], sizeof (T));
504520 } else {
505521 assert (llvm::isPowerOf2_64 (Size));
506522 BlockIdx++;
0 commit comments