ExprEngineC.cpp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. //=-- ExprEngineC.cpp - ExprEngine support for C expressions ----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines ExprEngine's support for C expressions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ExprCXX.h"
  14. #include "clang/AST/DeclCXX.h"
  15. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  17. using namespace clang;
  18. using namespace ento;
  19. using llvm::APSInt;
  20. /// Optionally conjure and return a symbol for offset when processing
  21. /// an expression \p Expression.
  22. /// If \p Other is a location, conjure a symbol for \p Symbol
  23. /// (offset) if it is unknown so that memory arithmetic always
  24. /// results in an ElementRegion.
  25. /// \p Count The number of times the current basic block was visited.
  26. static SVal conjureOffsetSymbolOnLocation(
  27. SVal Symbol, SVal Other, Expr* Expression, SValBuilder &svalBuilder,
  28. unsigned Count, const LocationContext *LCtx) {
  29. QualType Ty = Expression->getType();
  30. if (Other.getAs<Loc>() &&
  31. Ty->isIntegralOrEnumerationType() &&
  32. Symbol.isUnknown()) {
  33. return svalBuilder.conjureSymbolVal(Expression, LCtx, Ty, Count);
  34. }
  35. return Symbol;
  36. }
  37. void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
  38. ExplodedNode *Pred,
  39. ExplodedNodeSet &Dst) {
  40. Expr *LHS = B->getLHS()->IgnoreParens();
  41. Expr *RHS = B->getRHS()->IgnoreParens();
  42. // FIXME: Prechecks eventually go in ::Visit().
  43. ExplodedNodeSet CheckedSet;
  44. ExplodedNodeSet Tmp2;
  45. getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, B, *this);
  46. // With both the LHS and RHS evaluated, process the operation itself.
  47. for (ExplodedNodeSet::iterator it=CheckedSet.begin(), ei=CheckedSet.end();
  48. it != ei; ++it) {
  49. ProgramStateRef state = (*it)->getState();
  50. const LocationContext *LCtx = (*it)->getLocationContext();
  51. SVal LeftV = state->getSVal(LHS, LCtx);
  52. SVal RightV = state->getSVal(RHS, LCtx);
  53. BinaryOperator::Opcode Op = B->getOpcode();
  54. if (Op == BO_Assign) {
  55. // EXPERIMENTAL: "Conjured" symbols.
  56. // FIXME: Handle structs.
  57. if (RightV.isUnknown()) {
  58. unsigned Count = currBldrCtx->blockCount();
  59. RightV = svalBuilder.conjureSymbolVal(nullptr, B->getRHS(), LCtx,
  60. Count);
  61. }
  62. // Simulate the effects of a "store": bind the value of the RHS
  63. // to the L-Value represented by the LHS.
  64. SVal ExprVal = B->isGLValue() ? LeftV : RightV;
  65. evalStore(Tmp2, B, LHS, *it, state->BindExpr(B, LCtx, ExprVal),
  66. LeftV, RightV);
  67. continue;
  68. }
  69. if (!B->isAssignmentOp()) {
  70. StmtNodeBuilder Bldr(*it, Tmp2, *currBldrCtx);
  71. if (B->isAdditiveOp()) {
  72. // TODO: This can be removed after we enable history tracking with
  73. // SymSymExpr.
  74. unsigned Count = currBldrCtx->blockCount();
  75. RightV = conjureOffsetSymbolOnLocation(
  76. RightV, LeftV, RHS, svalBuilder, Count, LCtx);
  77. LeftV = conjureOffsetSymbolOnLocation(
  78. LeftV, RightV, LHS, svalBuilder, Count, LCtx);
  79. }
  80. // Although we don't yet model pointers-to-members, we do need to make
  81. // sure that the members of temporaries have a valid 'this' pointer for
  82. // other checks.
  83. if (B->getOpcode() == BO_PtrMemD)
  84. state = createTemporaryRegionIfNeeded(state, LCtx, LHS);
  85. // Process non-assignments except commas or short-circuited
  86. // logical expressions (LAnd and LOr).
  87. SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType());
  88. if (!Result.isUnknown()) {
  89. state = state->BindExpr(B, LCtx, Result);
  90. }
  91. Bldr.generateNode(B, *it, state);
  92. continue;
  93. }
  94. assert (B->isCompoundAssignmentOp());
  95. switch (Op) {
  96. default:
  97. llvm_unreachable("Invalid opcode for compound assignment.");
  98. case BO_MulAssign: Op = BO_Mul; break;
  99. case BO_DivAssign: Op = BO_Div; break;
  100. case BO_RemAssign: Op = BO_Rem; break;
  101. case BO_AddAssign: Op = BO_Add; break;
  102. case BO_SubAssign: Op = BO_Sub; break;
  103. case BO_ShlAssign: Op = BO_Shl; break;
  104. case BO_ShrAssign: Op = BO_Shr; break;
  105. case BO_AndAssign: Op = BO_And; break;
  106. case BO_XorAssign: Op = BO_Xor; break;
  107. case BO_OrAssign: Op = BO_Or; break;
  108. }
  109. // Perform a load (the LHS). This performs the checks for
  110. // null dereferences, and so on.
  111. ExplodedNodeSet Tmp;
  112. SVal location = LeftV;
  113. evalLoad(Tmp, B, LHS, *it, state, location);
  114. for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E;
  115. ++I) {
  116. state = (*I)->getState();
  117. const LocationContext *LCtx = (*I)->getLocationContext();
  118. SVal V = state->getSVal(LHS, LCtx);
  119. // Get the computation type.
  120. QualType CTy =
  121. cast<CompoundAssignOperator>(B)->getComputationResultType();
  122. CTy = getContext().getCanonicalType(CTy);
  123. QualType CLHSTy =
  124. cast<CompoundAssignOperator>(B)->getComputationLHSType();
  125. CLHSTy = getContext().getCanonicalType(CLHSTy);
  126. QualType LTy = getContext().getCanonicalType(LHS->getType());
  127. // Promote LHS.
  128. V = svalBuilder.evalCast(V, CLHSTy, LTy);
  129. // Compute the result of the operation.
  130. SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy),
  131. B->getType(), CTy);
  132. // EXPERIMENTAL: "Conjured" symbols.
  133. // FIXME: Handle structs.
  134. SVal LHSVal;
  135. if (Result.isUnknown()) {
  136. // The symbolic value is actually for the type of the left-hand side
  137. // expression, not the computation type, as this is the value the
  138. // LValue on the LHS will bind to.
  139. LHSVal = svalBuilder.conjureSymbolVal(nullptr, B->getRHS(), LCtx, LTy,
  140. currBldrCtx->blockCount());
  141. // However, we need to convert the symbol to the computation type.
  142. Result = svalBuilder.evalCast(LHSVal, CTy, LTy);
  143. }
  144. else {
  145. // The left-hand side may bind to a different value then the
  146. // computation type.
  147. LHSVal = svalBuilder.evalCast(Result, LTy, CTy);
  148. }
  149. // In C++, assignment and compound assignment operators return an
  150. // lvalue.
  151. if (B->isGLValue())
  152. state = state->BindExpr(B, LCtx, location);
  153. else
  154. state = state->BindExpr(B, LCtx, Result);
  155. evalStore(Tmp2, B, LHS, *I, state, location, LHSVal);
  156. }
  157. }
  158. // FIXME: postvisits eventually go in ::Visit()
  159. getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this);
  160. }
  161. void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
  162. ExplodedNodeSet &Dst) {
  163. CanQualType T = getContext().getCanonicalType(BE->getType());
  164. const BlockDecl *BD = BE->getBlockDecl();
  165. // Get the value of the block itself.
  166. SVal V = svalBuilder.getBlockPointer(BD, T,
  167. Pred->getLocationContext(),
  168. currBldrCtx->blockCount());
  169. ProgramStateRef State = Pred->getState();
  170. // If we created a new MemRegion for the block, we should explicitly bind
  171. // the captured variables.
  172. if (const BlockDataRegion *BDR =
  173. dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
  174. BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
  175. E = BDR->referenced_vars_end();
  176. auto CI = BD->capture_begin();
  177. auto CE = BD->capture_end();
  178. for (; I != E; ++I) {
  179. const VarRegion *capturedR = I.getCapturedRegion();
  180. const VarRegion *originalR = I.getOriginalRegion();
  181. // If the capture had a copy expression, use the result of evaluating
  182. // that expression, otherwise use the original value.
  183. // We rely on the invariant that the block declaration's capture variables
  184. // are a prefix of the BlockDataRegion's referenced vars (which may include
  185. // referenced globals, etc.) to enable fast lookup of the capture for a
  186. // given referenced var.
  187. const Expr *copyExpr = nullptr;
  188. if (CI != CE) {
  189. assert(CI->getVariable() == capturedR->getDecl());
  190. copyExpr = CI->getCopyExpr();
  191. CI++;
  192. }
  193. if (capturedR != originalR) {
  194. SVal originalV;
  195. const LocationContext *LCtx = Pred->getLocationContext();
  196. if (copyExpr) {
  197. originalV = State->getSVal(copyExpr, LCtx);
  198. } else {
  199. originalV = State->getSVal(loc::MemRegionVal(originalR));
  200. }
  201. State = State->bindLoc(loc::MemRegionVal(capturedR), originalV, LCtx);
  202. }
  203. }
  204. }
  205. ExplodedNodeSet Tmp;
  206. StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
  207. Bldr.generateNode(BE, Pred,
  208. State->BindExpr(BE, Pred->getLocationContext(), V),
  209. nullptr, ProgramPoint::PostLValueKind);
  210. // FIXME: Move all post/pre visits to ::Visit().
  211. getCheckerManager().runCheckersForPostStmt(Dst, Tmp, BE, *this);
  212. }
  213. ProgramStateRef ExprEngine::handleLValueBitCast(
  214. ProgramStateRef state, const Expr* Ex, const LocationContext* LCtx,
  215. QualType T, QualType ExTy, const CastExpr* CastE, StmtNodeBuilder& Bldr,
  216. ExplodedNode* Pred) {
  217. if (T->isLValueReferenceType()) {
  218. assert(!CastE->getType()->isLValueReferenceType());
  219. ExTy = getContext().getLValueReferenceType(ExTy);
  220. } else if (T->isRValueReferenceType()) {
  221. assert(!CastE->getType()->isRValueReferenceType());
  222. ExTy = getContext().getRValueReferenceType(ExTy);
  223. }
  224. // Delegate to SValBuilder to process.
  225. SVal OrigV = state->getSVal(Ex, LCtx);
  226. SVal V = svalBuilder.evalCast(OrigV, T, ExTy);
  227. // Negate the result if we're treating the boolean as a signed i1
  228. if (CastE->getCastKind() == CK_BooleanToSignedIntegral)
  229. V = evalMinus(V);
  230. state = state->BindExpr(CastE, LCtx, V);
  231. if (V.isUnknown() && !OrigV.isUnknown()) {
  232. state = escapeValue(state, OrigV, PSK_EscapeOther);
  233. }
  234. Bldr.generateNode(CastE, Pred, state);
  235. return state;
  236. }
  237. ProgramStateRef ExprEngine::handleLVectorSplat(
  238. ProgramStateRef state, const LocationContext* LCtx, const CastExpr* CastE,
  239. StmtNodeBuilder &Bldr, ExplodedNode* Pred) {
  240. // Recover some path sensitivity by conjuring a new value.
  241. QualType resultType = CastE->getType();
  242. if (CastE->isGLValue())
  243. resultType = getContext().getPointerType(resultType);
  244. SVal result = svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx,
  245. resultType,
  246. currBldrCtx->blockCount());
  247. state = state->BindExpr(CastE, LCtx, result);
  248. Bldr.generateNode(CastE, Pred, state);
  249. return state;
  250. }
  251. void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
  252. ExplodedNode *Pred, ExplodedNodeSet &Dst) {
  253. ExplodedNodeSet dstPreStmt;
  254. getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this);
  255. if (CastE->getCastKind() == CK_LValueToRValue) {
  256. for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
  257. I!=E; ++I) {
  258. ExplodedNode *subExprNode = *I;
  259. ProgramStateRef state = subExprNode->getState();
  260. const LocationContext *LCtx = subExprNode->getLocationContext();
  261. evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx));
  262. }
  263. return;
  264. }
  265. // All other casts.
  266. QualType T = CastE->getType();
  267. QualType ExTy = Ex->getType();
  268. if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
  269. T = ExCast->getTypeAsWritten();
  270. StmtNodeBuilder Bldr(dstPreStmt, Dst, *currBldrCtx);
  271. for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
  272. I != E; ++I) {
  273. Pred = *I;
  274. ProgramStateRef state = Pred->getState();
  275. const LocationContext *LCtx = Pred->getLocationContext();
  276. switch (CastE->getCastKind()) {
  277. case CK_LValueToRValue:
  278. llvm_unreachable("LValueToRValue casts handled earlier.");
  279. case CK_ToVoid:
  280. continue;
  281. // The analyzer doesn't do anything special with these casts,
  282. // since it understands retain/release semantics already.
  283. case CK_ARCProduceObject:
  284. case CK_ARCConsumeObject:
  285. case CK_ARCReclaimReturnedObject:
  286. case CK_ARCExtendBlockObject: // Fall-through.
  287. case CK_CopyAndAutoreleaseBlockObject:
  288. // The analyser can ignore atomic casts for now, although some future
  289. // checkers may want to make certain that you're not modifying the same
  290. // value through atomic and nonatomic pointers.
  291. case CK_AtomicToNonAtomic:
  292. case CK_NonAtomicToAtomic:
  293. // True no-ops.
  294. case CK_NoOp:
  295. case CK_ConstructorConversion:
  296. case CK_UserDefinedConversion:
  297. case CK_FunctionToPointerDecay:
  298. case CK_BuiltinFnToFnPtr: {
  299. // Copy the SVal of Ex to CastE.
  300. ProgramStateRef state = Pred->getState();
  301. const LocationContext *LCtx = Pred->getLocationContext();
  302. SVal V = state->getSVal(Ex, LCtx);
  303. state = state->BindExpr(CastE, LCtx, V);
  304. Bldr.generateNode(CastE, Pred, state);
  305. continue;
  306. }
  307. case CK_MemberPointerToBoolean:
  308. case CK_PointerToBoolean: {
  309. SVal V = state->getSVal(Ex, LCtx);
  310. auto PTMSV = V.getAs<nonloc::PointerToMember>();
  311. if (PTMSV)
  312. V = svalBuilder.makeTruthVal(!PTMSV->isNullMemberPointer(), ExTy);
  313. if (V.isUndef() || PTMSV) {
  314. state = state->BindExpr(CastE, LCtx, V);
  315. Bldr.generateNode(CastE, Pred, state);
  316. continue;
  317. }
  318. // Explicitly proceed with default handler for this case cascade.
  319. state =
  320. handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
  321. continue;
  322. }
  323. case CK_Dependent:
  324. case CK_ArrayToPointerDecay:
  325. case CK_BitCast:
  326. case CK_AddressSpaceConversion:
  327. case CK_BooleanToSignedIntegral:
  328. case CK_NullToPointer:
  329. case CK_IntegralToPointer:
  330. case CK_PointerToIntegral: {
  331. SVal V = state->getSVal(Ex, LCtx);
  332. if (V.getAs<nonloc::PointerToMember>()) {
  333. state = state->BindExpr(CastE, LCtx, UnknownVal());
  334. Bldr.generateNode(CastE, Pred, state);
  335. continue;
  336. }
  337. // Explicitly proceed with default handler for this case cascade.
  338. state =
  339. handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
  340. continue;
  341. }
  342. case CK_IntegralToBoolean:
  343. case CK_IntegralToFloating:
  344. case CK_FloatingToIntegral:
  345. case CK_FloatingToBoolean:
  346. case CK_FloatingCast:
  347. case CK_FloatingRealToComplex:
  348. case CK_FloatingComplexToReal:
  349. case CK_FloatingComplexToBoolean:
  350. case CK_FloatingComplexCast:
  351. case CK_FloatingComplexToIntegralComplex:
  352. case CK_IntegralRealToComplex:
  353. case CK_IntegralComplexToReal:
  354. case CK_IntegralComplexToBoolean:
  355. case CK_IntegralComplexCast:
  356. case CK_IntegralComplexToFloatingComplex:
  357. case CK_CPointerToObjCPointerCast:
  358. case CK_BlockPointerToObjCPointerCast:
  359. case CK_AnyPointerToBlockPointerCast:
  360. case CK_ObjCObjectLValueCast:
  361. case CK_ZeroToOCLOpaqueType:
  362. case CK_IntToOCLSampler:
  363. case CK_LValueBitCast:
  364. case CK_FixedPointCast:
  365. case CK_FixedPointToBoolean: {
  366. state =
  367. handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
  368. continue;
  369. }
  370. case CK_IntegralCast: {
  371. // Delegate to SValBuilder to process.
  372. SVal V = state->getSVal(Ex, LCtx);
  373. V = svalBuilder.evalIntegralCast(state, V, T, ExTy);
  374. state = state->BindExpr(CastE, LCtx, V);
  375. Bldr.generateNode(CastE, Pred, state);
  376. continue;
  377. }
  378. case CK_DerivedToBase:
  379. case CK_UncheckedDerivedToBase: {
  380. // For DerivedToBase cast, delegate to the store manager.
  381. SVal val = state->getSVal(Ex, LCtx);
  382. val = getStoreManager().evalDerivedToBase(val, CastE);
  383. state = state->BindExpr(CastE, LCtx, val);
  384. Bldr.generateNode(CastE, Pred, state);
  385. continue;
  386. }
  387. // Handle C++ dyn_cast.
  388. case CK_Dynamic: {
  389. SVal val = state->getSVal(Ex, LCtx);
  390. // Compute the type of the result.
  391. QualType resultType = CastE->getType();
  392. if (CastE->isGLValue())
  393. resultType = getContext().getPointerType(resultType);
  394. bool Failed = false;
  395. // Check if the value being cast evaluates to 0.
  396. if (val.isZeroConstant())
  397. Failed = true;
  398. // Else, evaluate the cast.
  399. else
  400. val = getStoreManager().attemptDownCast(val, T, Failed);
  401. if (Failed) {
  402. if (T->isReferenceType()) {
  403. // A bad_cast exception is thrown if input value is a reference.
  404. // Currently, we model this, by generating a sink.
  405. Bldr.generateSink(CastE, Pred, state);
  406. continue;
  407. } else {
  408. // If the cast fails on a pointer, bind to 0.
  409. state = state->BindExpr(CastE, LCtx, svalBuilder.makeNull());
  410. }
  411. } else {
  412. // If we don't know if the cast succeeded, conjure a new symbol.
  413. if (val.isUnknown()) {
  414. DefinedOrUnknownSVal NewSym =
  415. svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx, resultType,
  416. currBldrCtx->blockCount());
  417. state = state->BindExpr(CastE, LCtx, NewSym);
  418. } else
  419. // Else, bind to the derived region value.
  420. state = state->BindExpr(CastE, LCtx, val);
  421. }
  422. Bldr.generateNode(CastE, Pred, state);
  423. continue;
  424. }
  425. case CK_BaseToDerived: {
  426. SVal val = state->getSVal(Ex, LCtx);
  427. QualType resultType = CastE->getType();
  428. if (CastE->isGLValue())
  429. resultType = getContext().getPointerType(resultType);
  430. bool Failed = false;
  431. if (!val.isConstant()) {
  432. val = getStoreManager().attemptDownCast(val, T, Failed);
  433. }
  434. // Failed to cast or the result is unknown, fall back to conservative.
  435. if (Failed || val.isUnknown()) {
  436. val =
  437. svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx, resultType,
  438. currBldrCtx->blockCount());
  439. }
  440. state = state->BindExpr(CastE, LCtx, val);
  441. Bldr.generateNode(CastE, Pred, state);
  442. continue;
  443. }
  444. case CK_NullToMemberPointer: {
  445. SVal V = svalBuilder.getMemberPointer(nullptr);
  446. state = state->BindExpr(CastE, LCtx, V);
  447. Bldr.generateNode(CastE, Pred, state);
  448. continue;
  449. }
  450. case CK_DerivedToBaseMemberPointer:
  451. case CK_BaseToDerivedMemberPointer:
  452. case CK_ReinterpretMemberPointer: {
  453. SVal V = state->getSVal(Ex, LCtx);
  454. if (auto PTMSV = V.getAs<nonloc::PointerToMember>()) {
  455. SVal CastedPTMSV = svalBuilder.makePointerToMember(
  456. getBasicVals().accumCXXBase(
  457. llvm::make_range<CastExpr::path_const_iterator>(
  458. CastE->path_begin(), CastE->path_end()), *PTMSV));
  459. state = state->BindExpr(CastE, LCtx, CastedPTMSV);
  460. Bldr.generateNode(CastE, Pred, state);
  461. continue;
  462. }
  463. // Explicitly proceed with default handler for this case cascade.
  464. state = handleLVectorSplat(state, LCtx, CastE, Bldr, Pred);
  465. continue;
  466. }
  467. // Various C++ casts that are not handled yet.
  468. case CK_ToUnion:
  469. case CK_VectorSplat: {
  470. state = handleLVectorSplat(state, LCtx, CastE, Bldr, Pred);
  471. continue;
  472. }
  473. }
  474. }
  475. }
  476. void ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL,
  477. ExplodedNode *Pred,
  478. ExplodedNodeSet &Dst) {
  479. StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
  480. ProgramStateRef State = Pred->getState();
  481. const LocationContext *LCtx = Pred->getLocationContext();
  482. const Expr *Init = CL->getInitializer();
  483. SVal V = State->getSVal(CL->getInitializer(), LCtx);
  484. if (isa<CXXConstructExpr>(Init) || isa<CXXStdInitializerListExpr>(Init)) {
  485. // No work needed. Just pass the value up to this expression.
  486. } else {
  487. assert(isa<InitListExpr>(Init));
  488. Loc CLLoc = State->getLValue(CL, LCtx);
  489. State = State->bindLoc(CLLoc, V, LCtx);
  490. if (CL->isGLValue())
  491. V = CLLoc;
  492. }
  493. B.generateNode(CL, Pred, State->BindExpr(CL, LCtx, V));
  494. }
  495. void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
  496. ExplodedNodeSet &Dst) {
  497. // Assumption: The CFG has one DeclStmt per Decl.
  498. const VarDecl *VD = dyn_cast_or_null<VarDecl>(*DS->decl_begin());
  499. if (!VD) {
  500. //TODO:AZ: remove explicit insertion after refactoring is done.
  501. Dst.insert(Pred);
  502. return;
  503. }
  504. // FIXME: all pre/post visits should eventually be handled by ::Visit().
  505. ExplodedNodeSet dstPreVisit;
  506. getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this);
  507. ExplodedNodeSet dstEvaluated;
  508. StmtNodeBuilder B(dstPreVisit, dstEvaluated, *currBldrCtx);
  509. for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
  510. I!=E; ++I) {
  511. ExplodedNode *N = *I;
  512. ProgramStateRef state = N->getState();
  513. const LocationContext *LC = N->getLocationContext();
  514. // Decls without InitExpr are not initialized explicitly.
  515. if (const Expr *InitEx = VD->getInit()) {
  516. // Note in the state that the initialization has occurred.
  517. ExplodedNode *UpdatedN = N;
  518. SVal InitVal = state->getSVal(InitEx, LC);
  519. assert(DS->isSingleDecl());
  520. if (getObjectUnderConstruction(state, DS, LC)) {
  521. state = finishObjectConstruction(state, DS, LC);
  522. // We constructed the object directly in the variable.
  523. // No need to bind anything.
  524. B.generateNode(DS, UpdatedN, state);
  525. } else {
  526. // Recover some path-sensitivity if a scalar value evaluated to
  527. // UnknownVal.
  528. if (InitVal.isUnknown()) {
  529. QualType Ty = InitEx->getType();
  530. if (InitEx->isGLValue()) {
  531. Ty = getContext().getPointerType(Ty);
  532. }
  533. InitVal = svalBuilder.conjureSymbolVal(nullptr, InitEx, LC, Ty,
  534. currBldrCtx->blockCount());
  535. }
  536. B.takeNodes(UpdatedN);
  537. ExplodedNodeSet Dst2;
  538. evalBind(Dst2, DS, UpdatedN, state->getLValue(VD, LC), InitVal, true);
  539. B.addNodes(Dst2);
  540. }
  541. }
  542. else {
  543. B.generateNode(DS, N, state);
  544. }
  545. }
  546. getCheckerManager().runCheckersForPostStmt(Dst, B.getResults(), DS, *this);
  547. }
  548. void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
  549. ExplodedNodeSet &Dst) {
  550. assert(B->getOpcode() == BO_LAnd ||
  551. B->getOpcode() == BO_LOr);
  552. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  553. ProgramStateRef state = Pred->getState();
  554. if (B->getType()->isVectorType()) {
  555. // FIXME: We do not model vector arithmetic yet. When adding support for
  556. // that, note that the CFG-based reasoning below does not apply, because
  557. // logical operators on vectors are not short-circuit. Currently they are
  558. // modeled as short-circuit in Clang CFG but this is incorrect.
  559. // Do not set the value for the expression. It'd be UnknownVal by default.
  560. Bldr.generateNode(B, Pred, state);
  561. return;
  562. }
  563. ExplodedNode *N = Pred;
  564. while (!N->getLocation().getAs<BlockEntrance>()) {
  565. ProgramPoint P = N->getLocation();
  566. assert(P.getAs<PreStmt>()|| P.getAs<PreStmtPurgeDeadSymbols>());
  567. (void) P;
  568. assert(N->pred_size() == 1);
  569. N = *N->pred_begin();
  570. }
  571. assert(N->pred_size() == 1);
  572. N = *N->pred_begin();
  573. BlockEdge BE = N->getLocation().castAs<BlockEdge>();
  574. SVal X;
  575. // Determine the value of the expression by introspecting how we
  576. // got this location in the CFG. This requires looking at the previous
  577. // block we were in and what kind of control-flow transfer was involved.
  578. const CFGBlock *SrcBlock = BE.getSrc();
  579. // The only terminator (if there is one) that makes sense is a logical op.
  580. CFGTerminator T = SrcBlock->getTerminator();
  581. if (const BinaryOperator *Term = cast_or_null<BinaryOperator>(T.getStmt())) {
  582. (void) Term;
  583. assert(Term->isLogicalOp());
  584. assert(SrcBlock->succ_size() == 2);
  585. // Did we take the true or false branch?
  586. unsigned constant = (*SrcBlock->succ_begin() == BE.getDst()) ? 1 : 0;
  587. X = svalBuilder.makeIntVal(constant, B->getType());
  588. }
  589. else {
  590. // If there is no terminator, by construction the last statement
  591. // in SrcBlock is the value of the enclosing expression.
  592. // However, we still need to constrain that value to be 0 or 1.
  593. assert(!SrcBlock->empty());
  594. CFGStmt Elem = SrcBlock->rbegin()->castAs<CFGStmt>();
  595. const Expr *RHS = cast<Expr>(Elem.getStmt());
  596. SVal RHSVal = N->getState()->getSVal(RHS, Pred->getLocationContext());
  597. if (RHSVal.isUndef()) {
  598. X = RHSVal;
  599. } else {
  600. // We evaluate "RHSVal != 0" expression which result in 0 if the value is
  601. // known to be false, 1 if the value is known to be true and a new symbol
  602. // when the assumption is unknown.
  603. nonloc::ConcreteInt Zero(getBasicVals().getValue(0, B->getType()));
  604. X = evalBinOp(N->getState(), BO_NE,
  605. svalBuilder.evalCast(RHSVal, B->getType(), RHS->getType()),
  606. Zero, B->getType());
  607. }
  608. }
  609. Bldr.generateNode(B, Pred, state->BindExpr(B, Pred->getLocationContext(), X));
  610. }
  611. void ExprEngine::VisitInitListExpr(const InitListExpr *IE,
  612. ExplodedNode *Pred,
  613. ExplodedNodeSet &Dst) {
  614. StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
  615. ProgramStateRef state = Pred->getState();
  616. const LocationContext *LCtx = Pred->getLocationContext();
  617. QualType T = getContext().getCanonicalType(IE->getType());
  618. unsigned NumInitElements = IE->getNumInits();
  619. if (!IE->isGLValue() &&
  620. (T->isArrayType() || T->isRecordType() || T->isVectorType() ||
  621. T->isAnyComplexType())) {
  622. llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList();
  623. // Handle base case where the initializer has no elements.
  624. // e.g: static int* myArray[] = {};
  625. if (NumInitElements == 0) {
  626. SVal V = svalBuilder.makeCompoundVal(T, vals);
  627. B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
  628. return;
  629. }
  630. for (InitListExpr::const_reverse_iterator it = IE->rbegin(),
  631. ei = IE->rend(); it != ei; ++it) {
  632. SVal V = state->getSVal(cast<Expr>(*it), LCtx);
  633. vals = getBasicVals().prependSVal(V, vals);
  634. }
  635. B.generateNode(IE, Pred,
  636. state->BindExpr(IE, LCtx,
  637. svalBuilder.makeCompoundVal(T, vals)));
  638. return;
  639. }
  640. // Handle scalars: int{5} and int{} and GLvalues.
  641. // Note, if the InitListExpr is a GLvalue, it means that there is an address
  642. // representing it, so it must have a single init element.
  643. assert(NumInitElements <= 1);
  644. SVal V;
  645. if (NumInitElements == 0)
  646. V = getSValBuilder().makeZeroVal(T);
  647. else
  648. V = state->getSVal(IE->getInit(0), LCtx);
  649. B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
  650. }
  651. void ExprEngine::VisitGuardedExpr(const Expr *Ex,
  652. const Expr *L,
  653. const Expr *R,
  654. ExplodedNode *Pred,
  655. ExplodedNodeSet &Dst) {
  656. assert(L && R);
  657. StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
  658. ProgramStateRef state = Pred->getState();
  659. const LocationContext *LCtx = Pred->getLocationContext();
  660. const CFGBlock *SrcBlock = nullptr;
  661. // Find the predecessor block.
  662. ProgramStateRef SrcState = state;
  663. for (const ExplodedNode *N = Pred ; N ; N = *N->pred_begin()) {
  664. ProgramPoint PP = N->getLocation();
  665. if (PP.getAs<PreStmtPurgeDeadSymbols>() || PP.getAs<BlockEntrance>()) {
  666. // If the state N has multiple predecessors P, it means that successors
  667. // of P are all equivalent.
  668. // In turn, that means that all nodes at P are equivalent in terms
  669. // of observable behavior at N, and we can follow any of them.
  670. // FIXME: a more robust solution which does not walk up the tree.
  671. continue;
  672. }
  673. SrcBlock = PP.castAs<BlockEdge>().getSrc();
  674. SrcState = N->getState();
  675. break;
  676. }
  677. assert(SrcBlock && "missing function entry");
  678. // Find the last expression in the predecessor block. That is the
  679. // expression that is used for the value of the ternary expression.
  680. bool hasValue = false;
  681. SVal V;
  682. for (CFGElement CE : llvm::reverse(*SrcBlock)) {
  683. if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
  684. const Expr *ValEx = cast<Expr>(CS->getStmt());
  685. ValEx = ValEx->IgnoreParens();
  686. // For GNU extension '?:' operator, the left hand side will be an
  687. // OpaqueValueExpr, so get the underlying expression.
  688. if (const OpaqueValueExpr *OpaqueEx = dyn_cast<OpaqueValueExpr>(L))
  689. L = OpaqueEx->getSourceExpr();
  690. // If the last expression in the predecessor block matches true or false
  691. // subexpression, get its the value.
  692. if (ValEx == L->IgnoreParens() || ValEx == R->IgnoreParens()) {
  693. hasValue = true;
  694. V = SrcState->getSVal(ValEx, LCtx);
  695. }
  696. break;
  697. }
  698. }
  699. if (!hasValue)
  700. V = svalBuilder.conjureSymbolVal(nullptr, Ex, LCtx,
  701. currBldrCtx->blockCount());
  702. // Generate a new node with the binding from the appropriate path.
  703. B.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V, true));
  704. }
  705. void ExprEngine::
  706. VisitOffsetOfExpr(const OffsetOfExpr *OOE,
  707. ExplodedNode *Pred, ExplodedNodeSet &Dst) {
  708. StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
  709. Expr::EvalResult Result;
  710. if (OOE->EvaluateAsInt(Result, getContext())) {
  711. APSInt IV = Result.Val.getInt();
  712. assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType()));
  713. assert(OOE->getType()->isBuiltinType());
  714. assert(OOE->getType()->getAs<BuiltinType>()->isInteger());
  715. assert(IV.isSigned() == OOE->getType()->isSignedIntegerType());
  716. SVal X = svalBuilder.makeIntVal(IV);
  717. B.generateNode(OOE, Pred,
  718. Pred->getState()->BindExpr(OOE, Pred->getLocationContext(),
  719. X));
  720. }
  721. // FIXME: Handle the case where __builtin_offsetof is not a constant.
  722. }
  723. void ExprEngine::
  724. VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
  725. ExplodedNode *Pred,
  726. ExplodedNodeSet &Dst) {
  727. // FIXME: Prechecks eventually go in ::Visit().
  728. ExplodedNodeSet CheckedSet;
  729. getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, Ex, *this);
  730. ExplodedNodeSet EvalSet;
  731. StmtNodeBuilder Bldr(CheckedSet, EvalSet, *currBldrCtx);
  732. QualType T = Ex->getTypeOfArgument();
  733. for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
  734. I != E; ++I) {
  735. if (Ex->getKind() == UETT_SizeOf) {
  736. if (!T->isIncompleteType() && !T->isConstantSizeType()) {
  737. assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
  738. // FIXME: Add support for VLA type arguments and VLA expressions.
  739. // When that happens, we should probably refactor VLASizeChecker's code.
  740. continue;
  741. } else if (T->getAs<ObjCObjectType>()) {
  742. // Some code tries to take the sizeof an ObjCObjectType, relying that
  743. // the compiler has laid out its representation. Just report Unknown
  744. // for these.
  745. continue;
  746. }
  747. }
  748. APSInt Value = Ex->EvaluateKnownConstInt(getContext());
  749. CharUnits amt = CharUnits::fromQuantity(Value.getZExtValue());
  750. ProgramStateRef state = (*I)->getState();
  751. state = state->BindExpr(Ex, (*I)->getLocationContext(),
  752. svalBuilder.makeIntVal(amt.getQuantity(),
  753. Ex->getType()));
  754. Bldr.generateNode(Ex, *I, state);
  755. }
  756. getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, Ex, *this);
  757. }
  758. void ExprEngine::handleUOExtension(ExplodedNodeSet::iterator I,
  759. const UnaryOperator *U,
  760. StmtNodeBuilder &Bldr) {
  761. // FIXME: We can probably just have some magic in Environment::getSVal()
  762. // that propagates values, instead of creating a new node here.
  763. //
  764. // Unary "+" is a no-op, similar to a parentheses. We still have places
  765. // where it may be a block-level expression, so we need to
  766. // generate an extra node that just propagates the value of the
  767. // subexpression.
  768. const Expr *Ex = U->getSubExpr()->IgnoreParens();
  769. ProgramStateRef state = (*I)->getState();
  770. const LocationContext *LCtx = (*I)->getLocationContext();
  771. Bldr.generateNode(U, *I, state->BindExpr(U, LCtx,
  772. state->getSVal(Ex, LCtx)));
  773. }
  774. void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, ExplodedNode *Pred,
  775. ExplodedNodeSet &Dst) {
  776. // FIXME: Prechecks eventually go in ::Visit().
  777. ExplodedNodeSet CheckedSet;
  778. getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, U, *this);
  779. ExplodedNodeSet EvalSet;
  780. StmtNodeBuilder Bldr(CheckedSet, EvalSet, *currBldrCtx);
  781. for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
  782. I != E; ++I) {
  783. switch (U->getOpcode()) {
  784. default: {
  785. Bldr.takeNodes(*I);
  786. ExplodedNodeSet Tmp;
  787. VisitIncrementDecrementOperator(U, *I, Tmp);
  788. Bldr.addNodes(Tmp);
  789. break;
  790. }
  791. case UO_Real: {
  792. const Expr *Ex = U->getSubExpr()->IgnoreParens();
  793. // FIXME: We don't have complex SValues yet.
  794. if (Ex->getType()->isAnyComplexType()) {
  795. // Just report "Unknown."
  796. break;
  797. }
  798. // For all other types, UO_Real is an identity operation.
  799. assert (U->getType() == Ex->getType());
  800. ProgramStateRef state = (*I)->getState();
  801. const LocationContext *LCtx = (*I)->getLocationContext();
  802. Bldr.generateNode(U, *I, state->BindExpr(U, LCtx,
  803. state->getSVal(Ex, LCtx)));
  804. break;
  805. }
  806. case UO_Imag: {
  807. const Expr *Ex = U->getSubExpr()->IgnoreParens();
  808. // FIXME: We don't have complex SValues yet.
  809. if (Ex->getType()->isAnyComplexType()) {
  810. // Just report "Unknown."
  811. break;
  812. }
  813. // For all other types, UO_Imag returns 0.
  814. ProgramStateRef state = (*I)->getState();
  815. const LocationContext *LCtx = (*I)->getLocationContext();
  816. SVal X = svalBuilder.makeZeroVal(Ex->getType());
  817. Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, X));
  818. break;
  819. }
  820. case UO_AddrOf: {
  821. // Process pointer-to-member address operation.
  822. const Expr *Ex = U->getSubExpr()->IgnoreParens();
  823. if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex)) {
  824. const ValueDecl *VD = DRE->getDecl();
  825. if (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD)) {
  826. ProgramStateRef State = (*I)->getState();
  827. const LocationContext *LCtx = (*I)->getLocationContext();
  828. SVal SV = svalBuilder.getMemberPointer(cast<DeclaratorDecl>(VD));
  829. Bldr.generateNode(U, *I, State->BindExpr(U, LCtx, SV));
  830. break;
  831. }
  832. }
  833. // Explicitly proceed with default handler for this case cascade.
  834. handleUOExtension(I, U, Bldr);
  835. break;
  836. }
  837. case UO_Plus:
  838. assert(!U->isGLValue());
  839. LLVM_FALLTHROUGH;
  840. case UO_Deref:
  841. case UO_Extension: {
  842. handleUOExtension(I, U, Bldr);
  843. break;
  844. }
  845. case UO_LNot:
  846. case UO_Minus:
  847. case UO_Not: {
  848. assert (!U->isGLValue());
  849. const Expr *Ex = U->getSubExpr()->IgnoreParens();
  850. ProgramStateRef state = (*I)->getState();
  851. const LocationContext *LCtx = (*I)->getLocationContext();
  852. // Get the value of the subexpression.
  853. SVal V = state->getSVal(Ex, LCtx);
  854. if (V.isUnknownOrUndef()) {
  855. Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V));
  856. break;
  857. }
  858. switch (U->getOpcode()) {
  859. default:
  860. llvm_unreachable("Invalid Opcode.");
  861. case UO_Not:
  862. // FIXME: Do we need to handle promotions?
  863. state = state->BindExpr(U, LCtx, evalComplement(V.castAs<NonLoc>()));
  864. break;
  865. case UO_Minus:
  866. // FIXME: Do we need to handle promotions?
  867. state = state->BindExpr(U, LCtx, evalMinus(V.castAs<NonLoc>()));
  868. break;
  869. case UO_LNot:
  870. // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
  871. //
  872. // Note: technically we do "E == 0", but this is the same in the
  873. // transfer functions as "0 == E".
  874. SVal Result;
  875. if (Optional<Loc> LV = V.getAs<Loc>()) {
  876. Loc X = svalBuilder.makeNullWithType(Ex->getType());
  877. Result = evalBinOp(state, BO_EQ, *LV, X, U->getType());
  878. } else if (Ex->getType()->isFloatingType()) {
  879. // FIXME: handle floating point types.
  880. Result = UnknownVal();
  881. } else {
  882. nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
  883. Result = evalBinOp(state, BO_EQ, V.castAs<NonLoc>(), X,
  884. U->getType());
  885. }
  886. state = state->BindExpr(U, LCtx, Result);
  887. break;
  888. }
  889. Bldr.generateNode(U, *I, state);
  890. break;
  891. }
  892. }
  893. }
  894. getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, U, *this);
  895. }
  896. void ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U,
  897. ExplodedNode *Pred,
  898. ExplodedNodeSet &Dst) {
  899. // Handle ++ and -- (both pre- and post-increment).
  900. assert (U->isIncrementDecrementOp());
  901. const Expr *Ex = U->getSubExpr()->IgnoreParens();
  902. const LocationContext *LCtx = Pred->getLocationContext();
  903. ProgramStateRef state = Pred->getState();
  904. SVal loc = state->getSVal(Ex, LCtx);
  905. // Perform a load.
  906. ExplodedNodeSet Tmp;
  907. evalLoad(Tmp, U, Ex, Pred, state, loc);
  908. ExplodedNodeSet Dst2;
  909. StmtNodeBuilder Bldr(Tmp, Dst2, *currBldrCtx);
  910. for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end();I!=E;++I) {
  911. state = (*I)->getState();
  912. assert(LCtx == (*I)->getLocationContext());
  913. SVal V2_untested = state->getSVal(Ex, LCtx);
  914. // Propagate unknown and undefined values.
  915. if (V2_untested.isUnknownOrUndef()) {
  916. state = state->BindExpr(U, LCtx, V2_untested);
  917. // Perform the store, so that the uninitialized value detection happens.
  918. Bldr.takeNodes(*I);
  919. ExplodedNodeSet Dst3;
  920. evalStore(Dst3, U, U, *I, state, loc, V2_untested);
  921. Bldr.addNodes(Dst3);
  922. continue;
  923. }
  924. DefinedSVal V2 = V2_untested.castAs<DefinedSVal>();
  925. // Handle all other values.
  926. BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add : BO_Sub;
  927. // If the UnaryOperator has non-location type, use its type to create the
  928. // constant value. If the UnaryOperator has location type, create the
  929. // constant with int type and pointer width.
  930. SVal RHS;
  931. SVal Result;
  932. if (U->getType()->isAnyPointerType())
  933. RHS = svalBuilder.makeArrayIndex(1);
  934. else if (U->getType()->isIntegralOrEnumerationType())
  935. RHS = svalBuilder.makeIntVal(1, U->getType());
  936. else
  937. RHS = UnknownVal();
  938. // The use of an operand of type bool with the ++ operators is deprecated
  939. // but valid until C++17. And if the operand of the ++ operator is of type
  940. // bool, it is set to true until C++17. Note that for '_Bool', it is also
  941. // set to true when it encounters ++ operator.
  942. if (U->getType()->isBooleanType() && U->isIncrementOp())
  943. Result = svalBuilder.makeTruthVal(true, U->getType());
  944. else
  945. Result = evalBinOp(state, Op, V2, RHS, U->getType());
  946. // Conjure a new symbol if necessary to recover precision.
  947. if (Result.isUnknown()){
  948. DefinedOrUnknownSVal SymVal =
  949. svalBuilder.conjureSymbolVal(nullptr, U, LCtx,
  950. currBldrCtx->blockCount());
  951. Result = SymVal;
  952. // If the value is a location, ++/-- should always preserve
  953. // non-nullness. Check if the original value was non-null, and if so
  954. // propagate that constraint.
  955. if (Loc::isLocType(U->getType())) {
  956. DefinedOrUnknownSVal Constraint =
  957. svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType()));
  958. if (!state->assume(Constraint, true)) {
  959. // It isn't feasible for the original value to be null.
  960. // Propagate this constraint.
  961. Constraint = svalBuilder.evalEQ(state, SymVal,
  962. svalBuilder.makeZeroVal(U->getType()));
  963. state = state->assume(Constraint, false);
  964. assert(state);
  965. }
  966. }
  967. }
  968. // Since the lvalue-to-rvalue conversion is explicit in the AST,
  969. // we bind an l-value if the operator is prefix and an lvalue (in C++).
  970. if (U->isGLValue())
  971. state = state->BindExpr(U, LCtx, loc);
  972. else
  973. state = state->BindExpr(U, LCtx, U->isPostfix() ? V2 : Result);
  974. // Perform the store.
  975. Bldr.takeNodes(*I);
  976. ExplodedNodeSet Dst3;
  977. evalStore(Dst3, U, U, *I, state, loc, Result);
  978. Bldr.addNodes(Dst3);
  979. }
  980. Dst.insert(Dst2);
  981. }