Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 1 | //= CStringChecker.h - Checks calls to C string functions ----------*- C++ -*-// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines CStringChecker, which is an assortment of checks on calls |
| 11 | // to functions in <string.h>. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "GRExprEngineExperimentalChecks.h" |
| 16 | #include "clang/Checker/BugReporter/BugType.h" |
| 17 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
| 18 | #include "llvm/ADT/StringSwitch.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
| 23 | class CStringChecker : public CheckerVisitor<CStringChecker> { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 24 | BugType *BT_Null, *BT_Bounds, *BT_Overlap; |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 25 | public: |
| 26 | CStringChecker() |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 27 | : BT_Null(0), BT_Bounds(0), BT_Overlap(0) {} |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 28 | static void *getTag() { static int tag; return &tag; } |
| 29 | |
| 30 | bool EvalCallExpr(CheckerContext &C, const CallExpr *CE); |
| 31 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 32 | typedef void (CStringChecker::*FnCheck)(CheckerContext &, const CallExpr *); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 33 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 34 | void EvalMemcpy(CheckerContext &C, const CallExpr *CE); |
| 35 | void EvalMemmove(CheckerContext &C, const CallExpr *CE); |
| 36 | void EvalBcopy(CheckerContext &C, const CallExpr *CE); |
| 37 | void EvalCopyCommon(CheckerContext &C, const GRState *state, |
| 38 | const Expr *Size, const Expr *Source, const Expr *Dest, |
| 39 | bool Restricted = false); |
| 40 | |
| 41 | void EvalMemcmp(CheckerContext &C, const CallExpr *CE); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 42 | |
| 43 | // Utility methods |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 44 | std::pair<const GRState*, const GRState*> |
| 45 | AssumeZero(CheckerContext &C, const GRState *state, SVal V, QualType Ty); |
| 46 | |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 47 | const GRState *CheckNonNull(CheckerContext &C, const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 48 | const Expr *S, SVal l); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 49 | const GRState *CheckLocation(CheckerContext &C, const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 50 | const Expr *S, SVal l); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 51 | const GRState *CheckBufferAccess(CheckerContext &C, const GRState *state, |
| 52 | const Expr *Size, |
| 53 | const Expr *FirstBuf, |
| 54 | const Expr *SecondBuf = NULL); |
| 55 | const GRState *CheckOverlap(CheckerContext &C, const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 56 | const Expr *Size, const Expr *First, |
| 57 | const Expr *Second); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 58 | void EmitOverlapBug(CheckerContext &C, const GRState *state, |
| 59 | const Stmt *First, const Stmt *Second); |
| 60 | }; |
| 61 | } //end anonymous namespace |
| 62 | |
| 63 | void clang::RegisterCStringChecker(GRExprEngine &Eng) { |
| 64 | Eng.registerCheck(new CStringChecker()); |
| 65 | } |
| 66 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
| 68 | // Individual checks and utility methods. |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | std::pair<const GRState*, const GRState*> |
| 72 | CStringChecker::AssumeZero(CheckerContext &C, const GRState *state, SVal V, |
| 73 | QualType Ty) { |
| 74 | DefinedSVal *Val = dyn_cast<DefinedSVal>(&V); |
| 75 | if (!Val) |
| 76 | return std::pair<const GRState*, const GRState *>(state, state); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 77 | |
| 78 | ValueManager &ValMgr = C.getValueManager(); |
| 79 | SValuator &SV = ValMgr.getSValuator(); |
| 80 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 81 | DefinedOrUnknownSVal Zero = ValMgr.makeZeroVal(Ty); |
| 82 | DefinedOrUnknownSVal ValIsZero = SV.EvalEQ(state, *Val, Zero); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 83 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 84 | return state->Assume(ValIsZero); |
| 85 | } |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 86 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 87 | const GRState *CStringChecker::CheckNonNull(CheckerContext &C, |
| 88 | const GRState *state, |
| 89 | const Expr *S, SVal l) { |
| 90 | // If a previous check has failed, propagate the failure. |
| 91 | if (!state) |
| 92 | return NULL; |
| 93 | |
| 94 | const GRState *stateNull, *stateNonNull; |
| 95 | llvm::tie(stateNull, stateNonNull) = AssumeZero(C, state, l, S->getType()); |
| 96 | |
| 97 | if (stateNull && !stateNonNull) { |
| 98 | ExplodedNode *N = C.GenerateSink(stateNull); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 99 | if (!N) |
| 100 | return NULL; |
| 101 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 102 | if (!BT_Null) |
| 103 | BT_Null = new BuiltinBug("API", |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 104 | "Null pointer argument in call to byte string function"); |
| 105 | |
| 106 | // Generate a report for this bug. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 107 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 108 | EnhancedBugReport *report = new EnhancedBugReport(*BT, |
| 109 | BT->getDescription(), N); |
| 110 | |
| 111 | report->addRange(S->getSourceRange()); |
| 112 | report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, S); |
| 113 | C.EmitReport(report); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | // From here on, assume that the value is non-null. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 118 | assert(stateNonNull); |
| 119 | return stateNonNull; |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 122 | // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor? |
| 123 | const GRState *CStringChecker::CheckLocation(CheckerContext &C, |
| 124 | const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 125 | const Expr *S, SVal l) { |
| 126 | // If a previous check has failed, propagate the failure. |
| 127 | if (!state) |
| 128 | return NULL; |
| 129 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 130 | // Check for out of bound array element access. |
| 131 | const MemRegion *R = l.getAsRegion(); |
| 132 | if (!R) |
| 133 | return state; |
| 134 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 135 | const ElementRegion *ER = dyn_cast<ElementRegion>(R); |
| 136 | if (!ER) |
| 137 | return state; |
| 138 | |
| 139 | assert(ER->getValueType(C.getASTContext()) == C.getASTContext().CharTy && |
| 140 | "CheckLocation should only be called with char* ElementRegions"); |
| 141 | |
| 142 | // Get the size of the array. |
| 143 | const SubRegion *Super = cast<SubRegion>(ER->getSuperRegion()); |
| 144 | ValueManager &ValMgr = C.getValueManager(); |
| 145 | SVal Extent = ValMgr.convertToArrayIndex(Super->getExtent(ValMgr)); |
| 146 | DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent); |
| 147 | |
| 148 | // Get the index of the accessed element. |
| 149 | DefinedOrUnknownSVal &Idx = cast<DefinedOrUnknownSVal>(ER->getIndex()); |
| 150 | |
| 151 | const GRState *StInBound = state->AssumeInBound(Idx, Size, true); |
| 152 | const GRState *StOutBound = state->AssumeInBound(Idx, Size, false); |
| 153 | if (StOutBound && !StInBound) { |
| 154 | ExplodedNode *N = C.GenerateSink(StOutBound); |
| 155 | if (!N) |
| 156 | return NULL; |
| 157 | |
| 158 | if (!BT_Bounds) |
| 159 | BT_Bounds = new BuiltinBug("Out-of-bound array access", |
| 160 | "Byte string function accesses out-of-bound array element " |
| 161 | "(buffer overflow)"); |
| 162 | |
| 163 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 164 | // e.g., by referencing the original declaration or by saying *why* this |
| 165 | // reference is outside the range. |
| 166 | |
| 167 | // Generate a report for this bug. |
| 168 | BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Bounds); |
| 169 | RangedBugReport *report = new RangedBugReport(*BT, BT->getDescription(), N); |
| 170 | |
| 171 | report->addRange(S->getSourceRange()); |
| 172 | C.EmitReport(report); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | // Array bound check succeeded. From this point forward the array bound |
| 177 | // should always succeed. |
| 178 | return StInBound; |
| 179 | } |
| 180 | |
| 181 | const GRState *CStringChecker::CheckBufferAccess(CheckerContext &C, |
| 182 | const GRState *state, |
| 183 | const Expr *Size, |
| 184 | const Expr *FirstBuf, |
| 185 | const Expr *SecondBuf) { |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 186 | // If a previous check has failed, propagate the failure. |
| 187 | if (!state) |
| 188 | return NULL; |
| 189 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 190 | ValueManager &VM = C.getValueManager(); |
| 191 | SValuator &SV = VM.getSValuator(); |
| 192 | ASTContext &Ctx = C.getASTContext(); |
| 193 | |
| 194 | QualType SizeTy = Ctx.getSizeType(); |
| 195 | QualType PtrTy = Ctx.getPointerType(Ctx.CharTy); |
| 196 | |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 197 | // Check that the first buffer is non-null. |
| 198 | SVal BufVal = state->getSVal(FirstBuf); |
| 199 | state = CheckNonNull(C, state, FirstBuf, BufVal); |
| 200 | if (!state) |
| 201 | return NULL; |
| 202 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 203 | // Get the access length and make sure it is known. |
| 204 | SVal LengthVal = state->getSVal(Size); |
| 205 | NonLoc *Length = dyn_cast<NonLoc>(&LengthVal); |
| 206 | if (!Length) |
| 207 | return state; |
| 208 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 209 | // Compute the offset of the last element to be accessed: size-1. |
| 210 | NonLoc One = cast<NonLoc>(VM.makeIntVal(1, SizeTy)); |
| 211 | NonLoc LastOffset = cast<NonLoc>(SV.EvalBinOpNN(state, BinaryOperator::Sub, |
| 212 | *Length, One, SizeTy)); |
| 213 | |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 214 | // Check that the first buffer is sufficently long. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 215 | Loc BufStart = cast<Loc>(SV.EvalCast(BufVal, PtrTy, FirstBuf->getType())); |
| 216 | SVal BufEnd |
| 217 | = SV.EvalBinOpLN(state, BinaryOperator::Add, BufStart, LastOffset, PtrTy); |
| 218 | state = CheckLocation(C, state, FirstBuf, BufEnd); |
| 219 | |
| 220 | // If the buffer isn't large enough, abort. |
| 221 | if (!state) |
| 222 | return NULL; |
| 223 | |
| 224 | // If there's a second buffer, check it as well. |
| 225 | if (SecondBuf) { |
| 226 | BufVal = state->getSVal(SecondBuf); |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 227 | state = CheckNonNull(C, state, SecondBuf, BufVal); |
| 228 | if (!state) |
| 229 | return NULL; |
| 230 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 231 | BufStart = cast<Loc>(SV.EvalCast(BufVal, PtrTy, SecondBuf->getType())); |
| 232 | BufEnd |
| 233 | = SV.EvalBinOpLN(state, BinaryOperator::Add, BufStart, LastOffset, PtrTy); |
| 234 | state = CheckLocation(C, state, SecondBuf, BufEnd); |
| 235 | } |
| 236 | |
| 237 | // Large enough or not, return this state! |
| 238 | return state; |
| 239 | } |
| 240 | |
| 241 | const GRState *CStringChecker::CheckOverlap(CheckerContext &C, |
| 242 | const GRState *state, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 243 | const Expr *Size, |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 244 | const Expr *First, |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 245 | const Expr *Second) { |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 246 | // Do a simple check for overlap: if the two arguments are from the same |
| 247 | // buffer, see if the end of the first is greater than the start of the second |
| 248 | // or vice versa. |
| 249 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 250 | // If a previous check has failed, propagate the failure. |
| 251 | if (!state) |
| 252 | return NULL; |
| 253 | |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 254 | ValueManager &VM = state->getStateManager().getValueManager(); |
| 255 | SValuator &SV = VM.getSValuator(); |
| 256 | ASTContext &Ctx = VM.getContext(); |
| 257 | const GRState *stateTrue, *stateFalse; |
| 258 | |
| 259 | // Get the buffer values and make sure they're known locations. |
| 260 | SVal FirstVal = state->getSVal(First); |
| 261 | SVal SecondVal = state->getSVal(Second); |
| 262 | |
| 263 | Loc *FirstLoc = dyn_cast<Loc>(&FirstVal); |
| 264 | if (!FirstLoc) |
| 265 | return state; |
| 266 | |
| 267 | Loc *SecondLoc = dyn_cast<Loc>(&SecondVal); |
| 268 | if (!SecondLoc) |
| 269 | return state; |
| 270 | |
| 271 | // Are the two values the same? |
| 272 | DefinedOrUnknownSVal EqualTest = SV.EvalEQ(state, *FirstLoc, *SecondLoc); |
| 273 | llvm::tie(stateTrue, stateFalse) = state->Assume(EqualTest); |
| 274 | |
| 275 | if (stateTrue && !stateFalse) { |
| 276 | // If the values are known to be equal, that's automatically an overlap. |
| 277 | EmitOverlapBug(C, stateTrue, First, Second); |
| 278 | return NULL; |
| 279 | } |
| 280 | |
| 281 | // Assume the two expressions are not equal. |
| 282 | assert(stateFalse); |
| 283 | state = stateFalse; |
| 284 | |
| 285 | // Which value comes first? |
| 286 | QualType CmpTy = Ctx.IntTy; |
| 287 | SVal Reverse = SV.EvalBinOpLL(state, BinaryOperator::GT, |
| 288 | *FirstLoc, *SecondLoc, CmpTy); |
| 289 | DefinedOrUnknownSVal *ReverseTest = dyn_cast<DefinedOrUnknownSVal>(&Reverse); |
| 290 | if (!ReverseTest) |
| 291 | return state; |
| 292 | |
| 293 | llvm::tie(stateTrue, stateFalse) = state->Assume(*ReverseTest); |
| 294 | |
| 295 | if (stateTrue) { |
| 296 | if (stateFalse) { |
| 297 | // If we don't know which one comes first, we can't perform this test. |
| 298 | return state; |
| 299 | } else { |
| 300 | // Switch the values so that FirstVal is before SecondVal. |
| 301 | Loc *tmpLoc = FirstLoc; |
| 302 | FirstLoc = SecondLoc; |
| 303 | SecondLoc = tmpLoc; |
| 304 | |
| 305 | // Switch the Exprs as well, so that they still correspond. |
| 306 | const Expr *tmpExpr = First; |
| 307 | First = Second; |
| 308 | Second = tmpExpr; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | // Get the length, and make sure it too is known. |
| 313 | SVal LengthVal = state->getSVal(Size); |
| 314 | NonLoc *Length = dyn_cast<NonLoc>(&LengthVal); |
| 315 | if (!Length) |
| 316 | return state; |
| 317 | |
| 318 | // Convert the first buffer's start address to char*. |
| 319 | // Bail out if the cast fails. |
| 320 | QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy); |
| 321 | SVal FirstStart = SV.EvalCast(*FirstLoc, CharPtrTy, First->getType()); |
| 322 | Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart); |
| 323 | if (!FirstStartLoc) |
| 324 | return state; |
| 325 | |
| 326 | // Compute the end of the first buffer. Bail out if THAT fails. |
| 327 | SVal FirstEnd = SV.EvalBinOpLN(state, BinaryOperator::Add, |
| 328 | *FirstStartLoc, *Length, CharPtrTy); |
| 329 | Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd); |
| 330 | if (!FirstEndLoc) |
| 331 | return state; |
| 332 | |
| 333 | // Is the end of the first buffer past the start of the second buffer? |
| 334 | SVal Overlap = SV.EvalBinOpLL(state, BinaryOperator::GT, |
| 335 | *FirstEndLoc, *SecondLoc, CmpTy); |
| 336 | DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap); |
| 337 | if (!OverlapTest) |
| 338 | return state; |
| 339 | |
| 340 | llvm::tie(stateTrue, stateFalse) = state->Assume(*OverlapTest); |
| 341 | |
| 342 | if (stateTrue && !stateFalse) { |
| 343 | // Overlap! |
| 344 | EmitOverlapBug(C, stateTrue, First, Second); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | // Assume the two expressions don't overlap. |
| 349 | assert(stateFalse); |
| 350 | return stateFalse; |
| 351 | } |
| 352 | |
| 353 | void CStringChecker::EmitOverlapBug(CheckerContext &C, const GRState *state, |
| 354 | const Stmt *First, const Stmt *Second) { |
| 355 | ExplodedNode *N = C.GenerateSink(state); |
| 356 | if (!N) |
| 357 | return; |
| 358 | |
| 359 | if (!BT_Overlap) |
| 360 | BT_Overlap = new BugType("Unix API", "Improper arguments"); |
| 361 | |
| 362 | // Generate a report for this bug. |
| 363 | RangedBugReport *report = |
| 364 | new RangedBugReport(*BT_Overlap, |
| 365 | "Arguments must not be overlapping buffers", N); |
| 366 | report->addRange(First->getSourceRange()); |
| 367 | report->addRange(Second->getSourceRange()); |
| 368 | |
| 369 | C.EmitReport(report); |
| 370 | } |
| 371 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 372 | //===----------------------------------------------------------------------===// |
| 373 | // Evaluation of individual function calls. |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | |
| 376 | void CStringChecker::EvalCopyCommon(CheckerContext &C, const GRState *state, |
| 377 | const Expr *Size, const Expr *Dest, |
| 378 | const Expr *Source, bool Restricted) { |
| 379 | // See if the size argument is zero. |
| 380 | SVal SizeVal = state->getSVal(Size); |
| 381 | QualType SizeTy = Size->getType(); |
| 382 | |
| 383 | const GRState *StZeroSize, *StNonZeroSize; |
| 384 | llvm::tie(StZeroSize, StNonZeroSize) = AssumeZero(C, state, SizeVal, SizeTy); |
| 385 | |
| 386 | // If the size is zero, there won't be any actual memory access. |
| 387 | if (StZeroSize) |
| 388 | C.addTransition(StZeroSize); |
| 389 | |
| 390 | // If the size can be nonzero, we have to check the other arguments. |
| 391 | if (StNonZeroSize) { |
| 392 | state = StNonZeroSize; |
| 393 | state = CheckBufferAccess(C, state, Size, Dest, Source); |
| 394 | if (Restricted) |
| 395 | state = CheckOverlap(C, state, Size, Dest, Source); |
| 396 | if (state) |
| 397 | C.addTransition(state); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | |
| 402 | void CStringChecker::EvalMemcpy(CheckerContext &C, const CallExpr *CE) { |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 403 | // void *memcpy(void *restrict dst, const void *restrict src, size_t n); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 404 | // The return value is the address of the destination buffer. |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 405 | const Expr *Dest = CE->getArg(0); |
| 406 | const GRState *state = C.getState(); |
| 407 | state = state->BindExpr(CE, state->getSVal(Dest)); |
| 408 | EvalCopyCommon(C, state, CE->getArg(2), Dest, CE->getArg(1), true); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 411 | void CStringChecker::EvalMemmove(CheckerContext &C, const CallExpr *CE) { |
| 412 | // void *memmove(void *dst, const void *src, size_t n); |
| 413 | // The return value is the address of the destination buffer. |
| 414 | const Expr *Dest = CE->getArg(0); |
| 415 | const GRState *state = C.getState(); |
| 416 | state = state->BindExpr(CE, state->getSVal(Dest)); |
| 417 | EvalCopyCommon(C, state, CE->getArg(2), Dest, CE->getArg(1)); |
| 418 | } |
| 419 | |
| 420 | void CStringChecker::EvalBcopy(CheckerContext &C, const CallExpr *CE) { |
| 421 | // void bcopy(const void *src, void *dst, size_t n); |
| 422 | EvalCopyCommon(C, C.getState(), CE->getArg(2), CE->getArg(1), CE->getArg(0)); |
| 423 | } |
| 424 | |
| 425 | void CStringChecker::EvalMemcmp(CheckerContext &C, const CallExpr *CE) { |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 426 | // int memcmp(const void *s1, const void *s2, size_t n); |
| 427 | const Expr *Left = CE->getArg(0); |
| 428 | const Expr *Right = CE->getArg(1); |
| 429 | const Expr *Size = CE->getArg(2); |
| 430 | |
| 431 | const GRState *state = C.getState(); |
| 432 | ValueManager &ValMgr = C.getValueManager(); |
| 433 | SValuator &SV = ValMgr.getSValuator(); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 434 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 435 | // See if the size argument is zero. |
| 436 | SVal SizeVal = state->getSVal(Size); |
| 437 | QualType SizeTy = Size->getType(); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 438 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 439 | const GRState *StZeroSize, *StNonZeroSize; |
| 440 | llvm::tie(StZeroSize, StNonZeroSize) = AssumeZero(C, state, SizeVal, SizeTy); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 441 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 442 | // If the size can be zero, the result will be 0 in that case, and we don't |
| 443 | // have to check either of the buffers. |
| 444 | if (StZeroSize) { |
| 445 | state = StZeroSize; |
| 446 | state = state->BindExpr(CE, ValMgr.makeZeroVal(CE->getType())); |
| 447 | C.addTransition(state); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 450 | // If the size can be nonzero, we have to check the other arguments. |
| 451 | if (StNonZeroSize) { |
| 452 | state = StNonZeroSize; |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 453 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 454 | // If we know the two buffers are the same, we know the result is 0. |
| 455 | // First, get the two buffers' addresses. Another checker will have already |
| 456 | // made sure they're not undefined. |
| 457 | DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(state->getSVal(Left)); |
| 458 | DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(state->getSVal(Right)); |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 459 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 460 | // See if they are the same. |
| 461 | DefinedOrUnknownSVal SameBuf = SV.EvalEQ(state, LV, RV); |
| 462 | const GRState *StSameBuf, *StNotSameBuf; |
| 463 | llvm::tie(StSameBuf, StNotSameBuf) = state->Assume(SameBuf); |
| 464 | |
| 465 | // If the two arguments might be the same buffer, we know the result is zero, |
| 466 | // and we only need to check one size. |
| 467 | if (StSameBuf) { |
| 468 | state = StSameBuf; |
| 469 | state = CheckBufferAccess(C, state, Size, Left); |
| 470 | if (state) { |
| 471 | state = StSameBuf->BindExpr(CE, ValMgr.makeZeroVal(CE->getType())); |
| 472 | C.addTransition(state); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // If the two arguments might be different buffers, we have to check the |
| 477 | // size of both of them. |
| 478 | if (StNotSameBuf) { |
| 479 | state = StNotSameBuf; |
| 480 | state = CheckBufferAccess(C, state, Size, Left, Right); |
| 481 | if (state) { |
| 482 | // The return value is the comparison result, which we don't know. |
| 483 | unsigned Count = C.getNodeBuilder().getCurrentBlockCount(); |
| 484 | SVal CmpV = ValMgr.getConjuredSymbolVal(NULL, CE, CE->getType(), Count); |
| 485 | state = state->BindExpr(CE, CmpV); |
| 486 | C.addTransition(state); |
| 487 | } |
| 488 | } |
| 489 | } |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 492 | //===----------------------------------------------------------------------===// |
| 493 | // The driver method. |
| 494 | //===----------------------------------------------------------------------===// |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 495 | |
| 496 | bool CStringChecker::EvalCallExpr(CheckerContext &C, const CallExpr *CE) { |
| 497 | // Get the callee. All the functions we care about are C functions |
| 498 | // with simple identifiers. |
| 499 | const GRState *state = C.getState(); |
| 500 | const Expr *Callee = CE->getCallee(); |
| 501 | const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl(); |
| 502 | |
| 503 | if (!FD) |
| 504 | return false; |
| 505 | |
| 506 | // Get the name of the callee. If it's a builtin, strip off the prefix. |
| 507 | llvm::StringRef Name = FD->getName(); |
| 508 | if (Name.startswith("__builtin_")) |
| 509 | Name = Name.substr(10); |
| 510 | |
| 511 | FnCheck EvalFunction = llvm::StringSwitch<FnCheck>(Name) |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 512 | .Cases("memcpy", "__memcpy_chk", &CStringChecker::EvalMemcpy) |
Jordy Rose | bc56d1f | 2010-07-07 08:15:01 +0000 | [diff] [blame] | 513 | .Cases("memcmp", "bcmp", &CStringChecker::EvalMemcmp) |
Jordy Rose | a6b808c | 2010-07-07 07:48:06 +0000 | [diff] [blame] | 514 | .Cases("memmove", "__memmove_chk", &CStringChecker::EvalMemmove) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 515 | .Case("bcopy", &CStringChecker::EvalBcopy) |
| 516 | .Default(NULL); |
| 517 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 518 | // If the callee isn't a string function, let another checker handle it. |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 519 | if (!EvalFunction) |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 520 | return false; |
| 521 | |
Jordy Rose | d325ffb | 2010-07-08 23:57:29 +0000 | [diff] [blame] | 522 | // Check and evaluate the call. |
| 523 | (this->*EvalFunction)(C, CE); |
Jordy Rose | ccbf7ee | 2010-07-06 23:11:01 +0000 | [diff] [blame] | 524 | return true; |
| 525 | } |