Skip to content

Commit

Permalink
More workarounds for undefined behavior exposed when compiling in C++…
Browse files Browse the repository at this point in the history
…14 with

-fsized-deallocation. Disable sized deallocation for all objects derived from
TrailingObjects, as we expect the storage allocated for these objects to be
larger than the size of their dynamic type.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@259942 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
zygoloid committed Feb 5, 2016
1 parent 165b22a commit 2597d07
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion include/llvm/Support/TrailingObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ template <typename First> class AlignmentCalcHelper<First> {

/// The base class for TrailingObjects* classes.
class TrailingObjectsBase {
public:
/// Disable sized deallocation for all objects with trailing object storage;
/// the inferred size will typically not be correct.
void operator delete(void *P) { return ::operator delete(P); }

protected:
/// OverloadToken's purpose is to allow specifying function overloads
/// for different types, without actually taking the types as
Expand Down Expand Up @@ -290,7 +295,8 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl<
}

public:
// make this (privately inherited) class public.
// Make these (privately inherited) members public.
using ParentType::operator delete;
using ParentType::OverloadToken;

/// Returns a pointer to the trailing object array of the given type
Expand Down
4 changes: 4 additions & 0 deletions lib/IR/AttributeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ class AttributeSetNode final
void operator=(const AttributeSetNode &) = delete;
AttributeSetNode(const AttributeSetNode &) = delete;
public:
using TrailingObjects::operator delete;

static AttributeSetNode *get(LLVMContext &C, ArrayRef<Attribute> Attrs);

bool hasAttribute(Attribute::AttrKind Kind) const {
Expand Down Expand Up @@ -266,6 +268,8 @@ class AttributeSetImpl final
}
}

using TrailingObjects::operator delete;

/// \brief Get the context that created this AttributeSetImpl.
LLVMContext &getContext() { return Context; }

Expand Down
2 changes: 2 additions & 0 deletions unittests/Support/TrailingObjectsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Class1 final : protected TrailingObjects<Class1, short> {
void *Mem = ::operator new(totalSizeToAlloc<short>(NumShorts));
return new (Mem) Class1(ShortArray, NumShorts);
}
using TrailingObjects::operator delete;

short get(unsigned Num) const { return getTrailingObjects<short>()[Num]; }

Expand Down Expand Up @@ -78,6 +79,7 @@ class Class2 final : protected TrailingObjects<Class2, double, short> {
*C->getTrailingObjects<double>() = D;
return C;
}
using TrailingObjects::operator delete;

short getShort() const {
if (!HasShort)
Expand Down

0 comments on commit 2597d07

Please sign in to comment.