-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[clang][bytecode] Bail out from type-punning casts #163809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,6 +212,10 @@ template <class Emitter> | |
bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) { | ||
const Expr *SubExpr = CE->getSubExpr(); | ||
|
||
if (isPunningDereference(SubExpr)) | ||
return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, | ||
CE); | ||
|
||
if (DiscardResult) | ||
return this->delegate(SubExpr); | ||
|
||
|
@@ -5511,6 +5515,46 @@ bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) { | |
return true; | ||
} | ||
|
||
template <class Emitter> | ||
bool Compiler<Emitter>::isPunningDereference(const Expr *E) | ||
{ | ||
E = E->IgnoreParenImpCasts(); | ||
|
||
const auto *UO = dyn_cast<UnaryOperator>(E); | ||
if (!UO || UO->getOpcode() != UO_Deref) | ||
return false; | ||
|
||
const Expr *Base = UO->getSubExpr()->IgnoreParenImpCasts(); | ||
const auto *Cast = dyn_cast<CastExpr>(Base); | ||
if (!Cast) | ||
return false; | ||
|
||
// Only consider reinterpret-ish casts | ||
switch (Cast->getCastKind()) { | ||
case CK_BitCast: | ||
case CK_PointerToIntegral: | ||
case CK_IntegralToPointer: | ||
case CK_AddressSpaceConversion: | ||
break; | ||
default: | ||
return false; // CK_NoOp etc. are fine | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How did you come up with this list? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's tentative. I think we need to check for actual UBness of the cast, as there are cases where some casts are legal and should be allowed in CE For a POC to see if this fixes the issue I linked, I just put everything that remotely resembles a fishy cast |
||
|
||
QualType DestPtrTy = Cast->getType(); | ||
QualType SrcPtrTy = Cast->getSubExpr()->getType(); | ||
if (!DestPtrTy->isPointerType() || !SrcPtrTy->isPointerType()) | ||
return true; // super fishy, treat it as a pun | ||
|
||
QualType DestPointee = DestPtrTy->getPointeeType(); | ||
QualType SrcPointee = SrcPtrTy->getPointeeType(); | ||
|
||
// If pointee types differ (ignoring qualifiers), its a pun | ||
if (!Ctx.getASTContext().hasSameUnqualifiedType(DestPointee, SrcPointee)) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
static bool hasTrivialDefaultCtorParent(const FieldDecl *FD) { | ||
assert(FD); | ||
assert(FD->getParent()->isUnion()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to call this when we already switch'ed for the cast kind later.