CStringChecker.cpp 73 KB

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