Skip to content

Commit 07b2ba2

Browse files
authored
Reapply "[clang][bytecode] Fix incorrect offset in elem() (llvm#155157)" (llvm#155276)
This reverts commit 9642aad. Since elem() only works on primitive arrays anyway, we don't have to do the isArrayRoot() check at all.
1 parent 89ce871 commit 07b2ba2

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

clang/lib/AST/ByteCode/EvaluationResult.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ bool EvaluationResult::checkFullyInitialized(InterpState &S,
178178
static void collectBlocks(const Pointer &Ptr,
179179
llvm::SetVector<const Block *> &Blocks) {
180180
auto isUsefulPtr = [](const Pointer &P) -> bool {
181-
return P.isLive() && !P.isZero() && !P.isDummy() && P.isDereferencable() &&
182-
!P.isUnknownSizeArray() && !P.isOnePastEnd();
181+
return P.isLive() && P.isBlockPointer() && !P.isZero() && !P.isDummy() &&
182+
P.isDereferencable() && !P.isUnknownSizeArray() && !P.isOnePastEnd();
183183
};
184184

185185
if (!isUsefulPtr(Ptr))

clang/lib/AST/ByteCode/Pointer.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -694,15 +694,14 @@ class Pointer {
694694
assert(asBlockPointer().Pointee);
695695
assert(isDereferencable());
696696
assert(getFieldDesc()->isPrimitiveArray());
697+
assert(I < getFieldDesc()->getNumElems());
697698

698699
unsigned ElemByteOffset = I * getFieldDesc()->getElemSize();
699-
if (isArrayRoot())
700-
return *reinterpret_cast<T *>(asBlockPointer().Pointee->rawData() +
701-
asBlockPointer().Base + sizeof(InitMapPtr) +
702-
ElemByteOffset);
700+
unsigned ReadOffset = BS.Base + sizeof(InitMapPtr) + ElemByteOffset;
701+
assert(ReadOffset + sizeof(T) <=
702+
BS.Pointee->getDescriptor()->getAllocSize());
703703

704-
return *reinterpret_cast<T *>(asBlockPointer().Pointee->rawData() + Offset +
705-
ElemByteOffset);
704+
return *reinterpret_cast<T *>(BS.Pointee->rawData() + ReadOffset);
706705
}
707706

708707
/// Whether this block can be read from at all. This is only true for

clang/test/AST/ByteCode/invalid.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,11 @@ namespace Casts {
5858
/// Just make sure this doesn't crash.
5959
float PR9558 = reinterpret_cast<const float&>("asd");
6060
}
61+
62+
63+
/// This used to crash in collectBlock().
64+
struct S {
65+
};
66+
S s;
67+
S *sp[2] = {&s, &s};
68+
S *&spp = sp[1];

0 commit comments

Comments
 (0)