Skip to content

Commit f31fe83

Browse files
committed
Fix zero-array in 'struct hack'
1 parent b54ee4c commit f31fe83

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

include/phasar/PhasarLLVM/DataFlowSolver/IfdsIde/Problems/ExtendedTaintAnalysis/AbstractMemoryLocation.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ struct AbstractMemoryLoactionStorage : public llvm::FoldingSetNode {
3838
const llvm::Value *Baseptr;
3939
uint32_t Lifetime;
4040
uint32_t NumOffsets;
41-
ptrdiff_t Offsets[0]; // NOLINT
41+
/// The actual length of Offsets is a runtime-constant determined by
42+
/// NumOffsets. Note, that NumOffsets can be larger than 1
43+
ptrdiff_t Offsets[1]; // NOLINT
4244

4345
protected:
4446
AbstractMemoryLoactionStorage(

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AbstractMemoryLocationFactoryBase {
3737
struct Allocator {
3838
struct Block {
3939
Block *Next = nullptr;
40-
void *Data[0];
40+
void *Data[1]; // NOLINT
4141

4242
static Block *create(Block *Next, size_t NumPointerEntries);
4343
static void destroy(Block *Blck);
@@ -49,18 +49,23 @@ class AbstractMemoryLocationFactoryBase {
4949
Block *Root = nullptr;
5050
void **Pos = nullptr, **End = nullptr;
5151

52-
Allocator() = default;
52+
Allocator() noexcept = default;
5353
Allocator(size_t InitialCapacity);
54+
Allocator(const Allocator &) = delete;
55+
Allocator(Allocator &&Other) noexcept;
5456
~Allocator();
5557

58+
Allocator &operator=(const Allocator &) = delete;
59+
Allocator &operator=(Allocator &&Other) noexcept;
60+
5661
AbstractMemoryLocationImpl *create(const llvm::Value *Baseptr,
5762
size_t Lifetime,
5863
llvm::ArrayRef<ptrdiff_t> Offsets);
5964

6065
private:
6166
constexpr static size_t ExpectedNumAmLsPerBlock = 1024;
6267
constexpr static size_t MinNumPointersPerAML =
63-
sizeof(AbstractMemoryLocationImpl) / sizeof(void *);
68+
offsetof(AbstractMemoryLocationImpl, Offsets) / sizeof(void *);
6469
constexpr static size_t NumPointersPerBlock =
6570
(MinNumPointersPerAML + 3) * ExpectedNumAmLsPerBlock;
6671
};

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
namespace psr::detail {
1919

20+
/// We intentionally don't initialize the Data member as it will be a dynamic
21+
/// array at runtime
22+
// NOLINTNEXTLINE (cppcoreguidelines-pro-type-member-init)
2023
AbstractMemoryLocationFactoryBase::Allocator::Block::Block(Block *Next)
2124
: Next(Next) {}
2225

@@ -34,6 +37,11 @@ auto AbstractMemoryLocationFactoryBase::Allocator::Block::create(
3437
std::terminate();
3538
}
3639

40+
static_assert(
41+
alignof(AbstractMemoryLocationImpl) == alignof(size_t),
42+
"The alignment of the AbstractMemoryLocationImpl allocation cannot be "
43+
"guaranteed as it differs from the alignment of size_t");
44+
3745
auto *Ret = reinterpret_cast<Block *>(new size_t[1 + NumPointerEntries]);
3846

3947
new (Ret) Block(Next);
@@ -69,6 +77,21 @@ AbstractMemoryLocationFactoryBase::Allocator::~Allocator() {
6977
End = nullptr;
7078
}
7179

80+
AbstractMemoryLocationFactoryBase::Allocator::Allocator(
81+
Allocator &&Other) noexcept
82+
: Root(Other.Root), Pos(Other.Pos), End(Other.End) {
83+
Other.Root = nullptr;
84+
Other.Pos = nullptr;
85+
Other.End = nullptr;
86+
}
87+
88+
auto AbstractMemoryLocationFactoryBase::Allocator::operator=(
89+
Allocator &&Other) noexcept -> Allocator & {
90+
this->Allocator::~Allocator();
91+
new (this) Allocator(std::move(Other));
92+
return *this;
93+
}
94+
7295
AbstractMemoryLocationImpl *
7396
AbstractMemoryLocationFactoryBase::Allocator::create(
7497
const llvm::Value *Baseptr, size_t Lifetime,

0 commit comments

Comments
 (0)