Skip to content

Commit ae8c161

Browse files
fabianbs96MMory
andauthored
Use the ASAN API to better communicate to the sanitizer, what memory regions should actually be valid (#670)
Co-authored-by: Martin Mory <mmo@mail.upb.de>
1 parent 659cb8d commit ae8c161

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

include/phasar/PhasarLLVM/DataFlow/IfdsIde/Problems/ExtendedTaintAnalysis/AbstractMemoryLocationFactory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,15 @@ class AbstractMemoryLocationFactoryBase {
4141
Block *Next = nullptr;
4242

4343
static Block *create(Block *Next, size_t NumPointerEntries);
44-
static void destroy(Block *Blck);
44+
static void destroy(Block *Blck, size_t NumPointerEntries);
4545

4646
private:
4747
Block(Block *Next);
4848
};
4949

5050
Block *Root = nullptr;
5151
void **Pos = nullptr, **End = nullptr;
52+
size_t InitialCapacity{};
5253

5354
Allocator() noexcept = default;
5455
Allocator(size_t InitialCapacity);

include/phasar/Utils/StableVector.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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++;

lib/PhasarLLVM/DataFlow/IfdsIde/Problems/ExtendedTaintAnalysis/AbstractMemoryLocationFactory.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,17 @@ auto AbstractMemoryLocationFactoryBase::Allocator::Block::create(
3939
alignof(AbstractMemoryLocationImpl)}) size_t[1 + NumPointerEntries]);
4040

4141
new (Ret) Block(Next);
42+
43+
__asan_poison_memory_region(Ret->getTrailingObjects<void *>(),
44+
NumPointerEntries * sizeof(void *));
45+
4246
return Ret;
4347
}
4448

45-
void AbstractMemoryLocationFactoryBase::Allocator::Block::destroy(Block *Blck) {
49+
void AbstractMemoryLocationFactoryBase::Allocator::Block::destroy(
50+
Block *Blck, [[maybe_unused]] size_t NumPointerEntries) {
51+
__asan_unpoison_memory_region(Blck->getTrailingObjects<void *>(),
52+
NumPointerEntries * sizeof(void *));
4653
::operator delete[](Blck,
4754
std::align_val_t{alignof(AbstractMemoryLocationImpl)});
4855
}
@@ -61,10 +68,13 @@ AbstractMemoryLocationFactoryBase::Allocator::Allocator(
6168
}
6269

6370
AbstractMemoryLocationFactoryBase::Allocator::~Allocator() {
64-
auto *Blck = Root;
71+
auto *Rt = Root;
72+
auto *Blck = Rt;
6573
while (Blck) {
6674
auto *Nxt = Blck->Next;
67-
Block::destroy(Blck);
75+
Block::destroy(Blck, Blck == Rt
76+
? (MinNumPointersPerAML + 3) * InitialCapacity
77+
: NumPointersPerBlock);
6878
Blck = Nxt;
6979
}
7080
Root = nullptr;
@@ -110,6 +120,8 @@ AbstractMemoryLocationFactoryBase::Allocator::create(
110120

111121
Pos += NumPointersRequired;
112122

123+
__asan_unpoison_memory_region(Ret, NumPointersRequired * sizeof(void *));
124+
113125
new (Ret) AbstractMemoryLocationImpl(Baseptr, Offsets, Lifetime);
114126

115127
return Ret;

0 commit comments

Comments
 (0)