diff options
author | Ziqing Luo <ziqing@udel.edu> | 2025-05-13 10:24:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-13 10:24:02 -0700 |
commit | 41229581a456619c341daca8354b8c9ae1b150df (patch) | |
tree | d15d2fe3d8eab2380a9349c217ce084b6ee454ff /clang/lib/StaticAnalyzer | |
parent | 831592d6171bc62f6991387546512b9cf2ce1c9e (diff) | |
download | llvm-41229581a456619c341daca8354b8c9ae1b150df.zip llvm-41229581a456619c341daca8354b8c9ae1b150df.tar.gz llvm-41229581a456619c341daca8354b8c9ae1b150df.tar.bz2 |
[analyzer] Fix crashing __builtin_bit_cast (#139188)
Previously, CSA did not handle __builtin_bit_cast correctly. It
evaluated the LvalueToRvalue conversion for the casting expression,
but did not actually convert the value of the expression to be of the
destination type.
This commit fixes the problem.
rdar://149987320
Diffstat (limited to 'clang/lib/StaticAnalyzer')
-rw-r--r-- | clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp | 54 |
1 files changed, 43 insertions, 11 deletions
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp index 6e52df5..b91954e 100644 --- a/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp +++ b/clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp @@ -286,15 +286,47 @@ ProgramStateRef ExprEngine::handleLValueBitCast( void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex, ExplodedNode *Pred, ExplodedNodeSet &Dst) { - ExplodedNodeSet dstPreStmt; - getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this); - - if (CastE->getCastKind() == CK_LValueToRValue || - CastE->getCastKind() == CK_LValueToRValueBitCast) { - for (ExplodedNode *subExprNode : dstPreStmt) { - ProgramStateRef state = subExprNode->getState(); - const LocationContext *LCtx = subExprNode->getLocationContext(); - evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx)); + ExplodedNodeSet DstPreStmt; + getCheckerManager().runCheckersForPreStmt(DstPreStmt, Pred, CastE, *this); + + if (CastE->getCastKind() == CK_LValueToRValue) { + for (ExplodedNode *Node : DstPreStmt) { + ProgramStateRef State = Node->getState(); + const LocationContext *LCtx = Node->getLocationContext(); + evalLoad(Dst, CastE, CastE, Node, State, State->getSVal(Ex, LCtx)); + } + return; + } + if (CastE->getCastKind() == CK_LValueToRValueBitCast) { + // Handle `__builtin_bit_cast`: + ExplodedNodeSet DstEvalLoc; + + // Simulate the lvalue-to-rvalue conversion on `Ex`: + for (ExplodedNode *Node : DstPreStmt) { + ProgramStateRef State = Node->getState(); + const LocationContext *LCtx = Node->getLocationContext(); + evalLocation(DstEvalLoc, CastE, Ex, Node, State, State->getSVal(Ex, LCtx), + true); + } + // Simulate the operation that actually casts the original value to a new + // value of the destination type : + StmtNodeBuilder Bldr(DstEvalLoc, Dst, *currBldrCtx); + + for (ExplodedNode *Node : DstEvalLoc) { + ProgramStateRef State = Node->getState(); + const LocationContext *LCtx = Node->getLocationContext(); + // Although `Ex` is an lvalue, it could have `Loc::ConcreteInt` kind + // (e.g., `(int *)123456`). In such cases, there is no MemRegion + // available and we can't get the value to be casted. + SVal CastedV = UnknownVal(); + + if (const MemRegion *MR = State->getSVal(Ex, LCtx).getAsRegion()) { + SVal OrigV = State->getSVal(MR); + CastedV = svalBuilder.evalCast(svalBuilder.simplifySVal(State, OrigV), + CastE->getType(), Ex->getType()); + } + State = State->BindExpr(CastE, LCtx, CastedV); + Bldr.generateNode(CastE, Node, State); } return; } @@ -306,8 +338,8 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex, if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE)) T = ExCast->getTypeAsWritten(); - StmtNodeBuilder Bldr(dstPreStmt, Dst, *currBldrCtx); - for (ExplodedNode *Pred : dstPreStmt) { + StmtNodeBuilder Bldr(DstPreStmt, Dst, *currBldrCtx); + for (ExplodedNode *Pred : DstPreStmt) { ProgramStateRef state = Pred->getState(); const LocationContext *LCtx = Pred->getLocationContext(); |