CStringChecker.cpp 97 KB

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