Skip to content

Commit 0f4db1a

Browse files
authored
[clang][bytecode] Fix incorrect offset in elem() (#155157)
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 5569bf2 commit 0f4db1a

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
@@ -694,15 +694,20 @@ 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+
if (isArrayRoot()) {
701+
unsigned ReadOffset = BS.Base + sizeof(InitMapPtr) + ElemByteOffset;
702+
assert(ReadOffset + sizeof(T) <=
703+
BS.Pointee->getDescriptor()->getAllocSize());
704+
return *reinterpret_cast<T *>(BS.Pointee->rawData() + ReadOffset);
705+
}
703706

704-
return *reinterpret_cast<T *>(asBlockPointer().Pointee->rawData() + Offset +
705-
ElemByteOffset);
707+
unsigned ReadOffset = BS.Base + ElemByteOffset;
708+
assert(ReadOffset + sizeof(T) <=
709+
BS.Pointee->getDescriptor()->getAllocSize());
710+
return *reinterpret_cast<T *>(BS.Pointee->rawData() + ReadOffset);
706711
}
707712

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