CStringChecker.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  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. #include "ClangSACheckers.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  18. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
  20. #include "llvm/ADT/StringSwitch.h"
  21. using namespace clang;
  22. using namespace ento;
  23. namespace {
  24. class CStringChecker : public Checker< eval::Call,
  25. check::PreStmt<DeclStmt>,
  26. check::LiveSymbols,
  27. check::DeadSymbols,
  28. check::RegionChanges
  29. > {
  30. mutable llvm::OwningPtr<BugType> BT_Null, BT_Bounds, BT_BoundsWrite,
  31. BT_Overlap, BT_NotCString;
  32. public:
  33. static void *getTag() { static int tag; return &tag; }
  34. bool evalCall(const CallExpr *CE, CheckerContext &C) const;
  35. void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
  36. void checkLiveSymbols(const GRState *state, SymbolReaper &SR) const;
  37. void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
  38. bool wantsRegionChangeUpdate(const GRState *state) const;
  39. const GRState *checkRegionChanges(const GRState *state,
  40. const MemRegion * const *Begin,
  41. const MemRegion * const *End) const;
  42. typedef void (CStringChecker::*FnCheck)(CheckerContext &,
  43. const CallExpr *) const;
  44. void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
  45. void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
  46. void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
  47. void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
  48. void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
  49. const GRState *state,
  50. const Expr *Size, const Expr *Source, const Expr *Dest,
  51. bool Restricted = false,
  52. bool IsMempcpy = false) const;
  53. void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
  54. void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
  55. void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
  56. void evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
  57. bool IsStrnlen = false) const;
  58. void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
  59. void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
  60. void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
  61. void evalStrcpyCommon(CheckerContext &C, const CallExpr *CE, bool returnEnd,
  62. bool isBounded, bool isAppending) const;
  63. void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
  64. void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
  65. void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
  66. void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
  67. void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
  68. void evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
  69. bool isBounded = false, bool ignoreCase = false) const;
  70. // Utility methods
  71. std::pair<const GRState*, const GRState*>
  72. static assumeZero(CheckerContext &C,
  73. const GRState *state, SVal V, QualType Ty);
  74. static const GRState *setCStringLength(const GRState *state,
  75. const MemRegion *MR, SVal strLength);
  76. static SVal getCStringLengthForRegion(CheckerContext &C,
  77. const GRState *&state,
  78. const Expr *Ex, const MemRegion *MR);
  79. SVal getCStringLength(CheckerContext &C, const GRState *&state,
  80. const Expr *Ex, SVal Buf) const;
  81. const StringLiteral *getCStringLiteral(CheckerContext &C,
  82. const GRState *&state,
  83. const Expr *expr,
  84. SVal val) const;
  85. static const GRState *InvalidateBuffer(CheckerContext &C,
  86. const GRState *state,
  87. const Expr *Ex, SVal V);
  88. static bool SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx,
  89. const MemRegion *MR);
  90. // Re-usable checks
  91. const GRState *checkNonNull(CheckerContext &C, const GRState *state,
  92. const Expr *S, SVal l) const;
  93. const GRState *CheckLocation(CheckerContext &C, const GRState *state,
  94. const Expr *S, SVal l,
  95. bool IsDestination = false) const;
  96. const GRState *CheckBufferAccess(CheckerContext &C, const GRState *state,
  97. const Expr *Size,
  98. const Expr *FirstBuf,
  99. const Expr *SecondBuf = NULL,
  100. bool FirstIsDestination = false) const;
  101. const GRState *CheckOverlap(CheckerContext &C, const GRState *state,
  102. const Expr *Size, const Expr *First,
  103. const Expr *Second) const;
  104. void emitOverlapBug(CheckerContext &C, const GRState *state,
  105. const Stmt *First, const Stmt *Second) const;
  106. };
  107. class CStringLength {
  108. public:
  109. typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap;
  110. };
  111. } //end anonymous namespace
  112. namespace clang {
  113. namespace ento {
  114. template <>
  115. struct GRStateTrait<CStringLength>
  116. : public GRStatePartialTrait<CStringLength::EntryMap> {
  117. static void *GDMIndex() { return CStringChecker::getTag(); }
  118. };
  119. }
  120. }
  121. //===----------------------------------------------------------------------===//
  122. // Individual checks and utility methods.
  123. //===----------------------------------------------------------------------===//
  124. std::pair<const GRState*, const GRState*>
  125. CStringChecker::assumeZero(CheckerContext &C, const GRState *state, SVal V,
  126. QualType Ty) {
  127. DefinedSVal *val = dyn_cast<DefinedSVal>(&V);
  128. if (!val)
  129. return std::pair<const GRState*, const GRState *>(state, state);
  130. SValBuilder &svalBuilder = C.getSValBuilder();
  131. DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
  132. return state->assume(svalBuilder.evalEQ(state, *val, zero));
  133. }
  134. const GRState *CStringChecker::checkNonNull(CheckerContext &C,
  135. const GRState *state,
  136. const Expr *S, SVal l) const {
  137. // If a previous check has failed, propagate the failure.
  138. if (!state)
  139. return NULL;
  140. const GRState *stateNull, *stateNonNull;
  141. llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType());
  142. if (stateNull && !stateNonNull) {
  143. ExplodedNode *N = C.generateSink(stateNull);
  144. if (!N)
  145. return NULL;
  146. if (!BT_Null)
  147. BT_Null.reset(new BuiltinBug("API",
  148. "Null pointer argument in call to byte string function"));
  149. // Generate a report for this bug.
  150. BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get());
  151. EnhancedBugReport *report = new EnhancedBugReport(*BT,
  152. BT->getDescription(), N);
  153. report->addRange(S->getSourceRange());
  154. report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, S);
  155. C.EmitReport(report);
  156. return NULL;
  157. }
  158. // From here on, assume that the value is non-null.
  159. assert(stateNonNull);
  160. return stateNonNull;
  161. }
  162. // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
  163. const GRState *CStringChecker::CheckLocation(CheckerContext &C,
  164. const GRState *state,
  165. const Expr *S, SVal l,
  166. bool IsDestination) const {
  167. // If a previous check has failed, propagate the failure.
  168. if (!state)
  169. return NULL;
  170. // Check for out of bound array element access.
  171. const MemRegion *R = l.getAsRegion();
  172. if (!R)
  173. return state;
  174. const ElementRegion *ER = dyn_cast<ElementRegion>(R);
  175. if (!ER)
  176. return state;
  177. assert(ER->getValueType() == C.getASTContext().CharTy &&
  178. "CheckLocation should only be called with char* ElementRegions");
  179. // Get the size of the array.
  180. const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
  181. SValBuilder &svalBuilder = C.getSValBuilder();
  182. SVal Extent = svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
  183. DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent);
  184. // Get the index of the accessed element.
  185. DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
  186. const GRState *StInBound = state->assumeInBound(Idx, Size, true);
  187. const GRState *StOutBound = state->assumeInBound(Idx, Size, false);
  188. if (StOutBound && !StInBound) {
  189. ExplodedNode *N = C.generateSink(StOutBound);
  190. if (!N)
  191. return NULL;
  192. BuiltinBug *BT;
  193. if (IsDestination) {
  194. if (!BT_BoundsWrite) {
  195. BT_BoundsWrite.reset(new BuiltinBug("Out-of-bound array access",
  196. "Byte string function overflows destination buffer"));
  197. }
  198. BT = static_cast<BuiltinBug*>(BT_BoundsWrite.get());
  199. } else {
  200. if (!BT_Bounds) {
  201. BT_Bounds.reset(new BuiltinBug("Out-of-bound array access",
  202. "Byte string function accesses out-of-bound array element"));
  203. }
  204. BT = static_cast<BuiltinBug*>(BT_Bounds.get());
  205. }
  206. // FIXME: It would be nice to eventually make this diagnostic more clear,
  207. // e.g., by referencing the original declaration or by saying *why* this
  208. // reference is outside the range.
  209. // Generate a report for this bug.
  210. RangedBugReport *report = new RangedBugReport(*BT, BT->getDescription(), N);
  211. report->addRange(S->getSourceRange());
  212. C.EmitReport(report);
  213. return NULL;
  214. }
  215. // Array bound check succeeded. From this point forward the array bound
  216. // should always succeed.
  217. return StInBound;
  218. }
  219. const GRState *CStringChecker::CheckBufferAccess(CheckerContext &C,
  220. const GRState *state,
  221. const Expr *Size,
  222. const Expr *FirstBuf,
  223. const Expr *SecondBuf,
  224. bool FirstIsDestination) const {
  225. // If a previous check has failed, propagate the failure.
  226. if (!state)
  227. return NULL;
  228. SValBuilder &svalBuilder = C.getSValBuilder();
  229. ASTContext &Ctx = C.getASTContext();
  230. QualType sizeTy = Size->getType();
  231. QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
  232. // Check that the first buffer is non-null.
  233. SVal BufVal = state->getSVal(FirstBuf);
  234. state = checkNonNull(C, state, FirstBuf, BufVal);
  235. if (!state)
  236. return NULL;
  237. // Get the access length and make sure it is known.
  238. SVal LengthVal = state->getSVal(Size);
  239. NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
  240. if (!Length)
  241. return state;
  242. // Compute the offset of the last element to be accessed: size-1.
  243. NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
  244. NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub,
  245. *Length, One, sizeTy));
  246. // Check that the first buffer is sufficiently long.
  247. SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
  248. if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
  249. SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
  250. LastOffset, PtrTy);
  251. state = CheckLocation(C, state, FirstBuf, BufEnd, FirstIsDestination);
  252. // If the buffer isn't large enough, abort.
  253. if (!state)
  254. return NULL;
  255. }
  256. // If there's a second buffer, check it as well.
  257. if (SecondBuf) {
  258. BufVal = state->getSVal(SecondBuf);
  259. state = checkNonNull(C, state, SecondBuf, BufVal);
  260. if (!state)
  261. return NULL;
  262. BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType());
  263. if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
  264. SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
  265. LastOffset, PtrTy);
  266. state = CheckLocation(C, state, SecondBuf, BufEnd);
  267. }
  268. }
  269. // Large enough or not, return this state!
  270. return state;
  271. }
  272. const GRState *CStringChecker::CheckOverlap(CheckerContext &C,
  273. const GRState *state,
  274. const Expr *Size,
  275. const Expr *First,
  276. const Expr *Second) const {
  277. // Do a simple check for overlap: if the two arguments are from the same
  278. // buffer, see if the end of the first is greater than the start of the second
  279. // or vice versa.
  280. // If a previous check has failed, propagate the failure.
  281. if (!state)
  282. return NULL;
  283. const GRState *stateTrue, *stateFalse;
  284. // Get the buffer values and make sure they're known locations.
  285. SVal firstVal = state->getSVal(First);
  286. SVal secondVal = state->getSVal(Second);
  287. Loc *firstLoc = dyn_cast<Loc>(&firstVal);
  288. if (!firstLoc)
  289. return state;
  290. Loc *secondLoc = dyn_cast<Loc>(&secondVal);
  291. if (!secondLoc)
  292. return state;
  293. // Are the two values the same?
  294. SValBuilder &svalBuilder = C.getSValBuilder();
  295. llvm::tie(stateTrue, stateFalse) =
  296. state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
  297. if (stateTrue && !stateFalse) {
  298. // If the values are known to be equal, that's automatically an overlap.
  299. emitOverlapBug(C, stateTrue, First, Second);
  300. return NULL;
  301. }
  302. // assume the two expressions are not equal.
  303. assert(stateFalse);
  304. state = stateFalse;
  305. // Which value comes first?
  306. ASTContext &Ctx = svalBuilder.getContext();
  307. QualType cmpTy = Ctx.IntTy;
  308. SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT,
  309. *firstLoc, *secondLoc, cmpTy);
  310. DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse);
  311. if (!reverseTest)
  312. return state;
  313. llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
  314. if (stateTrue) {
  315. if (stateFalse) {
  316. // If we don't know which one comes first, we can't perform this test.
  317. return state;
  318. } else {
  319. // Switch the values so that firstVal is before secondVal.
  320. Loc *tmpLoc = firstLoc;
  321. firstLoc = secondLoc;
  322. secondLoc = tmpLoc;
  323. // Switch the Exprs as well, so that they still correspond.
  324. const Expr *tmpExpr = First;
  325. First = Second;
  326. Second = tmpExpr;
  327. }
  328. }
  329. // Get the length, and make sure it too is known.
  330. SVal LengthVal = state->getSVal(Size);
  331. NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
  332. if (!Length)
  333. return state;
  334. // Convert the first buffer's start address to char*.
  335. // Bail out if the cast fails.
  336. QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
  337. SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy, First->getType());
  338. Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart);
  339. if (!FirstStartLoc)
  340. return state;
  341. // Compute the end of the first buffer. Bail out if THAT fails.
  342. SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add,
  343. *FirstStartLoc, *Length, CharPtrTy);
  344. Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd);
  345. if (!FirstEndLoc)
  346. return state;
  347. // Is the end of the first buffer past the start of the second buffer?
  348. SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT,
  349. *FirstEndLoc, *secondLoc, cmpTy);
  350. DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap);
  351. if (!OverlapTest)
  352. return state;
  353. llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
  354. if (stateTrue && !stateFalse) {
  355. // Overlap!
  356. emitOverlapBug(C, stateTrue, First, Second);
  357. return NULL;
  358. }
  359. // assume the two expressions don't overlap.
  360. assert(stateFalse);
  361. return stateFalse;
  362. }
  363. void CStringChecker::emitOverlapBug(CheckerContext &C, const GRState *state,
  364. const Stmt *First, const Stmt *Second) const {
  365. ExplodedNode *N = C.generateSink(state);
  366. if (!N)
  367. return;
  368. if (!BT_Overlap)
  369. BT_Overlap.reset(new BugType("Unix API", "Improper arguments"));
  370. // Generate a report for this bug.
  371. RangedBugReport *report =
  372. new RangedBugReport(*BT_Overlap,
  373. "Arguments must not be overlapping buffers", N);
  374. report->addRange(First->getSourceRange());
  375. report->addRange(Second->getSourceRange());
  376. C.EmitReport(report);
  377. }
  378. const GRState *CStringChecker::setCStringLength(const GRState *state,
  379. const MemRegion *MR,
  380. SVal strLength) {
  381. assert(!strLength.isUndef() && "Attempt to set an undefined string length");
  382. if (strLength.isUnknown())
  383. return state;
  384. MR = MR->StripCasts();
  385. switch (MR->getKind()) {
  386. case MemRegion::StringRegionKind:
  387. // FIXME: This can happen if we strcpy() into a string region. This is
  388. // undefined [C99 6.4.5p6], but we should still warn about it.
  389. return state;
  390. case MemRegion::SymbolicRegionKind:
  391. case MemRegion::AllocaRegionKind:
  392. case MemRegion::VarRegionKind:
  393. case MemRegion::FieldRegionKind:
  394. case MemRegion::ObjCIvarRegionKind:
  395. return state->set<CStringLength>(MR, strLength);
  396. case MemRegion::ElementRegionKind:
  397. // FIXME: Handle element regions by upper-bounding the parent region's
  398. // string length.
  399. return state;
  400. default:
  401. // Other regions (mostly non-data) can't have a reliable C string length.
  402. // For now, just ignore the change.
  403. // FIXME: These are rare but not impossible. We should output some kind of
  404. // warning for things like strcpy((char[]){'a', 0}, "b");
  405. return state;
  406. }
  407. }
  408. SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
  409. const GRState *&state,
  410. const Expr *Ex,
  411. const MemRegion *MR) {
  412. // If there's a recorded length, go ahead and return it.
  413. const SVal *Recorded = state->get<CStringLength>(MR);
  414. if (Recorded)
  415. return *Recorded;
  416. // Otherwise, get a new symbol and update the state.
  417. unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
  418. SValBuilder &svalBuilder = C.getSValBuilder();
  419. QualType sizeTy = svalBuilder.getContext().getSizeType();
  420. SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
  421. MR, Ex, sizeTy, Count);
  422. state = state->set<CStringLength>(MR, strLength);
  423. return strLength;
  424. }
  425. SVal CStringChecker::getCStringLength(CheckerContext &C, const GRState *&state,
  426. const Expr *Ex, SVal Buf) const {
  427. const MemRegion *MR = Buf.getAsRegion();
  428. if (!MR) {
  429. // If we can't get a region, see if it's something we /know/ isn't a
  430. // C string. In the context of locations, the only time we can issue such
  431. // a warning is for labels.
  432. if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) {
  433. if (ExplodedNode *N = C.generateNode(state)) {
  434. if (!BT_NotCString)
  435. BT_NotCString.reset(new BuiltinBug("API",
  436. "Argument is not a null-terminated string."));
  437. llvm::SmallString<120> buf;
  438. llvm::raw_svector_ostream os(buf);
  439. os << "Argument to byte string function is the address of the label '"
  440. << Label->getLabel()->getName()
  441. << "', which is not a null-terminated string";
  442. // Generate a report for this bug.
  443. EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString,
  444. os.str(), N);
  445. report->addRange(Ex->getSourceRange());
  446. C.EmitReport(report);
  447. }
  448. return UndefinedVal();
  449. }
  450. // If it's not a region and not a label, give up.
  451. return UnknownVal();
  452. }
  453. // If we have a region, strip casts from it and see if we can figure out
  454. // its length. For anything we can't figure out, just return UnknownVal.
  455. MR = MR->StripCasts();
  456. switch (MR->getKind()) {
  457. case MemRegion::StringRegionKind: {
  458. // Modifying the contents of string regions is undefined [C99 6.4.5p6],
  459. // so we can assume that the byte length is the correct C string length.
  460. SValBuilder &svalBuilder = C.getSValBuilder();
  461. QualType sizeTy = svalBuilder.getContext().getSizeType();
  462. const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
  463. return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
  464. }
  465. case MemRegion::SymbolicRegionKind:
  466. case MemRegion::AllocaRegionKind:
  467. case MemRegion::VarRegionKind:
  468. case MemRegion::FieldRegionKind:
  469. case MemRegion::ObjCIvarRegionKind:
  470. return getCStringLengthForRegion(C, state, Ex, MR);
  471. case MemRegion::CompoundLiteralRegionKind:
  472. // FIXME: Can we track this? Is it necessary?
  473. return UnknownVal();
  474. case MemRegion::ElementRegionKind:
  475. // FIXME: How can we handle this? It's not good enough to subtract the
  476. // offset from the base string length; consider "123\x00567" and &a[5].
  477. return UnknownVal();
  478. default:
  479. // Other regions (mostly non-data) can't have a reliable C string length.
  480. // In this case, an error is emitted and UndefinedVal is returned.
  481. // The caller should always be prepared to handle this case.
  482. if (ExplodedNode *N = C.generateNode(state)) {
  483. if (!BT_NotCString)
  484. BT_NotCString.reset(new BuiltinBug("API",
  485. "Argument is not a null-terminated string."));
  486. llvm::SmallString<120> buf;
  487. llvm::raw_svector_ostream os(buf);
  488. os << "Argument to byte string function is ";
  489. if (SummarizeRegion(os, C.getASTContext(), MR))
  490. os << ", which is not a null-terminated string";
  491. else
  492. os << "not a null-terminated string";
  493. // Generate a report for this bug.
  494. EnhancedBugReport *report = new EnhancedBugReport(*BT_NotCString,
  495. os.str(), N);
  496. report->addRange(Ex->getSourceRange());
  497. C.EmitReport(report);
  498. }
  499. return UndefinedVal();
  500. }
  501. }
  502. const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
  503. const GRState *&state, const Expr *expr, SVal val) const {
  504. // Get the memory region pointed to by the val.
  505. const MemRegion *bufRegion = val.getAsRegion();
  506. if (!bufRegion)
  507. return NULL;
  508. // Strip casts off the memory region.
  509. bufRegion = bufRegion->StripCasts();
  510. // Cast the memory region to a string region.
  511. const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
  512. if (!strRegion)
  513. return NULL;
  514. // Return the actual string in the string region.
  515. return strRegion->getStringLiteral();
  516. }
  517. const GRState *CStringChecker::InvalidateBuffer(CheckerContext &C,
  518. const GRState *state,
  519. const Expr *E, SVal V) {
  520. Loc *L = dyn_cast<Loc>(&V);
  521. if (!L)
  522. return state;
  523. // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
  524. // some assumptions about the value that CFRefCount can't. Even so, it should
  525. // probably be refactored.
  526. if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) {
  527. const MemRegion *R = MR->getRegion()->StripCasts();
  528. // Are we dealing with an ElementRegion? If so, we should be invalidating
  529. // the super-region.
  530. if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
  531. R = ER->getSuperRegion();
  532. // FIXME: What about layers of ElementRegions?
  533. }
  534. // Invalidate this region.
  535. unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
  536. return state->invalidateRegion(R, E, Count, NULL);
  537. }
  538. // If we have a non-region value by chance, just remove the binding.
  539. // FIXME: is this necessary or correct? This handles the non-Region
  540. // cases. Is it ever valid to store to these?
  541. return state->unbindLoc(*L);
  542. }
  543. bool CStringChecker::SummarizeRegion(llvm::raw_ostream& os, ASTContext& Ctx,
  544. const MemRegion *MR) {
  545. const TypedRegion *TR = dyn_cast<TypedRegion>(MR);
  546. if (!TR)
  547. return false;
  548. switch (TR->getKind()) {
  549. case MemRegion::FunctionTextRegionKind: {
  550. const FunctionDecl *FD = cast<FunctionTextRegion>(TR)->getDecl();
  551. if (FD)
  552. os << "the address of the function '" << FD << "'";
  553. else
  554. os << "the address of a function";
  555. return true;
  556. }
  557. case MemRegion::BlockTextRegionKind:
  558. os << "block text";
  559. return true;
  560. case MemRegion::BlockDataRegionKind:
  561. os << "a block";
  562. return true;
  563. case MemRegion::CXXThisRegionKind:
  564. case MemRegion::CXXTempObjectRegionKind:
  565. os << "a C++ temp object of type " << TR->getValueType().getAsString();
  566. return true;
  567. case MemRegion::VarRegionKind:
  568. os << "a variable of type" << TR->getValueType().getAsString();
  569. return true;
  570. case MemRegion::FieldRegionKind:
  571. os << "a field of type " << TR->getValueType().getAsString();
  572. return true;
  573. case MemRegion::ObjCIvarRegionKind:
  574. os << "an instance variable of type " << TR->getValueType().getAsString();
  575. return true;
  576. default:
  577. return false;
  578. }
  579. }
  580. //===----------------------------------------------------------------------===//
  581. // evaluation of individual function calls.
  582. //===----------------------------------------------------------------------===//
  583. void CStringChecker::evalCopyCommon(CheckerContext &C,
  584. const CallExpr *CE,
  585. const GRState *state,
  586. const Expr *Size, const Expr *Dest,
  587. const Expr *Source, bool Restricted,
  588. bool IsMempcpy) const {
  589. // See if the size argument is zero.
  590. SVal sizeVal = state->getSVal(Size);
  591. QualType sizeTy = Size->getType();
  592. const GRState *stateZeroSize, *stateNonZeroSize;
  593. llvm::tie(stateZeroSize, stateNonZeroSize) = assumeZero(C, state, sizeVal, sizeTy);
  594. // Get the value of the Dest.
  595. SVal destVal = state->getSVal(Dest);
  596. // If the size is zero, there won't be any actual memory access, so
  597. // just bind the return value to the destination buffer and return.
  598. if (stateZeroSize) {
  599. C.addTransition(stateZeroSize);
  600. if (IsMempcpy)
  601. state->BindExpr(CE, destVal);
  602. else
  603. state->BindExpr(CE, sizeVal);
  604. return;
  605. }
  606. // If the size can be nonzero, we have to check the other arguments.
  607. if (stateNonZeroSize) {
  608. // Ensure the destination is not null. If it is NULL there will be a
  609. // NULL pointer dereference.
  610. state = checkNonNull(C, state, Dest, destVal);
  611. if (!state)
  612. return;
  613. // Get the value of the Src.
  614. SVal srcVal = state->getSVal(Source);
  615. // Ensure the source is not null. If it is NULL there will be a
  616. // NULL pointer dereference.
  617. state = checkNonNull(C, state, Source, srcVal);
  618. if (!state)
  619. return;
  620. // Ensure the buffers do not overlap.
  621. state = stateNonZeroSize;
  622. state = CheckBufferAccess(C, state, Size, Dest, Source,
  623. /* FirstIsDst = */ true);
  624. if (Restricted)
  625. state = CheckOverlap(C, state, Size, Dest, Source);
  626. if (state) {
  627. // If this is mempcpy, get the byte after the last byte copied and
  628. // bind the expr.
  629. if (IsMempcpy) {
  630. loc::MemRegionVal *destRegVal = dyn_cast<loc::MemRegionVal>(&destVal);
  631. // Get the length to copy.
  632. SVal lenVal = state->getSVal(Size);
  633. NonLoc *lenValNonLoc = dyn_cast<NonLoc>(&lenVal);
  634. // Get the byte after the last byte copied.
  635. SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add,
  636. *destRegVal,
  637. *lenValNonLoc,
  638. Dest->getType());
  639. // The byte after the last byte copied is the return value.
  640. state = state->BindExpr(CE, lastElement);
  641. }
  642. // Invalidate the destination.
  643. // FIXME: Even if we can't perfectly model the copy, we should see if we
  644. // can use LazyCompoundVals to copy the source values into the destination.
  645. // This would probably remove any existing bindings past the end of the
  646. // copied region, but that's still an improvement over blank invalidation.
  647. state = InvalidateBuffer(C, state, Dest, state->getSVal(Dest));
  648. C.addTransition(state);
  649. }
  650. }
  651. }
  652. void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
  653. // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
  654. // The return value is the address of the destination buffer.
  655. const Expr *Dest = CE->getArg(0);
  656. const GRState *state = C.getState();
  657. state = state->BindExpr(CE, state->getSVal(Dest));
  658. evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true);
  659. }
  660. void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
  661. // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
  662. // The return value is a pointer to the byte following the last written byte.
  663. const Expr *Dest = CE->getArg(0);
  664. const GRState *state = C.getState();
  665. evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true);
  666. }
  667. void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
  668. // void *memmove(void *dst, const void *src, size_t n);
  669. // The return value is the address of the destination buffer.
  670. const Expr *Dest = CE->getArg(0);
  671. const GRState *state = C.getState();
  672. state = state->BindExpr(CE, state->getSVal(Dest));
  673. evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1));
  674. }
  675. void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
  676. // void bcopy(const void *src, void *dst, size_t n);
  677. evalCopyCommon(C, CE, C.getState(),
  678. CE->getArg(2), CE->getArg(1), CE->getArg(0));
  679. }
  680. void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
  681. // int memcmp(const void *s1, const void *s2, size_t n);
  682. const Expr *Left = CE->getArg(0);
  683. const Expr *Right = CE->getArg(1);
  684. const Expr *Size = CE->getArg(2);
  685. const GRState *state = C.getState();
  686. SValBuilder &svalBuilder = C.getSValBuilder();
  687. // See if the size argument is zero.
  688. SVal sizeVal = state->getSVal(Size);
  689. QualType sizeTy = Size->getType();
  690. const GRState *stateZeroSize, *stateNonZeroSize;
  691. llvm::tie(stateZeroSize, stateNonZeroSize) =
  692. assumeZero(C, state, sizeVal, sizeTy);
  693. // If the size can be zero, the result will be 0 in that case, and we don't
  694. // have to check either of the buffers.
  695. if (stateZeroSize) {
  696. state = stateZeroSize;
  697. state = state->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType()));
  698. C.addTransition(state);
  699. }
  700. // If the size can be nonzero, we have to check the other arguments.
  701. if (stateNonZeroSize) {
  702. state = stateNonZeroSize;
  703. // If we know the two buffers are the same, we know the result is 0.
  704. // First, get the two buffers' addresses. Another checker will have already
  705. // made sure they're not undefined.
  706. DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(state->getSVal(Left));
  707. DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(state->getSVal(Right));
  708. // See if they are the same.
  709. DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
  710. const GRState *StSameBuf, *StNotSameBuf;
  711. llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
  712. // If the two arguments might be the same buffer, we know the result is zero,
  713. // and we only need to check one size.
  714. if (StSameBuf) {
  715. state = StSameBuf;
  716. state = CheckBufferAccess(C, state, Size, Left);
  717. if (state) {
  718. state = StSameBuf->BindExpr(CE, svalBuilder.makeZeroVal(CE->getType()));
  719. C.addTransition(state);
  720. }
  721. }
  722. // If the two arguments might be different buffers, we have to check the
  723. // size of both of them.
  724. if (StNotSameBuf) {
  725. state = StNotSameBuf;
  726. state = CheckBufferAccess(C, state, Size, Left, Right);
  727. if (state) {
  728. // The return value is the comparison result, which we don't know.
  729. unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
  730. SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
  731. state = state->BindExpr(CE, CmpV);
  732. C.addTransition(state);
  733. }
  734. }
  735. }
  736. }
  737. void CStringChecker::evalstrLength(CheckerContext &C,
  738. const CallExpr *CE) const {
  739. // size_t strlen(const char *s);
  740. evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
  741. }
  742. void CStringChecker::evalstrnLength(CheckerContext &C,
  743. const CallExpr *CE) const {
  744. // size_t strnlen(const char *s, size_t maxlen);
  745. evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
  746. }
  747. void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
  748. bool IsStrnlen) const {
  749. const GRState *state = C.getState();
  750. const Expr *Arg = CE->getArg(0);
  751. SVal ArgVal = state->getSVal(Arg);
  752. // Check that the argument is non-null.
  753. state = checkNonNull(C, state, Arg, ArgVal);
  754. if (state) {
  755. SVal strLength = getCStringLength(C, state, Arg, ArgVal);
  756. // If the argument isn't a valid C string, there's no valid state to
  757. // transition to.
  758. if (strLength.isUndef())
  759. return;
  760. // If the check is for strnlen() then bind the return value to no more than
  761. // the maxlen value.
  762. if (IsStrnlen) {
  763. const Expr *maxlenExpr = CE->getArg(1);
  764. SVal maxlenVal = state->getSVal(maxlenExpr);
  765. NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
  766. NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal);
  767. QualType cmpTy = C.getSValBuilder().getContext().IntTy;
  768. const GRState *stateTrue, *stateFalse;
  769. // Check if the strLength is greater than or equal to the maxlen
  770. llvm::tie(stateTrue, stateFalse) =
  771. state->assume(cast<DefinedOrUnknownSVal>
  772. (C.getSValBuilder().evalBinOpNN(state, BO_GE,
  773. *strLengthNL, *maxlenValNL,
  774. cmpTy)));
  775. // If the strLength is greater than or equal to the maxlen, set strLength
  776. // to maxlen
  777. if (stateTrue && !stateFalse) {
  778. strLength = maxlenVal;
  779. }
  780. }
  781. // If getCStringLength couldn't figure out the length, conjure a return
  782. // value, so it can be used in constraints, at least.
  783. if (strLength.isUnknown()) {
  784. unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
  785. strLength = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
  786. }
  787. // Bind the return value.
  788. state = state->BindExpr(CE, strLength);
  789. C.addTransition(state);
  790. }
  791. }
  792. void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
  793. // char *strcpy(char *restrict dst, const char *restrict src);
  794. evalStrcpyCommon(C, CE,
  795. /* returnEnd = */ false,
  796. /* isBounded = */ false,
  797. /* isAppending = */ false);
  798. }
  799. void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
  800. // char *strcpy(char *restrict dst, const char *restrict src);
  801. evalStrcpyCommon(C, CE,
  802. /* returnEnd = */ false,
  803. /* isBounded = */ true,
  804. /* isAppending = */ false);
  805. }
  806. void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
  807. // char *stpcpy(char *restrict dst, const char *restrict src);
  808. evalStrcpyCommon(C, CE,
  809. /* returnEnd = */ true,
  810. /* isBounded = */ false,
  811. /* isAppending = */ false);
  812. }
  813. void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
  814. //char *strcat(char *restrict s1, const char *restrict s2);
  815. evalStrcpyCommon(C, CE,
  816. /* returnEnd = */ false,
  817. /* isBounded = */ false,
  818. /* isAppending = */ true);
  819. }
  820. void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
  821. //char *strncat(char *restrict s1, const char *restrict s2, size_t n);
  822. evalStrcpyCommon(C, CE,
  823. /* returnEnd = */ false,
  824. /* isBounded = */ true,
  825. /* isAppending = */ true);
  826. }
  827. void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
  828. bool returnEnd, bool isBounded,
  829. bool isAppending) const {
  830. const GRState *state = C.getState();
  831. // Check that the destination is non-null.
  832. const Expr *Dst = CE->getArg(0);
  833. SVal DstVal = state->getSVal(Dst);
  834. state = checkNonNull(C, state, Dst, DstVal);
  835. if (!state)
  836. return;
  837. // Check that the source is non-null.
  838. const Expr *srcExpr = CE->getArg(1);
  839. SVal srcVal = state->getSVal(srcExpr);
  840. state = checkNonNull(C, state, srcExpr, srcVal);
  841. if (!state)
  842. return;
  843. // Get the string length of the source.
  844. SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
  845. // If the source isn't a valid C string, give up.
  846. if (strLength.isUndef())
  847. return;
  848. // If the function is strncpy, strncat, etc... it is bounded.
  849. if (isBounded) {
  850. // Get the max number of characters to copy.
  851. const Expr *lenExpr = CE->getArg(2);
  852. SVal lenVal = state->getSVal(lenExpr);
  853. NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
  854. NonLoc *lenValNL = dyn_cast<NonLoc>(&lenVal);
  855. QualType cmpTy = C.getSValBuilder().getContext().IntTy;
  856. const GRState *stateTrue, *stateFalse;
  857. // Check if the max number to copy is less than the length of the src.
  858. llvm::tie(stateTrue, stateFalse) =
  859. state->assume(cast<DefinedOrUnknownSVal>
  860. (C.getSValBuilder().evalBinOpNN(state, BO_GT,
  861. *strLengthNL, *lenValNL,
  862. cmpTy)));
  863. if (stateTrue) {
  864. // Max number to copy is less than the length of the src, so the actual
  865. // strLength copied is the max number arg.
  866. strLength = lenVal;
  867. }
  868. }
  869. // If this is an appending function (strcat, strncat...) then set the
  870. // string length to strlen(src) + strlen(dst) since the buffer will
  871. // ultimately contain both.
  872. if (isAppending) {
  873. // Get the string length of the destination, or give up.
  874. SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
  875. if (dstStrLength.isUndef())
  876. return;
  877. NonLoc *srcStrLengthNL = dyn_cast<NonLoc>(&strLength);
  878. NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength);
  879. // If src or dst cast to NonLoc is NULL, give up.
  880. if ((!srcStrLengthNL) || (!dstStrLengthNL))
  881. return;
  882. QualType addTy = C.getSValBuilder().getContext().getSizeType();
  883. strLength = C.getSValBuilder().evalBinOpNN(state, BO_Add,
  884. *srcStrLengthNL, *dstStrLengthNL,
  885. addTy);
  886. }
  887. SVal Result = (returnEnd ? UnknownVal() : DstVal);
  888. // If the destination is a MemRegion, try to check for a buffer overflow and
  889. // record the new string length.
  890. if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) {
  891. // If the length is known, we can check for an overflow.
  892. if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&strLength)) {
  893. SVal lastElement =
  894. C.getSValBuilder().evalBinOpLN(state, BO_Add, *dstRegVal,
  895. *knownStrLength, Dst->getType());
  896. state = CheckLocation(C, state, Dst, lastElement, /* IsDst = */ true);
  897. if (!state)
  898. return;
  899. // If this is a stpcpy-style copy, the last element is the return value.
  900. if (returnEnd)
  901. Result = lastElement;
  902. }
  903. // Invalidate the destination. This must happen before we set the C string
  904. // length because invalidation will clear the length.
  905. // FIXME: Even if we can't perfectly model the copy, we should see if we
  906. // can use LazyCompoundVals to copy the source values into the destination.
  907. // This would probably remove any existing bindings past the end of the
  908. // string, but that's still an improvement over blank invalidation.
  909. state = InvalidateBuffer(C, state, Dst, *dstRegVal);
  910. // Set the C string length of the destination.
  911. state = setCStringLength(state, dstRegVal->getRegion(), strLength);
  912. }
  913. // If this is a stpcpy-style copy, but we were unable to check for a buffer
  914. // overflow, we still need a result. Conjure a return value.
  915. if (returnEnd && Result.isUnknown()) {
  916. SValBuilder &svalBuilder = C.getSValBuilder();
  917. unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
  918. strLength = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
  919. }
  920. // Set the return value.
  921. state = state->BindExpr(CE, Result);
  922. C.addTransition(state);
  923. }
  924. void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
  925. //int strcmp(const char *restrict s1, const char *restrict s2);
  926. evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false);
  927. }
  928. void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
  929. //int strncmp(const char *restrict s1, const char *restrict s2, size_t n);
  930. evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false);
  931. }
  932. void CStringChecker::evalStrcasecmp(CheckerContext &C,
  933. const CallExpr *CE) const {
  934. //int strcasecmp(const char *restrict s1, const char *restrict s2);
  935. evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true);
  936. }
  937. void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
  938. bool isBounded, bool ignoreCase) const {
  939. const GRState *state = C.getState();
  940. // Check that the first string is non-null
  941. const Expr *s1 = CE->getArg(0);
  942. SVal s1Val = state->getSVal(s1);
  943. state = checkNonNull(C, state, s1, s1Val);
  944. if (!state)
  945. return;
  946. // Check that the second string is non-null.
  947. const Expr *s2 = CE->getArg(1);
  948. SVal s2Val = state->getSVal(s2);
  949. state = checkNonNull(C, state, s2, s2Val);
  950. if (!state)
  951. return;
  952. // Get the string length of the first string or give up.
  953. SVal s1Length = getCStringLength(C, state, s1, s1Val);
  954. if (s1Length.isUndef())
  955. return;
  956. // Get the string length of the second string or give up.
  957. SVal s2Length = getCStringLength(C, state, s2, s2Val);
  958. if (s2Length.isUndef())
  959. return;
  960. // Get the string literal of the first string.
  961. const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val);
  962. if (!s1StrLiteral)
  963. return;
  964. llvm::StringRef s1StrRef = s1StrLiteral->getString();
  965. // Get the string literal of the second string.
  966. const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val);
  967. if (!s2StrLiteral)
  968. return;
  969. llvm::StringRef s2StrRef = s2StrLiteral->getString();
  970. int result;
  971. if (isBounded) {
  972. // Get the max number of characters to compare.
  973. const Expr *lenExpr = CE->getArg(2);
  974. SVal lenVal = state->getSVal(lenExpr);
  975. // Dynamically cast the length to a ConcreteInt. If it is not a ConcreteInt
  976. // then give up, otherwise get the value and use it as the bounds.
  977. nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&lenVal);
  978. if (!CI)
  979. return;
  980. llvm::APSInt lenInt(CI->getValue());
  981. // Compare using the bounds provided like strncmp() does.
  982. if (ignoreCase) {
  983. // TODO Implement compare_lower(RHS, n) in LLVM StringRef.
  984. // result = s1StrRef.compare_lower(s2StrRef,
  985. // (size_t)lenInt.getLimitedValue());
  986. // For now, give up.
  987. return;
  988. } else {
  989. result = s1StrRef.compare(s2StrRef, (size_t)lenInt.getLimitedValue());
  990. }
  991. } else {
  992. // Compare string 1 to string 2 the same way strcmp() does.
  993. if (ignoreCase) {
  994. result = s1StrRef.compare_lower(s2StrRef);
  995. } else {
  996. result = s1StrRef.compare(s2StrRef);
  997. }
  998. }
  999. // Build the SVal of the comparison to bind the return value.
  1000. SValBuilder &svalBuilder = C.getSValBuilder();
  1001. QualType intTy = svalBuilder.getContext().IntTy;
  1002. SVal resultVal = svalBuilder.makeIntVal(result, intTy);
  1003. // Bind the return value of the expression.
  1004. // Set the return value.
  1005. state = state->BindExpr(CE, resultVal);
  1006. C.addTransition(state);
  1007. }
  1008. //===----------------------------------------------------------------------===//
  1009. // The driver method, and other Checker callbacks.
  1010. //===----------------------------------------------------------------------===//
  1011. bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
  1012. // Get the callee. All the functions we care about are C functions
  1013. // with simple identifiers.
  1014. const GRState *state = C.getState();
  1015. const Expr *Callee = CE->getCallee();
  1016. const FunctionDecl *FD = state->getSVal(Callee).getAsFunctionDecl();
  1017. if (!FD)
  1018. return false;
  1019. // Get the name of the callee. If it's a builtin, strip off the prefix.
  1020. IdentifierInfo *II = FD->getIdentifier();
  1021. if (!II) // if no identifier, not a simple C function
  1022. return false;
  1023. llvm::StringRef Name = II->getName();
  1024. if (Name.startswith("__builtin_"))
  1025. Name = Name.substr(10);
  1026. FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
  1027. .Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy)
  1028. .Case("mempcpy", &CStringChecker::evalMempcpy)
  1029. .Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp)
  1030. .Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove)
  1031. .Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy)
  1032. .Cases("strncpy", "__strncpy_chk", &CStringChecker::evalStrncpy)
  1033. .Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy)
  1034. .Cases("strcat", "__strcat_chk", &CStringChecker::evalStrcat)
  1035. .Cases("strncat", "__strncat_chk", &CStringChecker::evalStrncat)
  1036. .Case("strlen", &CStringChecker::evalstrLength)
  1037. .Case("strnlen", &CStringChecker::evalstrnLength)
  1038. .Case("strcmp", &CStringChecker::evalStrcmp)
  1039. .Case("strncmp", &CStringChecker::evalStrncmp)
  1040. .Case("strcasecmp", &CStringChecker::evalStrcasecmp)
  1041. .Case("bcopy", &CStringChecker::evalBcopy)
  1042. .Default(NULL);
  1043. // If the callee isn't a string function, let another checker handle it.
  1044. if (!evalFunction)
  1045. return false;
  1046. // Check and evaluate the call.
  1047. (this->*evalFunction)(C, CE);
  1048. return true;
  1049. }
  1050. void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
  1051. // Record string length for char a[] = "abc";
  1052. const GRState *state = C.getState();
  1053. for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end();
  1054. I != E; ++I) {
  1055. const VarDecl *D = dyn_cast<VarDecl>(*I);
  1056. if (!D)
  1057. continue;
  1058. // FIXME: Handle array fields of structs.
  1059. if (!D->getType()->isArrayType())
  1060. continue;
  1061. const Expr *Init = D->getInit();
  1062. if (!Init)
  1063. continue;
  1064. if (!isa<StringLiteral>(Init))
  1065. continue;
  1066. Loc VarLoc = state->getLValue(D, C.getPredecessor()->getLocationContext());
  1067. const MemRegion *MR = VarLoc.getAsRegion();
  1068. if (!MR)
  1069. continue;
  1070. SVal StrVal = state->getSVal(Init);
  1071. assert(StrVal.isValid() && "Initializer string is unknown or undefined");
  1072. DefinedOrUnknownSVal strLength
  1073. = cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal));
  1074. state = state->set<CStringLength>(MR, strLength);
  1075. }
  1076. C.addTransition(state);
  1077. }
  1078. bool CStringChecker::wantsRegionChangeUpdate(const GRState *state) const {
  1079. CStringLength::EntryMap Entries = state->get<CStringLength>();
  1080. return !Entries.isEmpty();
  1081. }
  1082. const GRState *
  1083. CStringChecker::checkRegionChanges(const GRState *state,
  1084. const MemRegion * const *Begin,
  1085. const MemRegion * const *End) const {
  1086. CStringLength::EntryMap Entries = state->get<CStringLength>();
  1087. if (Entries.isEmpty())
  1088. return state;
  1089. llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
  1090. llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
  1091. // First build sets for the changed regions and their super-regions.
  1092. for ( ; Begin != End; ++Begin) {
  1093. const MemRegion *MR = *Begin;
  1094. Invalidated.insert(MR);
  1095. SuperRegions.insert(MR);
  1096. while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
  1097. MR = SR->getSuperRegion();
  1098. SuperRegions.insert(MR);
  1099. }
  1100. }
  1101. CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
  1102. // Then loop over the entries in the current state.
  1103. for (CStringLength::EntryMap::iterator I = Entries.begin(),
  1104. E = Entries.end(); I != E; ++I) {
  1105. const MemRegion *MR = I.getKey();
  1106. // Is this entry for a super-region of a changed region?
  1107. if (SuperRegions.count(MR)) {
  1108. Entries = F.remove(Entries, MR);
  1109. continue;
  1110. }
  1111. // Is this entry for a sub-region of a changed region?
  1112. const MemRegion *Super = MR;
  1113. while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
  1114. Super = SR->getSuperRegion();
  1115. if (Invalidated.count(Super)) {
  1116. Entries = F.remove(Entries, MR);
  1117. break;
  1118. }
  1119. }
  1120. }
  1121. return state->set<CStringLength>(Entries);
  1122. }
  1123. void CStringChecker::checkLiveSymbols(const GRState *state,
  1124. SymbolReaper &SR) const {
  1125. // Mark all symbols in our string length map as valid.
  1126. CStringLength::EntryMap Entries = state->get<CStringLength>();
  1127. for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
  1128. I != E; ++I) {
  1129. SVal Len = I.getData();
  1130. if (SymbolRef Sym = Len.getAsSymbol())
  1131. SR.markInUse(Sym);
  1132. }
  1133. }
  1134. void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
  1135. CheckerContext &C) const {
  1136. if (!SR.hasDeadSymbols())
  1137. return;
  1138. const GRState *state = C.getState();
  1139. CStringLength::EntryMap Entries = state->get<CStringLength>();
  1140. if (Entries.isEmpty())
  1141. return;
  1142. CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
  1143. for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
  1144. I != E; ++I) {
  1145. SVal Len = I.getData();
  1146. if (SymbolRef Sym = Len.getAsSymbol()) {
  1147. if (SR.isDead(Sym))
  1148. Entries = F.remove(Entries, I.getKey());
  1149. }
  1150. }
  1151. state = state->set<CStringLength>(Entries);
  1152. C.generateNode(state);
  1153. }
  1154. void ento::registerCStringChecker(CheckerManager &mgr) {
  1155. mgr.registerChecker<CStringChecker>();
  1156. }