CStringChecker.cpp 87 KB

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