CStringChecker.cpp 81 KB

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