35#include "llvm/Support/Casting.h"
39#define DEBUG_TYPE "dataflow"
46 if (
Block ==
nullptr) {
50 if (!ACFG.isBlockReachable(*
Block))
52 if (
Block->getBlockID() == CurBlockID)
54 const auto &State = BlockToState[
Block->getBlockID()];
67 if (LHSValue == RHSValue && LHSValue)
75 if (
auto *LHSBool = dyn_cast_or_null<BoolValue>(LHSValue))
76 if (
auto *RHSBool = dyn_cast_or_null<BoolValue>(RHSValue))
77 return Env.
makeIff(*LHSBool, *RHSBool);
79 if (
auto *LHSPtr = dyn_cast_or_null<PointerValue>(LHSValue))
80 if (
auto *RHSPtr = dyn_cast_or_null<PointerValue>(RHSValue))
84 if (&LHSPtr->getPointeeLoc() == &RHSPtr->getPointeeLoc())
91 if (
auto *Top = llvm::dyn_cast<TopBoolValue>(&
V)) {
107 auto *B = dyn_cast_or_null<BoolValue>(Val);
112 if (&UnpackedVal == Val)
147 TransferVisitor(
const StmtToEnvMap &StmtToEnv, Environment &Env,
148 Environment::ValueModel &Model)
149 : StmtToEnv(StmtToEnv), Env(Env), Model(Model) {}
151 void VisitBinaryOperator(
const BinaryOperator *S) {
152 const Expr *LHS = S->getLHS();
153 assert(LHS !=
nullptr);
155 const Expr *RHS = S->getRHS();
156 assert(RHS !=
nullptr);
162 if (S->isCompoundAssignmentOp())
165 switch (S->getOpcode()) {
167 auto *LHSLoc = Env.getStorageLocation(*LHS);
168 if (LHSLoc ==
nullptr)
171 auto *RHSVal = Env.getValue(*RHS);
172 if (RHSVal ==
nullptr) {
173 RHSVal = Env.createValue(LHS->getType());
174 if (RHSVal ==
nullptr) {
178 Env.clearValue(*LHSLoc);
184 Env.setValue(*LHSLoc, *RHSVal);
187 Env.setStorageLocation(*S, *LHSLoc);
192 BoolValue &LHSVal = getLogicOperatorSubExprValue(*LHS);
193 BoolValue &RHSVal = getLogicOperatorSubExprValue(*RHS);
195 if (S->getOpcode() == BO_LAnd)
196 Env.setValue(*S, Env.makeAnd(LHSVal, RHSVal));
198 Env.setValue(*S, Env.makeOr(LHSVal, RHSVal));
204 Env.setValue(*S, S->getOpcode() == BO_EQ ? LHSEqRHSValue
205 : Env.makeNot(LHSEqRHSValue));
217 void VisitDeclRefExpr(
const DeclRefExpr *S) {
218 const ValueDecl *VD = S->getDecl();
219 assert(VD !=
nullptr);
233 auto *DeclLoc = Env.getStorageLocation(*VD);
234 if (DeclLoc ==
nullptr)
237 Env.setStorageLocation(*S, *DeclLoc);
240 void VisitDeclStmt(
const DeclStmt *S) {
248 void ProcessVarDecl(
const VarDecl &D) {
250 if (D.hasGlobalStorage())
256 if (D.getType()->isReferenceType() && Env.getStorageLocation(D) !=
nullptr)
259 assert(Env.getStorageLocation(D) ==
nullptr);
261 Env.setStorageLocation(D, Env.createObject(D));
266 if (
const auto *Decomp = dyn_cast<DecompositionDecl>(&D)) {
272 for (
const auto *B : Decomp->bindings()) {
273 if (
auto *ME = dyn_cast_or_null<MemberExpr>(B->getBinding())) {
274 auto *DE = dyn_cast_or_null<DeclRefExpr>(ME->getBase());
280 VisitDeclRefExpr(DE);
283 if (
auto *Loc = Env.getStorageLocation(*ME))
284 Env.setStorageLocation(*B, *Loc);
285 }
else if (
auto *VD = B->getHoldingVar()) {
293 auto *VDLoc = Env.getStorageLocation(*VD);
294 assert(VDLoc !=
nullptr);
295 Env.setStorageLocation(*B, *VDLoc);
301 void VisitCastExpr(
const CastExpr *S) {
302 const Expr *SubExpr = S->getSubExpr();
303 assert(SubExpr !=
nullptr);
305 switch (S->getCastKind()) {
306 case CK_IntegralToBoolean: {
310 if (
auto *SubExprVal =
311 dyn_cast_or_null<BoolValue>(Env.getValue(*SubExpr)))
312 Env.setValue(*S, *SubExprVal);
316 Env.setValue(*S, Env.makeAtomicBoolValue());
320 case CK_LValueToRValue: {
324 if (SubExprVal ==
nullptr)
327 Env.setValue(*S, *SubExprVal);
331 case CK_BaseToDerived: {
336 RecordStorageLocation *Loc =
nullptr;
337 if (S->getType()->isPointerType()) {
338 auto *PV = Env.get<PointerValue>(*SubExpr);
343 assert(S->getType()->isRecordType());
344 if (SubExpr->isGLValue()) {
345 Loc = Env.get<RecordStorageLocation>(*SubExpr);
347 Loc = &Env.getResultObjectLocation(*SubExpr);
356 QualType Derived = S->getType().getNonReferenceType();
357 if (Derived->isPointerType()) {
358 Derived = Derived->getPointeeType();
363 for (
const FieldDecl *Field :
364 Env.getDataflowAnalysisContext().getModeledFields(Derived)) {
365 assert(Field !=
nullptr);
366 QualType FieldType =
Field->getType();
367 if (FieldType->isReferenceType()) {
368 Loc->addChild(*Field,
nullptr);
370 Loc->addChild(*Field, &Env.createStorageLocation(FieldType));
373 for (
const auto &Entry :
374 Env.getDataflowAnalysisContext().getSyntheticFields(Derived)) {
375 Loc->addSyntheticField(Entry.getKey(),
376 Env.createStorageLocation(Entry.getValue()));
379 Env.initializeFieldsWithValues(*Loc, Derived);
384 case CK_IntegralCast:
390 case CK_UncheckedDerivedToBase:
391 case CK_DerivedToBase:
392 case CK_ConstructorConversion:
393 case CK_UserDefinedConversion:
401 case CK_NullToPointer: {
402 auto &NullPointerVal =
403 Env.getOrCreateNullPointerValue(S->getType()->getPointeeType());
404 Env.setValue(*S, NullPointerVal);
407 case CK_NullToMemberPointer:
411 case CK_FunctionToPointerDecay: {
412 StorageLocation *PointeeLoc = Env.getStorageLocation(*SubExpr);
413 if (PointeeLoc ==
nullptr)
416 Env.setValue(*S, Env.create<PointerValue>(*PointeeLoc));
419 case CK_BuiltinFnToFnPtr:
430 void VisitUnaryOperator(
const UnaryOperator *S) {
431 const Expr *SubExpr = S->getSubExpr();
432 assert(SubExpr !=
nullptr);
434 switch (S->getOpcode()) {
436 const auto *SubExprVal = Env.get<PointerValue>(*SubExpr);
437 if (SubExprVal ==
nullptr)
440 Env.setStorageLocation(*S, SubExprVal->getPointeeLoc());
445 if (S->getType()->isMemberPointerType())
448 if (StorageLocation *PointeeLoc = Env.getStorageLocation(*SubExpr))
449 Env.setValue(*S, Env.create<PointerValue>(*PointeeLoc));
453 auto *SubExprVal = dyn_cast_or_null<BoolValue>(Env.getValue(*SubExpr));
454 if (SubExprVal ==
nullptr)
457 Env.setValue(*S, Env.makeNot(*SubExprVal));
467 if (StorageLocation *Loc = Env.getStorageLocation(*S->getSubExpr()))
468 Env.clearValue(*Loc);
476 if (StorageLocation *Loc = Env.getStorageLocation(*S->getSubExpr()))
477 Env.clearValue(*Loc);
484 void VisitCXXThisExpr(
const CXXThisExpr *S) {
485 auto *ThisPointeeLoc = Env.getThisPointeeStorageLocation();
486 if (ThisPointeeLoc ==
nullptr)
491 Env.setValue(*S, Env.create<PointerValue>(*ThisPointeeLoc));
494 void VisitCXXNewExpr(
const CXXNewExpr *S) {
495 if (
Value *Val = Env.createValue(S->getType()))
496 Env.setValue(*S, *Val);
499 void VisitCXXDeleteExpr(
const CXXDeleteExpr *S) {
506 void VisitReturnStmt(
const ReturnStmt *S) {
507 if (!Env.getDataflowAnalysisContext().getOptions().ContextSensitiveOpts)
510 auto *
Ret = S->getRetValue();
514 if (
Ret->isPRValue()) {
515 if (
Ret->getType()->isRecordType())
518 auto *Val = Env.getValue(*Ret);
523 Env.setReturnValue(Val);
525 auto *Loc = Env.getStorageLocation(*Ret);
530 Env.setReturnStorageLocation(Loc);
534 void VisitMemberExpr(
const MemberExpr *S) {
535 ValueDecl *
Member = S->getMemberDecl();
536 assert(
Member !=
nullptr);
539 if (
Member->isFunctionOrFunctionTemplate())
546 if (
auto *D = dyn_cast<VarDecl>(
Member)) {
547 if (D->hasGlobalStorage()) {
548 auto *VarDeclLoc = Env.getStorageLocation(*D);
549 if (VarDeclLoc ==
nullptr)
552 Env.setStorageLocation(*S, *VarDeclLoc);
558 if (BaseLoc ==
nullptr)
561 auto *MemberLoc = BaseLoc->getChild(*
Member);
562 if (MemberLoc ==
nullptr)
564 Env.setStorageLocation(*S, *MemberLoc);
567 void VisitCXXDefaultArgExpr(
const CXXDefaultArgExpr *S) {
568 const Expr *ArgExpr = S->getExpr();
569 assert(ArgExpr !=
nullptr);
572 if (S->isPRValue() && S->getType()->isRecordType()) {
573 auto &Loc = Env.getResultObjectLocation(*S);
574 Env.initializeFieldsWithValues(Loc);
578 void VisitCXXDefaultInitExpr(
const CXXDefaultInitExpr *S) {
579 const Expr *InitExpr = S->getExpr();
580 assert(InitExpr !=
nullptr);
585 if (S->getType()->isRecordType() && S->isPRValue())
591 void VisitCXXConstructExpr(
const CXXConstructExpr *S) {
592 const CXXConstructorDecl *ConstructorDecl = S->getConstructor();
593 assert(ConstructorDecl !=
nullptr);
598 if (!S->getType()->isRecordType()) {
599 transferInlineCall(S, ConstructorDecl);
603 RecordStorageLocation &Loc = Env.getResultObjectLocation(*S);
605 if (ConstructorDecl->isCopyOrMoveConstructor()) {
608 assert(S->getNumArgs() != 0);
610 const Expr *Arg = S->getArg(0);
611 assert(Arg !=
nullptr);
613 auto *ArgLoc = Env.get<RecordStorageLocation>(*Arg);
614 if (ArgLoc ==
nullptr)
632 Env.initializeFieldsWithValues(Loc, S->getType());
634 transferInlineCall(S, ConstructorDecl);
637 void VisitCXXOperatorCallExpr(
const CXXOperatorCallExpr *S) {
638 if (S->getOperator() == OO_Equal) {
639 assert(S->getNumArgs() == 2);
641 const Expr *Arg0 = S->getArg(0);
642 assert(Arg0 !=
nullptr);
644 const Expr *Arg1 = S->getArg(1);
645 assert(Arg1 !=
nullptr);
649 dyn_cast_or_null<CXXMethodDecl>(S->getDirectCallee());
652 if (!
Method->isCopyAssignmentOperator() &&
653 !
Method->isMoveAssignmentOperator())
656 RecordStorageLocation *LocSrc =
nullptr;
657 if (Arg1->isPRValue()) {
658 LocSrc = &Env.getResultObjectLocation(*Arg1);
660 LocSrc = Env.get<RecordStorageLocation>(*Arg1);
662 auto *LocDst = Env.get<RecordStorageLocation>(*Arg0);
664 if (LocSrc ==
nullptr || LocDst ==
nullptr)
672 copyRecord(*LocSrc, *LocDst, Env, Arg0->getType());
677 if (S->getType().getCanonicalType().getUnqualifiedType() !=
678 LocDst->getType().getCanonicalType().getUnqualifiedType()) {
679 auto ReturnDecl = S->getType()->getAsCXXRecordDecl();
680 auto DstDecl = LocDst->getType()->getAsCXXRecordDecl();
681 if (ReturnDecl ==
nullptr || DstDecl ==
nullptr)
683 if (!DstDecl->isDerivedFrom(ReturnDecl))
688 Env.setStorageLocation(*S, *LocDst);
690 copyRecord(*LocDst, Env.getResultObjectLocation(*S), Env);
700 void VisitCXXRewrittenBinaryOperator(
const CXXRewrittenBinaryOperator *RBO) {
704 void VisitCallExpr(
const CallExpr *S) {
708 if (S->isCallToStdMove()) {
709 assert(S->getNumArgs() == 1);
711 const Expr *Arg = S->getArg(0);
712 assert(Arg !=
nullptr);
714 auto *ArgLoc = Env.getStorageLocation(*Arg);
715 if (ArgLoc ==
nullptr)
718 Env.setStorageLocation(*S, *ArgLoc);
719 }
else if (S->getDirectCallee() !=
nullptr &&
720 S->getDirectCallee()->getBuiltinID() ==
721 Builtin::BI__builtin_expect) {
722 assert(S->getNumArgs() > 0);
723 assert(S->getArg(0) !=
nullptr);
724 auto *ArgVal = Env.getValue(*S->getArg(0));
725 if (ArgVal ==
nullptr)
727 Env.setValue(*S, *ArgVal);
728 }
else if (
const FunctionDecl *F = S->getDirectCallee()) {
729 transferInlineCall(S, F);
733 if (S->getType()->isRecordType() && S->isPRValue()) {
734 RecordStorageLocation &Loc = Env.getResultObjectLocation(*S);
735 Env.initializeFieldsWithValues(Loc);
740 void VisitMaterializeTemporaryExpr(
const MaterializeTemporaryExpr *S) {
741 const Expr *SubExpr = S->getSubExpr();
742 assert(SubExpr !=
nullptr);
744 StorageLocation &Loc = Env.createStorageLocation(*S);
745 Env.setStorageLocation(*S, Loc);
747 if (SubExpr->getType()->isRecordType())
752 if (
Value *SubExprVal = Env.getValue(*SubExpr))
753 Env.setValue(Loc, *SubExprVal);
756 void VisitCXXBindTemporaryExpr(
const CXXBindTemporaryExpr *S) {
757 const Expr *SubExpr = S->getSubExpr();
758 assert(SubExpr !=
nullptr);
763 void VisitConditionalOperator(
const ConditionalOperator *S) {
764 const Environment *TrueEnv = StmtToEnv.getEnvironment(*S->getTrueExpr());
765 const Environment *FalseEnv = StmtToEnv.getEnvironment(*S->getFalseExpr());
767 if (TrueEnv ==
nullptr || FalseEnv ==
nullptr) {
775 if (S->isGLValue()) {
776 StorageLocation *TrueLoc = TrueEnv->getStorageLocation(*S->getTrueExpr());
777 StorageLocation *FalseLoc =
778 FalseEnv->getStorageLocation(*S->getFalseExpr());
779 if (TrueLoc == FalseLoc && TrueLoc !=
nullptr) {
780 Env.setStorageLocation(*S, *TrueLoc);
781 }
else if (!S->getType()->isRecordType()) {
794 S->getType(), TrueEnv->getValue(*S->getTrueExpr()), *TrueEnv,
795 FalseEnv->getValue(*S->getFalseExpr()), *FalseEnv, Env,
797 StorageLocation &Loc = Env.createStorageLocation(*S);
798 Env.setStorageLocation(*S, Loc);
799 Env.setValue(Loc, *Val);
802 }
else if (!S->getType()->isRecordType()) {
819 S->getType(), TrueEnv->getValue(*S->getTrueExpr()), *TrueEnv,
820 FalseEnv->getValue(*S->getFalseExpr()), *FalseEnv, Env, Model))
821 Env.setValue(*S, *Val);
825 void VisitInitListExpr(
const InitListExpr *S) {
826 QualType
Type = S->getType();
828 if (!
Type->isRecordType()) {
831 if (!
Type->isArrayType() && S->getNumInits() == 1)
837 if (S->isSemanticForm() && S->isTransparent())
840 RecordStorageLocation &Loc = Env.getResultObjectLocation(*S);
847 RecordInitListHelper InitListHelper(S);
849 for (
auto [Field,
Init] : InitListHelper.field_inits()) {
850 if (
Field->getType()->isRecordType())
852 if (
Field->getType()->isReferenceType()) {
853 assert(
Field->getType().getCanonicalType()->getPointeeType() ==
854 Init->getType().getCanonicalType());
855 Loc.setChild(*Field, &Env.createObject(
Field->getType(),
Init));
858 assert(
Field->getType().getCanonicalType().getUnqualifiedType() ==
859 Init->getType().getCanonicalType().getUnqualifiedType());
860 StorageLocation *FieldLoc = Loc.getChild(*Field);
862 assert(FieldLoc !=
nullptr);
865 Init->getType()->isPointerType())
867 &Env.getOrCreateNullPointerValue(
Init->getType()->getPointeeType());
869 Val = Env.createValue(
Field->getType());
871 Env.setValue(*FieldLoc, *Val);
874 for (
const auto &[FieldName, FieldLoc] : Loc.synthetic_fields()) {
875 QualType FieldType = FieldLoc->getType();
876 if (FieldType->isRecordType()) {
879 if (
Value *Val = Env.createValue(FieldType))
880 Env.setValue(*FieldLoc, *Val);
887 void VisitCXXBoolLiteralExpr(
const CXXBoolLiteralExpr *S) {
888 Env.setValue(*S, Env.getBoolLiteralValue(S->getValue()));
891 void VisitIntegerLiteral(
const IntegerLiteral *S) {
892 Env.setValue(*S, Env.getIntLiteralValue(S->getValue()));
895 void VisitParenExpr(
const ParenExpr *S) {
899 auto *SubExpr = S->getSubExpr();
900 assert(SubExpr !=
nullptr);
904 void VisitExprWithCleanups(
const ExprWithCleanups *S) {
908 auto *SubExpr = S->getSubExpr();
909 assert(SubExpr !=
nullptr);
915 BoolValue &getLogicOperatorSubExprValue(
const Expr &SubExpr) {
919 if (
const Environment *SubExprEnv = StmtToEnv.getEnvironment(SubExpr))
921 dyn_cast_or_null<BoolValue>(SubExprEnv->getValue(SubExpr)))
929 if (Env.getValue(SubExpr) ==
nullptr)
931 if (
auto *Val = dyn_cast_or_null<BoolValue>(Env.getValue(SubExpr)))
936 return Env.makeAtomicBoolValue();
941 template <
typename E>
942 void transferInlineCall(
const E *S,
const FunctionDecl *F) {
943 const auto &Options = Env.getDataflowAnalysisContext().getOptions();
944 if (!(Options.ContextSensitiveOpts &&
945 Env.canDescend(Options.ContextSensitiveOpts->Depth, F)))
948 const AdornedCFG *ACFG = Env.getDataflowAnalysisContext().getAdornedCFG(F);
956 auto ExitBlock = ACFG->getCFG().getExit().getBlockID();
958 auto CalleeEnv = Env.pushCall(S);
963 auto Analysis = NoopAnalysis(ACFG->getDecl().getASTContext(),
964 DataflowAnalysisOptions{Options});
966 auto BlockToOutputState =
968 assert(BlockToOutputState);
969 assert(ExitBlock < BlockToOutputState->size());
971 auto &ExitState = (*BlockToOutputState)[ExitBlock];
974 Env.popCall(S, ExitState->Env);
977 const StmtToEnvMap &StmtToEnv;
979 Environment::ValueModel &Model;
986 TransferVisitor(StmtToEnv, Env, Model).Visit(&S);
Defines enum values for all the target-independent builtin functions.
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines an enumeration for C++ overloaded operators.
C Language Family Type Representation.
Represents a single basic block in a source-level CFG.
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
This represents one expression.
Stmt - This represents one statement.
bool isNullPtrType() const
bool isRecordType() const
BoolValue & makeBoolValue(const Formula &)
Creates a BoolValue wrapping a particular formula.
Supplements Environment with non-standard comparison and join operations.
Holds the state of the program (store and heap) at a given program point.
BoolValue & makeIff(BoolValue &LHS, BoolValue &RHS) const
Returns a boolean value represents LHS <=> RHS.
StorageLocation * getStorageLocation(const ValueDecl &D) const
Returns the storage location assigned to D in the environment, or null if D isn't assigned a storage ...
BoolValue & makeAtomicBoolValue() const
Returns an atomic boolean value.
Value * getValue(const StorageLocation &Loc) const
Returns the value assigned to Loc in the environment or null if Loc isn't assigned a value in the env...
BoolValue & getBoolLiteralValue(bool Value) const
Returns a symbolic boolean value that models a boolean literal equal to Value
DataflowAnalysisContext & getDataflowAnalysisContext() const
Returns the DataflowAnalysisContext used by the environment.
static Value * joinValues(QualType Ty, Value *Val1, const Environment &Env1, Value *Val2, const Environment &Env2, Environment &JoinedEnv, Environment::ValueModel &Model)
Returns a value that approximates both Val1 and Val2, or null if no such value can be produced.
void setStorageLocation(const ValueDecl &D, StorageLocation &Loc)
Assigns Loc as the storage location of D in the environment.
void setValue(const StorageLocation &Loc, Value &Val)
Assigns Val as the value of Loc in the environment.
Maps statements to the environments of basic blocks that contain them.
const Environment * getEnvironment(const Stmt &S) const
Returns the environment of the basic block that contains S.
Base class for all values computed by abstract interpretation.
void transfer(const StmtToEnvMap &StmtToEnv, const Stmt &S, Environment &Env, Environment::ValueModel &Model)
Evaluates S and updates Env accordingly.
static void propagateValueOrStorageLocation(const Expr &From, const Expr &To, Environment &Env)
static void propagateStorageLocation(const Expr &From, const Expr &To, Environment &Env)
void copyRecord(RecordStorageLocation &Src, RecordStorageLocation &Dst, Environment &Env, QualType TypeToCopy=QualType())
Copies a record (struct, class, or union) from Src to Dst.
static void propagateValue(const Expr &From, const Expr &To, Environment &Env)
static Value * maybeUnpackLValueExpr(const Expr &E, Environment &Env)
static BoolValue & unpackValue(BoolValue &V, Environment &Env)
static BoolValue & evaluateBooleanEquality(const Expr &LHS, const Expr &RHS, Environment &Env)
RecordStorageLocation * getBaseObjectLocation(const MemberExpr &ME, const Environment &Env)
Returns the storage location for the base object of a MemberExpr, or null if none is defined in the e...
llvm::Expected< std::vector< std::optional< DataflowAnalysisState< typename AnalysisT::Lattice > > > > runDataflowAnalysis(const AdornedCFG &ACFG, AnalysisT &Analysis, const Environment &InitEnv, CFGEltCallbacks< AnalysisT > PostAnalysisCallbacks={}, std::int32_t MaxBlockVisits=kDefaultMaxBlockVisits)
Performs dataflow analysis and returns a mapping from basic block IDs to dataflow analysis states tha...
bool Ret(InterpState &S, CodePtr &PC)
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
@ Type
The name was classified as a type.
U cast(CodeGen::Address addr)