clang API Documentation
00001 // SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- C++ -*- 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines SimpleSValBuilder, a basic implementation of SValBuilder. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h" 00015 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 00016 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 00017 00018 using namespace clang; 00019 using namespace ento; 00020 00021 namespace { 00022 class SimpleSValBuilder : public SValBuilder { 00023 protected: 00024 virtual SVal dispatchCast(SVal val, QualType castTy); 00025 virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy); 00026 virtual SVal evalCastFromLoc(Loc val, QualType castTy); 00027 00028 public: 00029 SimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, 00030 ProgramStateManager &stateMgr) 00031 : SValBuilder(alloc, context, stateMgr) {} 00032 virtual ~SimpleSValBuilder() {} 00033 00034 virtual SVal evalMinus(NonLoc val); 00035 virtual SVal evalComplement(NonLoc val); 00036 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, 00037 NonLoc lhs, NonLoc rhs, QualType resultTy); 00038 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op, 00039 Loc lhs, Loc rhs, QualType resultTy); 00040 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op, 00041 Loc lhs, NonLoc rhs, QualType resultTy); 00042 00043 /// getKnownValue - evaluates a given SVal. If the SVal has only one possible 00044 /// (integer) value, that value is returned. Otherwise, returns NULL. 00045 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal V); 00046 00047 SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op, 00048 const llvm::APSInt &RHS, QualType resultTy); 00049 }; 00050 } // end anonymous namespace 00051 00052 SValBuilder *ento::createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, 00053 ASTContext &context, 00054 ProgramStateManager &stateMgr) { 00055 return new SimpleSValBuilder(alloc, context, stateMgr); 00056 } 00057 00058 //===----------------------------------------------------------------------===// 00059 // Transfer function for Casts. 00060 //===----------------------------------------------------------------------===// 00061 00062 SVal SimpleSValBuilder::dispatchCast(SVal Val, QualType CastTy) { 00063 assert(isa<Loc>(&Val) || isa<NonLoc>(&Val)); 00064 return isa<Loc>(Val) ? evalCastFromLoc(cast<Loc>(Val), CastTy) 00065 : evalCastFromNonLoc(cast<NonLoc>(Val), CastTy); 00066 } 00067 00068 SVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) { 00069 00070 bool isLocType = Loc::isLocType(castTy); 00071 00072 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) { 00073 if (isLocType) 00074 return LI->getLoc(); 00075 00076 // FIXME: Correctly support promotions/truncations. 00077 unsigned castSize = Context.getTypeSize(castTy); 00078 if (castSize == LI->getNumBits()) 00079 return val; 00080 return makeLocAsInteger(LI->getLoc(), castSize); 00081 } 00082 00083 if (const SymExpr *se = val.getAsSymbolicExpression()) { 00084 QualType T = Context.getCanonicalType(se->getType(Context)); 00085 // If types are the same or both are integers, ignore the cast. 00086 // FIXME: Remove this hack when we support symbolic truncation/extension. 00087 // HACK: If both castTy and T are integers, ignore the cast. This is 00088 // not a permanent solution. Eventually we want to precisely handle 00089 // extension/truncation of symbolic integers. This prevents us from losing 00090 // precision when we assign 'x = y' and 'y' is symbolic and x and y are 00091 // different integer types. 00092 if (haveSameType(T, castTy)) 00093 return val; 00094 00095 if (!isLocType) 00096 return makeNonLoc(se, T, castTy); 00097 return UnknownVal(); 00098 } 00099 00100 // If value is a non integer constant, produce unknown. 00101 if (!isa<nonloc::ConcreteInt>(val)) 00102 return UnknownVal(); 00103 00104 // Only handle casts from integers to integers - if val is an integer constant 00105 // being cast to a non integer type, produce unknown. 00106 if (!isLocType && !castTy->isIntegerType()) 00107 return UnknownVal(); 00108 00109 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue(); 00110 BasicVals.getAPSIntType(castTy).apply(i); 00111 00112 if (isLocType) 00113 return makeIntLocVal(i); 00114 else 00115 return makeIntVal(i); 00116 } 00117 00118 SVal SimpleSValBuilder::evalCastFromLoc(Loc val, QualType castTy) { 00119 00120 // Casts from pointers -> pointers, just return the lval. 00121 // 00122 // Casts from pointers -> references, just return the lval. These 00123 // can be introduced by the frontend for corner cases, e.g 00124 // casting from va_list* to __builtin_va_list&. 00125 // 00126 if (Loc::isLocType(castTy) || castTy->isReferenceType()) 00127 return val; 00128 00129 // FIXME: Handle transparent unions where a value can be "transparently" 00130 // lifted into a union type. 00131 if (castTy->isUnionType()) 00132 return UnknownVal(); 00133 00134 if (castTy->isIntegerType()) { 00135 unsigned BitWidth = Context.getTypeSize(castTy); 00136 00137 if (!isa<loc::ConcreteInt>(val)) 00138 return makeLocAsInteger(val, BitWidth); 00139 00140 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue(); 00141 BasicVals.getAPSIntType(castTy).apply(i); 00142 return makeIntVal(i); 00143 } 00144 00145 // All other cases: return 'UnknownVal'. This includes casting pointers 00146 // to floats, which is probably badness it itself, but this is a good 00147 // intermediate solution until we do something better. 00148 return UnknownVal(); 00149 } 00150 00151 //===----------------------------------------------------------------------===// 00152 // Transfer function for unary operators. 00153 //===----------------------------------------------------------------------===// 00154 00155 SVal SimpleSValBuilder::evalMinus(NonLoc val) { 00156 switch (val.getSubKind()) { 00157 case nonloc::ConcreteIntKind: 00158 return cast<nonloc::ConcreteInt>(val).evalMinus(*this); 00159 default: 00160 return UnknownVal(); 00161 } 00162 } 00163 00164 SVal SimpleSValBuilder::evalComplement(NonLoc X) { 00165 switch (X.getSubKind()) { 00166 case nonloc::ConcreteIntKind: 00167 return cast<nonloc::ConcreteInt>(X).evalComplement(*this); 00168 default: 00169 return UnknownVal(); 00170 } 00171 } 00172 00173 //===----------------------------------------------------------------------===// 00174 // Transfer function for binary operators. 00175 //===----------------------------------------------------------------------===// 00176 00177 static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) { 00178 switch (op) { 00179 default: 00180 llvm_unreachable("Invalid opcode."); 00181 case BO_LT: return BO_GE; 00182 case BO_GT: return BO_LE; 00183 case BO_LE: return BO_GT; 00184 case BO_GE: return BO_LT; 00185 case BO_EQ: return BO_NE; 00186 case BO_NE: return BO_EQ; 00187 } 00188 } 00189 00190 static BinaryOperator::Opcode ReverseComparison(BinaryOperator::Opcode op) { 00191 switch (op) { 00192 default: 00193 llvm_unreachable("Invalid opcode."); 00194 case BO_LT: return BO_GT; 00195 case BO_GT: return BO_LT; 00196 case BO_LE: return BO_GE; 00197 case BO_GE: return BO_LE; 00198 case BO_EQ: 00199 case BO_NE: 00200 return op; 00201 } 00202 } 00203 00204 SVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS, 00205 BinaryOperator::Opcode op, 00206 const llvm::APSInt &RHS, 00207 QualType resultTy) { 00208 bool isIdempotent = false; 00209 00210 // Check for a few special cases with known reductions first. 00211 switch (op) { 00212 default: 00213 // We can't reduce this case; just treat it normally. 00214 break; 00215 case BO_Mul: 00216 // a*0 and a*1 00217 if (RHS == 0) 00218 return makeIntVal(0, resultTy); 00219 else if (RHS == 1) 00220 isIdempotent = true; 00221 break; 00222 case BO_Div: 00223 // a/0 and a/1 00224 if (RHS == 0) 00225 // This is also handled elsewhere. 00226 return UndefinedVal(); 00227 else if (RHS == 1) 00228 isIdempotent = true; 00229 break; 00230 case BO_Rem: 00231 // a%0 and a%1 00232 if (RHS == 0) 00233 // This is also handled elsewhere. 00234 return UndefinedVal(); 00235 else if (RHS == 1) 00236 return makeIntVal(0, resultTy); 00237 break; 00238 case BO_Add: 00239 case BO_Sub: 00240 case BO_Shl: 00241 case BO_Shr: 00242 case BO_Xor: 00243 // a+0, a-0, a<<0, a>>0, a^0 00244 if (RHS == 0) 00245 isIdempotent = true; 00246 break; 00247 case BO_And: 00248 // a&0 and a&(~0) 00249 if (RHS == 0) 00250 return makeIntVal(0, resultTy); 00251 else if (RHS.isAllOnesValue()) 00252 isIdempotent = true; 00253 break; 00254 case BO_Or: 00255 // a|0 and a|(~0) 00256 if (RHS == 0) 00257 isIdempotent = true; 00258 else if (RHS.isAllOnesValue()) { 00259 const llvm::APSInt &Result = BasicVals.Convert(resultTy, RHS); 00260 return nonloc::ConcreteInt(Result); 00261 } 00262 break; 00263 } 00264 00265 // Idempotent ops (like a*1) can still change the type of an expression. 00266 // Wrap the LHS up in a NonLoc again and let evalCastFromNonLoc do the 00267 // dirty work. 00268 if (isIdempotent) 00269 return evalCastFromNonLoc(nonloc::SymbolVal(LHS), resultTy); 00270 00271 // If we reach this point, the expression cannot be simplified. 00272 // Make a SymbolVal for the entire expression, after converting the RHS. 00273 const llvm::APSInt *ConvertedRHS = &RHS; 00274 if (BinaryOperator::isComparisonOp(op)) { 00275 // We're looking for a type big enough to compare the symbolic value 00276 // with the given constant. 00277 // FIXME: This is an approximation of Sema::UsualArithmeticConversions. 00278 ASTContext &Ctx = getContext(); 00279 QualType SymbolType = LHS->getType(Ctx); 00280 uint64_t ValWidth = RHS.getBitWidth(); 00281 uint64_t TypeWidth = Ctx.getTypeSize(SymbolType); 00282 00283 if (ValWidth < TypeWidth) { 00284 // If the value is too small, extend it. 00285 ConvertedRHS = &BasicVals.Convert(SymbolType, RHS); 00286 } else if (ValWidth == TypeWidth) { 00287 // If the value is signed but the symbol is unsigned, do the comparison 00288 // in unsigned space. [C99 6.3.1.8] 00289 // (For the opposite case, the value is already unsigned.) 00290 if (RHS.isSigned() && !SymbolType->isSignedIntegerOrEnumerationType()) 00291 ConvertedRHS = &BasicVals.Convert(SymbolType, RHS); 00292 } 00293 } else 00294 ConvertedRHS = &BasicVals.Convert(resultTy, RHS); 00295 00296 return makeNonLoc(LHS, op, *ConvertedRHS, resultTy); 00297 } 00298 00299 SVal SimpleSValBuilder::evalBinOpNN(ProgramStateRef state, 00300 BinaryOperator::Opcode op, 00301 NonLoc lhs, NonLoc rhs, 00302 QualType resultTy) { 00303 NonLoc InputLHS = lhs; 00304 NonLoc InputRHS = rhs; 00305 00306 // Handle trivial case where left-side and right-side are the same. 00307 if (lhs == rhs) 00308 switch (op) { 00309 default: 00310 break; 00311 case BO_EQ: 00312 case BO_LE: 00313 case BO_GE: 00314 return makeTruthVal(true, resultTy); 00315 case BO_LT: 00316 case BO_GT: 00317 case BO_NE: 00318 return makeTruthVal(false, resultTy); 00319 case BO_Xor: 00320 case BO_Sub: 00321 return makeIntVal(0, resultTy); 00322 case BO_Or: 00323 case BO_And: 00324 return evalCastFromNonLoc(lhs, resultTy); 00325 } 00326 00327 while (1) { 00328 switch (lhs.getSubKind()) { 00329 default: 00330 return makeSymExprValNN(state, op, lhs, rhs, resultTy); 00331 case nonloc::LocAsIntegerKind: { 00332 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc(); 00333 switch (rhs.getSubKind()) { 00334 case nonloc::LocAsIntegerKind: 00335 return evalBinOpLL(state, op, lhsL, 00336 cast<nonloc::LocAsInteger>(rhs).getLoc(), 00337 resultTy); 00338 case nonloc::ConcreteIntKind: { 00339 // Transform the integer into a location and compare. 00340 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue(); 00341 BasicVals.getAPSIntType(Context.VoidPtrTy).apply(i); 00342 return evalBinOpLL(state, op, lhsL, makeLoc(i), resultTy); 00343 } 00344 default: 00345 switch (op) { 00346 case BO_EQ: 00347 return makeTruthVal(false, resultTy); 00348 case BO_NE: 00349 return makeTruthVal(true, resultTy); 00350 default: 00351 // This case also handles pointer arithmetic. 00352 return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy); 00353 } 00354 } 00355 } 00356 case nonloc::ConcreteIntKind: { 00357 llvm::APSInt LHSValue = cast<nonloc::ConcreteInt>(lhs).getValue(); 00358 00359 // If we're dealing with two known constants, just perform the operation. 00360 if (const llvm::APSInt *KnownRHSValue = getKnownValue(state, rhs)) { 00361 llvm::APSInt RHSValue = *KnownRHSValue; 00362 if (BinaryOperator::isComparisonOp(op)) { 00363 // We're looking for a type big enough to compare the two values. 00364 // FIXME: This is not correct. char + short will result in a promotion 00365 // to int. Unfortunately we have lost types by this point. 00366 APSIntType CompareType = std::max(APSIntType(LHSValue), 00367 APSIntType(RHSValue)); 00368 CompareType.apply(LHSValue); 00369 CompareType.apply(RHSValue); 00370 } else if (!BinaryOperator::isShiftOp(op)) { 00371 APSIntType IntType = BasicVals.getAPSIntType(resultTy); 00372 IntType.apply(LHSValue); 00373 IntType.apply(RHSValue); 00374 } 00375 00376 const llvm::APSInt *Result = 00377 BasicVals.evalAPSInt(op, LHSValue, RHSValue); 00378 if (!Result) 00379 return UndefinedVal(); 00380 00381 return nonloc::ConcreteInt(*Result); 00382 } 00383 00384 // Swap the left and right sides and flip the operator if doing so 00385 // allows us to better reason about the expression (this is a form 00386 // of expression canonicalization). 00387 // While we're at it, catch some special cases for non-commutative ops. 00388 switch (op) { 00389 case BO_LT: 00390 case BO_GT: 00391 case BO_LE: 00392 case BO_GE: 00393 op = ReverseComparison(op); 00394 // FALL-THROUGH 00395 case BO_EQ: 00396 case BO_NE: 00397 case BO_Add: 00398 case BO_Mul: 00399 case BO_And: 00400 case BO_Xor: 00401 case BO_Or: 00402 std::swap(lhs, rhs); 00403 continue; 00404 case BO_Shr: 00405 // (~0)>>a 00406 if (LHSValue.isAllOnesValue() && LHSValue.isSigned()) 00407 return evalCastFromNonLoc(lhs, resultTy); 00408 // FALL-THROUGH 00409 case BO_Shl: 00410 // 0<<a and 0>>a 00411 if (LHSValue == 0) 00412 return evalCastFromNonLoc(lhs, resultTy); 00413 return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy); 00414 default: 00415 return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy); 00416 } 00417 } 00418 case nonloc::SymbolValKind: { 00419 // We only handle LHS as simple symbols or SymIntExprs. 00420 SymbolRef Sym = cast<nonloc::SymbolVal>(lhs).getSymbol(); 00421 00422 // LHS is a symbolic expression. 00423 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(Sym)) { 00424 00425 // Is this a logical not? (!x is represented as x == 0.) 00426 if (op == BO_EQ && rhs.isZeroConstant()) { 00427 // We know how to negate certain expressions. Simplify them here. 00428 00429 BinaryOperator::Opcode opc = symIntExpr->getOpcode(); 00430 switch (opc) { 00431 default: 00432 // We don't know how to negate this operation. 00433 // Just handle it as if it were a normal comparison to 0. 00434 break; 00435 case BO_LAnd: 00436 case BO_LOr: 00437 llvm_unreachable("Logical operators handled by branching logic."); 00438 case BO_Assign: 00439 case BO_MulAssign: 00440 case BO_DivAssign: 00441 case BO_RemAssign: 00442 case BO_AddAssign: 00443 case BO_SubAssign: 00444 case BO_ShlAssign: 00445 case BO_ShrAssign: 00446 case BO_AndAssign: 00447 case BO_XorAssign: 00448 case BO_OrAssign: 00449 case BO_Comma: 00450 llvm_unreachable("'=' and ',' operators handled by ExprEngine."); 00451 case BO_PtrMemD: 00452 case BO_PtrMemI: 00453 llvm_unreachable("Pointer arithmetic not handled here."); 00454 case BO_LT: 00455 case BO_GT: 00456 case BO_LE: 00457 case BO_GE: 00458 case BO_EQ: 00459 case BO_NE: 00460 // Negate the comparison and make a value. 00461 opc = NegateComparison(opc); 00462 assert(symIntExpr->getType(Context) == resultTy); 00463 return makeNonLoc(symIntExpr->getLHS(), opc, 00464 symIntExpr->getRHS(), resultTy); 00465 } 00466 } 00467 00468 // For now, only handle expressions whose RHS is a constant. 00469 if (const llvm::APSInt *RHSValue = getKnownValue(state, rhs)) { 00470 // If both the LHS and the current expression are additive, 00471 // fold their constants and try again. 00472 if (BinaryOperator::isAdditiveOp(op)) { 00473 BinaryOperator::Opcode lop = symIntExpr->getOpcode(); 00474 if (BinaryOperator::isAdditiveOp(lop)) { 00475 // Convert the two constants to a common type, then combine them. 00476 00477 // resultTy may not be the best type to convert to, but it's 00478 // probably the best choice in expressions with mixed type 00479 // (such as x+1U+2LL). The rules for implicit conversions should 00480 // choose a reasonable type to preserve the expression, and will 00481 // at least match how the value is going to be used. 00482 APSIntType IntType = BasicVals.getAPSIntType(resultTy); 00483 const llvm::APSInt &first = IntType.convert(symIntExpr->getRHS()); 00484 const llvm::APSInt &second = IntType.convert(*RHSValue); 00485 00486 const llvm::APSInt *newRHS; 00487 if (lop == op) 00488 newRHS = BasicVals.evalAPSInt(BO_Add, first, second); 00489 else 00490 newRHS = BasicVals.evalAPSInt(BO_Sub, first, second); 00491 00492 assert(newRHS && "Invalid operation despite common type!"); 00493 rhs = nonloc::ConcreteInt(*newRHS); 00494 lhs = nonloc::SymbolVal(symIntExpr->getLHS()); 00495 op = lop; 00496 continue; 00497 } 00498 } 00499 00500 // Otherwise, make a SymIntExpr out of the expression. 00501 return MakeSymIntVal(symIntExpr, op, *RHSValue, resultTy); 00502 } 00503 00504 00505 } else if (isa<SymbolData>(Sym)) { 00506 // Does the symbol simplify to a constant? If so, "fold" the constant 00507 // by setting 'lhs' to a ConcreteInt and try again. 00508 if (const llvm::APSInt *Constant = state->getSymVal(Sym)) { 00509 lhs = nonloc::ConcreteInt(*Constant); 00510 continue; 00511 } 00512 00513 // Is the RHS a constant? 00514 if (const llvm::APSInt *RHSValue = getKnownValue(state, rhs)) 00515 return MakeSymIntVal(Sym, op, *RHSValue, resultTy); 00516 } 00517 00518 // Give up -- this is not a symbolic expression we can handle. 00519 return makeSymExprValNN(state, op, InputLHS, InputRHS, resultTy); 00520 } 00521 } 00522 } 00523 } 00524 00525 // FIXME: all this logic will change if/when we have MemRegion::getLocation(). 00526 SVal SimpleSValBuilder::evalBinOpLL(ProgramStateRef state, 00527 BinaryOperator::Opcode op, 00528 Loc lhs, Loc rhs, 00529 QualType resultTy) { 00530 // Only comparisons and subtractions are valid operations on two pointers. 00531 // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15]. 00532 // However, if a pointer is casted to an integer, evalBinOpNN may end up 00533 // calling this function with another operation (PR7527). We don't attempt to 00534 // model this for now, but it could be useful, particularly when the 00535 // "location" is actually an integer value that's been passed through a void*. 00536 if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub)) 00537 return UnknownVal(); 00538 00539 // Special cases for when both sides are identical. 00540 if (lhs == rhs) { 00541 switch (op) { 00542 default: 00543 llvm_unreachable("Unimplemented operation for two identical values"); 00544 case BO_Sub: 00545 return makeZeroVal(resultTy); 00546 case BO_EQ: 00547 case BO_LE: 00548 case BO_GE: 00549 return makeTruthVal(true, resultTy); 00550 case BO_NE: 00551 case BO_LT: 00552 case BO_GT: 00553 return makeTruthVal(false, resultTy); 00554 } 00555 } 00556 00557 switch (lhs.getSubKind()) { 00558 default: 00559 llvm_unreachable("Ordering not implemented for this Loc."); 00560 00561 case loc::GotoLabelKind: 00562 // The only thing we know about labels is that they're non-null. 00563 if (rhs.isZeroConstant()) { 00564 switch (op) { 00565 default: 00566 break; 00567 case BO_Sub: 00568 return evalCastFromLoc(lhs, resultTy); 00569 case BO_EQ: 00570 case BO_LE: 00571 case BO_LT: 00572 return makeTruthVal(false, resultTy); 00573 case BO_NE: 00574 case BO_GT: 00575 case BO_GE: 00576 return makeTruthVal(true, resultTy); 00577 } 00578 } 00579 // There may be two labels for the same location, and a function region may 00580 // have the same address as a label at the start of the function (depending 00581 // on the ABI). 00582 // FIXME: we can probably do a comparison against other MemRegions, though. 00583 // FIXME: is there a way to tell if two labels refer to the same location? 00584 return UnknownVal(); 00585 00586 case loc::ConcreteIntKind: { 00587 // If one of the operands is a symbol and the other is a constant, 00588 // build an expression for use by the constraint manager. 00589 if (SymbolRef rSym = rhs.getAsLocSymbol()) { 00590 // We can only build expressions with symbols on the left, 00591 // so we need a reversible operator. 00592 if (!BinaryOperator::isComparisonOp(op)) 00593 return UnknownVal(); 00594 00595 const llvm::APSInt &lVal = cast<loc::ConcreteInt>(lhs).getValue(); 00596 return makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy); 00597 } 00598 00599 // If both operands are constants, just perform the operation. 00600 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) { 00601 SVal ResultVal = cast<loc::ConcreteInt>(lhs).evalBinOp(BasicVals, op, 00602 *rInt); 00603 if (Loc *Result = dyn_cast<Loc>(&ResultVal)) 00604 return evalCastFromLoc(*Result, resultTy); 00605 else 00606 return UnknownVal(); 00607 } 00608 00609 // Special case comparisons against NULL. 00610 // This must come after the test if the RHS is a symbol, which is used to 00611 // build constraints. The address of any non-symbolic region is guaranteed 00612 // to be non-NULL, as is any label. 00613 assert(isa<loc::MemRegionVal>(rhs) || isa<loc::GotoLabel>(rhs)); 00614 if (lhs.isZeroConstant()) { 00615 switch (op) { 00616 default: 00617 break; 00618 case BO_EQ: 00619 case BO_GT: 00620 case BO_GE: 00621 return makeTruthVal(false, resultTy); 00622 case BO_NE: 00623 case BO_LT: 00624 case BO_LE: 00625 return makeTruthVal(true, resultTy); 00626 } 00627 } 00628 00629 // Comparing an arbitrary integer to a region or label address is 00630 // completely unknowable. 00631 return UnknownVal(); 00632 } 00633 case loc::MemRegionKind: { 00634 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) { 00635 // If one of the operands is a symbol and the other is a constant, 00636 // build an expression for use by the constraint manager. 00637 if (SymbolRef lSym = lhs.getAsLocSymbol()) 00638 return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy); 00639 00640 // Special case comparisons to NULL. 00641 // This must come after the test if the LHS is a symbol, which is used to 00642 // build constraints. The address of any non-symbolic region is guaranteed 00643 // to be non-NULL. 00644 if (rInt->isZeroConstant()) { 00645 switch (op) { 00646 default: 00647 break; 00648 case BO_Sub: 00649 return evalCastFromLoc(lhs, resultTy); 00650 case BO_EQ: 00651 case BO_LT: 00652 case BO_LE: 00653 return makeTruthVal(false, resultTy); 00654 case BO_NE: 00655 case BO_GT: 00656 case BO_GE: 00657 return makeTruthVal(true, resultTy); 00658 } 00659 } 00660 00661 // Comparing a region to an arbitrary integer is completely unknowable. 00662 return UnknownVal(); 00663 } 00664 00665 // Get both values as regions, if possible. 00666 const MemRegion *LeftMR = lhs.getAsRegion(); 00667 assert(LeftMR && "MemRegionKind SVal doesn't have a region!"); 00668 00669 const MemRegion *RightMR = rhs.getAsRegion(); 00670 if (!RightMR) 00671 // The RHS is probably a label, which in theory could address a region. 00672 // FIXME: we can probably make a more useful statement about non-code 00673 // regions, though. 00674 return UnknownVal(); 00675 00676 // If both values wrap regions, see if they're from different base regions. 00677 const MemRegion *LeftBase = LeftMR->getBaseRegion(); 00678 const MemRegion *RightBase = RightMR->getBaseRegion(); 00679 if (LeftBase != RightBase && 00680 !isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) { 00681 switch (op) { 00682 default: 00683 return UnknownVal(); 00684 case BO_EQ: 00685 return makeTruthVal(false, resultTy); 00686 case BO_NE: 00687 return makeTruthVal(true, resultTy); 00688 } 00689 } 00690 00691 // The two regions are from the same base region. See if they're both a 00692 // type of region we know how to compare. 00693 const MemSpaceRegion *LeftMS = LeftBase->getMemorySpace(); 00694 const MemSpaceRegion *RightMS = RightBase->getMemorySpace(); 00695 00696 // Heuristic: assume that no symbolic region (whose memory space is 00697 // unknown) is on the stack. 00698 // FIXME: we should be able to be more precise once we can do better 00699 // aliasing constraints for symbolic regions, but this is a reasonable, 00700 // albeit unsound, assumption that holds most of the time. 00701 if (isa<StackSpaceRegion>(LeftMS) ^ isa<StackSpaceRegion>(RightMS)) { 00702 switch (op) { 00703 default: 00704 break; 00705 case BO_EQ: 00706 return makeTruthVal(false, resultTy); 00707 case BO_NE: 00708 return makeTruthVal(true, resultTy); 00709 } 00710 } 00711 00712 // FIXME: If/when there is a getAsRawOffset() for FieldRegions, this 00713 // ElementRegion path and the FieldRegion path below should be unified. 00714 if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) { 00715 // First see if the right region is also an ElementRegion. 00716 const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR); 00717 if (!RightER) 00718 return UnknownVal(); 00719 00720 // Next, see if the two ERs have the same super-region and matching types. 00721 // FIXME: This should do something useful even if the types don't match, 00722 // though if both indexes are constant the RegionRawOffset path will 00723 // give the correct answer. 00724 if (LeftER->getSuperRegion() == RightER->getSuperRegion() && 00725 LeftER->getElementType() == RightER->getElementType()) { 00726 // Get the left index and cast it to the correct type. 00727 // If the index is unknown or undefined, bail out here. 00728 SVal LeftIndexVal = LeftER->getIndex(); 00729 NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal); 00730 if (!LeftIndex) 00731 return UnknownVal(); 00732 LeftIndexVal = evalCastFromNonLoc(*LeftIndex, resultTy); 00733 LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal); 00734 if (!LeftIndex) 00735 return UnknownVal(); 00736 00737 // Do the same for the right index. 00738 SVal RightIndexVal = RightER->getIndex(); 00739 NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal); 00740 if (!RightIndex) 00741 return UnknownVal(); 00742 RightIndexVal = evalCastFromNonLoc(*RightIndex, resultTy); 00743 RightIndex = dyn_cast<NonLoc>(&RightIndexVal); 00744 if (!RightIndex) 00745 return UnknownVal(); 00746 00747 // Actually perform the operation. 00748 // evalBinOpNN expects the two indexes to already be the right type. 00749 return evalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy); 00750 } 00751 00752 // If the element indexes aren't comparable, see if the raw offsets are. 00753 RegionRawOffset LeftOffset = LeftER->getAsArrayOffset(); 00754 RegionRawOffset RightOffset = RightER->getAsArrayOffset(); 00755 00756 if (LeftOffset.getRegion() != NULL && 00757 LeftOffset.getRegion() == RightOffset.getRegion()) { 00758 CharUnits left = LeftOffset.getOffset(); 00759 CharUnits right = RightOffset.getOffset(); 00760 00761 switch (op) { 00762 default: 00763 return UnknownVal(); 00764 case BO_LT: 00765 return makeTruthVal(left < right, resultTy); 00766 case BO_GT: 00767 return makeTruthVal(left > right, resultTy); 00768 case BO_LE: 00769 return makeTruthVal(left <= right, resultTy); 00770 case BO_GE: 00771 return makeTruthVal(left >= right, resultTy); 00772 case BO_EQ: 00773 return makeTruthVal(left == right, resultTy); 00774 case BO_NE: 00775 return makeTruthVal(left != right, resultTy); 00776 } 00777 } 00778 00779 // If we get here, we have no way of comparing the ElementRegions. 00780 return UnknownVal(); 00781 } 00782 00783 // See if both regions are fields of the same structure. 00784 // FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars. 00785 if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) { 00786 // Only comparisons are meaningful here! 00787 if (!BinaryOperator::isComparisonOp(op)) 00788 return UnknownVal(); 00789 00790 // First see if the right region is also a FieldRegion. 00791 const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR); 00792 if (!RightFR) 00793 return UnknownVal(); 00794 00795 // Next, see if the two FRs have the same super-region. 00796 // FIXME: This doesn't handle casts yet, and simply stripping the casts 00797 // doesn't help. 00798 if (LeftFR->getSuperRegion() != RightFR->getSuperRegion()) 00799 return UnknownVal(); 00800 00801 const FieldDecl *LeftFD = LeftFR->getDecl(); 00802 const FieldDecl *RightFD = RightFR->getDecl(); 00803 const RecordDecl *RD = LeftFD->getParent(); 00804 00805 // Make sure the two FRs are from the same kind of record. Just in case! 00806 // FIXME: This is probably where inheritance would be a problem. 00807 if (RD != RightFD->getParent()) 00808 return UnknownVal(); 00809 00810 // We know for sure that the two fields are not the same, since that 00811 // would have given us the same SVal. 00812 if (op == BO_EQ) 00813 return makeTruthVal(false, resultTy); 00814 if (op == BO_NE) 00815 return makeTruthVal(true, resultTy); 00816 00817 // Iterate through the fields and see which one comes first. 00818 // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field 00819 // members and the units in which bit-fields reside have addresses that 00820 // increase in the order in which they are declared." 00821 bool leftFirst = (op == BO_LT || op == BO_LE); 00822 for (RecordDecl::field_iterator I = RD->field_begin(), 00823 E = RD->field_end(); I!=E; ++I) { 00824 if (&*I == LeftFD) 00825 return makeTruthVal(leftFirst, resultTy); 00826 if (&*I == RightFD) 00827 return makeTruthVal(!leftFirst, resultTy); 00828 } 00829 00830 llvm_unreachable("Fields not found in parent record's definition"); 00831 } 00832 00833 // If we get here, we have no way of comparing the regions. 00834 return UnknownVal(); 00835 } 00836 } 00837 } 00838 00839 SVal SimpleSValBuilder::evalBinOpLN(ProgramStateRef state, 00840 BinaryOperator::Opcode op, 00841 Loc lhs, NonLoc rhs, QualType resultTy) { 00842 00843 // Special case: rhs is a zero constant. 00844 if (rhs.isZeroConstant()) 00845 return lhs; 00846 00847 // Special case: 'rhs' is an integer that has the same width as a pointer and 00848 // we are using the integer location in a comparison. Normally this cannot be 00849 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32 00850 // can generate comparisons that trigger this code. 00851 // FIXME: Are all locations guaranteed to have pointer width? 00852 if (BinaryOperator::isComparisonOp(op)) { 00853 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) { 00854 const llvm::APSInt *x = &rhsInt->getValue(); 00855 ASTContext &ctx = Context; 00856 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) { 00857 // Convert the signedness of the integer (if necessary). 00858 if (x->isSigned()) 00859 x = &getBasicValueFactory().getValue(*x, true); 00860 00861 return evalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy); 00862 } 00863 } 00864 return UnknownVal(); 00865 } 00866 00867 // We are dealing with pointer arithmetic. 00868 00869 // Handle pointer arithmetic on constant values. 00870 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) { 00871 if (loc::ConcreteInt *lhsInt = dyn_cast<loc::ConcreteInt>(&lhs)) { 00872 const llvm::APSInt &leftI = lhsInt->getValue(); 00873 assert(leftI.isUnsigned()); 00874 llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true); 00875 00876 // Convert the bitwidth of rightI. This should deal with overflow 00877 // since we are dealing with concrete values. 00878 rightI = rightI.extOrTrunc(leftI.getBitWidth()); 00879 00880 // Offset the increment by the pointer size. 00881 llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true); 00882 rightI *= Multiplicand; 00883 00884 // Compute the adjusted pointer. 00885 switch (op) { 00886 case BO_Add: 00887 rightI = leftI + rightI; 00888 break; 00889 case BO_Sub: 00890 rightI = leftI - rightI; 00891 break; 00892 default: 00893 llvm_unreachable("Invalid pointer arithmetic operation"); 00894 } 00895 return loc::ConcreteInt(getBasicValueFactory().getValue(rightI)); 00896 } 00897 } 00898 00899 // Handle cases where 'lhs' is a region. 00900 if (const MemRegion *region = lhs.getAsRegion()) { 00901 rhs = cast<NonLoc>(convertToArrayIndex(rhs)); 00902 SVal index = UnknownVal(); 00903 const MemRegion *superR = 0; 00904 QualType elementType; 00905 00906 if (const ElementRegion *elemReg = dyn_cast<ElementRegion>(region)) { 00907 assert(op == BO_Add || op == BO_Sub); 00908 index = evalBinOpNN(state, op, elemReg->getIndex(), rhs, 00909 getArrayIndexType()); 00910 superR = elemReg->getSuperRegion(); 00911 elementType = elemReg->getElementType(); 00912 } 00913 else if (isa<SubRegion>(region)) { 00914 superR = region; 00915 index = rhs; 00916 if (const PointerType *PT = resultTy->getAs<PointerType>()) { 00917 elementType = PT->getPointeeType(); 00918 } 00919 else { 00920 const ObjCObjectPointerType *OT = 00921 resultTy->getAs<ObjCObjectPointerType>(); 00922 elementType = OT->getPointeeType(); 00923 } 00924 } 00925 00926 if (NonLoc *indexV = dyn_cast<NonLoc>(&index)) { 00927 return loc::MemRegionVal(MemMgr.getElementRegion(elementType, *indexV, 00928 superR, getContext())); 00929 } 00930 } 00931 return UnknownVal(); 00932 } 00933 00934 const llvm::APSInt *SimpleSValBuilder::getKnownValue(ProgramStateRef state, 00935 SVal V) { 00936 if (V.isUnknownOrUndef()) 00937 return NULL; 00938 00939 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V)) 00940 return &X->getValue(); 00941 00942 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V)) 00943 return &X->getValue(); 00944 00945 if (SymbolRef Sym = V.getAsSymbol()) 00946 return state->getSymVal(Sym); 00947 00948 // FIXME: Add support for SymExprs. 00949 return NULL; 00950 }