[clang] [clang][bytecode] Handle invalid temporary descriptors (PR #125033)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 29 22:03:54 PST 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/125033
This happens e.g. when a vector element type is not primitive.
>From c63d51e21ccef8dd5be008ffedb3f7d4748811f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 30 Jan 2025 06:41:28 +0100
Subject: [PATCH] [clang][bytecode] Handle invalid temporary descriptors
This happens e.g. when a vector element type is not primitive.
---
clang/lib/AST/ByteCode/Compiler.cpp | 41 ++++++++++++++++++++---------
clang/lib/AST/ByteCode/Compiler.h | 2 +-
2 files changed, 29 insertions(+), 14 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 4659d0e00784d9..f7f4f713c787f2 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -562,8 +562,10 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
// We're creating a complex value here, so we need to
// allocate storage for it.
if (!Initializing) {
- unsigned LocalIndex = allocateTemporary(CE);
- if (!this->emitGetPtrLocal(LocalIndex, CE))
+ std::optional<unsigned> LocalIndex = allocateTemporary(CE);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, CE))
return false;
}
@@ -679,8 +681,10 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
assert(CE->getType()->isVectorType());
if (!Initializing) {
- unsigned LocalIndex = allocateTemporary(CE);
- if (!this->emitGetPtrLocal(LocalIndex, CE))
+ std::optional<unsigned> LocalIndex = allocateTemporary(CE);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, CE))
return false;
}
unsigned ToSize = CE->getType()->getAs<VectorType>()->getNumElements();
@@ -759,8 +763,10 @@ bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
return true;
if (!Initializing) {
- unsigned LocalIndex = allocateTemporary(E);
- if (!this->emitGetPtrLocal(LocalIndex, E))
+ std::optional<unsigned> LocalIndex = allocateTemporary(E);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
}
@@ -1118,8 +1124,10 @@ template <class Emitter>
bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
// Prepare storage for result.
if (!Initializing) {
- unsigned LocalIndex = allocateTemporary(E);
- if (!this->emitGetPtrLocal(LocalIndex, E))
+ std::optional<unsigned> LocalIndex = allocateTemporary(E);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
}
@@ -1175,7 +1183,10 @@ bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
if (!LHSIsComplex) {
// This is using the RHS type for the fake-complex LHS.
- LHSOffset = allocateTemporary(RHS);
+ std::optional<unsigned> LocalIndex = allocateTemporary(RHS);
+ if (!LocalIndex)
+ return false;
+ LHSOffset = *LocalIndex;
if (!this->emitGetPtrLocal(LHSOffset, E))
return false;
@@ -1347,8 +1358,10 @@ bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
// Prepare storage for result.
if (!Initializing && !E->isCompoundAssignmentOp()) {
- unsigned LocalIndex = allocateTemporary(E);
- if (!this->emitGetPtrLocal(LocalIndex, E))
+ std::optional<unsigned> LocalIndex = allocateTemporary(E);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
return false;
}
@@ -4170,14 +4183,16 @@ Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
}
template <class Emitter>
-unsigned Compiler<Emitter>::allocateTemporary(const Expr *E) {
+std::optional<unsigned> Compiler<Emitter>::allocateTemporary(const Expr *E) {
QualType Ty = E->getType();
assert(!Ty->isRecordType());
Descriptor *D = P.createDescriptor(
E, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
/*IsTemporary=*/true, /*IsMutable=*/false, /*Init=*/nullptr);
- assert(D);
+
+ if (!D)
+ return std::nullopt;
Scope::Local Local = this->createLocal(D);
VariableScope<Emitter> *S = VarScope;
diff --git a/clang/lib/AST/ByteCode/Compiler.h b/clang/lib/AST/ByteCode/Compiler.h
index f9a597a16ef4ae..5a02f38d78dec8 100644
--- a/clang/lib/AST/ByteCode/Compiler.h
+++ b/clang/lib/AST/ByteCode/Compiler.h
@@ -309,7 +309,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
std::optional<unsigned>
allocateLocal(DeclTy &&Decl, QualType Ty = QualType(),
const ValueDecl *ExtendingDecl = nullptr);
- unsigned allocateTemporary(const Expr *E);
+ std::optional<unsigned> allocateTemporary(const Expr *E);
private:
friend class VariableScope<Emitter>;
More information about the cfe-commits
mailing list