10b57cec5SDimitry Andric // SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- C++ -*- 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file defines SimpleSValBuilder, a basic implementation of SValBuilder. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 140b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h" 155ffd83dbSDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 160b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/SValVisitor.h" 18bdd1243dSDimitry Andric #include <optional> 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric using namespace clang; 210b57cec5SDimitry Andric using namespace ento; 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric namespace { 240b57cec5SDimitry Andric class SimpleSValBuilder : public SValBuilder { 250eae32dcSDimitry Andric 2681ad6265SDimitry Andric // Query the constraint manager whether the SVal has only one possible 2781ad6265SDimitry Andric // (integer) value. If that is the case, the value is returned. Otherwise, 2881ad6265SDimitry Andric // returns NULL. 2981ad6265SDimitry Andric // This is an implementation detail. Checkers should use `getKnownValue()` 3081ad6265SDimitry Andric // instead. 3181ad6265SDimitry Andric const llvm::APSInt *getConstValue(ProgramStateRef state, SVal V); 3281ad6265SDimitry Andric 330eae32dcSDimitry Andric // With one `simplifySValOnce` call, a compound symbols might collapse to 340eae32dcSDimitry Andric // simpler symbol tree that is still possible to further simplify. Thus, we 350eae32dcSDimitry Andric // do the simplification on a new symbol tree until we reach the simplest 360eae32dcSDimitry Andric // form, i.e. the fixpoint. 370eae32dcSDimitry Andric // Consider the following symbol `(b * b) * b * b` which has this tree: 380eae32dcSDimitry Andric // * 390eae32dcSDimitry Andric // / \ 400eae32dcSDimitry Andric // * b 410eae32dcSDimitry Andric // / \ 420eae32dcSDimitry Andric // / b 430eae32dcSDimitry Andric // (b * b) 440eae32dcSDimitry Andric // Now, if the `b * b == 1` new constraint is added then during the first 450eae32dcSDimitry Andric // iteration we have the following transformations: 460eae32dcSDimitry Andric // * * 470eae32dcSDimitry Andric // / \ / \ 480eae32dcSDimitry Andric // * b --> b b 490eae32dcSDimitry Andric // / \ 500eae32dcSDimitry Andric // / b 510eae32dcSDimitry Andric // 1 520eae32dcSDimitry Andric // We need another iteration to reach the final result `1`. 530eae32dcSDimitry Andric SVal simplifyUntilFixpoint(ProgramStateRef State, SVal Val); 540eae32dcSDimitry Andric 550eae32dcSDimitry Andric // Recursively descends into symbolic expressions and replaces symbols 5681ad6265SDimitry Andric // with their known values (in the sense of the getConstValue() method). 570eae32dcSDimitry Andric // We traverse the symbol tree and query the constraint values for the 580eae32dcSDimitry Andric // sub-trees and if a value is a constant we do the constant folding. 590eae32dcSDimitry Andric SVal simplifySValOnce(ProgramStateRef State, SVal V); 600eae32dcSDimitry Andric 610b57cec5SDimitry Andric public: 620b57cec5SDimitry Andric SimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, 630b57cec5SDimitry Andric ProgramStateManager &stateMgr) 640b57cec5SDimitry Andric : SValBuilder(alloc, context, stateMgr) {} 650b57cec5SDimitry Andric ~SimpleSValBuilder() override {} 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, 680b57cec5SDimitry Andric NonLoc lhs, NonLoc rhs, QualType resultTy) override; 690b57cec5SDimitry Andric SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op, 700b57cec5SDimitry Andric Loc lhs, Loc rhs, QualType resultTy) override; 710b57cec5SDimitry Andric SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op, 720b57cec5SDimitry Andric Loc lhs, NonLoc rhs, QualType resultTy) override; 730b57cec5SDimitry Andric 7481ad6265SDimitry Andric /// Evaluates a given SVal by recursively evaluating and 7581ad6265SDimitry Andric /// simplifying the children SVals. If the SVal has only one possible 760b57cec5SDimitry Andric /// (integer) value, that value is returned. Otherwise, returns NULL. 770b57cec5SDimitry Andric const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal V) override; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric SVal simplifySVal(ProgramStateRef State, SVal V) override; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op, 820b57cec5SDimitry Andric const llvm::APSInt &RHS, QualType resultTy); 830b57cec5SDimitry Andric }; 840b57cec5SDimitry Andric } // end anonymous namespace 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric SValBuilder *ento::createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, 870b57cec5SDimitry Andric ASTContext &context, 880b57cec5SDimitry Andric ProgramStateManager &stateMgr) { 890b57cec5SDimitry Andric return new SimpleSValBuilder(alloc, context, stateMgr); 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 9281ad6265SDimitry Andric // Checks if the negation the value and flipping sign preserve 9381ad6265SDimitry Andric // the semantics on the operation in the resultType 9481ad6265SDimitry Andric static bool isNegationValuePreserving(const llvm::APSInt &Value, 9581ad6265SDimitry Andric APSIntType ResultType) { 9681ad6265SDimitry Andric const unsigned ValueBits = Value.getSignificantBits(); 9781ad6265SDimitry Andric if (ValueBits == ResultType.getBitWidth()) { 9881ad6265SDimitry Andric // The value is the lowest negative value that is representable 9981ad6265SDimitry Andric // in signed integer with bitWith of result type. The 10081ad6265SDimitry Andric // negation is representable if resultType is unsigned. 10181ad6265SDimitry Andric return ResultType.isUnsigned(); 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric 10481ad6265SDimitry Andric // If resultType bitWith is higher that number of bits required 10581ad6265SDimitry Andric // to represent RHS, the sign flip produce same value. 10681ad6265SDimitry Andric return ValueBits < ResultType.getBitWidth(); 1070b57cec5SDimitry Andric } 1080b57cec5SDimitry Andric 1090b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1100b57cec5SDimitry Andric // Transfer function for binary operators. 1110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric SVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS, 1140b57cec5SDimitry Andric BinaryOperator::Opcode op, 1150b57cec5SDimitry Andric const llvm::APSInt &RHS, 1160b57cec5SDimitry Andric QualType resultTy) { 1170b57cec5SDimitry Andric bool isIdempotent = false; 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric // Check for a few special cases with known reductions first. 1200b57cec5SDimitry Andric switch (op) { 1210b57cec5SDimitry Andric default: 1220b57cec5SDimitry Andric // We can't reduce this case; just treat it normally. 1230b57cec5SDimitry Andric break; 1240b57cec5SDimitry Andric case BO_Mul: 1250b57cec5SDimitry Andric // a*0 and a*1 1260b57cec5SDimitry Andric if (RHS == 0) 1270b57cec5SDimitry Andric return makeIntVal(0, resultTy); 1280b57cec5SDimitry Andric else if (RHS == 1) 1290b57cec5SDimitry Andric isIdempotent = true; 1300b57cec5SDimitry Andric break; 1310b57cec5SDimitry Andric case BO_Div: 1320b57cec5SDimitry Andric // a/0 and a/1 1330b57cec5SDimitry Andric if (RHS == 0) 1340b57cec5SDimitry Andric // This is also handled elsewhere. 1350b57cec5SDimitry Andric return UndefinedVal(); 1360b57cec5SDimitry Andric else if (RHS == 1) 1370b57cec5SDimitry Andric isIdempotent = true; 1380b57cec5SDimitry Andric break; 1390b57cec5SDimitry Andric case BO_Rem: 1400b57cec5SDimitry Andric // a%0 and a%1 1410b57cec5SDimitry Andric if (RHS == 0) 1420b57cec5SDimitry Andric // This is also handled elsewhere. 1430b57cec5SDimitry Andric return UndefinedVal(); 1440b57cec5SDimitry Andric else if (RHS == 1) 1450b57cec5SDimitry Andric return makeIntVal(0, resultTy); 1460b57cec5SDimitry Andric break; 1470b57cec5SDimitry Andric case BO_Add: 1480b57cec5SDimitry Andric case BO_Sub: 1490b57cec5SDimitry Andric case BO_Shl: 1500b57cec5SDimitry Andric case BO_Shr: 1510b57cec5SDimitry Andric case BO_Xor: 1520b57cec5SDimitry Andric // a+0, a-0, a<<0, a>>0, a^0 1530b57cec5SDimitry Andric if (RHS == 0) 1540b57cec5SDimitry Andric isIdempotent = true; 1550b57cec5SDimitry Andric break; 1560b57cec5SDimitry Andric case BO_And: 1570b57cec5SDimitry Andric // a&0 and a&(~0) 1580b57cec5SDimitry Andric if (RHS == 0) 1590b57cec5SDimitry Andric return makeIntVal(0, resultTy); 160349cc55cSDimitry Andric else if (RHS.isAllOnes()) 1610b57cec5SDimitry Andric isIdempotent = true; 1620b57cec5SDimitry Andric break; 1630b57cec5SDimitry Andric case BO_Or: 1640b57cec5SDimitry Andric // a|0 and a|(~0) 1650b57cec5SDimitry Andric if (RHS == 0) 1660b57cec5SDimitry Andric isIdempotent = true; 167349cc55cSDimitry Andric else if (RHS.isAllOnes()) { 1680b57cec5SDimitry Andric const llvm::APSInt &Result = BasicVals.Convert(resultTy, RHS); 1690b57cec5SDimitry Andric return nonloc::ConcreteInt(Result); 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric break; 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric // Idempotent ops (like a*1) can still change the type of an expression. 175fe6060f1SDimitry Andric // Wrap the LHS up in a NonLoc again and let evalCast do the 1760b57cec5SDimitry Andric // dirty work. 1770b57cec5SDimitry Andric if (isIdempotent) 178fe6060f1SDimitry Andric return evalCast(nonloc::SymbolVal(LHS), resultTy, QualType{}); 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric // If we reach this point, the expression cannot be simplified. 1810b57cec5SDimitry Andric // Make a SymbolVal for the entire expression, after converting the RHS. 1820b57cec5SDimitry Andric const llvm::APSInt *ConvertedRHS = &RHS; 1830b57cec5SDimitry Andric if (BinaryOperator::isComparisonOp(op)) { 1840b57cec5SDimitry Andric // We're looking for a type big enough to compare the symbolic value 1850b57cec5SDimitry Andric // with the given constant. 1860b57cec5SDimitry Andric // FIXME: This is an approximation of Sema::UsualArithmeticConversions. 1870b57cec5SDimitry Andric ASTContext &Ctx = getContext(); 1880b57cec5SDimitry Andric QualType SymbolType = LHS->getType(); 1890b57cec5SDimitry Andric uint64_t ValWidth = RHS.getBitWidth(); 1900b57cec5SDimitry Andric uint64_t TypeWidth = Ctx.getTypeSize(SymbolType); 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric if (ValWidth < TypeWidth) { 1930b57cec5SDimitry Andric // If the value is too small, extend it. 1940b57cec5SDimitry Andric ConvertedRHS = &BasicVals.Convert(SymbolType, RHS); 1950b57cec5SDimitry Andric } else if (ValWidth == TypeWidth) { 1960b57cec5SDimitry Andric // If the value is signed but the symbol is unsigned, do the comparison 1970b57cec5SDimitry Andric // in unsigned space. [C99 6.3.1.8] 1980b57cec5SDimitry Andric // (For the opposite case, the value is already unsigned.) 1990b57cec5SDimitry Andric if (RHS.isSigned() && !SymbolType->isSignedIntegerOrEnumerationType()) 2000b57cec5SDimitry Andric ConvertedRHS = &BasicVals.Convert(SymbolType, RHS); 2010b57cec5SDimitry Andric } 20281ad6265SDimitry Andric } else if (BinaryOperator::isAdditiveOp(op) && RHS.isNegative()) { 20381ad6265SDimitry Andric // Change a+(-N) into a-N, and a-(-N) into a+N 20481ad6265SDimitry Andric // Adjust addition/subtraction of negative value, to 20581ad6265SDimitry Andric // subtraction/addition of the negated value. 20681ad6265SDimitry Andric APSIntType resultIntTy = BasicVals.getAPSIntType(resultTy); 20781ad6265SDimitry Andric if (isNegationValuePreserving(RHS, resultIntTy)) { 20881ad6265SDimitry Andric ConvertedRHS = &BasicVals.getValue(-resultIntTy.convert(RHS)); 20981ad6265SDimitry Andric op = (op == BO_Add) ? BO_Sub : BO_Add; 21081ad6265SDimitry Andric } else { 21181ad6265SDimitry Andric ConvertedRHS = &BasicVals.Convert(resultTy, RHS); 21281ad6265SDimitry Andric } 2130b57cec5SDimitry Andric } else 2140b57cec5SDimitry Andric ConvertedRHS = &BasicVals.Convert(resultTy, RHS); 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric return makeNonLoc(LHS, op, *ConvertedRHS, resultTy); 2170b57cec5SDimitry Andric } 2180b57cec5SDimitry Andric 2190b57cec5SDimitry Andric // See if Sym is known to be a relation Rel with Bound. 2200b57cec5SDimitry Andric static bool isInRelation(BinaryOperator::Opcode Rel, SymbolRef Sym, 2210b57cec5SDimitry Andric llvm::APSInt Bound, ProgramStateRef State) { 2220b57cec5SDimitry Andric SValBuilder &SVB = State->getStateManager().getSValBuilder(); 2230b57cec5SDimitry Andric SVal Result = 2240b57cec5SDimitry Andric SVB.evalBinOpNN(State, Rel, nonloc::SymbolVal(Sym), 2250b57cec5SDimitry Andric nonloc::ConcreteInt(Bound), SVB.getConditionType()); 2260b57cec5SDimitry Andric if (auto DV = Result.getAs<DefinedSVal>()) { 2270b57cec5SDimitry Andric return !State->assume(*DV, false); 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric return false; 2300b57cec5SDimitry Andric } 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric // See if Sym is known to be within [min/4, max/4], where min and max 2330b57cec5SDimitry Andric // are the bounds of the symbol's integral type. With such symbols, 2340b57cec5SDimitry Andric // some manipulations can be performed without the risk of overflow. 2350b57cec5SDimitry Andric // assume() doesn't cause infinite recursion because we should be dealing 2360b57cec5SDimitry Andric // with simpler symbols on every recursive call. 2370b57cec5SDimitry Andric static bool isWithinConstantOverflowBounds(SymbolRef Sym, 2380b57cec5SDimitry Andric ProgramStateRef State) { 2390b57cec5SDimitry Andric SValBuilder &SVB = State->getStateManager().getSValBuilder(); 2400b57cec5SDimitry Andric BasicValueFactory &BV = SVB.getBasicValueFactory(); 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric QualType T = Sym->getType(); 2430b57cec5SDimitry Andric assert(T->isSignedIntegerOrEnumerationType() && 2440b57cec5SDimitry Andric "This only works with signed integers!"); 2450b57cec5SDimitry Andric APSIntType AT = BV.getAPSIntType(T); 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric llvm::APSInt Max = AT.getMaxValue() / AT.getValue(4), Min = -Max; 2480b57cec5SDimitry Andric return isInRelation(BO_LE, Sym, Max, State) && 2490b57cec5SDimitry Andric isInRelation(BO_GE, Sym, Min, State); 2500b57cec5SDimitry Andric } 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric // Same for the concrete integers: see if I is within [min/4, max/4]. 2530b57cec5SDimitry Andric static bool isWithinConstantOverflowBounds(llvm::APSInt I) { 2540b57cec5SDimitry Andric APSIntType AT(I); 2550b57cec5SDimitry Andric assert(!AT.isUnsigned() && 2560b57cec5SDimitry Andric "This only works with signed integers!"); 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric llvm::APSInt Max = AT.getMaxValue() / AT.getValue(4), Min = -Max; 2590b57cec5SDimitry Andric return (I <= Max) && (I >= -Max); 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric static std::pair<SymbolRef, llvm::APSInt> 2630b57cec5SDimitry Andric decomposeSymbol(SymbolRef Sym, BasicValueFactory &BV) { 2640b57cec5SDimitry Andric if (const auto *SymInt = dyn_cast<SymIntExpr>(Sym)) 2650b57cec5SDimitry Andric if (BinaryOperator::isAdditiveOp(SymInt->getOpcode())) 2660b57cec5SDimitry Andric return std::make_pair(SymInt->getLHS(), 2670b57cec5SDimitry Andric (SymInt->getOpcode() == BO_Add) ? 2680b57cec5SDimitry Andric (SymInt->getRHS()) : 2690b57cec5SDimitry Andric (-SymInt->getRHS())); 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric // Fail to decompose: "reduce" the problem to the "$x + 0" case. 2720b57cec5SDimitry Andric return std::make_pair(Sym, BV.getValue(0, Sym->getType())); 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // Simplify "(LSym + LInt) Op (RSym + RInt)" assuming all values are of the 2760b57cec5SDimitry Andric // same signed integral type and no overflows occur (which should be checked 2770b57cec5SDimitry Andric // by the caller). 2780b57cec5SDimitry Andric static NonLoc doRearrangeUnchecked(ProgramStateRef State, 2790b57cec5SDimitry Andric BinaryOperator::Opcode Op, 2800b57cec5SDimitry Andric SymbolRef LSym, llvm::APSInt LInt, 2810b57cec5SDimitry Andric SymbolRef RSym, llvm::APSInt RInt) { 2820b57cec5SDimitry Andric SValBuilder &SVB = State->getStateManager().getSValBuilder(); 2830b57cec5SDimitry Andric BasicValueFactory &BV = SVB.getBasicValueFactory(); 2840b57cec5SDimitry Andric SymbolManager &SymMgr = SVB.getSymbolManager(); 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric QualType SymTy = LSym->getType(); 2870b57cec5SDimitry Andric assert(SymTy == RSym->getType() && 2880b57cec5SDimitry Andric "Symbols are not of the same type!"); 2890b57cec5SDimitry Andric assert(APSIntType(LInt) == BV.getAPSIntType(SymTy) && 2900b57cec5SDimitry Andric "Integers are not of the same type as symbols!"); 2910b57cec5SDimitry Andric assert(APSIntType(RInt) == BV.getAPSIntType(SymTy) && 2920b57cec5SDimitry Andric "Integers are not of the same type as symbols!"); 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric QualType ResultTy; 2950b57cec5SDimitry Andric if (BinaryOperator::isComparisonOp(Op)) 2960b57cec5SDimitry Andric ResultTy = SVB.getConditionType(); 2970b57cec5SDimitry Andric else if (BinaryOperator::isAdditiveOp(Op)) 2980b57cec5SDimitry Andric ResultTy = SymTy; 2990b57cec5SDimitry Andric else 3000b57cec5SDimitry Andric llvm_unreachable("Operation not suitable for unchecked rearrangement!"); 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric if (LSym == RSym) 3030b57cec5SDimitry Andric return SVB.evalBinOpNN(State, Op, nonloc::ConcreteInt(LInt), 3040b57cec5SDimitry Andric nonloc::ConcreteInt(RInt), ResultTy) 3050b57cec5SDimitry Andric .castAs<NonLoc>(); 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric SymbolRef ResultSym = nullptr; 3080b57cec5SDimitry Andric BinaryOperator::Opcode ResultOp; 3090b57cec5SDimitry Andric llvm::APSInt ResultInt; 3100b57cec5SDimitry Andric if (BinaryOperator::isComparisonOp(Op)) { 3110b57cec5SDimitry Andric // Prefer comparing to a non-negative number. 3120b57cec5SDimitry Andric // FIXME: Maybe it'd be better to have consistency in 3130b57cec5SDimitry Andric // "$x - $y" vs. "$y - $x" because those are solver's keys. 3140b57cec5SDimitry Andric if (LInt > RInt) { 3150b57cec5SDimitry Andric ResultSym = SymMgr.getSymSymExpr(RSym, BO_Sub, LSym, SymTy); 3160b57cec5SDimitry Andric ResultOp = BinaryOperator::reverseComparisonOp(Op); 3170b57cec5SDimitry Andric ResultInt = LInt - RInt; // Opposite order! 3180b57cec5SDimitry Andric } else { 3190b57cec5SDimitry Andric ResultSym = SymMgr.getSymSymExpr(LSym, BO_Sub, RSym, SymTy); 3200b57cec5SDimitry Andric ResultOp = Op; 3210b57cec5SDimitry Andric ResultInt = RInt - LInt; // Opposite order! 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric } else { 3240b57cec5SDimitry Andric ResultSym = SymMgr.getSymSymExpr(LSym, Op, RSym, SymTy); 3250b57cec5SDimitry Andric ResultInt = (Op == BO_Add) ? (LInt + RInt) : (LInt - RInt); 3260b57cec5SDimitry Andric ResultOp = BO_Add; 3270b57cec5SDimitry Andric // Bring back the cosmetic difference. 3280b57cec5SDimitry Andric if (ResultInt < 0) { 3290b57cec5SDimitry Andric ResultInt = -ResultInt; 3300b57cec5SDimitry Andric ResultOp = BO_Sub; 3310b57cec5SDimitry Andric } else if (ResultInt == 0) { 3320b57cec5SDimitry Andric // Shortcut: Simplify "$x + 0" to "$x". 3330b57cec5SDimitry Andric return nonloc::SymbolVal(ResultSym); 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric } 3360b57cec5SDimitry Andric const llvm::APSInt &PersistentResultInt = BV.getValue(ResultInt); 3370b57cec5SDimitry Andric return nonloc::SymbolVal( 3380b57cec5SDimitry Andric SymMgr.getSymIntExpr(ResultSym, ResultOp, PersistentResultInt, ResultTy)); 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // Rearrange if symbol type matches the result type and if the operator is a 3420b57cec5SDimitry Andric // comparison operator, both symbol and constant must be within constant 3430b57cec5SDimitry Andric // overflow bounds. 3440b57cec5SDimitry Andric static bool shouldRearrange(ProgramStateRef State, BinaryOperator::Opcode Op, 3450b57cec5SDimitry Andric SymbolRef Sym, llvm::APSInt Int, QualType Ty) { 3460b57cec5SDimitry Andric return Sym->getType() == Ty && 3470b57cec5SDimitry Andric (!BinaryOperator::isComparisonOp(Op) || 3480b57cec5SDimitry Andric (isWithinConstantOverflowBounds(Sym, State) && 3490b57cec5SDimitry Andric isWithinConstantOverflowBounds(Int))); 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric 352bdd1243dSDimitry Andric static std::optional<NonLoc> tryRearrange(ProgramStateRef State, 3530b57cec5SDimitry Andric BinaryOperator::Opcode Op, NonLoc Lhs, 3540b57cec5SDimitry Andric NonLoc Rhs, QualType ResultTy) { 3550b57cec5SDimitry Andric ProgramStateManager &StateMgr = State->getStateManager(); 3560b57cec5SDimitry Andric SValBuilder &SVB = StateMgr.getSValBuilder(); 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric // We expect everything to be of the same type - this type. 3590b57cec5SDimitry Andric QualType SingleTy; 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric // FIXME: After putting complexity threshold to the symbols we can always 3620b57cec5SDimitry Andric // rearrange additive operations but rearrange comparisons only if 3630b57cec5SDimitry Andric // option is set. 364349cc55cSDimitry Andric if (!SVB.getAnalyzerOptions().ShouldAggressivelySimplifyBinaryOperation) 365bdd1243dSDimitry Andric return std::nullopt; 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric SymbolRef LSym = Lhs.getAsSymbol(); 3680b57cec5SDimitry Andric if (!LSym) 369bdd1243dSDimitry Andric return std::nullopt; 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric if (BinaryOperator::isComparisonOp(Op)) { 3720b57cec5SDimitry Andric SingleTy = LSym->getType(); 3730b57cec5SDimitry Andric if (ResultTy != SVB.getConditionType()) 374bdd1243dSDimitry Andric return std::nullopt; 3750b57cec5SDimitry Andric // Initialize SingleTy later with a symbol's type. 3760b57cec5SDimitry Andric } else if (BinaryOperator::isAdditiveOp(Op)) { 3770b57cec5SDimitry Andric SingleTy = ResultTy; 3780b57cec5SDimitry Andric if (LSym->getType() != SingleTy) 379bdd1243dSDimitry Andric return std::nullopt; 3800b57cec5SDimitry Andric } else { 3810b57cec5SDimitry Andric // Don't rearrange other operations. 382bdd1243dSDimitry Andric return std::nullopt; 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric assert(!SingleTy.isNull() && "We should have figured out the type by now!"); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric // Rearrange signed symbolic expressions only 3880b57cec5SDimitry Andric if (!SingleTy->isSignedIntegerOrEnumerationType()) 389bdd1243dSDimitry Andric return std::nullopt; 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric SymbolRef RSym = Rhs.getAsSymbol(); 3920b57cec5SDimitry Andric if (!RSym || RSym->getType() != SingleTy) 393bdd1243dSDimitry Andric return std::nullopt; 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric BasicValueFactory &BV = State->getBasicVals(); 3960b57cec5SDimitry Andric llvm::APSInt LInt, RInt; 3970b57cec5SDimitry Andric std::tie(LSym, LInt) = decomposeSymbol(LSym, BV); 3980b57cec5SDimitry Andric std::tie(RSym, RInt) = decomposeSymbol(RSym, BV); 3990b57cec5SDimitry Andric if (!shouldRearrange(State, Op, LSym, LInt, SingleTy) || 4000b57cec5SDimitry Andric !shouldRearrange(State, Op, RSym, RInt, SingleTy)) 401bdd1243dSDimitry Andric return std::nullopt; 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric // We know that no overflows can occur anymore. 4040b57cec5SDimitry Andric return doRearrangeUnchecked(State, Op, LSym, LInt, RSym, RInt); 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric SVal SimpleSValBuilder::evalBinOpNN(ProgramStateRef state, 4080b57cec5SDimitry Andric BinaryOperator::Opcode op, 4090b57cec5SDimitry Andric NonLoc lhs, NonLoc rhs, 4100b57cec5SDimitry Andric QualType resultTy) { 4110b57cec5SDimitry Andric NonLoc InputLHS = lhs; 4120b57cec5SDimitry Andric NonLoc InputRHS = rhs; 4130b57cec5SDimitry Andric 4144824e7fdSDimitry Andric // Constraints may have changed since the creation of a bound SVal. Check if 4154824e7fdSDimitry Andric // the values can be simplified based on those new constraints. 4164824e7fdSDimitry Andric SVal simplifiedLhs = simplifySVal(state, lhs); 4174824e7fdSDimitry Andric SVal simplifiedRhs = simplifySVal(state, rhs); 4184824e7fdSDimitry Andric if (auto simplifiedLhsAsNonLoc = simplifiedLhs.getAs<NonLoc>()) 4194824e7fdSDimitry Andric lhs = *simplifiedLhsAsNonLoc; 4204824e7fdSDimitry Andric if (auto simplifiedRhsAsNonLoc = simplifiedRhs.getAs<NonLoc>()) 4214824e7fdSDimitry Andric rhs = *simplifiedRhsAsNonLoc; 4224824e7fdSDimitry Andric 4230b57cec5SDimitry Andric // Handle trivial case where left-side and right-side are the same. 4240b57cec5SDimitry Andric if (lhs == rhs) 4250b57cec5SDimitry Andric switch (op) { 4260b57cec5SDimitry Andric default: 4270b57cec5SDimitry Andric break; 4280b57cec5SDimitry Andric case BO_EQ: 4290b57cec5SDimitry Andric case BO_LE: 4300b57cec5SDimitry Andric case BO_GE: 4310b57cec5SDimitry Andric return makeTruthVal(true, resultTy); 4320b57cec5SDimitry Andric case BO_LT: 4330b57cec5SDimitry Andric case BO_GT: 4340b57cec5SDimitry Andric case BO_NE: 4350b57cec5SDimitry Andric return makeTruthVal(false, resultTy); 4360b57cec5SDimitry Andric case BO_Xor: 4370b57cec5SDimitry Andric case BO_Sub: 4380b57cec5SDimitry Andric if (resultTy->isIntegralOrEnumerationType()) 4390b57cec5SDimitry Andric return makeIntVal(0, resultTy); 440fe6060f1SDimitry Andric return evalCast(makeIntVal(0, /*isUnsigned=*/false), resultTy, 441fe6060f1SDimitry Andric QualType{}); 4420b57cec5SDimitry Andric case BO_Or: 4430b57cec5SDimitry Andric case BO_And: 444fe6060f1SDimitry Andric return evalCast(lhs, resultTy, QualType{}); 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric 44704eeddc0SDimitry Andric while (true) { 4480b57cec5SDimitry Andric switch (lhs.getSubKind()) { 4490b57cec5SDimitry Andric default: 4500b57cec5SDimitry Andric return makeSymExprValNN(op, lhs, rhs, resultTy); 4510b57cec5SDimitry Andric case nonloc::PointerToMemberKind: { 4520b57cec5SDimitry Andric assert(rhs.getSubKind() == nonloc::PointerToMemberKind && 4530b57cec5SDimitry Andric "Both SVals should have pointer-to-member-type"); 4540b57cec5SDimitry Andric auto LPTM = lhs.castAs<nonloc::PointerToMember>(), 4550b57cec5SDimitry Andric RPTM = rhs.castAs<nonloc::PointerToMember>(); 4560b57cec5SDimitry Andric auto LPTMD = LPTM.getPTMData(), RPTMD = RPTM.getPTMData(); 4570b57cec5SDimitry Andric switch (op) { 4580b57cec5SDimitry Andric case BO_EQ: 4590b57cec5SDimitry Andric return makeTruthVal(LPTMD == RPTMD, resultTy); 4600b57cec5SDimitry Andric case BO_NE: 4610b57cec5SDimitry Andric return makeTruthVal(LPTMD != RPTMD, resultTy); 4620b57cec5SDimitry Andric default: 4630b57cec5SDimitry Andric return UnknownVal(); 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric case nonloc::LocAsIntegerKind: { 4670b57cec5SDimitry Andric Loc lhsL = lhs.castAs<nonloc::LocAsInteger>().getLoc(); 4680b57cec5SDimitry Andric switch (rhs.getSubKind()) { 4690b57cec5SDimitry Andric case nonloc::LocAsIntegerKind: 4700b57cec5SDimitry Andric // FIXME: at the moment the implementation 4710b57cec5SDimitry Andric // of modeling "pointers as integers" is not complete. 4720b57cec5SDimitry Andric if (!BinaryOperator::isComparisonOp(op)) 4730b57cec5SDimitry Andric return UnknownVal(); 4740b57cec5SDimitry Andric return evalBinOpLL(state, op, lhsL, 4750b57cec5SDimitry Andric rhs.castAs<nonloc::LocAsInteger>().getLoc(), 4760b57cec5SDimitry Andric resultTy); 4770b57cec5SDimitry Andric case nonloc::ConcreteIntKind: { 4780b57cec5SDimitry Andric // FIXME: at the moment the implementation 4790b57cec5SDimitry Andric // of modeling "pointers as integers" is not complete. 4800b57cec5SDimitry Andric if (!BinaryOperator::isComparisonOp(op)) 4810b57cec5SDimitry Andric return UnknownVal(); 4820b57cec5SDimitry Andric // Transform the integer into a location and compare. 4830b57cec5SDimitry Andric // FIXME: This only makes sense for comparisons. If we want to, say, 4840b57cec5SDimitry Andric // add 1 to a LocAsInteger, we'd better unpack the Loc and add to it, 4850b57cec5SDimitry Andric // then pack it back into a LocAsInteger. 4860b57cec5SDimitry Andric llvm::APSInt i = rhs.castAs<nonloc::ConcreteInt>().getValue(); 4870b57cec5SDimitry Andric // If the region has a symbolic base, pay attention to the type; it 4880b57cec5SDimitry Andric // might be coming from a non-default address space. For non-symbolic 4890b57cec5SDimitry Andric // regions it doesn't matter that much because such comparisons would 4900b57cec5SDimitry Andric // most likely evaluate to concrete false anyway. FIXME: We might 4910b57cec5SDimitry Andric // still need to handle the non-comparison case. 4920b57cec5SDimitry Andric if (SymbolRef lSym = lhs.getAsLocSymbol(true)) 4930b57cec5SDimitry Andric BasicVals.getAPSIntType(lSym->getType()).apply(i); 4940b57cec5SDimitry Andric else 4950b57cec5SDimitry Andric BasicVals.getAPSIntType(Context.VoidPtrTy).apply(i); 4960b57cec5SDimitry Andric return evalBinOpLL(state, op, lhsL, makeLoc(i), resultTy); 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric default: 4990b57cec5SDimitry Andric switch (op) { 5000b57cec5SDimitry Andric case BO_EQ: 5010b57cec5SDimitry Andric return makeTruthVal(false, resultTy); 5020b57cec5SDimitry Andric case BO_NE: 5030b57cec5SDimitry Andric return makeTruthVal(true, resultTy); 5040b57cec5SDimitry Andric default: 5050b57cec5SDimitry Andric // This case also handles pointer arithmetic. 5060b57cec5SDimitry Andric return makeSymExprValNN(op, InputLHS, InputRHS, resultTy); 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric } 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric case nonloc::ConcreteIntKind: { 5110b57cec5SDimitry Andric llvm::APSInt LHSValue = lhs.castAs<nonloc::ConcreteInt>().getValue(); 5120b57cec5SDimitry Andric 5130b57cec5SDimitry Andric // If we're dealing with two known constants, just perform the operation. 51481ad6265SDimitry Andric if (const llvm::APSInt *KnownRHSValue = getConstValue(state, rhs)) { 5150b57cec5SDimitry Andric llvm::APSInt RHSValue = *KnownRHSValue; 5160b57cec5SDimitry Andric if (BinaryOperator::isComparisonOp(op)) { 5170b57cec5SDimitry Andric // We're looking for a type big enough to compare the two values. 5180b57cec5SDimitry Andric // FIXME: This is not correct. char + short will result in a promotion 5190b57cec5SDimitry Andric // to int. Unfortunately we have lost types by this point. 5200b57cec5SDimitry Andric APSIntType CompareType = std::max(APSIntType(LHSValue), 5210b57cec5SDimitry Andric APSIntType(RHSValue)); 5220b57cec5SDimitry Andric CompareType.apply(LHSValue); 5230b57cec5SDimitry Andric CompareType.apply(RHSValue); 5240b57cec5SDimitry Andric } else if (!BinaryOperator::isShiftOp(op)) { 5250b57cec5SDimitry Andric APSIntType IntType = BasicVals.getAPSIntType(resultTy); 5260b57cec5SDimitry Andric IntType.apply(LHSValue); 5270b57cec5SDimitry Andric IntType.apply(RHSValue); 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric const llvm::APSInt *Result = 5310b57cec5SDimitry Andric BasicVals.evalAPSInt(op, LHSValue, RHSValue); 5320b57cec5SDimitry Andric if (!Result) 5330b57cec5SDimitry Andric return UndefinedVal(); 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric return nonloc::ConcreteInt(*Result); 5360b57cec5SDimitry Andric } 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric // Swap the left and right sides and flip the operator if doing so 5390b57cec5SDimitry Andric // allows us to better reason about the expression (this is a form 5400b57cec5SDimitry Andric // of expression canonicalization). 5410b57cec5SDimitry Andric // While we're at it, catch some special cases for non-commutative ops. 5420b57cec5SDimitry Andric switch (op) { 5430b57cec5SDimitry Andric case BO_LT: 5440b57cec5SDimitry Andric case BO_GT: 5450b57cec5SDimitry Andric case BO_LE: 5460b57cec5SDimitry Andric case BO_GE: 5470b57cec5SDimitry Andric op = BinaryOperator::reverseComparisonOp(op); 548bdd1243dSDimitry Andric [[fallthrough]]; 5490b57cec5SDimitry Andric case BO_EQ: 5500b57cec5SDimitry Andric case BO_NE: 5510b57cec5SDimitry Andric case BO_Add: 5520b57cec5SDimitry Andric case BO_Mul: 5530b57cec5SDimitry Andric case BO_And: 5540b57cec5SDimitry Andric case BO_Xor: 5550b57cec5SDimitry Andric case BO_Or: 5560b57cec5SDimitry Andric std::swap(lhs, rhs); 5570b57cec5SDimitry Andric continue; 5580b57cec5SDimitry Andric case BO_Shr: 5590b57cec5SDimitry Andric // (~0)>>a 560349cc55cSDimitry Andric if (LHSValue.isAllOnes() && LHSValue.isSigned()) 561fe6060f1SDimitry Andric return evalCast(lhs, resultTy, QualType{}); 562bdd1243dSDimitry Andric [[fallthrough]]; 5630b57cec5SDimitry Andric case BO_Shl: 5640b57cec5SDimitry Andric // 0<<a and 0>>a 5650b57cec5SDimitry Andric if (LHSValue == 0) 566fe6060f1SDimitry Andric return evalCast(lhs, resultTy, QualType{}); 5670b57cec5SDimitry Andric return makeSymExprValNN(op, InputLHS, InputRHS, resultTy); 568fe6060f1SDimitry Andric case BO_Div: 569fe6060f1SDimitry Andric // 0 / x == 0 5705ffd83dbSDimitry Andric case BO_Rem: 5715ffd83dbSDimitry Andric // 0 % x == 0 5725ffd83dbSDimitry Andric if (LHSValue == 0) 5735ffd83dbSDimitry Andric return makeZeroVal(resultTy); 574bdd1243dSDimitry Andric [[fallthrough]]; 5750b57cec5SDimitry Andric default: 5760b57cec5SDimitry Andric return makeSymExprValNN(op, InputLHS, InputRHS, resultTy); 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric } 5790b57cec5SDimitry Andric case nonloc::SymbolValKind: { 5800b57cec5SDimitry Andric // We only handle LHS as simple symbols or SymIntExprs. 5810b57cec5SDimitry Andric SymbolRef Sym = lhs.castAs<nonloc::SymbolVal>().getSymbol(); 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric // LHS is a symbolic expression. 5840b57cec5SDimitry Andric if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(Sym)) { 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric // Is this a logical not? (!x is represented as x == 0.) 5870b57cec5SDimitry Andric if (op == BO_EQ && rhs.isZeroConstant()) { 5880b57cec5SDimitry Andric // We know how to negate certain expressions. Simplify them here. 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric BinaryOperator::Opcode opc = symIntExpr->getOpcode(); 5910b57cec5SDimitry Andric switch (opc) { 5920b57cec5SDimitry Andric default: 5930b57cec5SDimitry Andric // We don't know how to negate this operation. 5940b57cec5SDimitry Andric // Just handle it as if it were a normal comparison to 0. 5950b57cec5SDimitry Andric break; 5960b57cec5SDimitry Andric case BO_LAnd: 5970b57cec5SDimitry Andric case BO_LOr: 5980b57cec5SDimitry Andric llvm_unreachable("Logical operators handled by branching logic."); 5990b57cec5SDimitry Andric case BO_Assign: 6000b57cec5SDimitry Andric case BO_MulAssign: 6010b57cec5SDimitry Andric case BO_DivAssign: 6020b57cec5SDimitry Andric case BO_RemAssign: 6030b57cec5SDimitry Andric case BO_AddAssign: 6040b57cec5SDimitry Andric case BO_SubAssign: 6050b57cec5SDimitry Andric case BO_ShlAssign: 6060b57cec5SDimitry Andric case BO_ShrAssign: 6070b57cec5SDimitry Andric case BO_AndAssign: 6080b57cec5SDimitry Andric case BO_XorAssign: 6090b57cec5SDimitry Andric case BO_OrAssign: 6100b57cec5SDimitry Andric case BO_Comma: 6110b57cec5SDimitry Andric llvm_unreachable("'=' and ',' operators handled by ExprEngine."); 6120b57cec5SDimitry Andric case BO_PtrMemD: 6130b57cec5SDimitry Andric case BO_PtrMemI: 6140b57cec5SDimitry Andric llvm_unreachable("Pointer arithmetic not handled here."); 6150b57cec5SDimitry Andric case BO_LT: 6160b57cec5SDimitry Andric case BO_GT: 6170b57cec5SDimitry Andric case BO_LE: 6180b57cec5SDimitry Andric case BO_GE: 6190b57cec5SDimitry Andric case BO_EQ: 6200b57cec5SDimitry Andric case BO_NE: 6210b57cec5SDimitry Andric assert(resultTy->isBooleanType() || 6220b57cec5SDimitry Andric resultTy == getConditionType()); 6230b57cec5SDimitry Andric assert(symIntExpr->getType()->isBooleanType() || 6240b57cec5SDimitry Andric getContext().hasSameUnqualifiedType(symIntExpr->getType(), 6250b57cec5SDimitry Andric getConditionType())); 6260b57cec5SDimitry Andric // Negate the comparison and make a value. 6270b57cec5SDimitry Andric opc = BinaryOperator::negateComparisonOp(opc); 6280b57cec5SDimitry Andric return makeNonLoc(symIntExpr->getLHS(), opc, 6290b57cec5SDimitry Andric symIntExpr->getRHS(), resultTy); 6300b57cec5SDimitry Andric } 6310b57cec5SDimitry Andric } 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric // For now, only handle expressions whose RHS is a constant. 63481ad6265SDimitry Andric if (const llvm::APSInt *RHSValue = getConstValue(state, rhs)) { 6350b57cec5SDimitry Andric // If both the LHS and the current expression are additive, 6360b57cec5SDimitry Andric // fold their constants and try again. 6370b57cec5SDimitry Andric if (BinaryOperator::isAdditiveOp(op)) { 6380b57cec5SDimitry Andric BinaryOperator::Opcode lop = symIntExpr->getOpcode(); 6390b57cec5SDimitry Andric if (BinaryOperator::isAdditiveOp(lop)) { 6400b57cec5SDimitry Andric // Convert the two constants to a common type, then combine them. 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric // resultTy may not be the best type to convert to, but it's 6430b57cec5SDimitry Andric // probably the best choice in expressions with mixed type 6440b57cec5SDimitry Andric // (such as x+1U+2LL). The rules for implicit conversions should 6450b57cec5SDimitry Andric // choose a reasonable type to preserve the expression, and will 6460b57cec5SDimitry Andric // at least match how the value is going to be used. 6470b57cec5SDimitry Andric APSIntType IntType = BasicVals.getAPSIntType(resultTy); 6480b57cec5SDimitry Andric const llvm::APSInt &first = IntType.convert(symIntExpr->getRHS()); 6490b57cec5SDimitry Andric const llvm::APSInt &second = IntType.convert(*RHSValue); 6500b57cec5SDimitry Andric 65181ad6265SDimitry Andric // If the op and lop agrees, then we just need to 65281ad6265SDimitry Andric // sum the constants. Otherwise, we change to operation 65381ad6265SDimitry Andric // type if substraction would produce negative value 65481ad6265SDimitry Andric // (and cause overflow for unsigned integers), 65581ad6265SDimitry Andric // as consequence x+1U-10 produces x-9U, instead 65681ad6265SDimitry Andric // of x+4294967287U, that would be produced without this 65781ad6265SDimitry Andric // additional check. 6580b57cec5SDimitry Andric const llvm::APSInt *newRHS; 65981ad6265SDimitry Andric if (lop == op) { 6600b57cec5SDimitry Andric newRHS = BasicVals.evalAPSInt(BO_Add, first, second); 66181ad6265SDimitry Andric } else if (first >= second) { 6620b57cec5SDimitry Andric newRHS = BasicVals.evalAPSInt(BO_Sub, first, second); 66381ad6265SDimitry Andric op = lop; 66481ad6265SDimitry Andric } else { 66581ad6265SDimitry Andric newRHS = BasicVals.evalAPSInt(BO_Sub, second, first); 66681ad6265SDimitry Andric } 6670b57cec5SDimitry Andric 6680b57cec5SDimitry Andric assert(newRHS && "Invalid operation despite common type!"); 6690b57cec5SDimitry Andric rhs = nonloc::ConcreteInt(*newRHS); 6700b57cec5SDimitry Andric lhs = nonloc::SymbolVal(symIntExpr->getLHS()); 6710b57cec5SDimitry Andric continue; 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric } 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric // Otherwise, make a SymIntExpr out of the expression. 6760b57cec5SDimitry Andric return MakeSymIntVal(symIntExpr, op, *RHSValue, resultTy); 6770b57cec5SDimitry Andric } 6780b57cec5SDimitry Andric } 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric // Is the RHS a constant? 68181ad6265SDimitry Andric if (const llvm::APSInt *RHSValue = getConstValue(state, rhs)) 6820b57cec5SDimitry Andric return MakeSymIntVal(Sym, op, *RHSValue, resultTy); 6830b57cec5SDimitry Andric 684bdd1243dSDimitry Andric if (std::optional<NonLoc> V = tryRearrange(state, op, lhs, rhs, resultTy)) 6850b57cec5SDimitry Andric return *V; 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric // Give up -- this is not a symbolic expression we can handle. 6880b57cec5SDimitry Andric return makeSymExprValNN(op, InputLHS, InputRHS, resultTy); 6890b57cec5SDimitry Andric } 6900b57cec5SDimitry Andric } 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric static SVal evalBinOpFieldRegionFieldRegion(const FieldRegion *LeftFR, 6950b57cec5SDimitry Andric const FieldRegion *RightFR, 6960b57cec5SDimitry Andric BinaryOperator::Opcode op, 6970b57cec5SDimitry Andric QualType resultTy, 6980b57cec5SDimitry Andric SimpleSValBuilder &SVB) { 6990b57cec5SDimitry Andric // Only comparisons are meaningful here! 7000b57cec5SDimitry Andric if (!BinaryOperator::isComparisonOp(op)) 7010b57cec5SDimitry Andric return UnknownVal(); 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric // Next, see if the two FRs have the same super-region. 7040b57cec5SDimitry Andric // FIXME: This doesn't handle casts yet, and simply stripping the casts 7050b57cec5SDimitry Andric // doesn't help. 7060b57cec5SDimitry Andric if (LeftFR->getSuperRegion() != RightFR->getSuperRegion()) 7070b57cec5SDimitry Andric return UnknownVal(); 7080b57cec5SDimitry Andric 7090b57cec5SDimitry Andric const FieldDecl *LeftFD = LeftFR->getDecl(); 7100b57cec5SDimitry Andric const FieldDecl *RightFD = RightFR->getDecl(); 7110b57cec5SDimitry Andric const RecordDecl *RD = LeftFD->getParent(); 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric // Make sure the two FRs are from the same kind of record. Just in case! 7140b57cec5SDimitry Andric // FIXME: This is probably where inheritance would be a problem. 7150b57cec5SDimitry Andric if (RD != RightFD->getParent()) 7160b57cec5SDimitry Andric return UnknownVal(); 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric // We know for sure that the two fields are not the same, since that 7190b57cec5SDimitry Andric // would have given us the same SVal. 7200b57cec5SDimitry Andric if (op == BO_EQ) 7210b57cec5SDimitry Andric return SVB.makeTruthVal(false, resultTy); 7220b57cec5SDimitry Andric if (op == BO_NE) 7230b57cec5SDimitry Andric return SVB.makeTruthVal(true, resultTy); 7240b57cec5SDimitry Andric 7250b57cec5SDimitry Andric // Iterate through the fields and see which one comes first. 7260b57cec5SDimitry Andric // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field 7270b57cec5SDimitry Andric // members and the units in which bit-fields reside have addresses that 7280b57cec5SDimitry Andric // increase in the order in which they are declared." 7290b57cec5SDimitry Andric bool leftFirst = (op == BO_LT || op == BO_LE); 7300b57cec5SDimitry Andric for (const auto *I : RD->fields()) { 7310b57cec5SDimitry Andric if (I == LeftFD) 7320b57cec5SDimitry Andric return SVB.makeTruthVal(leftFirst, resultTy); 7330b57cec5SDimitry Andric if (I == RightFD) 7340b57cec5SDimitry Andric return SVB.makeTruthVal(!leftFirst, resultTy); 7350b57cec5SDimitry Andric } 7360b57cec5SDimitry Andric 7370b57cec5SDimitry Andric llvm_unreachable("Fields not found in parent record's definition"); 7380b57cec5SDimitry Andric } 7390b57cec5SDimitry Andric 74081ad6265SDimitry Andric // This is used in debug builds only for now because some downstream users 74181ad6265SDimitry Andric // may hit this assert in their subsequent merges. 74281ad6265SDimitry Andric // There are still places in the analyzer where equal bitwidth Locs 74381ad6265SDimitry Andric // are compared, and need to be found and corrected. Recent previous fixes have 74481ad6265SDimitry Andric // addressed the known problems of making NULLs with specific bitwidths 74581ad6265SDimitry Andric // for Loc comparisons along with deprecation of APIs for the same purpose. 74681ad6265SDimitry Andric // 74781ad6265SDimitry Andric static void assertEqualBitWidths(ProgramStateRef State, Loc RhsLoc, 74881ad6265SDimitry Andric Loc LhsLoc) { 74981ad6265SDimitry Andric // Implements a "best effort" check for RhsLoc and LhsLoc bit widths 75081ad6265SDimitry Andric ASTContext &Ctx = State->getStateManager().getContext(); 75181ad6265SDimitry Andric uint64_t RhsBitwidth = 75281ad6265SDimitry Andric RhsLoc.getType(Ctx).isNull() ? 0 : Ctx.getTypeSize(RhsLoc.getType(Ctx)); 75381ad6265SDimitry Andric uint64_t LhsBitwidth = 75481ad6265SDimitry Andric LhsLoc.getType(Ctx).isNull() ? 0 : Ctx.getTypeSize(LhsLoc.getType(Ctx)); 75581ad6265SDimitry Andric if (RhsBitwidth && LhsBitwidth && 75681ad6265SDimitry Andric (LhsLoc.getSubKind() == RhsLoc.getSubKind())) { 75781ad6265SDimitry Andric assert(RhsBitwidth == LhsBitwidth && 75881ad6265SDimitry Andric "RhsLoc and LhsLoc bitwidth must be same!"); 75981ad6265SDimitry Andric } 76081ad6265SDimitry Andric } 76181ad6265SDimitry Andric 7620b57cec5SDimitry Andric // FIXME: all this logic will change if/when we have MemRegion::getLocation(). 7630b57cec5SDimitry Andric SVal SimpleSValBuilder::evalBinOpLL(ProgramStateRef state, 7640b57cec5SDimitry Andric BinaryOperator::Opcode op, 7650b57cec5SDimitry Andric Loc lhs, Loc rhs, 7660b57cec5SDimitry Andric QualType resultTy) { 76781ad6265SDimitry Andric 76881ad6265SDimitry Andric // Assert that bitwidth of lhs and rhs are the same. 76981ad6265SDimitry Andric // This can happen if two different address spaces are used, 77081ad6265SDimitry Andric // and the bitwidths of the address spaces are different. 77181ad6265SDimitry Andric // See LIT case clang/test/Analysis/cstring-checker-addressspace.c 77281ad6265SDimitry Andric // FIXME: See comment above in the function assertEqualBitWidths 77381ad6265SDimitry Andric assertEqualBitWidths(state, rhs, lhs); 77481ad6265SDimitry Andric 7750b57cec5SDimitry Andric // Only comparisons and subtractions are valid operations on two pointers. 7760b57cec5SDimitry Andric // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15]. 7770b57cec5SDimitry Andric // However, if a pointer is casted to an integer, evalBinOpNN may end up 7780b57cec5SDimitry Andric // calling this function with another operation (PR7527). We don't attempt to 7790b57cec5SDimitry Andric // model this for now, but it could be useful, particularly when the 7800b57cec5SDimitry Andric // "location" is actually an integer value that's been passed through a void*. 7810b57cec5SDimitry Andric if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub)) 7820b57cec5SDimitry Andric return UnknownVal(); 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric // Special cases for when both sides are identical. 7850b57cec5SDimitry Andric if (lhs == rhs) { 7860b57cec5SDimitry Andric switch (op) { 7870b57cec5SDimitry Andric default: 7880b57cec5SDimitry Andric llvm_unreachable("Unimplemented operation for two identical values"); 7890b57cec5SDimitry Andric case BO_Sub: 7900b57cec5SDimitry Andric return makeZeroVal(resultTy); 7910b57cec5SDimitry Andric case BO_EQ: 7920b57cec5SDimitry Andric case BO_LE: 7930b57cec5SDimitry Andric case BO_GE: 7940b57cec5SDimitry Andric return makeTruthVal(true, resultTy); 7950b57cec5SDimitry Andric case BO_NE: 7960b57cec5SDimitry Andric case BO_LT: 7970b57cec5SDimitry Andric case BO_GT: 7980b57cec5SDimitry Andric return makeTruthVal(false, resultTy); 7990b57cec5SDimitry Andric } 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric 8020b57cec5SDimitry Andric switch (lhs.getSubKind()) { 8030b57cec5SDimitry Andric default: 8040b57cec5SDimitry Andric llvm_unreachable("Ordering not implemented for this Loc."); 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric case loc::GotoLabelKind: 8070b57cec5SDimitry Andric // The only thing we know about labels is that they're non-null. 8080b57cec5SDimitry Andric if (rhs.isZeroConstant()) { 8090b57cec5SDimitry Andric switch (op) { 8100b57cec5SDimitry Andric default: 8110b57cec5SDimitry Andric break; 8120b57cec5SDimitry Andric case BO_Sub: 813fe6060f1SDimitry Andric return evalCast(lhs, resultTy, QualType{}); 8140b57cec5SDimitry Andric case BO_EQ: 8150b57cec5SDimitry Andric case BO_LE: 8160b57cec5SDimitry Andric case BO_LT: 8170b57cec5SDimitry Andric return makeTruthVal(false, resultTy); 8180b57cec5SDimitry Andric case BO_NE: 8190b57cec5SDimitry Andric case BO_GT: 8200b57cec5SDimitry Andric case BO_GE: 8210b57cec5SDimitry Andric return makeTruthVal(true, resultTy); 8220b57cec5SDimitry Andric } 8230b57cec5SDimitry Andric } 8240b57cec5SDimitry Andric // There may be two labels for the same location, and a function region may 8250b57cec5SDimitry Andric // have the same address as a label at the start of the function (depending 8260b57cec5SDimitry Andric // on the ABI). 8270b57cec5SDimitry Andric // FIXME: we can probably do a comparison against other MemRegions, though. 8280b57cec5SDimitry Andric // FIXME: is there a way to tell if two labels refer to the same location? 8290b57cec5SDimitry Andric return UnknownVal(); 8300b57cec5SDimitry Andric 8310b57cec5SDimitry Andric case loc::ConcreteIntKind: { 83281ad6265SDimitry Andric auto L = lhs.castAs<loc::ConcreteInt>(); 83381ad6265SDimitry Andric 8340b57cec5SDimitry Andric // If one of the operands is a symbol and the other is a constant, 8350b57cec5SDimitry Andric // build an expression for use by the constraint manager. 8360b57cec5SDimitry Andric if (SymbolRef rSym = rhs.getAsLocSymbol()) { 8370b57cec5SDimitry Andric // We can only build expressions with symbols on the left, 8380b57cec5SDimitry Andric // so we need a reversible operator. 8390b57cec5SDimitry Andric if (!BinaryOperator::isComparisonOp(op) || op == BO_Cmp) 8400b57cec5SDimitry Andric return UnknownVal(); 8410b57cec5SDimitry Andric 8420b57cec5SDimitry Andric op = BinaryOperator::reverseComparisonOp(op); 84381ad6265SDimitry Andric return makeNonLoc(rSym, op, L.getValue(), resultTy); 8440b57cec5SDimitry Andric } 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric // If both operands are constants, just perform the operation. 847bdd1243dSDimitry Andric if (std::optional<loc::ConcreteInt> rInt = rhs.getAs<loc::ConcreteInt>()) { 84881ad6265SDimitry Andric assert(BinaryOperator::isComparisonOp(op) || op == BO_Sub); 8490b57cec5SDimitry Andric 85081ad6265SDimitry Andric if (const auto *ResultInt = 85181ad6265SDimitry Andric BasicVals.evalAPSInt(op, L.getValue(), rInt->getValue())) 85281ad6265SDimitry Andric return evalCast(nonloc::ConcreteInt(*ResultInt), resultTy, QualType{}); 8530b57cec5SDimitry Andric return UnknownVal(); 8540b57cec5SDimitry Andric } 8550b57cec5SDimitry Andric 8560b57cec5SDimitry Andric // Special case comparisons against NULL. 8570b57cec5SDimitry Andric // This must come after the test if the RHS is a symbol, which is used to 8580b57cec5SDimitry Andric // build constraints. The address of any non-symbolic region is guaranteed 8590b57cec5SDimitry Andric // to be non-NULL, as is any label. 86081ad6265SDimitry Andric assert((isa<loc::MemRegionVal, loc::GotoLabel>(rhs))); 8610b57cec5SDimitry Andric if (lhs.isZeroConstant()) { 8620b57cec5SDimitry Andric switch (op) { 8630b57cec5SDimitry Andric default: 8640b57cec5SDimitry Andric break; 8650b57cec5SDimitry Andric case BO_EQ: 8660b57cec5SDimitry Andric case BO_GT: 8670b57cec5SDimitry Andric case BO_GE: 8680b57cec5SDimitry Andric return makeTruthVal(false, resultTy); 8690b57cec5SDimitry Andric case BO_NE: 8700b57cec5SDimitry Andric case BO_LT: 8710b57cec5SDimitry Andric case BO_LE: 8720b57cec5SDimitry Andric return makeTruthVal(true, resultTy); 8730b57cec5SDimitry Andric } 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric // Comparing an arbitrary integer to a region or label address is 8770b57cec5SDimitry Andric // completely unknowable. 8780b57cec5SDimitry Andric return UnknownVal(); 8790b57cec5SDimitry Andric } 8800b57cec5SDimitry Andric case loc::MemRegionValKind: { 881bdd1243dSDimitry Andric if (std::optional<loc::ConcreteInt> rInt = rhs.getAs<loc::ConcreteInt>()) { 8820b57cec5SDimitry Andric // If one of the operands is a symbol and the other is a constant, 8830b57cec5SDimitry Andric // build an expression for use by the constraint manager. 8840b57cec5SDimitry Andric if (SymbolRef lSym = lhs.getAsLocSymbol(true)) { 8850b57cec5SDimitry Andric if (BinaryOperator::isComparisonOp(op)) 8860b57cec5SDimitry Andric return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy); 8870b57cec5SDimitry Andric return UnknownVal(); 8880b57cec5SDimitry Andric } 8890b57cec5SDimitry Andric // Special case comparisons to NULL. 8900b57cec5SDimitry Andric // This must come after the test if the LHS is a symbol, which is used to 8910b57cec5SDimitry Andric // build constraints. The address of any non-symbolic region is guaranteed 8920b57cec5SDimitry Andric // to be non-NULL. 8930b57cec5SDimitry Andric if (rInt->isZeroConstant()) { 8940b57cec5SDimitry Andric if (op == BO_Sub) 895fe6060f1SDimitry Andric return evalCast(lhs, resultTy, QualType{}); 8960b57cec5SDimitry Andric 8970b57cec5SDimitry Andric if (BinaryOperator::isComparisonOp(op)) { 8980b57cec5SDimitry Andric QualType boolType = getContext().BoolTy; 899fe6060f1SDimitry Andric NonLoc l = evalCast(lhs, boolType, QualType{}).castAs<NonLoc>(); 9000b57cec5SDimitry Andric NonLoc r = makeTruthVal(false, boolType).castAs<NonLoc>(); 9010b57cec5SDimitry Andric return evalBinOpNN(state, op, l, r, resultTy); 9020b57cec5SDimitry Andric } 9030b57cec5SDimitry Andric } 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric // Comparing a region to an arbitrary integer is completely unknowable. 9060b57cec5SDimitry Andric return UnknownVal(); 9070b57cec5SDimitry Andric } 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric // Get both values as regions, if possible. 9100b57cec5SDimitry Andric const MemRegion *LeftMR = lhs.getAsRegion(); 9110b57cec5SDimitry Andric assert(LeftMR && "MemRegionValKind SVal doesn't have a region!"); 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric const MemRegion *RightMR = rhs.getAsRegion(); 9140b57cec5SDimitry Andric if (!RightMR) 9150b57cec5SDimitry Andric // The RHS is probably a label, which in theory could address a region. 9160b57cec5SDimitry Andric // FIXME: we can probably make a more useful statement about non-code 9170b57cec5SDimitry Andric // regions, though. 9180b57cec5SDimitry Andric return UnknownVal(); 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric const MemRegion *LeftBase = LeftMR->getBaseRegion(); 9210b57cec5SDimitry Andric const MemRegion *RightBase = RightMR->getBaseRegion(); 9220b57cec5SDimitry Andric const MemSpaceRegion *LeftMS = LeftBase->getMemorySpace(); 9230b57cec5SDimitry Andric const MemSpaceRegion *RightMS = RightBase->getMemorySpace(); 9240b57cec5SDimitry Andric const MemSpaceRegion *UnknownMS = MemMgr.getUnknownRegion(); 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric // If the two regions are from different known memory spaces they cannot be 9270b57cec5SDimitry Andric // equal. Also, assume that no symbolic region (whose memory space is 9280b57cec5SDimitry Andric // unknown) is on the stack. 9290b57cec5SDimitry Andric if (LeftMS != RightMS && 9300b57cec5SDimitry Andric ((LeftMS != UnknownMS && RightMS != UnknownMS) || 9310b57cec5SDimitry Andric (isa<StackSpaceRegion>(LeftMS) || isa<StackSpaceRegion>(RightMS)))) { 9320b57cec5SDimitry Andric switch (op) { 9330b57cec5SDimitry Andric default: 9340b57cec5SDimitry Andric return UnknownVal(); 9350b57cec5SDimitry Andric case BO_EQ: 9360b57cec5SDimitry Andric return makeTruthVal(false, resultTy); 9370b57cec5SDimitry Andric case BO_NE: 9380b57cec5SDimitry Andric return makeTruthVal(true, resultTy); 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric // If both values wrap regions, see if they're from different base regions. 9430b57cec5SDimitry Andric // Note, heap base symbolic regions are assumed to not alias with 9440b57cec5SDimitry Andric // each other; for example, we assume that malloc returns different address 9450b57cec5SDimitry Andric // on each invocation. 9460b57cec5SDimitry Andric // FIXME: ObjC object pointers always reside on the heap, but currently 9470b57cec5SDimitry Andric // we treat their memory space as unknown, because symbolic pointers 9480b57cec5SDimitry Andric // to ObjC objects may alias. There should be a way to construct 9490b57cec5SDimitry Andric // possibly-aliasing heap-based regions. For instance, MacOSXApiChecker 9500b57cec5SDimitry Andric // guesses memory space for ObjC object pointers manually instead of 9510b57cec5SDimitry Andric // relying on us. 9520b57cec5SDimitry Andric if (LeftBase != RightBase && 9530b57cec5SDimitry Andric ((!isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) || 9540b57cec5SDimitry Andric (isa<HeapSpaceRegion>(LeftMS) || isa<HeapSpaceRegion>(RightMS))) ){ 9550b57cec5SDimitry Andric switch (op) { 9560b57cec5SDimitry Andric default: 9570b57cec5SDimitry Andric return UnknownVal(); 9580b57cec5SDimitry Andric case BO_EQ: 9590b57cec5SDimitry Andric return makeTruthVal(false, resultTy); 9600b57cec5SDimitry Andric case BO_NE: 9610b57cec5SDimitry Andric return makeTruthVal(true, resultTy); 9620b57cec5SDimitry Andric } 9630b57cec5SDimitry Andric } 9640b57cec5SDimitry Andric 9650b57cec5SDimitry Andric // Handle special cases for when both regions are element regions. 9660b57cec5SDimitry Andric const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR); 9670b57cec5SDimitry Andric const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR); 9680b57cec5SDimitry Andric if (RightER && LeftER) { 9690b57cec5SDimitry Andric // Next, see if the two ERs have the same super-region and matching types. 9700b57cec5SDimitry Andric // FIXME: This should do something useful even if the types don't match, 9710b57cec5SDimitry Andric // though if both indexes are constant the RegionRawOffset path will 9720b57cec5SDimitry Andric // give the correct answer. 9730b57cec5SDimitry Andric if (LeftER->getSuperRegion() == RightER->getSuperRegion() && 9740b57cec5SDimitry Andric LeftER->getElementType() == RightER->getElementType()) { 9750b57cec5SDimitry Andric // Get the left index and cast it to the correct type. 9760b57cec5SDimitry Andric // If the index is unknown or undefined, bail out here. 9770b57cec5SDimitry Andric SVal LeftIndexVal = LeftER->getIndex(); 978bdd1243dSDimitry Andric std::optional<NonLoc> LeftIndex = LeftIndexVal.getAs<NonLoc>(); 9790b57cec5SDimitry Andric if (!LeftIndex) 9800b57cec5SDimitry Andric return UnknownVal(); 981fe6060f1SDimitry Andric LeftIndexVal = evalCast(*LeftIndex, ArrayIndexTy, QualType{}); 9820b57cec5SDimitry Andric LeftIndex = LeftIndexVal.getAs<NonLoc>(); 9830b57cec5SDimitry Andric if (!LeftIndex) 9840b57cec5SDimitry Andric return UnknownVal(); 9850b57cec5SDimitry Andric 9860b57cec5SDimitry Andric // Do the same for the right index. 9870b57cec5SDimitry Andric SVal RightIndexVal = RightER->getIndex(); 988bdd1243dSDimitry Andric std::optional<NonLoc> RightIndex = RightIndexVal.getAs<NonLoc>(); 9890b57cec5SDimitry Andric if (!RightIndex) 9900b57cec5SDimitry Andric return UnknownVal(); 991fe6060f1SDimitry Andric RightIndexVal = evalCast(*RightIndex, ArrayIndexTy, QualType{}); 9920b57cec5SDimitry Andric RightIndex = RightIndexVal.getAs<NonLoc>(); 9930b57cec5SDimitry Andric if (!RightIndex) 9940b57cec5SDimitry Andric return UnknownVal(); 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric // Actually perform the operation. 9970b57cec5SDimitry Andric // evalBinOpNN expects the two indexes to already be the right type. 9980b57cec5SDimitry Andric return evalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy); 9990b57cec5SDimitry Andric } 10000b57cec5SDimitry Andric } 10010b57cec5SDimitry Andric 10020b57cec5SDimitry Andric // Special handling of the FieldRegions, even with symbolic offsets. 10030b57cec5SDimitry Andric const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR); 10040b57cec5SDimitry Andric const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR); 10050b57cec5SDimitry Andric if (RightFR && LeftFR) { 10060b57cec5SDimitry Andric SVal R = evalBinOpFieldRegionFieldRegion(LeftFR, RightFR, op, resultTy, 10070b57cec5SDimitry Andric *this); 10080b57cec5SDimitry Andric if (!R.isUnknown()) 10090b57cec5SDimitry Andric return R; 10100b57cec5SDimitry Andric } 10110b57cec5SDimitry Andric 10120b57cec5SDimitry Andric // Compare the regions using the raw offsets. 10130b57cec5SDimitry Andric RegionOffset LeftOffset = LeftMR->getAsOffset(); 10140b57cec5SDimitry Andric RegionOffset RightOffset = RightMR->getAsOffset(); 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric if (LeftOffset.getRegion() != nullptr && 10170b57cec5SDimitry Andric LeftOffset.getRegion() == RightOffset.getRegion() && 10180b57cec5SDimitry Andric !LeftOffset.hasSymbolicOffset() && !RightOffset.hasSymbolicOffset()) { 10190b57cec5SDimitry Andric int64_t left = LeftOffset.getOffset(); 10200b57cec5SDimitry Andric int64_t right = RightOffset.getOffset(); 10210b57cec5SDimitry Andric 10220b57cec5SDimitry Andric switch (op) { 10230b57cec5SDimitry Andric default: 10240b57cec5SDimitry Andric return UnknownVal(); 10250b57cec5SDimitry Andric case BO_LT: 10260b57cec5SDimitry Andric return makeTruthVal(left < right, resultTy); 10270b57cec5SDimitry Andric case BO_GT: 10280b57cec5SDimitry Andric return makeTruthVal(left > right, resultTy); 10290b57cec5SDimitry Andric case BO_LE: 10300b57cec5SDimitry Andric return makeTruthVal(left <= right, resultTy); 10310b57cec5SDimitry Andric case BO_GE: 10320b57cec5SDimitry Andric return makeTruthVal(left >= right, resultTy); 10330b57cec5SDimitry Andric case BO_EQ: 10340b57cec5SDimitry Andric return makeTruthVal(left == right, resultTy); 10350b57cec5SDimitry Andric case BO_NE: 10360b57cec5SDimitry Andric return makeTruthVal(left != right, resultTy); 10370b57cec5SDimitry Andric } 10380b57cec5SDimitry Andric } 10390b57cec5SDimitry Andric 10400b57cec5SDimitry Andric // At this point we're not going to get a good answer, but we can try 10410b57cec5SDimitry Andric // conjuring an expression instead. 10420b57cec5SDimitry Andric SymbolRef LHSSym = lhs.getAsLocSymbol(); 10430b57cec5SDimitry Andric SymbolRef RHSSym = rhs.getAsLocSymbol(); 10440b57cec5SDimitry Andric if (LHSSym && RHSSym) 10450b57cec5SDimitry Andric return makeNonLoc(LHSSym, op, RHSSym, resultTy); 10460b57cec5SDimitry Andric 10470b57cec5SDimitry Andric // If we get here, we have no way of comparing the regions. 10480b57cec5SDimitry Andric return UnknownVal(); 10490b57cec5SDimitry Andric } 10500b57cec5SDimitry Andric } 10510b57cec5SDimitry Andric } 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andric SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state, 1054e8d8bef9SDimitry Andric BinaryOperator::Opcode op, Loc lhs, 1055e8d8bef9SDimitry Andric NonLoc rhs, QualType resultTy) { 10560b57cec5SDimitry Andric if (op >= BO_PtrMemD && op <= BO_PtrMemI) { 10570b57cec5SDimitry Andric if (auto PTMSV = rhs.getAs<nonloc::PointerToMember>()) { 10580b57cec5SDimitry Andric if (PTMSV->isNullMemberPointer()) 10590b57cec5SDimitry Andric return UndefinedVal(); 1060e8d8bef9SDimitry Andric 1061e8d8bef9SDimitry Andric auto getFieldLValue = [&](const auto *FD) -> SVal { 10620b57cec5SDimitry Andric SVal Result = lhs; 10630b57cec5SDimitry Andric 10640b57cec5SDimitry Andric for (const auto &I : *PTMSV) 10650b57cec5SDimitry Andric Result = StateMgr.getStoreManager().evalDerivedToBase( 10660b57cec5SDimitry Andric Result, I->getType(), I->isVirtual()); 1067e8d8bef9SDimitry Andric 10680b57cec5SDimitry Andric return state->getLValue(FD, Result); 1069e8d8bef9SDimitry Andric }; 1070e8d8bef9SDimitry Andric 1071e8d8bef9SDimitry Andric if (const auto *FD = PTMSV->getDeclAs<FieldDecl>()) { 1072e8d8bef9SDimitry Andric return getFieldLValue(FD); 1073e8d8bef9SDimitry Andric } 1074e8d8bef9SDimitry Andric if (const auto *FD = PTMSV->getDeclAs<IndirectFieldDecl>()) { 1075e8d8bef9SDimitry Andric return getFieldLValue(FD); 10760b57cec5SDimitry Andric } 10770b57cec5SDimitry Andric } 10780b57cec5SDimitry Andric 10790b57cec5SDimitry Andric return rhs; 10800b57cec5SDimitry Andric } 10810b57cec5SDimitry Andric 10820b57cec5SDimitry Andric assert(!BinaryOperator::isComparisonOp(op) && 10830b57cec5SDimitry Andric "arguments to comparison ops must be of the same type"); 10840b57cec5SDimitry Andric 10850b57cec5SDimitry Andric // Special case: rhs is a zero constant. 10860b57cec5SDimitry Andric if (rhs.isZeroConstant()) 10870b57cec5SDimitry Andric return lhs; 10880b57cec5SDimitry Andric 10890b57cec5SDimitry Andric // Perserve the null pointer so that it can be found by the DerefChecker. 10900b57cec5SDimitry Andric if (lhs.isZeroConstant()) 10910b57cec5SDimitry Andric return lhs; 10920b57cec5SDimitry Andric 10930b57cec5SDimitry Andric // We are dealing with pointer arithmetic. 10940b57cec5SDimitry Andric 10950b57cec5SDimitry Andric // Handle pointer arithmetic on constant values. 1096bdd1243dSDimitry Andric if (std::optional<nonloc::ConcreteInt> rhsInt = 1097bdd1243dSDimitry Andric rhs.getAs<nonloc::ConcreteInt>()) { 1098bdd1243dSDimitry Andric if (std::optional<loc::ConcreteInt> lhsInt = 1099bdd1243dSDimitry Andric lhs.getAs<loc::ConcreteInt>()) { 11000b57cec5SDimitry Andric const llvm::APSInt &leftI = lhsInt->getValue(); 11010b57cec5SDimitry Andric assert(leftI.isUnsigned()); 11020b57cec5SDimitry Andric llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true); 11030b57cec5SDimitry Andric 11040b57cec5SDimitry Andric // Convert the bitwidth of rightI. This should deal with overflow 11050b57cec5SDimitry Andric // since we are dealing with concrete values. 11060b57cec5SDimitry Andric rightI = rightI.extOrTrunc(leftI.getBitWidth()); 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric // Offset the increment by the pointer size. 11090b57cec5SDimitry Andric llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true); 11100b57cec5SDimitry Andric QualType pointeeType = resultTy->getPointeeType(); 11110b57cec5SDimitry Andric Multiplicand = getContext().getTypeSizeInChars(pointeeType).getQuantity(); 11120b57cec5SDimitry Andric rightI *= Multiplicand; 11130b57cec5SDimitry Andric 11140b57cec5SDimitry Andric // Compute the adjusted pointer. 11150b57cec5SDimitry Andric switch (op) { 11160b57cec5SDimitry Andric case BO_Add: 11170b57cec5SDimitry Andric rightI = leftI + rightI; 11180b57cec5SDimitry Andric break; 11190b57cec5SDimitry Andric case BO_Sub: 11200b57cec5SDimitry Andric rightI = leftI - rightI; 11210b57cec5SDimitry Andric break; 11220b57cec5SDimitry Andric default: 11230b57cec5SDimitry Andric llvm_unreachable("Invalid pointer arithmetic operation"); 11240b57cec5SDimitry Andric } 11250b57cec5SDimitry Andric return loc::ConcreteInt(getBasicValueFactory().getValue(rightI)); 11260b57cec5SDimitry Andric } 11270b57cec5SDimitry Andric } 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andric // Handle cases where 'lhs' is a region. 11300b57cec5SDimitry Andric if (const MemRegion *region = lhs.getAsRegion()) { 11310b57cec5SDimitry Andric rhs = convertToArrayIndex(rhs).castAs<NonLoc>(); 11320b57cec5SDimitry Andric SVal index = UnknownVal(); 11330b57cec5SDimitry Andric const SubRegion *superR = nullptr; 11340b57cec5SDimitry Andric // We need to know the type of the pointer in order to add an integer to it. 11350b57cec5SDimitry Andric // Depending on the type, different amount of bytes is added. 11360b57cec5SDimitry Andric QualType elementType; 11370b57cec5SDimitry Andric 11380b57cec5SDimitry Andric if (const ElementRegion *elemReg = dyn_cast<ElementRegion>(region)) { 11390b57cec5SDimitry Andric assert(op == BO_Add || op == BO_Sub); 11400b57cec5SDimitry Andric index = evalBinOpNN(state, op, elemReg->getIndex(), rhs, 11410b57cec5SDimitry Andric getArrayIndexType()); 11420b57cec5SDimitry Andric superR = cast<SubRegion>(elemReg->getSuperRegion()); 11430b57cec5SDimitry Andric elementType = elemReg->getElementType(); 11440b57cec5SDimitry Andric } 11450b57cec5SDimitry Andric else if (isa<SubRegion>(region)) { 11460b57cec5SDimitry Andric assert(op == BO_Add || op == BO_Sub); 11470b57cec5SDimitry Andric index = (op == BO_Add) ? rhs : evalMinus(rhs); 11480b57cec5SDimitry Andric superR = cast<SubRegion>(region); 11490b57cec5SDimitry Andric // TODO: Is this actually reliable? Maybe improving our MemRegion 11500b57cec5SDimitry Andric // hierarchy to provide typed regions for all non-void pointers would be 11510b57cec5SDimitry Andric // better. For instance, we cannot extend this towards LocAsInteger 11520b57cec5SDimitry Andric // operations, where result type of the expression is integer. 11530b57cec5SDimitry Andric if (resultTy->isAnyPointerType()) 11540b57cec5SDimitry Andric elementType = resultTy->getPointeeType(); 11550b57cec5SDimitry Andric } 11560b57cec5SDimitry Andric 11570b57cec5SDimitry Andric // Represent arithmetic on void pointers as arithmetic on char pointers. 11580b57cec5SDimitry Andric // It is fine when a TypedValueRegion of char value type represents 11590b57cec5SDimitry Andric // a void pointer. Note that arithmetic on void pointers is a GCC extension. 11600b57cec5SDimitry Andric if (elementType->isVoidType()) 11610b57cec5SDimitry Andric elementType = getContext().CharTy; 11620b57cec5SDimitry Andric 1163bdd1243dSDimitry Andric if (std::optional<NonLoc> indexV = index.getAs<NonLoc>()) { 11640b57cec5SDimitry Andric return loc::MemRegionVal(MemMgr.getElementRegion(elementType, *indexV, 11650b57cec5SDimitry Andric superR, getContext())); 11660b57cec5SDimitry Andric } 11670b57cec5SDimitry Andric } 11680b57cec5SDimitry Andric return UnknownVal(); 11690b57cec5SDimitry Andric } 11700b57cec5SDimitry Andric 117181ad6265SDimitry Andric const llvm::APSInt *SimpleSValBuilder::getConstValue(ProgramStateRef state, 11720b57cec5SDimitry Andric SVal V) { 11730b57cec5SDimitry Andric if (V.isUnknownOrUndef()) 11740b57cec5SDimitry Andric return nullptr; 11750b57cec5SDimitry Andric 1176bdd1243dSDimitry Andric if (std::optional<loc::ConcreteInt> X = V.getAs<loc::ConcreteInt>()) 11770b57cec5SDimitry Andric return &X->getValue(); 11780b57cec5SDimitry Andric 1179bdd1243dSDimitry Andric if (std::optional<nonloc::ConcreteInt> X = V.getAs<nonloc::ConcreteInt>()) 11800b57cec5SDimitry Andric return &X->getValue(); 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric if (SymbolRef Sym = V.getAsSymbol()) 11830b57cec5SDimitry Andric return state->getConstraintManager().getSymVal(state, Sym); 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric return nullptr; 11860b57cec5SDimitry Andric } 11870b57cec5SDimitry Andric 118881ad6265SDimitry Andric const llvm::APSInt *SimpleSValBuilder::getKnownValue(ProgramStateRef state, 118981ad6265SDimitry Andric SVal V) { 119081ad6265SDimitry Andric return getConstValue(state, simplifySVal(state, V)); 119181ad6265SDimitry Andric } 119281ad6265SDimitry Andric 11930eae32dcSDimitry Andric SVal SimpleSValBuilder::simplifyUntilFixpoint(ProgramStateRef State, SVal Val) { 11940eae32dcSDimitry Andric SVal SimplifiedVal = simplifySValOnce(State, Val); 11950eae32dcSDimitry Andric while (SimplifiedVal != Val) { 11960eae32dcSDimitry Andric Val = SimplifiedVal; 11970eae32dcSDimitry Andric SimplifiedVal = simplifySValOnce(State, Val); 11980eae32dcSDimitry Andric } 11990eae32dcSDimitry Andric return SimplifiedVal; 12000eae32dcSDimitry Andric } 12010eae32dcSDimitry Andric 12020b57cec5SDimitry Andric SVal SimpleSValBuilder::simplifySVal(ProgramStateRef State, SVal V) { 12030eae32dcSDimitry Andric return simplifyUntilFixpoint(State, V); 12040eae32dcSDimitry Andric } 12050eae32dcSDimitry Andric 12060eae32dcSDimitry Andric SVal SimpleSValBuilder::simplifySValOnce(ProgramStateRef State, SVal V) { 12070b57cec5SDimitry Andric // For now, this function tries to constant-fold symbols inside a 12080b57cec5SDimitry Andric // nonloc::SymbolVal, and does nothing else. More simplifications should 12090b57cec5SDimitry Andric // be possible, such as constant-folding an index in an ElementRegion. 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andric class Simplifier : public FullSValVisitor<Simplifier, SVal> { 12120b57cec5SDimitry Andric ProgramStateRef State; 12130b57cec5SDimitry Andric SValBuilder &SVB; 12140b57cec5SDimitry Andric 12150b57cec5SDimitry Andric // Cache results for the lifetime of the Simplifier. Results change every 12160b57cec5SDimitry Andric // time new constraints are added to the program state, which is the whole 12170b57cec5SDimitry Andric // point of simplifying, and for that very reason it's pointless to maintain 12180b57cec5SDimitry Andric // the same cache for the duration of the whole analysis. 12190b57cec5SDimitry Andric llvm::DenseMap<SymbolRef, SVal> Cached; 12200b57cec5SDimitry Andric 12210b57cec5SDimitry Andric static bool isUnchanged(SymbolRef Sym, SVal Val) { 12220b57cec5SDimitry Andric return Sym == Val.getAsSymbol(); 12230b57cec5SDimitry Andric } 12240b57cec5SDimitry Andric 12250b57cec5SDimitry Andric SVal cache(SymbolRef Sym, SVal V) { 12260b57cec5SDimitry Andric Cached[Sym] = V; 12270b57cec5SDimitry Andric return V; 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric SVal skip(SymbolRef Sym) { 12310b57cec5SDimitry Andric return cache(Sym, SVB.makeSymbolVal(Sym)); 12320b57cec5SDimitry Andric } 12330b57cec5SDimitry Andric 12344824e7fdSDimitry Andric // Return the known const value for the Sym if available, or return Undef 12354824e7fdSDimitry Andric // otherwise. 12364824e7fdSDimitry Andric SVal getConst(SymbolRef Sym) { 12374824e7fdSDimitry Andric const llvm::APSInt *Const = 12384824e7fdSDimitry Andric State->getConstraintManager().getSymVal(State, Sym); 12394824e7fdSDimitry Andric if (Const) 12404824e7fdSDimitry Andric return Loc::isLocType(Sym->getType()) ? (SVal)SVB.makeIntLocVal(*Const) 12414824e7fdSDimitry Andric : (SVal)SVB.makeIntVal(*Const); 12424824e7fdSDimitry Andric return UndefinedVal(); 12434824e7fdSDimitry Andric } 12444824e7fdSDimitry Andric 12454824e7fdSDimitry Andric SVal getConstOrVisit(SymbolRef Sym) { 12464824e7fdSDimitry Andric const SVal Ret = getConst(Sym); 12474824e7fdSDimitry Andric if (Ret.isUndef()) 12484824e7fdSDimitry Andric return Visit(Sym); 12494824e7fdSDimitry Andric return Ret; 12504824e7fdSDimitry Andric } 12514824e7fdSDimitry Andric 12520b57cec5SDimitry Andric public: 12530b57cec5SDimitry Andric Simplifier(ProgramStateRef State) 12540b57cec5SDimitry Andric : State(State), SVB(State->getStateManager().getSValBuilder()) {} 12550b57cec5SDimitry Andric 12560b57cec5SDimitry Andric SVal VisitSymbolData(const SymbolData *S) { 12570b57cec5SDimitry Andric // No cache here. 12580b57cec5SDimitry Andric if (const llvm::APSInt *I = 125981ad6265SDimitry Andric State->getConstraintManager().getSymVal(State, S)) 12600b57cec5SDimitry Andric return Loc::isLocType(S->getType()) ? (SVal)SVB.makeIntLocVal(*I) 12610b57cec5SDimitry Andric : (SVal)SVB.makeIntVal(*I); 12620b57cec5SDimitry Andric return SVB.makeSymbolVal(S); 12630b57cec5SDimitry Andric } 12640b57cec5SDimitry Andric 12650b57cec5SDimitry Andric SVal VisitSymIntExpr(const SymIntExpr *S) { 12660b57cec5SDimitry Andric auto I = Cached.find(S); 12670b57cec5SDimitry Andric if (I != Cached.end()) 12680b57cec5SDimitry Andric return I->second; 12690b57cec5SDimitry Andric 12704824e7fdSDimitry Andric SVal LHS = getConstOrVisit(S->getLHS()); 12710b57cec5SDimitry Andric if (isUnchanged(S->getLHS(), LHS)) 12720b57cec5SDimitry Andric return skip(S); 12730b57cec5SDimitry Andric 12740b57cec5SDimitry Andric SVal RHS; 12750b57cec5SDimitry Andric // By looking at the APSInt in the right-hand side of S, we cannot 12760b57cec5SDimitry Andric // figure out if it should be treated as a Loc or as a NonLoc. 12770b57cec5SDimitry Andric // So make our guess by recalling that we cannot multiply pointers 12780b57cec5SDimitry Andric // or compare a pointer to an integer. 12790b57cec5SDimitry Andric if (Loc::isLocType(S->getLHS()->getType()) && 12800b57cec5SDimitry Andric BinaryOperator::isComparisonOp(S->getOpcode())) { 12810b57cec5SDimitry Andric // The usual conversion of $sym to &SymRegion{$sym}, as they have 12820b57cec5SDimitry Andric // the same meaning for Loc-type symbols, but the latter form 12830b57cec5SDimitry Andric // is preferred in SVal computations for being Loc itself. 12840b57cec5SDimitry Andric if (SymbolRef Sym = LHS.getAsSymbol()) { 12850b57cec5SDimitry Andric assert(Loc::isLocType(Sym->getType())); 12860b57cec5SDimitry Andric LHS = SVB.makeLoc(Sym); 12870b57cec5SDimitry Andric } 12880b57cec5SDimitry Andric RHS = SVB.makeIntLocVal(S->getRHS()); 12890b57cec5SDimitry Andric } else { 12900b57cec5SDimitry Andric RHS = SVB.makeIntVal(S->getRHS()); 12910b57cec5SDimitry Andric } 12920b57cec5SDimitry Andric 12930b57cec5SDimitry Andric return cache( 12940b57cec5SDimitry Andric S, SVB.evalBinOp(State, S->getOpcode(), LHS, RHS, S->getType())); 12950b57cec5SDimitry Andric } 12960b57cec5SDimitry Andric 12974824e7fdSDimitry Andric SVal VisitIntSymExpr(const IntSymExpr *S) { 12984824e7fdSDimitry Andric auto I = Cached.find(S); 12994824e7fdSDimitry Andric if (I != Cached.end()) 13004824e7fdSDimitry Andric return I->second; 13014824e7fdSDimitry Andric 13024824e7fdSDimitry Andric SVal RHS = getConstOrVisit(S->getRHS()); 13034824e7fdSDimitry Andric if (isUnchanged(S->getRHS(), RHS)) 13044824e7fdSDimitry Andric return skip(S); 13054824e7fdSDimitry Andric 13064824e7fdSDimitry Andric SVal LHS = SVB.makeIntVal(S->getLHS()); 13074824e7fdSDimitry Andric return cache( 13084824e7fdSDimitry Andric S, SVB.evalBinOp(State, S->getOpcode(), LHS, RHS, S->getType())); 13094824e7fdSDimitry Andric } 13104824e7fdSDimitry Andric 13110b57cec5SDimitry Andric SVal VisitSymSymExpr(const SymSymExpr *S) { 13120b57cec5SDimitry Andric auto I = Cached.find(S); 13130b57cec5SDimitry Andric if (I != Cached.end()) 13140b57cec5SDimitry Andric return I->second; 13150b57cec5SDimitry Andric 13160b57cec5SDimitry Andric // For now don't try to simplify mixed Loc/NonLoc expressions 13170b57cec5SDimitry Andric // because they often appear from LocAsInteger operations 13180b57cec5SDimitry Andric // and we don't know how to combine a LocAsInteger 13190b57cec5SDimitry Andric // with a concrete value. 13200b57cec5SDimitry Andric if (Loc::isLocType(S->getLHS()->getType()) != 13210b57cec5SDimitry Andric Loc::isLocType(S->getRHS()->getType())) 13220b57cec5SDimitry Andric return skip(S); 13230b57cec5SDimitry Andric 13244824e7fdSDimitry Andric SVal LHS = getConstOrVisit(S->getLHS()); 13254824e7fdSDimitry Andric SVal RHS = getConstOrVisit(S->getRHS()); 13264824e7fdSDimitry Andric 13270b57cec5SDimitry Andric if (isUnchanged(S->getLHS(), LHS) && isUnchanged(S->getRHS(), RHS)) 13280b57cec5SDimitry Andric return skip(S); 13290b57cec5SDimitry Andric 13300b57cec5SDimitry Andric return cache( 13310b57cec5SDimitry Andric S, SVB.evalBinOp(State, S->getOpcode(), LHS, RHS, S->getType())); 13320b57cec5SDimitry Andric } 13330b57cec5SDimitry Andric 133481ad6265SDimitry Andric SVal VisitSymbolCast(const SymbolCast *S) { 133581ad6265SDimitry Andric auto I = Cached.find(S); 133681ad6265SDimitry Andric if (I != Cached.end()) 133781ad6265SDimitry Andric return I->second; 133881ad6265SDimitry Andric const SymExpr *OpSym = S->getOperand(); 133981ad6265SDimitry Andric SVal OpVal = getConstOrVisit(OpSym); 134081ad6265SDimitry Andric if (isUnchanged(OpSym, OpVal)) 134181ad6265SDimitry Andric return skip(S); 134281ad6265SDimitry Andric 134381ad6265SDimitry Andric return cache(S, SVB.evalCast(OpVal, S->getType(), OpSym->getType())); 134481ad6265SDimitry Andric } 134581ad6265SDimitry Andric 134681ad6265SDimitry Andric SVal VisitUnarySymExpr(const UnarySymExpr *S) { 134781ad6265SDimitry Andric auto I = Cached.find(S); 134881ad6265SDimitry Andric if (I != Cached.end()) 134981ad6265SDimitry Andric return I->second; 135081ad6265SDimitry Andric SVal Op = getConstOrVisit(S->getOperand()); 135181ad6265SDimitry Andric if (isUnchanged(S->getOperand(), Op)) 135281ad6265SDimitry Andric return skip(S); 135381ad6265SDimitry Andric 135481ad6265SDimitry Andric return cache( 135581ad6265SDimitry Andric S, SVB.evalUnaryOp(State, S->getOpcode(), Op, S->getType())); 135681ad6265SDimitry Andric } 135781ad6265SDimitry Andric 13580b57cec5SDimitry Andric SVal VisitSymExpr(SymbolRef S) { return nonloc::SymbolVal(S); } 13590b57cec5SDimitry Andric 13600b57cec5SDimitry Andric SVal VisitMemRegion(const MemRegion *R) { return loc::MemRegionVal(R); } 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andric SVal VisitNonLocSymbolVal(nonloc::SymbolVal V) { 13630b57cec5SDimitry Andric // Simplification is much more costly than computing complexity. 13640b57cec5SDimitry Andric // For high complexity, it may be not worth it. 13650b57cec5SDimitry Andric return Visit(V.getSymbol()); 13660b57cec5SDimitry Andric } 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric SVal VisitSVal(SVal V) { return V; } 13690b57cec5SDimitry Andric }; 13700b57cec5SDimitry Andric 13710b57cec5SDimitry Andric SVal SimplifiedV = Simplifier(State).Visit(V); 13720b57cec5SDimitry Andric return SimplifiedV; 13730b57cec5SDimitry Andric } 1374