Skip to content

Commit c466589

Browse files
committed
[clang][bytecode] Fix incorrect offset in elem()
We need to use the base offset in both cases. Also, add additional assertions to make sure we don't miss this case again. Fixes #155132
1 parent c346f40 commit c466589

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -696,15 +696,20 @@ class Pointer {
696696
assert(asBlockPointer().Pointee);
697697
assert(isDereferencable());
698698
assert(getFieldDesc()->isPrimitiveArray());
699+
assert(I < getFieldDesc()->getNumElems());
699700

700701
unsigned ElemByteOffset = I * getFieldDesc()->getElemSize();
701-
if (isArrayRoot())
702-
return *reinterpret_cast<T *>(asBlockPointer().Pointee->rawData() +
703-
asBlockPointer().Base + sizeof(InitMapPtr) +
704-
ElemByteOffset);
702+
if (isArrayRoot()) {
703+
unsigned ReadOffset = BS.Base + sizeof(InitMapPtr) + ElemByteOffset;
704+
assert(ReadOffset + sizeof(T) <=
705+
BS.Pointee->getDescriptor()->getAllocSize());
706+
return *reinterpret_cast<T *>(BS.Pointee->rawData() + ReadOffset);
707+
}
705708

706-
return *reinterpret_cast<T *>(asBlockPointer().Pointee->rawData() + Offset +
707-
ElemByteOffset);
709+
unsigned ReadOffset = BS.Base + ElemByteOffset;
710+
assert(ReadOffset + sizeof(T) <=
711+
BS.Pointee->getDescriptor()->getAllocSize());
712+
return *reinterpret_cast<T *>(BS.Pointee->rawData() + ReadOffset);
708713
}
709714

710715
/// 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)