CStringChecker.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. //= CStringChecker.cpp - 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/ProgramStateTrait.h"
  20. #include "llvm/ADT/SmallString.h"
  21. #include "llvm/ADT/STLExtras.h"
  22. #include "llvm/ADT/StringSwitch.h"
  23. using namespace clang;
  24. using namespace ento;
  25. namespace {
  26. class CStringChecker : public Checker< eval::Call,
  27. check::PreStmt<DeclStmt>,
  28. check::LiveSymbols,
  29. check::DeadSymbols,
  30. check::RegionChanges
  31. > {
  32. mutable OwningPtr<BugType> BT_Null, BT_Bounds,
  33. BT_Overlap, BT_NotCString,
  34. BT_AdditionOverflow;
  35. mutable const char *CurrentFunctionDescription;
  36. public:
  37. static void *getTag() { static int tag; return &tag; }
  38. bool evalCall(const CallExpr *CE, CheckerContext &C) const;
  39. void checkPreStmt(const DeclStmt *DS, CheckerContext &C) const;
  40. void checkLiveSymbols(ProgramStateRef state, SymbolReaper &SR) const;
  41. void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
  42. bool wantsRegionChangeUpdate(ProgramStateRef state) const;
  43. ProgramStateRef
  44. checkRegionChanges(ProgramStateRef state,
  45. const StoreManager::InvalidatedSymbols *,
  46. ArrayRef<const MemRegion *> ExplicitRegions,
  47. ArrayRef<const MemRegion *> Regions) const;
  48. typedef void (CStringChecker::*FnCheck)(CheckerContext &,
  49. const CallExpr *) const;
  50. void evalMemcpy(CheckerContext &C, const CallExpr *CE) const;
  51. void evalMempcpy(CheckerContext &C, const CallExpr *CE) const;
  52. void evalMemmove(CheckerContext &C, const CallExpr *CE) const;
  53. void evalBcopy(CheckerContext &C, const CallExpr *CE) const;
  54. void evalCopyCommon(CheckerContext &C, const CallExpr *CE,
  55. ProgramStateRef state,
  56. const Expr *Size,
  57. const Expr *Source,
  58. const Expr *Dest,
  59. bool Restricted = false,
  60. bool IsMempcpy = false) const;
  61. void evalMemcmp(CheckerContext &C, const CallExpr *CE) const;
  62. void evalstrLength(CheckerContext &C, const CallExpr *CE) const;
  63. void evalstrnLength(CheckerContext &C, const CallExpr *CE) const;
  64. void evalstrLengthCommon(CheckerContext &C,
  65. const CallExpr *CE,
  66. bool IsStrnlen = false) const;
  67. void evalStrcpy(CheckerContext &C, const CallExpr *CE) const;
  68. void evalStrncpy(CheckerContext &C, const CallExpr *CE) const;
  69. void evalStpcpy(CheckerContext &C, const CallExpr *CE) const;
  70. void evalStrcpyCommon(CheckerContext &C,
  71. const CallExpr *CE,
  72. bool returnEnd,
  73. bool isBounded,
  74. bool isAppending) const;
  75. void evalStrcat(CheckerContext &C, const CallExpr *CE) const;
  76. void evalStrncat(CheckerContext &C, const CallExpr *CE) const;
  77. void evalStrcmp(CheckerContext &C, const CallExpr *CE) const;
  78. void evalStrncmp(CheckerContext &C, const CallExpr *CE) const;
  79. void evalStrcasecmp(CheckerContext &C, const CallExpr *CE) const;
  80. void evalStrncasecmp(CheckerContext &C, const CallExpr *CE) const;
  81. void evalStrcmpCommon(CheckerContext &C,
  82. const CallExpr *CE,
  83. bool isBounded = false,
  84. bool ignoreCase = false) const;
  85. // Utility methods
  86. std::pair<ProgramStateRef , ProgramStateRef >
  87. static assumeZero(CheckerContext &C,
  88. ProgramStateRef state, SVal V, QualType Ty);
  89. static ProgramStateRef setCStringLength(ProgramStateRef state,
  90. const MemRegion *MR,
  91. SVal strLength);
  92. static SVal getCStringLengthForRegion(CheckerContext &C,
  93. ProgramStateRef &state,
  94. const Expr *Ex,
  95. const MemRegion *MR,
  96. bool hypothetical);
  97. SVal getCStringLength(CheckerContext &C,
  98. ProgramStateRef &state,
  99. const Expr *Ex,
  100. SVal Buf,
  101. bool hypothetical = false) const;
  102. const StringLiteral *getCStringLiteral(CheckerContext &C,
  103. ProgramStateRef &state,
  104. const Expr *expr,
  105. SVal val) const;
  106. static ProgramStateRef InvalidateBuffer(CheckerContext &C,
  107. ProgramStateRef state,
  108. const Expr *Ex, SVal V);
  109. static bool SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
  110. const MemRegion *MR);
  111. // Re-usable checks
  112. ProgramStateRef checkNonNull(CheckerContext &C,
  113. ProgramStateRef state,
  114. const Expr *S,
  115. SVal l) const;
  116. ProgramStateRef CheckLocation(CheckerContext &C,
  117. ProgramStateRef state,
  118. const Expr *S,
  119. SVal l,
  120. const char *message = NULL) const;
  121. ProgramStateRef CheckBufferAccess(CheckerContext &C,
  122. ProgramStateRef state,
  123. const Expr *Size,
  124. const Expr *FirstBuf,
  125. const Expr *SecondBuf,
  126. const char *firstMessage = NULL,
  127. const char *secondMessage = NULL,
  128. bool WarnAboutSize = false) const;
  129. ProgramStateRef CheckBufferAccess(CheckerContext &C,
  130. ProgramStateRef state,
  131. const Expr *Size,
  132. const Expr *Buf,
  133. const char *message = NULL,
  134. bool WarnAboutSize = false) const {
  135. // This is a convenience override.
  136. return CheckBufferAccess(C, state, Size, Buf, NULL, message, NULL,
  137. WarnAboutSize);
  138. }
  139. ProgramStateRef CheckOverlap(CheckerContext &C,
  140. ProgramStateRef state,
  141. const Expr *Size,
  142. const Expr *First,
  143. const Expr *Second) const;
  144. void emitOverlapBug(CheckerContext &C,
  145. ProgramStateRef state,
  146. const Stmt *First,
  147. const Stmt *Second) const;
  148. ProgramStateRef checkAdditionOverflow(CheckerContext &C,
  149. ProgramStateRef state,
  150. NonLoc left,
  151. NonLoc right) const;
  152. };
  153. class CStringLength {
  154. public:
  155. typedef llvm::ImmutableMap<const MemRegion *, SVal> EntryMap;
  156. };
  157. } //end anonymous namespace
  158. namespace clang {
  159. namespace ento {
  160. template <>
  161. struct ProgramStateTrait<CStringLength>
  162. : public ProgramStatePartialTrait<CStringLength::EntryMap> {
  163. static void *GDMIndex() { return CStringChecker::getTag(); }
  164. };
  165. }
  166. }
  167. //===----------------------------------------------------------------------===//
  168. // Individual checks and utility methods.
  169. //===----------------------------------------------------------------------===//
  170. std::pair<ProgramStateRef , ProgramStateRef >
  171. CStringChecker::assumeZero(CheckerContext &C, ProgramStateRef state, SVal V,
  172. QualType Ty) {
  173. DefinedSVal *val = dyn_cast<DefinedSVal>(&V);
  174. if (!val)
  175. return std::pair<ProgramStateRef , ProgramStateRef >(state, state);
  176. SValBuilder &svalBuilder = C.getSValBuilder();
  177. DefinedOrUnknownSVal zero = svalBuilder.makeZeroVal(Ty);
  178. return state->assume(svalBuilder.evalEQ(state, *val, zero));
  179. }
  180. ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
  181. ProgramStateRef state,
  182. const Expr *S, SVal l) const {
  183. // If a previous check has failed, propagate the failure.
  184. if (!state)
  185. return NULL;
  186. ProgramStateRef stateNull, stateNonNull;
  187. llvm::tie(stateNull, stateNonNull) = assumeZero(C, state, l, S->getType());
  188. if (stateNull && !stateNonNull) {
  189. ExplodedNode *N = C.generateSink(stateNull);
  190. if (!N)
  191. return NULL;
  192. if (!BT_Null)
  193. BT_Null.reset(new BuiltinBug("API",
  194. "Null pointer argument in call to byte string function"));
  195. llvm::SmallString<80> buf;
  196. llvm::raw_svector_ostream os(buf);
  197. assert(CurrentFunctionDescription);
  198. os << "Null pointer argument in call to " << CurrentFunctionDescription;
  199. // Generate a report for this bug.
  200. BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Null.get());
  201. BugReport *report = new BugReport(*BT, os.str(), N);
  202. report->addRange(S->getSourceRange());
  203. report->addVisitor(bugreporter::getTrackNullOrUndefValueVisitor(N, S));
  204. C.EmitReport(report);
  205. return NULL;
  206. }
  207. // From here on, assume that the value is non-null.
  208. assert(stateNonNull);
  209. return stateNonNull;
  210. }
  211. // FIXME: This was originally copied from ArrayBoundChecker.cpp. Refactor?
  212. ProgramStateRef CStringChecker::CheckLocation(CheckerContext &C,
  213. ProgramStateRef state,
  214. const Expr *S, SVal l,
  215. const char *warningMsg) const {
  216. // If a previous check has failed, propagate the failure.
  217. if (!state)
  218. return NULL;
  219. // Check for out of bound array element access.
  220. const MemRegion *R = l.getAsRegion();
  221. if (!R)
  222. return state;
  223. const ElementRegion *ER = dyn_cast<ElementRegion>(R);
  224. if (!ER)
  225. return state;
  226. assert(ER->getValueType() == C.getASTContext().CharTy &&
  227. "CheckLocation should only be called with char* ElementRegions");
  228. // Get the size of the array.
  229. const SubRegion *superReg = cast<SubRegion>(ER->getSuperRegion());
  230. SValBuilder &svalBuilder = C.getSValBuilder();
  231. SVal Extent =
  232. svalBuilder.convertToArrayIndex(superReg->getExtent(svalBuilder));
  233. DefinedOrUnknownSVal Size = cast<DefinedOrUnknownSVal>(Extent);
  234. // Get the index of the accessed element.
  235. DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
  236. ProgramStateRef StInBound = state->assumeInBound(Idx, Size, true);
  237. ProgramStateRef StOutBound = state->assumeInBound(Idx, Size, false);
  238. if (StOutBound && !StInBound) {
  239. ExplodedNode *N = C.generateSink(StOutBound);
  240. if (!N)
  241. return NULL;
  242. if (!BT_Bounds) {
  243. BT_Bounds.reset(new BuiltinBug("Out-of-bound array access",
  244. "Byte string function accesses out-of-bound array element"));
  245. }
  246. BuiltinBug *BT = static_cast<BuiltinBug*>(BT_Bounds.get());
  247. // Generate a report for this bug.
  248. BugReport *report;
  249. if (warningMsg) {
  250. report = new BugReport(*BT, warningMsg, N);
  251. } else {
  252. assert(CurrentFunctionDescription);
  253. assert(CurrentFunctionDescription[0] != '\0');
  254. llvm::SmallString<80> buf;
  255. llvm::raw_svector_ostream os(buf);
  256. os << (char)toupper(CurrentFunctionDescription[0])
  257. << &CurrentFunctionDescription[1]
  258. << " accesses out-of-bound array element";
  259. report = new BugReport(*BT, os.str(), N);
  260. }
  261. // FIXME: It would be nice to eventually make this diagnostic more clear,
  262. // e.g., by referencing the original declaration or by saying *why* this
  263. // reference is outside the range.
  264. report->addRange(S->getSourceRange());
  265. C.EmitReport(report);
  266. return NULL;
  267. }
  268. // Array bound check succeeded. From this point forward the array bound
  269. // should always succeed.
  270. return StInBound;
  271. }
  272. ProgramStateRef CStringChecker::CheckBufferAccess(CheckerContext &C,
  273. ProgramStateRef state,
  274. const Expr *Size,
  275. const Expr *FirstBuf,
  276. const Expr *SecondBuf,
  277. const char *firstMessage,
  278. const char *secondMessage,
  279. bool WarnAboutSize) const {
  280. // If a previous check has failed, propagate the failure.
  281. if (!state)
  282. return NULL;
  283. SValBuilder &svalBuilder = C.getSValBuilder();
  284. ASTContext &Ctx = svalBuilder.getContext();
  285. const LocationContext *LCtx = C.getLocationContext();
  286. QualType sizeTy = Size->getType();
  287. QualType PtrTy = Ctx.getPointerType(Ctx.CharTy);
  288. // Check that the first buffer is non-null.
  289. SVal BufVal = state->getSVal(FirstBuf, LCtx);
  290. state = checkNonNull(C, state, FirstBuf, BufVal);
  291. if (!state)
  292. return NULL;
  293. // Get the access length and make sure it is known.
  294. // FIXME: This assumes the caller has already checked that the access length
  295. // is positive. And that it's unsigned.
  296. SVal LengthVal = state->getSVal(Size, LCtx);
  297. NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
  298. if (!Length)
  299. return state;
  300. // Compute the offset of the last element to be accessed: size-1.
  301. NonLoc One = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
  302. NonLoc LastOffset = cast<NonLoc>(svalBuilder.evalBinOpNN(state, BO_Sub,
  303. *Length, One, sizeTy));
  304. // Check that the first buffer is sufficiently long.
  305. SVal BufStart = svalBuilder.evalCast(BufVal, PtrTy, FirstBuf->getType());
  306. if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
  307. const Expr *warningExpr = (WarnAboutSize ? Size : FirstBuf);
  308. SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
  309. LastOffset, PtrTy);
  310. state = CheckLocation(C, state, warningExpr, BufEnd, firstMessage);
  311. // If the buffer isn't large enough, abort.
  312. if (!state)
  313. return NULL;
  314. }
  315. // If there's a second buffer, check it as well.
  316. if (SecondBuf) {
  317. BufVal = state->getSVal(SecondBuf, LCtx);
  318. state = checkNonNull(C, state, SecondBuf, BufVal);
  319. if (!state)
  320. return NULL;
  321. BufStart = svalBuilder.evalCast(BufVal, PtrTy, SecondBuf->getType());
  322. if (Loc *BufLoc = dyn_cast<Loc>(&BufStart)) {
  323. const Expr *warningExpr = (WarnAboutSize ? Size : SecondBuf);
  324. SVal BufEnd = svalBuilder.evalBinOpLN(state, BO_Add, *BufLoc,
  325. LastOffset, PtrTy);
  326. state = CheckLocation(C, state, warningExpr, BufEnd, secondMessage);
  327. }
  328. }
  329. // Large enough or not, return this state!
  330. return state;
  331. }
  332. ProgramStateRef CStringChecker::CheckOverlap(CheckerContext &C,
  333. ProgramStateRef state,
  334. const Expr *Size,
  335. const Expr *First,
  336. const Expr *Second) const {
  337. // Do a simple check for overlap: if the two arguments are from the same
  338. // buffer, see if the end of the first is greater than the start of the second
  339. // or vice versa.
  340. // If a previous check has failed, propagate the failure.
  341. if (!state)
  342. return NULL;
  343. ProgramStateRef stateTrue, stateFalse;
  344. // Get the buffer values and make sure they're known locations.
  345. const LocationContext *LCtx = C.getLocationContext();
  346. SVal firstVal = state->getSVal(First, LCtx);
  347. SVal secondVal = state->getSVal(Second, LCtx);
  348. Loc *firstLoc = dyn_cast<Loc>(&firstVal);
  349. if (!firstLoc)
  350. return state;
  351. Loc *secondLoc = dyn_cast<Loc>(&secondVal);
  352. if (!secondLoc)
  353. return state;
  354. // Are the two values the same?
  355. SValBuilder &svalBuilder = C.getSValBuilder();
  356. llvm::tie(stateTrue, stateFalse) =
  357. state->assume(svalBuilder.evalEQ(state, *firstLoc, *secondLoc));
  358. if (stateTrue && !stateFalse) {
  359. // If the values are known to be equal, that's automatically an overlap.
  360. emitOverlapBug(C, stateTrue, First, Second);
  361. return NULL;
  362. }
  363. // assume the two expressions are not equal.
  364. assert(stateFalse);
  365. state = stateFalse;
  366. // Which value comes first?
  367. QualType cmpTy = svalBuilder.getConditionType();
  368. SVal reverse = svalBuilder.evalBinOpLL(state, BO_GT,
  369. *firstLoc, *secondLoc, cmpTy);
  370. DefinedOrUnknownSVal *reverseTest = dyn_cast<DefinedOrUnknownSVal>(&reverse);
  371. if (!reverseTest)
  372. return state;
  373. llvm::tie(stateTrue, stateFalse) = state->assume(*reverseTest);
  374. if (stateTrue) {
  375. if (stateFalse) {
  376. // If we don't know which one comes first, we can't perform this test.
  377. return state;
  378. } else {
  379. // Switch the values so that firstVal is before secondVal.
  380. Loc *tmpLoc = firstLoc;
  381. firstLoc = secondLoc;
  382. secondLoc = tmpLoc;
  383. // Switch the Exprs as well, so that they still correspond.
  384. const Expr *tmpExpr = First;
  385. First = Second;
  386. Second = tmpExpr;
  387. }
  388. }
  389. // Get the length, and make sure it too is known.
  390. SVal LengthVal = state->getSVal(Size, LCtx);
  391. NonLoc *Length = dyn_cast<NonLoc>(&LengthVal);
  392. if (!Length)
  393. return state;
  394. // Convert the first buffer's start address to char*.
  395. // Bail out if the cast fails.
  396. ASTContext &Ctx = svalBuilder.getContext();
  397. QualType CharPtrTy = Ctx.getPointerType(Ctx.CharTy);
  398. SVal FirstStart = svalBuilder.evalCast(*firstLoc, CharPtrTy,
  399. First->getType());
  400. Loc *FirstStartLoc = dyn_cast<Loc>(&FirstStart);
  401. if (!FirstStartLoc)
  402. return state;
  403. // Compute the end of the first buffer. Bail out if THAT fails.
  404. SVal FirstEnd = svalBuilder.evalBinOpLN(state, BO_Add,
  405. *FirstStartLoc, *Length, CharPtrTy);
  406. Loc *FirstEndLoc = dyn_cast<Loc>(&FirstEnd);
  407. if (!FirstEndLoc)
  408. return state;
  409. // Is the end of the first buffer past the start of the second buffer?
  410. SVal Overlap = svalBuilder.evalBinOpLL(state, BO_GT,
  411. *FirstEndLoc, *secondLoc, cmpTy);
  412. DefinedOrUnknownSVal *OverlapTest = dyn_cast<DefinedOrUnknownSVal>(&Overlap);
  413. if (!OverlapTest)
  414. return state;
  415. llvm::tie(stateTrue, stateFalse) = state->assume(*OverlapTest);
  416. if (stateTrue && !stateFalse) {
  417. // Overlap!
  418. emitOverlapBug(C, stateTrue, First, Second);
  419. return NULL;
  420. }
  421. // assume the two expressions don't overlap.
  422. assert(stateFalse);
  423. return stateFalse;
  424. }
  425. void CStringChecker::emitOverlapBug(CheckerContext &C, ProgramStateRef state,
  426. const Stmt *First, const Stmt *Second) const {
  427. ExplodedNode *N = C.generateSink(state);
  428. if (!N)
  429. return;
  430. if (!BT_Overlap)
  431. BT_Overlap.reset(new BugType("Unix API", "Improper arguments"));
  432. // Generate a report for this bug.
  433. BugReport *report =
  434. new BugReport(*BT_Overlap,
  435. "Arguments must not be overlapping buffers", N);
  436. report->addRange(First->getSourceRange());
  437. report->addRange(Second->getSourceRange());
  438. C.EmitReport(report);
  439. }
  440. ProgramStateRef CStringChecker::checkAdditionOverflow(CheckerContext &C,
  441. ProgramStateRef state,
  442. NonLoc left,
  443. NonLoc right) const {
  444. // If a previous check has failed, propagate the failure.
  445. if (!state)
  446. return NULL;
  447. SValBuilder &svalBuilder = C.getSValBuilder();
  448. BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
  449. QualType sizeTy = svalBuilder.getContext().getSizeType();
  450. const llvm::APSInt &maxValInt = BVF.getMaxValue(sizeTy);
  451. NonLoc maxVal = svalBuilder.makeIntVal(maxValInt);
  452. SVal maxMinusRight;
  453. if (isa<nonloc::ConcreteInt>(right)) {
  454. maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, right,
  455. sizeTy);
  456. } else {
  457. // Try switching the operands. (The order of these two assignments is
  458. // important!)
  459. maxMinusRight = svalBuilder.evalBinOpNN(state, BO_Sub, maxVal, left,
  460. sizeTy);
  461. left = right;
  462. }
  463. if (NonLoc *maxMinusRightNL = dyn_cast<NonLoc>(&maxMinusRight)) {
  464. QualType cmpTy = svalBuilder.getConditionType();
  465. // If left > max - right, we have an overflow.
  466. SVal willOverflow = svalBuilder.evalBinOpNN(state, BO_GT, left,
  467. *maxMinusRightNL, cmpTy);
  468. ProgramStateRef stateOverflow, stateOkay;
  469. llvm::tie(stateOverflow, stateOkay) =
  470. state->assume(cast<DefinedOrUnknownSVal>(willOverflow));
  471. if (stateOverflow && !stateOkay) {
  472. // We have an overflow. Emit a bug report.
  473. ExplodedNode *N = C.generateSink(stateOverflow);
  474. if (!N)
  475. return NULL;
  476. if (!BT_AdditionOverflow)
  477. BT_AdditionOverflow.reset(new BuiltinBug("API",
  478. "Sum of expressions causes overflow"));
  479. // This isn't a great error message, but this should never occur in real
  480. // code anyway -- you'd have to create a buffer longer than a size_t can
  481. // represent, which is sort of a contradiction.
  482. const char *warning =
  483. "This expression will create a string whose length is too big to "
  484. "be represented as a size_t";
  485. // Generate a report for this bug.
  486. BugReport *report = new BugReport(*BT_AdditionOverflow, warning, N);
  487. C.EmitReport(report);
  488. return NULL;
  489. }
  490. // From now on, assume an overflow didn't occur.
  491. assert(stateOkay);
  492. state = stateOkay;
  493. }
  494. return state;
  495. }
  496. ProgramStateRef CStringChecker::setCStringLength(ProgramStateRef state,
  497. const MemRegion *MR,
  498. SVal strLength) {
  499. assert(!strLength.isUndef() && "Attempt to set an undefined string length");
  500. MR = MR->StripCasts();
  501. switch (MR->getKind()) {
  502. case MemRegion::StringRegionKind:
  503. // FIXME: This can happen if we strcpy() into a string region. This is
  504. // undefined [C99 6.4.5p6], but we should still warn about it.
  505. return state;
  506. case MemRegion::SymbolicRegionKind:
  507. case MemRegion::AllocaRegionKind:
  508. case MemRegion::VarRegionKind:
  509. case MemRegion::FieldRegionKind:
  510. case MemRegion::ObjCIvarRegionKind:
  511. // These are the types we can currently track string lengths for.
  512. break;
  513. case MemRegion::ElementRegionKind:
  514. // FIXME: Handle element regions by upper-bounding the parent region's
  515. // string length.
  516. return state;
  517. default:
  518. // Other regions (mostly non-data) can't have a reliable C string length.
  519. // For now, just ignore the change.
  520. // FIXME: These are rare but not impossible. We should output some kind of
  521. // warning for things like strcpy((char[]){'a', 0}, "b");
  522. return state;
  523. }
  524. if (strLength.isUnknown())
  525. return state->remove<CStringLength>(MR);
  526. return state->set<CStringLength>(MR, strLength);
  527. }
  528. SVal CStringChecker::getCStringLengthForRegion(CheckerContext &C,
  529. ProgramStateRef &state,
  530. const Expr *Ex,
  531. const MemRegion *MR,
  532. bool hypothetical) {
  533. if (!hypothetical) {
  534. // If there's a recorded length, go ahead and return it.
  535. const SVal *Recorded = state->get<CStringLength>(MR);
  536. if (Recorded)
  537. return *Recorded;
  538. }
  539. // Otherwise, get a new symbol and update the state.
  540. unsigned Count = C.getCurrentBlockCount();
  541. SValBuilder &svalBuilder = C.getSValBuilder();
  542. QualType sizeTy = svalBuilder.getContext().getSizeType();
  543. SVal strLength = svalBuilder.getMetadataSymbolVal(CStringChecker::getTag(),
  544. MR, Ex, sizeTy, Count);
  545. if (!hypothetical)
  546. state = state->set<CStringLength>(MR, strLength);
  547. return strLength;
  548. }
  549. SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
  550. const Expr *Ex, SVal Buf,
  551. bool hypothetical) const {
  552. const MemRegion *MR = Buf.getAsRegion();
  553. if (!MR) {
  554. // If we can't get a region, see if it's something we /know/ isn't a
  555. // C string. In the context of locations, the only time we can issue such
  556. // a warning is for labels.
  557. if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&Buf)) {
  558. if (ExplodedNode *N = C.addTransition(state)) {
  559. if (!BT_NotCString)
  560. BT_NotCString.reset(new BuiltinBug("API",
  561. "Argument is not a null-terminated string."));
  562. llvm::SmallString<120> buf;
  563. llvm::raw_svector_ostream os(buf);
  564. assert(CurrentFunctionDescription);
  565. os << "Argument to " << CurrentFunctionDescription
  566. << " is the address of the label '" << Label->getLabel()->getName()
  567. << "', which is not a null-terminated string";
  568. // Generate a report for this bug.
  569. BugReport *report = new BugReport(*BT_NotCString,
  570. os.str(), N);
  571. report->addRange(Ex->getSourceRange());
  572. C.EmitReport(report);
  573. }
  574. return UndefinedVal();
  575. }
  576. // If it's not a region and not a label, give up.
  577. return UnknownVal();
  578. }
  579. // If we have a region, strip casts from it and see if we can figure out
  580. // its length. For anything we can't figure out, just return UnknownVal.
  581. MR = MR->StripCasts();
  582. switch (MR->getKind()) {
  583. case MemRegion::StringRegionKind: {
  584. // Modifying the contents of string regions is undefined [C99 6.4.5p6],
  585. // so we can assume that the byte length is the correct C string length.
  586. SValBuilder &svalBuilder = C.getSValBuilder();
  587. QualType sizeTy = svalBuilder.getContext().getSizeType();
  588. const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
  589. return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy);
  590. }
  591. case MemRegion::SymbolicRegionKind:
  592. case MemRegion::AllocaRegionKind:
  593. case MemRegion::VarRegionKind:
  594. case MemRegion::FieldRegionKind:
  595. case MemRegion::ObjCIvarRegionKind:
  596. return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
  597. case MemRegion::CompoundLiteralRegionKind:
  598. // FIXME: Can we track this? Is it necessary?
  599. return UnknownVal();
  600. case MemRegion::ElementRegionKind:
  601. // FIXME: How can we handle this? It's not good enough to subtract the
  602. // offset from the base string length; consider "123\x00567" and &a[5].
  603. return UnknownVal();
  604. default:
  605. // Other regions (mostly non-data) can't have a reliable C string length.
  606. // In this case, an error is emitted and UndefinedVal is returned.
  607. // The caller should always be prepared to handle this case.
  608. if (ExplodedNode *N = C.addTransition(state)) {
  609. if (!BT_NotCString)
  610. BT_NotCString.reset(new BuiltinBug("API",
  611. "Argument is not a null-terminated string."));
  612. llvm::SmallString<120> buf;
  613. llvm::raw_svector_ostream os(buf);
  614. assert(CurrentFunctionDescription);
  615. os << "Argument to " << CurrentFunctionDescription << " is ";
  616. if (SummarizeRegion(os, C.getASTContext(), MR))
  617. os << ", which is not a null-terminated string";
  618. else
  619. os << "not a null-terminated string";
  620. // Generate a report for this bug.
  621. BugReport *report = new BugReport(*BT_NotCString,
  622. os.str(), N);
  623. report->addRange(Ex->getSourceRange());
  624. C.EmitReport(report);
  625. }
  626. return UndefinedVal();
  627. }
  628. }
  629. const StringLiteral *CStringChecker::getCStringLiteral(CheckerContext &C,
  630. ProgramStateRef &state, const Expr *expr, SVal val) const {
  631. // Get the memory region pointed to by the val.
  632. const MemRegion *bufRegion = val.getAsRegion();
  633. if (!bufRegion)
  634. return NULL;
  635. // Strip casts off the memory region.
  636. bufRegion = bufRegion->StripCasts();
  637. // Cast the memory region to a string region.
  638. const StringRegion *strRegion= dyn_cast<StringRegion>(bufRegion);
  639. if (!strRegion)
  640. return NULL;
  641. // Return the actual string in the string region.
  642. return strRegion->getStringLiteral();
  643. }
  644. ProgramStateRef CStringChecker::InvalidateBuffer(CheckerContext &C,
  645. ProgramStateRef state,
  646. const Expr *E, SVal V) {
  647. Loc *L = dyn_cast<Loc>(&V);
  648. if (!L)
  649. return state;
  650. // FIXME: This is a simplified version of what's in CFRefCount.cpp -- it makes
  651. // some assumptions about the value that CFRefCount can't. Even so, it should
  652. // probably be refactored.
  653. if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(L)) {
  654. const MemRegion *R = MR->getRegion()->StripCasts();
  655. // Are we dealing with an ElementRegion? If so, we should be invalidating
  656. // the super-region.
  657. if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
  658. R = ER->getSuperRegion();
  659. // FIXME: What about layers of ElementRegions?
  660. }
  661. // Invalidate this region.
  662. unsigned Count = C.getCurrentBlockCount();
  663. return state->invalidateRegions(R, E, Count);
  664. }
  665. // If we have a non-region value by chance, just remove the binding.
  666. // FIXME: is this necessary or correct? This handles the non-Region
  667. // cases. Is it ever valid to store to these?
  668. return state->unbindLoc(*L);
  669. }
  670. bool CStringChecker::SummarizeRegion(raw_ostream &os, ASTContext &Ctx,
  671. const MemRegion *MR) {
  672. const TypedValueRegion *TVR = dyn_cast<TypedValueRegion>(MR);
  673. switch (MR->getKind()) {
  674. case MemRegion::FunctionTextRegionKind: {
  675. const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl();
  676. if (FD)
  677. os << "the address of the function '" << *FD << '\'';
  678. else
  679. os << "the address of a function";
  680. return true;
  681. }
  682. case MemRegion::BlockTextRegionKind:
  683. os << "block text";
  684. return true;
  685. case MemRegion::BlockDataRegionKind:
  686. os << "a block";
  687. return true;
  688. case MemRegion::CXXThisRegionKind:
  689. case MemRegion::CXXTempObjectRegionKind:
  690. os << "a C++ temp object of type " << TVR->getValueType().getAsString();
  691. return true;
  692. case MemRegion::VarRegionKind:
  693. os << "a variable of type" << TVR->getValueType().getAsString();
  694. return true;
  695. case MemRegion::FieldRegionKind:
  696. os << "a field of type " << TVR->getValueType().getAsString();
  697. return true;
  698. case MemRegion::ObjCIvarRegionKind:
  699. os << "an instance variable of type " << TVR->getValueType().getAsString();
  700. return true;
  701. default:
  702. return false;
  703. }
  704. }
  705. //===----------------------------------------------------------------------===//
  706. // evaluation of individual function calls.
  707. //===----------------------------------------------------------------------===//
  708. void CStringChecker::evalCopyCommon(CheckerContext &C,
  709. const CallExpr *CE,
  710. ProgramStateRef state,
  711. const Expr *Size, const Expr *Dest,
  712. const Expr *Source, bool Restricted,
  713. bool IsMempcpy) const {
  714. CurrentFunctionDescription = "memory copy function";
  715. // See if the size argument is zero.
  716. const LocationContext *LCtx = C.getLocationContext();
  717. SVal sizeVal = state->getSVal(Size, LCtx);
  718. QualType sizeTy = Size->getType();
  719. ProgramStateRef stateZeroSize, stateNonZeroSize;
  720. llvm::tie(stateZeroSize, stateNonZeroSize) =
  721. assumeZero(C, state, sizeVal, sizeTy);
  722. // Get the value of the Dest.
  723. SVal destVal = state->getSVal(Dest, LCtx);
  724. // If the size is zero, there won't be any actual memory access, so
  725. // just bind the return value to the destination buffer and return.
  726. if (stateZeroSize) {
  727. stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, destVal);
  728. C.addTransition(stateZeroSize);
  729. }
  730. // If the size can be nonzero, we have to check the other arguments.
  731. if (stateNonZeroSize) {
  732. state = stateNonZeroSize;
  733. // Ensure the destination is not null. If it is NULL there will be a
  734. // NULL pointer dereference.
  735. state = checkNonNull(C, state, Dest, destVal);
  736. if (!state)
  737. return;
  738. // Get the value of the Src.
  739. SVal srcVal = state->getSVal(Source, LCtx);
  740. // Ensure the source is not null. If it is NULL there will be a
  741. // NULL pointer dereference.
  742. state = checkNonNull(C, state, Source, srcVal);
  743. if (!state)
  744. return;
  745. // Ensure the accesses are valid and that the buffers do not overlap.
  746. const char * const writeWarning =
  747. "Memory copy function overflows destination buffer";
  748. state = CheckBufferAccess(C, state, Size, Dest, Source,
  749. writeWarning, /* sourceWarning = */ NULL);
  750. if (Restricted)
  751. state = CheckOverlap(C, state, Size, Dest, Source);
  752. if (!state)
  753. return;
  754. // If this is mempcpy, get the byte after the last byte copied and
  755. // bind the expr.
  756. if (IsMempcpy) {
  757. loc::MemRegionVal *destRegVal = dyn_cast<loc::MemRegionVal>(&destVal);
  758. assert(destRegVal && "Destination should be a known MemRegionVal here");
  759. // Get the length to copy.
  760. NonLoc *lenValNonLoc = dyn_cast<NonLoc>(&sizeVal);
  761. if (lenValNonLoc) {
  762. // Get the byte after the last byte copied.
  763. SVal lastElement = C.getSValBuilder().evalBinOpLN(state, BO_Add,
  764. *destRegVal,
  765. *lenValNonLoc,
  766. Dest->getType());
  767. // The byte after the last byte copied is the return value.
  768. state = state->BindExpr(CE, LCtx, lastElement);
  769. } else {
  770. // If we don't know how much we copied, we can at least
  771. // conjure a return value for later.
  772. unsigned Count = C.getCurrentBlockCount();
  773. SVal result =
  774. C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
  775. state = state->BindExpr(CE, LCtx, result);
  776. }
  777. } else {
  778. // All other copies return the destination buffer.
  779. // (Well, bcopy() has a void return type, but this won't hurt.)
  780. state = state->BindExpr(CE, LCtx, destVal);
  781. }
  782. // Invalidate the destination.
  783. // FIXME: Even if we can't perfectly model the copy, we should see if we
  784. // can use LazyCompoundVals to copy the source values into the destination.
  785. // This would probably remove any existing bindings past the end of the
  786. // copied region, but that's still an improvement over blank invalidation.
  787. state = InvalidateBuffer(C, state, Dest,
  788. state->getSVal(Dest, C.getLocationContext()));
  789. C.addTransition(state);
  790. }
  791. }
  792. void CStringChecker::evalMemcpy(CheckerContext &C, const CallExpr *CE) const {
  793. // void *memcpy(void *restrict dst, const void *restrict src, size_t n);
  794. // The return value is the address of the destination buffer.
  795. const Expr *Dest = CE->getArg(0);
  796. ProgramStateRef state = C.getState();
  797. evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true);
  798. }
  799. void CStringChecker::evalMempcpy(CheckerContext &C, const CallExpr *CE) const {
  800. // void *mempcpy(void *restrict dst, const void *restrict src, size_t n);
  801. // The return value is a pointer to the byte following the last written byte.
  802. const Expr *Dest = CE->getArg(0);
  803. ProgramStateRef state = C.getState();
  804. evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1), true, true);
  805. }
  806. void CStringChecker::evalMemmove(CheckerContext &C, const CallExpr *CE) const {
  807. // void *memmove(void *dst, const void *src, size_t n);
  808. // The return value is the address of the destination buffer.
  809. const Expr *Dest = CE->getArg(0);
  810. ProgramStateRef state = C.getState();
  811. evalCopyCommon(C, CE, state, CE->getArg(2), Dest, CE->getArg(1));
  812. }
  813. void CStringChecker::evalBcopy(CheckerContext &C, const CallExpr *CE) const {
  814. // void bcopy(const void *src, void *dst, size_t n);
  815. evalCopyCommon(C, CE, C.getState(),
  816. CE->getArg(2), CE->getArg(1), CE->getArg(0));
  817. }
  818. void CStringChecker::evalMemcmp(CheckerContext &C, const CallExpr *CE) const {
  819. // int memcmp(const void *s1, const void *s2, size_t n);
  820. CurrentFunctionDescription = "memory comparison function";
  821. const Expr *Left = CE->getArg(0);
  822. const Expr *Right = CE->getArg(1);
  823. const Expr *Size = CE->getArg(2);
  824. ProgramStateRef state = C.getState();
  825. SValBuilder &svalBuilder = C.getSValBuilder();
  826. // See if the size argument is zero.
  827. const LocationContext *LCtx = C.getLocationContext();
  828. SVal sizeVal = state->getSVal(Size, LCtx);
  829. QualType sizeTy = Size->getType();
  830. ProgramStateRef stateZeroSize, stateNonZeroSize;
  831. llvm::tie(stateZeroSize, stateNonZeroSize) =
  832. assumeZero(C, state, sizeVal, sizeTy);
  833. // If the size can be zero, the result will be 0 in that case, and we don't
  834. // have to check either of the buffers.
  835. if (stateZeroSize) {
  836. state = stateZeroSize;
  837. state = state->BindExpr(CE, LCtx,
  838. svalBuilder.makeZeroVal(CE->getType()));
  839. C.addTransition(state);
  840. }
  841. // If the size can be nonzero, we have to check the other arguments.
  842. if (stateNonZeroSize) {
  843. state = stateNonZeroSize;
  844. // If we know the two buffers are the same, we know the result is 0.
  845. // First, get the two buffers' addresses. Another checker will have already
  846. // made sure they're not undefined.
  847. DefinedOrUnknownSVal LV =
  848. cast<DefinedOrUnknownSVal>(state->getSVal(Left, LCtx));
  849. DefinedOrUnknownSVal RV =
  850. cast<DefinedOrUnknownSVal>(state->getSVal(Right, LCtx));
  851. // See if they are the same.
  852. DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
  853. ProgramStateRef StSameBuf, StNotSameBuf;
  854. llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
  855. // If the two arguments might be the same buffer, we know the result is 0,
  856. // and we only need to check one size.
  857. if (StSameBuf) {
  858. state = StSameBuf;
  859. state = CheckBufferAccess(C, state, Size, Left);
  860. if (state) {
  861. state = StSameBuf->BindExpr(CE, LCtx,
  862. svalBuilder.makeZeroVal(CE->getType()));
  863. C.addTransition(state);
  864. }
  865. }
  866. // If the two arguments might be different buffers, we have to check the
  867. // size of both of them.
  868. if (StNotSameBuf) {
  869. state = StNotSameBuf;
  870. state = CheckBufferAccess(C, state, Size, Left, Right);
  871. if (state) {
  872. // The return value is the comparison result, which we don't know.
  873. unsigned Count = C.getCurrentBlockCount();
  874. SVal CmpV = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
  875. state = state->BindExpr(CE, LCtx, CmpV);
  876. C.addTransition(state);
  877. }
  878. }
  879. }
  880. }
  881. void CStringChecker::evalstrLength(CheckerContext &C,
  882. const CallExpr *CE) const {
  883. // size_t strlen(const char *s);
  884. evalstrLengthCommon(C, CE, /* IsStrnlen = */ false);
  885. }
  886. void CStringChecker::evalstrnLength(CheckerContext &C,
  887. const CallExpr *CE) const {
  888. // size_t strnlen(const char *s, size_t maxlen);
  889. evalstrLengthCommon(C, CE, /* IsStrnlen = */ true);
  890. }
  891. void CStringChecker::evalstrLengthCommon(CheckerContext &C, const CallExpr *CE,
  892. bool IsStrnlen) const {
  893. CurrentFunctionDescription = "string length function";
  894. ProgramStateRef state = C.getState();
  895. const LocationContext *LCtx = C.getLocationContext();
  896. if (IsStrnlen) {
  897. const Expr *maxlenExpr = CE->getArg(1);
  898. SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
  899. ProgramStateRef stateZeroSize, stateNonZeroSize;
  900. llvm::tie(stateZeroSize, stateNonZeroSize) =
  901. assumeZero(C, state, maxlenVal, maxlenExpr->getType());
  902. // If the size can be zero, the result will be 0 in that case, and we don't
  903. // have to check the string itself.
  904. if (stateZeroSize) {
  905. SVal zero = C.getSValBuilder().makeZeroVal(CE->getType());
  906. stateZeroSize = stateZeroSize->BindExpr(CE, LCtx, zero);
  907. C.addTransition(stateZeroSize);
  908. }
  909. // If the size is GUARANTEED to be zero, we're done!
  910. if (!stateNonZeroSize)
  911. return;
  912. // Otherwise, record the assumption that the size is nonzero.
  913. state = stateNonZeroSize;
  914. }
  915. // Check that the string argument is non-null.
  916. const Expr *Arg = CE->getArg(0);
  917. SVal ArgVal = state->getSVal(Arg, LCtx);
  918. state = checkNonNull(C, state, Arg, ArgVal);
  919. if (!state)
  920. return;
  921. SVal strLength = getCStringLength(C, state, Arg, ArgVal);
  922. // If the argument isn't a valid C string, there's no valid state to
  923. // transition to.
  924. if (strLength.isUndef())
  925. return;
  926. DefinedOrUnknownSVal result = UnknownVal();
  927. // If the check is for strnlen() then bind the return value to no more than
  928. // the maxlen value.
  929. if (IsStrnlen) {
  930. QualType cmpTy = C.getSValBuilder().getConditionType();
  931. // It's a little unfortunate to be getting this again,
  932. // but it's not that expensive...
  933. const Expr *maxlenExpr = CE->getArg(1);
  934. SVal maxlenVal = state->getSVal(maxlenExpr, LCtx);
  935. NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
  936. NonLoc *maxlenValNL = dyn_cast<NonLoc>(&maxlenVal);
  937. if (strLengthNL && maxlenValNL) {
  938. ProgramStateRef stateStringTooLong, stateStringNotTooLong;
  939. // Check if the strLength is greater than the maxlen.
  940. llvm::tie(stateStringTooLong, stateStringNotTooLong) =
  941. state->assume(cast<DefinedOrUnknownSVal>
  942. (C.getSValBuilder().evalBinOpNN(state, BO_GT,
  943. *strLengthNL,
  944. *maxlenValNL,
  945. cmpTy)));
  946. if (stateStringTooLong && !stateStringNotTooLong) {
  947. // If the string is longer than maxlen, return maxlen.
  948. result = *maxlenValNL;
  949. } else if (stateStringNotTooLong && !stateStringTooLong) {
  950. // If the string is shorter than maxlen, return its length.
  951. result = *strLengthNL;
  952. }
  953. }
  954. if (result.isUnknown()) {
  955. // If we don't have enough information for a comparison, there's
  956. // no guarantee the full string length will actually be returned.
  957. // All we know is the return value is the min of the string length
  958. // and the limit. This is better than nothing.
  959. unsigned Count = C.getCurrentBlockCount();
  960. result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
  961. NonLoc *resultNL = cast<NonLoc>(&result);
  962. if (strLengthNL) {
  963. state = state->assume(cast<DefinedOrUnknownSVal>
  964. (C.getSValBuilder().evalBinOpNN(state, BO_LE,
  965. *resultNL,
  966. *strLengthNL,
  967. cmpTy)), true);
  968. }
  969. if (maxlenValNL) {
  970. state = state->assume(cast<DefinedOrUnknownSVal>
  971. (C.getSValBuilder().evalBinOpNN(state, BO_LE,
  972. *resultNL,
  973. *maxlenValNL,
  974. cmpTy)), true);
  975. }
  976. }
  977. } else {
  978. // This is a plain strlen(), not strnlen().
  979. result = cast<DefinedOrUnknownSVal>(strLength);
  980. // If we don't know the length of the string, conjure a return
  981. // value, so it can be used in constraints, at least.
  982. if (result.isUnknown()) {
  983. unsigned Count = C.getCurrentBlockCount();
  984. result = C.getSValBuilder().getConjuredSymbolVal(NULL, CE, Count);
  985. }
  986. }
  987. // Bind the return value.
  988. assert(!result.isUnknown() && "Should have conjured a value by now");
  989. state = state->BindExpr(CE, LCtx, result);
  990. C.addTransition(state);
  991. }
  992. void CStringChecker::evalStrcpy(CheckerContext &C, const CallExpr *CE) const {
  993. // char *strcpy(char *restrict dst, const char *restrict src);
  994. evalStrcpyCommon(C, CE,
  995. /* returnEnd = */ false,
  996. /* isBounded = */ false,
  997. /* isAppending = */ false);
  998. }
  999. void CStringChecker::evalStrncpy(CheckerContext &C, const CallExpr *CE) const {
  1000. // char *strncpy(char *restrict dst, const char *restrict src, size_t n);
  1001. evalStrcpyCommon(C, CE,
  1002. /* returnEnd = */ false,
  1003. /* isBounded = */ true,
  1004. /* isAppending = */ false);
  1005. }
  1006. void CStringChecker::evalStpcpy(CheckerContext &C, const CallExpr *CE) const {
  1007. // char *stpcpy(char *restrict dst, const char *restrict src);
  1008. evalStrcpyCommon(C, CE,
  1009. /* returnEnd = */ true,
  1010. /* isBounded = */ false,
  1011. /* isAppending = */ false);
  1012. }
  1013. void CStringChecker::evalStrcat(CheckerContext &C, const CallExpr *CE) const {
  1014. //char *strcat(char *restrict s1, const char *restrict s2);
  1015. evalStrcpyCommon(C, CE,
  1016. /* returnEnd = */ false,
  1017. /* isBounded = */ false,
  1018. /* isAppending = */ true);
  1019. }
  1020. void CStringChecker::evalStrncat(CheckerContext &C, const CallExpr *CE) const {
  1021. //char *strncat(char *restrict s1, const char *restrict s2, size_t n);
  1022. evalStrcpyCommon(C, CE,
  1023. /* returnEnd = */ false,
  1024. /* isBounded = */ true,
  1025. /* isAppending = */ true);
  1026. }
  1027. void CStringChecker::evalStrcpyCommon(CheckerContext &C, const CallExpr *CE,
  1028. bool returnEnd, bool isBounded,
  1029. bool isAppending) const {
  1030. CurrentFunctionDescription = "string copy function";
  1031. ProgramStateRef state = C.getState();
  1032. const LocationContext *LCtx = C.getLocationContext();
  1033. // Check that the destination is non-null.
  1034. const Expr *Dst = CE->getArg(0);
  1035. SVal DstVal = state->getSVal(Dst, LCtx);
  1036. state = checkNonNull(C, state, Dst, DstVal);
  1037. if (!state)
  1038. return;
  1039. // Check that the source is non-null.
  1040. const Expr *srcExpr = CE->getArg(1);
  1041. SVal srcVal = state->getSVal(srcExpr, LCtx);
  1042. state = checkNonNull(C, state, srcExpr, srcVal);
  1043. if (!state)
  1044. return;
  1045. // Get the string length of the source.
  1046. SVal strLength = getCStringLength(C, state, srcExpr, srcVal);
  1047. // If the source isn't a valid C string, give up.
  1048. if (strLength.isUndef())
  1049. return;
  1050. SValBuilder &svalBuilder = C.getSValBuilder();
  1051. QualType cmpTy = svalBuilder.getConditionType();
  1052. QualType sizeTy = svalBuilder.getContext().getSizeType();
  1053. // These two values allow checking two kinds of errors:
  1054. // - actual overflows caused by a source that doesn't fit in the destination
  1055. // - potential overflows caused by a bound that could exceed the destination
  1056. SVal amountCopied = UnknownVal();
  1057. SVal maxLastElementIndex = UnknownVal();
  1058. const char *boundWarning = NULL;
  1059. // If the function is strncpy, strncat, etc... it is bounded.
  1060. if (isBounded) {
  1061. // Get the max number of characters to copy.
  1062. const Expr *lenExpr = CE->getArg(2);
  1063. SVal lenVal = state->getSVal(lenExpr, LCtx);
  1064. // Protect against misdeclared strncpy().
  1065. lenVal = svalBuilder.evalCast(lenVal, sizeTy, lenExpr->getType());
  1066. NonLoc *strLengthNL = dyn_cast<NonLoc>(&strLength);
  1067. NonLoc *lenValNL = dyn_cast<NonLoc>(&lenVal);
  1068. // If we know both values, we might be able to figure out how much
  1069. // we're copying.
  1070. if (strLengthNL && lenValNL) {
  1071. ProgramStateRef stateSourceTooLong, stateSourceNotTooLong;
  1072. // Check if the max number to copy is less than the length of the src.
  1073. // If the bound is equal to the source length, strncpy won't null-
  1074. // terminate the result!
  1075. llvm::tie(stateSourceTooLong, stateSourceNotTooLong) =
  1076. state->assume(cast<DefinedOrUnknownSVal>
  1077. (svalBuilder.evalBinOpNN(state, BO_GE, *strLengthNL,
  1078. *lenValNL, cmpTy)));
  1079. if (stateSourceTooLong && !stateSourceNotTooLong) {
  1080. // Max number to copy is less than the length of the src, so the actual
  1081. // strLength copied is the max number arg.
  1082. state = stateSourceTooLong;
  1083. amountCopied = lenVal;
  1084. } else if (!stateSourceTooLong && stateSourceNotTooLong) {
  1085. // The source buffer entirely fits in the bound.
  1086. state = stateSourceNotTooLong;
  1087. amountCopied = strLength;
  1088. }
  1089. }
  1090. // We still want to know if the bound is known to be too large.
  1091. if (lenValNL) {
  1092. if (isAppending) {
  1093. // For strncat, the check is strlen(dst) + lenVal < sizeof(dst)
  1094. // Get the string length of the destination. If the destination is
  1095. // memory that can't have a string length, we shouldn't be copying
  1096. // into it anyway.
  1097. SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
  1098. if (dstStrLength.isUndef())
  1099. return;
  1100. if (NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength)) {
  1101. maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Add,
  1102. *lenValNL,
  1103. *dstStrLengthNL,
  1104. sizeTy);
  1105. boundWarning = "Size argument is greater than the free space in the "
  1106. "destination buffer";
  1107. }
  1108. } else {
  1109. // For strncpy, this is just checking that lenVal <= sizeof(dst)
  1110. // (Yes, strncpy and strncat differ in how they treat termination.
  1111. // strncat ALWAYS terminates, but strncpy doesn't.)
  1112. NonLoc one = cast<NonLoc>(svalBuilder.makeIntVal(1, sizeTy));
  1113. maxLastElementIndex = svalBuilder.evalBinOpNN(state, BO_Sub, *lenValNL,
  1114. one, sizeTy);
  1115. boundWarning = "Size argument is greater than the length of the "
  1116. "destination buffer";
  1117. }
  1118. }
  1119. // If we couldn't pin down the copy length, at least bound it.
  1120. // FIXME: We should actually run this code path for append as well, but
  1121. // right now it creates problems with constraints (since we can end up
  1122. // trying to pass constraints from symbol to symbol).
  1123. if (amountCopied.isUnknown() && !isAppending) {
  1124. // Try to get a "hypothetical" string length symbol, which we can later
  1125. // set as a real value if that turns out to be the case.
  1126. amountCopied = getCStringLength(C, state, lenExpr, srcVal, true);
  1127. assert(!amountCopied.isUndef());
  1128. if (NonLoc *amountCopiedNL = dyn_cast<NonLoc>(&amountCopied)) {
  1129. if (lenValNL) {
  1130. // amountCopied <= lenVal
  1131. SVal copiedLessThanBound = svalBuilder.evalBinOpNN(state, BO_LE,
  1132. *amountCopiedNL,
  1133. *lenValNL,
  1134. cmpTy);
  1135. state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanBound),
  1136. true);
  1137. if (!state)
  1138. return;
  1139. }
  1140. if (strLengthNL) {
  1141. // amountCopied <= strlen(source)
  1142. SVal copiedLessThanSrc = svalBuilder.evalBinOpNN(state, BO_LE,
  1143. *amountCopiedNL,
  1144. *strLengthNL,
  1145. cmpTy);
  1146. state = state->assume(cast<DefinedOrUnknownSVal>(copiedLessThanSrc),
  1147. true);
  1148. if (!state)
  1149. return;
  1150. }
  1151. }
  1152. }
  1153. } else {
  1154. // The function isn't bounded. The amount copied should match the length
  1155. // of the source buffer.
  1156. amountCopied = strLength;
  1157. }
  1158. assert(state);
  1159. // This represents the number of characters copied into the destination
  1160. // buffer. (It may not actually be the strlen if the destination buffer
  1161. // is not terminated.)
  1162. SVal finalStrLength = UnknownVal();
  1163. // If this is an appending function (strcat, strncat...) then set the
  1164. // string length to strlen(src) + strlen(dst) since the buffer will
  1165. // ultimately contain both.
  1166. if (isAppending) {
  1167. // Get the string length of the destination. If the destination is memory
  1168. // that can't have a string length, we shouldn't be copying into it anyway.
  1169. SVal dstStrLength = getCStringLength(C, state, Dst, DstVal);
  1170. if (dstStrLength.isUndef())
  1171. return;
  1172. NonLoc *srcStrLengthNL = dyn_cast<NonLoc>(&amountCopied);
  1173. NonLoc *dstStrLengthNL = dyn_cast<NonLoc>(&dstStrLength);
  1174. // If we know both string lengths, we might know the final string length.
  1175. if (srcStrLengthNL && dstStrLengthNL) {
  1176. // Make sure the two lengths together don't overflow a size_t.
  1177. state = checkAdditionOverflow(C, state, *srcStrLengthNL, *dstStrLengthNL);
  1178. if (!state)
  1179. return;
  1180. finalStrLength = svalBuilder.evalBinOpNN(state, BO_Add, *srcStrLengthNL,
  1181. *dstStrLengthNL, sizeTy);
  1182. }
  1183. // If we couldn't get a single value for the final string length,
  1184. // we can at least bound it by the individual lengths.
  1185. if (finalStrLength.isUnknown()) {
  1186. // Try to get a "hypothetical" string length symbol, which we can later
  1187. // set as a real value if that turns out to be the case.
  1188. finalStrLength = getCStringLength(C, state, CE, DstVal, true);
  1189. assert(!finalStrLength.isUndef());
  1190. if (NonLoc *finalStrLengthNL = dyn_cast<NonLoc>(&finalStrLength)) {
  1191. if (srcStrLengthNL) {
  1192. // finalStrLength >= srcStrLength
  1193. SVal sourceInResult = svalBuilder.evalBinOpNN(state, BO_GE,
  1194. *finalStrLengthNL,
  1195. *srcStrLengthNL,
  1196. cmpTy);
  1197. state = state->assume(cast<DefinedOrUnknownSVal>(sourceInResult),
  1198. true);
  1199. if (!state)
  1200. return;
  1201. }
  1202. if (dstStrLengthNL) {
  1203. // finalStrLength >= dstStrLength
  1204. SVal destInResult = svalBuilder.evalBinOpNN(state, BO_GE,
  1205. *finalStrLengthNL,
  1206. *dstStrLengthNL,
  1207. cmpTy);
  1208. state = state->assume(cast<DefinedOrUnknownSVal>(destInResult),
  1209. true);
  1210. if (!state)
  1211. return;
  1212. }
  1213. }
  1214. }
  1215. } else {
  1216. // Otherwise, this is a copy-over function (strcpy, strncpy, ...), and
  1217. // the final string length will match the input string length.
  1218. finalStrLength = amountCopied;
  1219. }
  1220. // The final result of the function will either be a pointer past the last
  1221. // copied element, or a pointer to the start of the destination buffer.
  1222. SVal Result = (returnEnd ? UnknownVal() : DstVal);
  1223. assert(state);
  1224. // If the destination is a MemRegion, try to check for a buffer overflow and
  1225. // record the new string length.
  1226. if (loc::MemRegionVal *dstRegVal = dyn_cast<loc::MemRegionVal>(&DstVal)) {
  1227. QualType ptrTy = Dst->getType();
  1228. // If we have an exact value on a bounded copy, use that to check for
  1229. // overflows, rather than our estimate about how much is actually copied.
  1230. if (boundWarning) {
  1231. if (NonLoc *maxLastNL = dyn_cast<NonLoc>(&maxLastElementIndex)) {
  1232. SVal maxLastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
  1233. *maxLastNL, ptrTy);
  1234. state = CheckLocation(C, state, CE->getArg(2), maxLastElement,
  1235. boundWarning);
  1236. if (!state)
  1237. return;
  1238. }
  1239. }
  1240. // Then, if the final length is known...
  1241. if (NonLoc *knownStrLength = dyn_cast<NonLoc>(&finalStrLength)) {
  1242. SVal lastElement = svalBuilder.evalBinOpLN(state, BO_Add, *dstRegVal,
  1243. *knownStrLength, ptrTy);
  1244. // ...and we haven't checked the bound, we'll check the actual copy.
  1245. if (!boundWarning) {
  1246. const char * const warningMsg =
  1247. "String copy function overflows destination buffer";
  1248. state = CheckLocation(C, state, Dst, lastElement, warningMsg);
  1249. if (!state)
  1250. return;
  1251. }
  1252. // If this is a stpcpy-style copy, the last element is the return value.
  1253. if (returnEnd)
  1254. Result = lastElement;
  1255. }
  1256. // Invalidate the destination. This must happen before we set the C string
  1257. // length because invalidation will clear the length.
  1258. // FIXME: Even if we can't perfectly model the copy, we should see if we
  1259. // can use LazyCompoundVals to copy the source values into the destination.
  1260. // This would probably remove any existing bindings past the end of the
  1261. // string, but that's still an improvement over blank invalidation.
  1262. state = InvalidateBuffer(C, state, Dst, *dstRegVal);
  1263. // Set the C string length of the destination, if we know it.
  1264. if (isBounded && !isAppending) {
  1265. // strncpy is annoying in that it doesn't guarantee to null-terminate
  1266. // the result string. If the original string didn't fit entirely inside
  1267. // the bound (including the null-terminator), we don't know how long the
  1268. // result is.
  1269. if (amountCopied != strLength)
  1270. finalStrLength = UnknownVal();
  1271. }
  1272. state = setCStringLength(state, dstRegVal->getRegion(), finalStrLength);
  1273. }
  1274. assert(state);
  1275. // If this is a stpcpy-style copy, but we were unable to check for a buffer
  1276. // overflow, we still need a result. Conjure a return value.
  1277. if (returnEnd && Result.isUnknown()) {
  1278. unsigned Count = C.getCurrentBlockCount();
  1279. Result = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
  1280. }
  1281. // Set the return value.
  1282. state = state->BindExpr(CE, LCtx, Result);
  1283. C.addTransition(state);
  1284. }
  1285. void CStringChecker::evalStrcmp(CheckerContext &C, const CallExpr *CE) const {
  1286. //int strcmp(const char *s1, const char *s2);
  1287. evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ false);
  1288. }
  1289. void CStringChecker::evalStrncmp(CheckerContext &C, const CallExpr *CE) const {
  1290. //int strncmp(const char *s1, const char *s2, size_t n);
  1291. evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ false);
  1292. }
  1293. void CStringChecker::evalStrcasecmp(CheckerContext &C,
  1294. const CallExpr *CE) const {
  1295. //int strcasecmp(const char *s1, const char *s2);
  1296. evalStrcmpCommon(C, CE, /* isBounded = */ false, /* ignoreCase = */ true);
  1297. }
  1298. void CStringChecker::evalStrncasecmp(CheckerContext &C,
  1299. const CallExpr *CE) const {
  1300. //int strncasecmp(const char *s1, const char *s2, size_t n);
  1301. evalStrcmpCommon(C, CE, /* isBounded = */ true, /* ignoreCase = */ true);
  1302. }
  1303. void CStringChecker::evalStrcmpCommon(CheckerContext &C, const CallExpr *CE,
  1304. bool isBounded, bool ignoreCase) const {
  1305. CurrentFunctionDescription = "string comparison function";
  1306. ProgramStateRef state = C.getState();
  1307. const LocationContext *LCtx = C.getLocationContext();
  1308. // Check that the first string is non-null
  1309. const Expr *s1 = CE->getArg(0);
  1310. SVal s1Val = state->getSVal(s1, LCtx);
  1311. state = checkNonNull(C, state, s1, s1Val);
  1312. if (!state)
  1313. return;
  1314. // Check that the second string is non-null.
  1315. const Expr *s2 = CE->getArg(1);
  1316. SVal s2Val = state->getSVal(s2, LCtx);
  1317. state = checkNonNull(C, state, s2, s2Val);
  1318. if (!state)
  1319. return;
  1320. // Get the string length of the first string or give up.
  1321. SVal s1Length = getCStringLength(C, state, s1, s1Val);
  1322. if (s1Length.isUndef())
  1323. return;
  1324. // Get the string length of the second string or give up.
  1325. SVal s2Length = getCStringLength(C, state, s2, s2Val);
  1326. if (s2Length.isUndef())
  1327. return;
  1328. // If we know the two buffers are the same, we know the result is 0.
  1329. // First, get the two buffers' addresses. Another checker will have already
  1330. // made sure they're not undefined.
  1331. DefinedOrUnknownSVal LV = cast<DefinedOrUnknownSVal>(s1Val);
  1332. DefinedOrUnknownSVal RV = cast<DefinedOrUnknownSVal>(s2Val);
  1333. // See if they are the same.
  1334. SValBuilder &svalBuilder = C.getSValBuilder();
  1335. DefinedOrUnknownSVal SameBuf = svalBuilder.evalEQ(state, LV, RV);
  1336. ProgramStateRef StSameBuf, StNotSameBuf;
  1337. llvm::tie(StSameBuf, StNotSameBuf) = state->assume(SameBuf);
  1338. // If the two arguments might be the same buffer, we know the result is 0,
  1339. // and we only need to check one size.
  1340. if (StSameBuf) {
  1341. StSameBuf = StSameBuf->BindExpr(CE, LCtx,
  1342. svalBuilder.makeZeroVal(CE->getType()));
  1343. C.addTransition(StSameBuf);
  1344. // If the two arguments are GUARANTEED to be the same, we're done!
  1345. if (!StNotSameBuf)
  1346. return;
  1347. }
  1348. assert(StNotSameBuf);
  1349. state = StNotSameBuf;
  1350. // At this point we can go about comparing the two buffers.
  1351. // For now, we only do this if they're both known string literals.
  1352. // Attempt to extract string literals from both expressions.
  1353. const StringLiteral *s1StrLiteral = getCStringLiteral(C, state, s1, s1Val);
  1354. const StringLiteral *s2StrLiteral = getCStringLiteral(C, state, s2, s2Val);
  1355. bool canComputeResult = false;
  1356. if (s1StrLiteral && s2StrLiteral) {
  1357. StringRef s1StrRef = s1StrLiteral->getString();
  1358. StringRef s2StrRef = s2StrLiteral->getString();
  1359. if (isBounded) {
  1360. // Get the max number of characters to compare.
  1361. const Expr *lenExpr = CE->getArg(2);
  1362. SVal lenVal = state->getSVal(lenExpr, LCtx);
  1363. // If the length is known, we can get the right substrings.
  1364. if (const llvm::APSInt *len = svalBuilder.getKnownValue(state, lenVal)) {
  1365. // Create substrings of each to compare the prefix.
  1366. s1StrRef = s1StrRef.substr(0, (size_t)len->getZExtValue());
  1367. s2StrRef = s2StrRef.substr(0, (size_t)len->getZExtValue());
  1368. canComputeResult = true;
  1369. }
  1370. } else {
  1371. // This is a normal, unbounded strcmp.
  1372. canComputeResult = true;
  1373. }
  1374. if (canComputeResult) {
  1375. // Real strcmp stops at null characters.
  1376. size_t s1Term = s1StrRef.find('\0');
  1377. if (s1Term != StringRef::npos)
  1378. s1StrRef = s1StrRef.substr(0, s1Term);
  1379. size_t s2Term = s2StrRef.find('\0');
  1380. if (s2Term != StringRef::npos)
  1381. s2StrRef = s2StrRef.substr(0, s2Term);
  1382. // Use StringRef's comparison methods to compute the actual result.
  1383. int result;
  1384. if (ignoreCase) {
  1385. // Compare string 1 to string 2 the same way strcasecmp() does.
  1386. result = s1StrRef.compare_lower(s2StrRef);
  1387. } else {
  1388. // Compare string 1 to string 2 the same way strcmp() does.
  1389. result = s1StrRef.compare(s2StrRef);
  1390. }
  1391. // Build the SVal of the comparison and bind the return value.
  1392. SVal resultVal = svalBuilder.makeIntVal(result, CE->getType());
  1393. state = state->BindExpr(CE, LCtx, resultVal);
  1394. }
  1395. }
  1396. if (!canComputeResult) {
  1397. // Conjure a symbolic value. It's the best we can do.
  1398. unsigned Count = C.getCurrentBlockCount();
  1399. SVal resultVal = svalBuilder.getConjuredSymbolVal(NULL, CE, Count);
  1400. state = state->BindExpr(CE, LCtx, resultVal);
  1401. }
  1402. // Record this as a possible path.
  1403. C.addTransition(state);
  1404. }
  1405. //===----------------------------------------------------------------------===//
  1406. // The driver method, and other Checker callbacks.
  1407. //===----------------------------------------------------------------------===//
  1408. bool CStringChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
  1409. StringRef Name = C.getCalleeName(CE);
  1410. if (Name.empty())
  1411. return false;
  1412. if (Name.startswith("__builtin_"))
  1413. Name = Name.substr(10);
  1414. FnCheck evalFunction = llvm::StringSwitch<FnCheck>(Name)
  1415. .Cases("memcpy", "__memcpy_chk", &CStringChecker::evalMemcpy)
  1416. .Cases("mempcpy", "__mempcpy_chk", &CStringChecker::evalMempcpy)
  1417. .Cases("memcmp", "bcmp", &CStringChecker::evalMemcmp)
  1418. .Cases("memmove", "__memmove_chk", &CStringChecker::evalMemmove)
  1419. .Cases("strcpy", "__strcpy_chk", &CStringChecker::evalStrcpy)
  1420. .Cases("strncpy", "__strncpy_chk", &CStringChecker::evalStrncpy)
  1421. .Cases("stpcpy", "__stpcpy_chk", &CStringChecker::evalStpcpy)
  1422. .Cases("strcat", "__strcat_chk", &CStringChecker::evalStrcat)
  1423. .Cases("strncat", "__strncat_chk", &CStringChecker::evalStrncat)
  1424. .Case("strlen", &CStringChecker::evalstrLength)
  1425. .Case("strnlen", &CStringChecker::evalstrnLength)
  1426. .Case("strcmp", &CStringChecker::evalStrcmp)
  1427. .Case("strncmp", &CStringChecker::evalStrncmp)
  1428. .Case("strcasecmp", &CStringChecker::evalStrcasecmp)
  1429. .Case("strncasecmp", &CStringChecker::evalStrncasecmp)
  1430. .Case("bcopy", &CStringChecker::evalBcopy)
  1431. .Default(NULL);
  1432. // If the callee isn't a string function, let another checker handle it.
  1433. if (!evalFunction)
  1434. return false;
  1435. // Make sure each function sets its own description.
  1436. // (But don't bother in a release build.)
  1437. assert(!(CurrentFunctionDescription = NULL));
  1438. // Check and evaluate the call.
  1439. (this->*evalFunction)(C, CE);
  1440. return true;
  1441. }
  1442. void CStringChecker::checkPreStmt(const DeclStmt *DS, CheckerContext &C) const {
  1443. // Record string length for char a[] = "abc";
  1444. ProgramStateRef state = C.getState();
  1445. for (DeclStmt::const_decl_iterator I = DS->decl_begin(), E = DS->decl_end();
  1446. I != E; ++I) {
  1447. const VarDecl *D = dyn_cast<VarDecl>(*I);
  1448. if (!D)
  1449. continue;
  1450. // FIXME: Handle array fields of structs.
  1451. if (!D->getType()->isArrayType())
  1452. continue;
  1453. const Expr *Init = D->getInit();
  1454. if (!Init)
  1455. continue;
  1456. if (!isa<StringLiteral>(Init))
  1457. continue;
  1458. Loc VarLoc = state->getLValue(D, C.getLocationContext());
  1459. const MemRegion *MR = VarLoc.getAsRegion();
  1460. if (!MR)
  1461. continue;
  1462. SVal StrVal = state->getSVal(Init, C.getLocationContext());
  1463. assert(StrVal.isValid() && "Initializer string is unknown or undefined");
  1464. DefinedOrUnknownSVal strLength
  1465. = cast<DefinedOrUnknownSVal>(getCStringLength(C, state, Init, StrVal));
  1466. state = state->set<CStringLength>(MR, strLength);
  1467. }
  1468. C.addTransition(state);
  1469. }
  1470. bool CStringChecker::wantsRegionChangeUpdate(ProgramStateRef state) const {
  1471. CStringLength::EntryMap Entries = state->get<CStringLength>();
  1472. return !Entries.isEmpty();
  1473. }
  1474. ProgramStateRef
  1475. CStringChecker::checkRegionChanges(ProgramStateRef state,
  1476. const StoreManager::InvalidatedSymbols *,
  1477. ArrayRef<const MemRegion *> ExplicitRegions,
  1478. ArrayRef<const MemRegion *> Regions) const {
  1479. CStringLength::EntryMap Entries = state->get<CStringLength>();
  1480. if (Entries.isEmpty())
  1481. return state;
  1482. llvm::SmallPtrSet<const MemRegion *, 8> Invalidated;
  1483. llvm::SmallPtrSet<const MemRegion *, 32> SuperRegions;
  1484. // First build sets for the changed regions and their super-regions.
  1485. for (ArrayRef<const MemRegion *>::iterator
  1486. I = Regions.begin(), E = Regions.end(); I != E; ++I) {
  1487. const MemRegion *MR = *I;
  1488. Invalidated.insert(MR);
  1489. SuperRegions.insert(MR);
  1490. while (const SubRegion *SR = dyn_cast<SubRegion>(MR)) {
  1491. MR = SR->getSuperRegion();
  1492. SuperRegions.insert(MR);
  1493. }
  1494. }
  1495. CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
  1496. // Then loop over the entries in the current state.
  1497. for (CStringLength::EntryMap::iterator I = Entries.begin(),
  1498. E = Entries.end(); I != E; ++I) {
  1499. const MemRegion *MR = I.getKey();
  1500. // Is this entry for a super-region of a changed region?
  1501. if (SuperRegions.count(MR)) {
  1502. Entries = F.remove(Entries, MR);
  1503. continue;
  1504. }
  1505. // Is this entry for a sub-region of a changed region?
  1506. const MemRegion *Super = MR;
  1507. while (const SubRegion *SR = dyn_cast<SubRegion>(Super)) {
  1508. Super = SR->getSuperRegion();
  1509. if (Invalidated.count(Super)) {
  1510. Entries = F.remove(Entries, MR);
  1511. break;
  1512. }
  1513. }
  1514. }
  1515. return state->set<CStringLength>(Entries);
  1516. }
  1517. void CStringChecker::checkLiveSymbols(ProgramStateRef state,
  1518. SymbolReaper &SR) const {
  1519. // Mark all symbols in our string length map as valid.
  1520. CStringLength::EntryMap Entries = state->get<CStringLength>();
  1521. for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
  1522. I != E; ++I) {
  1523. SVal Len = I.getData();
  1524. for (SymExpr::symbol_iterator si = Len.symbol_begin(),
  1525. se = Len.symbol_end(); si != se; ++si)
  1526. SR.markInUse(*si);
  1527. }
  1528. }
  1529. void CStringChecker::checkDeadSymbols(SymbolReaper &SR,
  1530. CheckerContext &C) const {
  1531. if (!SR.hasDeadSymbols())
  1532. return;
  1533. ProgramStateRef state = C.getState();
  1534. CStringLength::EntryMap Entries = state->get<CStringLength>();
  1535. if (Entries.isEmpty())
  1536. return;
  1537. CStringLength::EntryMap::Factory &F = state->get_context<CStringLength>();
  1538. for (CStringLength::EntryMap::iterator I = Entries.begin(), E = Entries.end();
  1539. I != E; ++I) {
  1540. SVal Len = I.getData();
  1541. if (SymbolRef Sym = Len.getAsSymbol()) {
  1542. if (SR.isDead(Sym))
  1543. Entries = F.remove(Entries, I.getKey());
  1544. }
  1545. }
  1546. state = state->set<CStringLength>(Entries);
  1547. C.addTransition(state);
  1548. }
  1549. void ento::registerCStringChecker(CheckerManager &mgr) {
  1550. mgr.registerChecker<CStringChecker>();
  1551. }