Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/lib/AST/ByteCode/InterpBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,21 @@ bool Block::hasPointer(const Pointer *P) const {

void Block::movePointersTo(Block *B) {
assert(B != this);
unsigned MDDiff = static_cast<int>(B->Desc->getMetadataSize()) -
static_cast<int>(Desc->getMetadataSize());

while (Pointers) {
Pointer *P = Pointers;

this->removePointer(P);
P->BS.Pointee = B;

// If the metadata size changed between the two blocks, move the pointer
// base/offset. Realistically, this should only happen when we move pointers
// from a dummy pointer to a global one.
P->BS.Base += MDDiff;
P->Offset += MDDiff;

B->addPointer(P);
}
assert(!this->hasPointers());
Expand Down
11 changes: 10 additions & 1 deletion clang/lib/AST/ByteCode/MemberPointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ std::optional<Pointer> MemberPointer::toPointer(const Context &Ctx) const {
if (!Base.isBlockPointer())
return std::nullopt;

unsigned BlockMDSize = Base.block()->getDescriptor()->getMetadataSize();

if (PtrOffset >= 0) {
// If the resulting base would be too small, return nullopt.
if (Base.BS.Base < static_cast<unsigned>(PtrOffset) ||
(Base.BS.Base - PtrOffset < BlockMDSize))
return std::nullopt;
}

Pointer CastedBase =
(PtrOffset < 0 ? Base.atField(-PtrOffset) : Base.atFieldSub(PtrOffset));

Expand All @@ -31,7 +40,7 @@ std::optional<Pointer> MemberPointer::toPointer(const Context &Ctx) const {
return std::nullopt;

unsigned Offset = 0;
Offset += CastedBase.block()->getDescriptor()->getMetadataSize();
Offset += BlockMDSize;

if (const auto *FD = dyn_cast<FieldDecl>(Dcl)) {
if (FD->getParent() == BaseRecord->getDecl())
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Pointer::Pointer(Block *Pointee, uint64_t BaseAndOffset)
Pointer::Pointer(Block *Pointee, unsigned Base, uint64_t Offset)
: Offset(Offset), StorageKind(Storage::Block) {
assert((Base == RootPtrMark || Base % alignof(void *) == 0) && "wrong base");
assert(Base >= Pointee->getDescriptor()->getMetadataSize());

BS = {Pointee, Base, nullptr, nullptr};

Expand Down
21 changes: 21 additions & 0 deletions clang/test/AST/ByteCode/cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,24 @@ namespace PointerCast {
// expected-note {{cast that performs the conversions of a reinterpret_cast}}
};
}

namespace DummyToGlobalBlockMove {
struct Baz {
unsigned int n;
};

struct AP {
const AP *p;
const Baz *lp;
};

class Bar {
public:
static Baz _m[];
static const AP m;
};

const AP Bar::m = {0, &Bar::_m[0]};
Baz Bar::_m[] = {{0}};
const AP m = {&Bar ::m};
}
Loading