Skip to content

Commit d2d8e2e

Browse files
authored
[clang][bytecode] Handle invalid temporary descriptors (llvm#125033)
This happens e.g. when a vector element type is not primitive.
1 parent e6d16f9 commit d2d8e2e

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,10 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
562562
// We're creating a complex value here, so we need to
563563
// allocate storage for it.
564564
if (!Initializing) {
565-
unsigned LocalIndex = allocateTemporary(CE);
566-
if (!this->emitGetPtrLocal(LocalIndex, CE))
565+
std::optional<unsigned> LocalIndex = allocateTemporary(CE);
566+
if (!LocalIndex)
567+
return false;
568+
if (!this->emitGetPtrLocal(*LocalIndex, CE))
567569
return false;
568570
}
569571

@@ -679,8 +681,10 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
679681
assert(CE->getType()->isVectorType());
680682

681683
if (!Initializing) {
682-
unsigned LocalIndex = allocateTemporary(CE);
683-
if (!this->emitGetPtrLocal(LocalIndex, CE))
684+
std::optional<unsigned> LocalIndex = allocateTemporary(CE);
685+
if (!LocalIndex)
686+
return false;
687+
if (!this->emitGetPtrLocal(*LocalIndex, CE))
684688
return false;
685689
}
686690
unsigned ToSize = CE->getType()->getAs<VectorType>()->getNumElements();
@@ -759,8 +763,10 @@ bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
759763
return true;
760764

761765
if (!Initializing) {
762-
unsigned LocalIndex = allocateTemporary(E);
763-
if (!this->emitGetPtrLocal(LocalIndex, E))
766+
std::optional<unsigned> LocalIndex = allocateTemporary(E);
767+
if (!LocalIndex)
768+
return false;
769+
if (!this->emitGetPtrLocal(*LocalIndex, E))
764770
return false;
765771
}
766772

@@ -1118,8 +1124,10 @@ template <class Emitter>
11181124
bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11191125
// Prepare storage for result.
11201126
if (!Initializing) {
1121-
unsigned LocalIndex = allocateTemporary(E);
1122-
if (!this->emitGetPtrLocal(LocalIndex, E))
1127+
std::optional<unsigned> LocalIndex = allocateTemporary(E);
1128+
if (!LocalIndex)
1129+
return false;
1130+
if (!this->emitGetPtrLocal(*LocalIndex, E))
11231131
return false;
11241132
}
11251133

@@ -1175,7 +1183,10 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
11751183

11761184
if (!LHSIsComplex) {
11771185
// This is using the RHS type for the fake-complex LHS.
1178-
LHSOffset = allocateTemporary(RHS);
1186+
std::optional<unsigned> LocalIndex = allocateTemporary(RHS);
1187+
if (!LocalIndex)
1188+
return false;
1189+
LHSOffset = *LocalIndex;
11791190

11801191
if (!this->emitGetPtrLocal(LHSOffset, E))
11811192
return false;
@@ -1347,8 +1358,10 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
13471358

13481359
// Prepare storage for result.
13491360
if (!Initializing && !E->isCompoundAssignmentOp()) {
1350-
unsigned LocalIndex = allocateTemporary(E);
1351-
if (!this->emitGetPtrLocal(LocalIndex, E))
1361+
std::optional<unsigned> LocalIndex = allocateTemporary(E);
1362+
if (!LocalIndex)
1363+
return false;
1364+
if (!this->emitGetPtrLocal(*LocalIndex, E))
13521365
return false;
13531366
}
13541367

@@ -4170,14 +4183,16 @@ Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
41704183
}
41714184

41724185
template <class Emitter>
4173-
unsigned Compiler<Emitter>::allocateTemporary(const Expr *E) {
4186+
std::optional<unsigned> Compiler<Emitter>::allocateTemporary(const Expr *E) {
41744187
QualType Ty = E->getType();
41754188
assert(!Ty->isRecordType());
41764189

41774190
Descriptor *D = P.createDescriptor(
41784191
E, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
41794192
/*IsTemporary=*/true, /*IsMutable=*/false, /*Init=*/nullptr);
4180-
assert(D);
4193+
4194+
if (!D)
4195+
return std::nullopt;
41814196

41824197
Scope::Local Local = this->createLocal(D);
41834198
VariableScope<Emitter> *S = VarScope;

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
309309
std::optional<unsigned>
310310
allocateLocal(DeclTy &&Decl, QualType Ty = QualType(),
311311
const ValueDecl *ExtendingDecl = nullptr);
312-
unsigned allocateTemporary(const Expr *E);
312+
std::optional<unsigned> allocateTemporary(const Expr *E);
313313

314314
private:
315315
friend class VariableScope<Emitter>;

0 commit comments

Comments
 (0)