CStringChecker.cpp 93 KB

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