StreamChecker.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. //===-- StreamChecker.cpp -----------------------------------------*- C++ -*--//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines checkers that model and check stream handling functions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ClangSACheckers.h"
  14. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  15. #include "clang/StaticAnalyzer/Core/Checker.h"
  16. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
  20. #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
  21. #include "llvm/ADT/ImmutableMap.h"
  22. using namespace clang;
  23. using namespace ento;
  24. namespace {
  25. struct StreamState {
  26. enum Kind { Opened, Closed, OpenFailed, Escaped } K;
  27. const Stmt *S;
  28. StreamState(Kind k, const Stmt *s) : K(k), S(s) {}
  29. bool isOpened() const { return K == Opened; }
  30. bool isClosed() const { return K == Closed; }
  31. //bool isOpenFailed() const { return K == OpenFailed; }
  32. //bool isEscaped() const { return K == Escaped; }
  33. bool operator==(const StreamState &X) const {
  34. return K == X.K && S == X.S;
  35. }
  36. static StreamState getOpened(const Stmt *s) { return StreamState(Opened, s); }
  37. static StreamState getClosed(const Stmt *s) { return StreamState(Closed, s); }
  38. static StreamState getOpenFailed(const Stmt *s) {
  39. return StreamState(OpenFailed, s);
  40. }
  41. static StreamState getEscaped(const Stmt *s) {
  42. return StreamState(Escaped, s);
  43. }
  44. void Profile(llvm::FoldingSetNodeID &ID) const {
  45. ID.AddInteger(K);
  46. ID.AddPointer(S);
  47. }
  48. };
  49. class StreamChecker : public Checker<eval::Call,
  50. check::DeadSymbols > {
  51. mutable IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread,
  52. *II_fwrite,
  53. *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos,
  54. *II_clearerr, *II_feof, *II_ferror, *II_fileno;
  55. mutable OwningPtr<BuiltinBug> BT_nullfp, BT_illegalwhence,
  56. BT_doubleclose, BT_ResourceLeak;
  57. public:
  58. StreamChecker()
  59. : II_fopen(0), II_tmpfile(0) ,II_fclose(0), II_fread(0), II_fwrite(0),
  60. II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0),
  61. II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0) {}
  62. bool evalCall(const CallExpr *CE, CheckerContext &C) const;
  63. void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
  64. private:
  65. void Fopen(CheckerContext &C, const CallExpr *CE) const;
  66. void Tmpfile(CheckerContext &C, const CallExpr *CE) const;
  67. void Fclose(CheckerContext &C, const CallExpr *CE) const;
  68. void Fread(CheckerContext &C, const CallExpr *CE) const;
  69. void Fwrite(CheckerContext &C, const CallExpr *CE) const;
  70. void Fseek(CheckerContext &C, const CallExpr *CE) const;
  71. void Ftell(CheckerContext &C, const CallExpr *CE) const;
  72. void Rewind(CheckerContext &C, const CallExpr *CE) const;
  73. void Fgetpos(CheckerContext &C, const CallExpr *CE) const;
  74. void Fsetpos(CheckerContext &C, const CallExpr *CE) const;
  75. void Clearerr(CheckerContext &C, const CallExpr *CE) const;
  76. void Feof(CheckerContext &C, const CallExpr *CE) const;
  77. void Ferror(CheckerContext &C, const CallExpr *CE) const;
  78. void Fileno(CheckerContext &C, const CallExpr *CE) const;
  79. void OpenFileAux(CheckerContext &C, const CallExpr *CE) const;
  80. ProgramStateRef CheckNullStream(SVal SV, ProgramStateRef state,
  81. CheckerContext &C) const;
  82. ProgramStateRef CheckDoubleClose(const CallExpr *CE, ProgramStateRef state,
  83. CheckerContext &C) const;
  84. };
  85. } // end anonymous namespace
  86. REGISTER_MAP_WITH_PROGRAMSTATE(StreamMap, SymbolRef, StreamState)
  87. bool StreamChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
  88. const FunctionDecl *FD = C.getCalleeDecl(CE);
  89. if (!FD || FD->getKind() != Decl::Function)
  90. return false;
  91. ASTContext &Ctx = C.getASTContext();
  92. if (!II_fopen)
  93. II_fopen = &Ctx.Idents.get("fopen");
  94. if (!II_tmpfile)
  95. II_tmpfile = &Ctx.Idents.get("tmpfile");
  96. if (!II_fclose)
  97. II_fclose = &Ctx.Idents.get("fclose");
  98. if (!II_fread)
  99. II_fread = &Ctx.Idents.get("fread");
  100. if (!II_fwrite)
  101. II_fwrite = &Ctx.Idents.get("fwrite");
  102. if (!II_fseek)
  103. II_fseek = &Ctx.Idents.get("fseek");
  104. if (!II_ftell)
  105. II_ftell = &Ctx.Idents.get("ftell");
  106. if (!II_rewind)
  107. II_rewind = &Ctx.Idents.get("rewind");
  108. if (!II_fgetpos)
  109. II_fgetpos = &Ctx.Idents.get("fgetpos");
  110. if (!II_fsetpos)
  111. II_fsetpos = &Ctx.Idents.get("fsetpos");
  112. if (!II_clearerr)
  113. II_clearerr = &Ctx.Idents.get("clearerr");
  114. if (!II_feof)
  115. II_feof = &Ctx.Idents.get("feof");
  116. if (!II_ferror)
  117. II_ferror = &Ctx.Idents.get("ferror");
  118. if (!II_fileno)
  119. II_fileno = &Ctx.Idents.get("fileno");
  120. if (FD->getIdentifier() == II_fopen) {
  121. Fopen(C, CE);
  122. return true;
  123. }
  124. if (FD->getIdentifier() == II_tmpfile) {
  125. Tmpfile(C, CE);
  126. return true;
  127. }
  128. if (FD->getIdentifier() == II_fclose) {
  129. Fclose(C, CE);
  130. return true;
  131. }
  132. if (FD->getIdentifier() == II_fread) {
  133. Fread(C, CE);
  134. return true;
  135. }
  136. if (FD->getIdentifier() == II_fwrite) {
  137. Fwrite(C, CE);
  138. return true;
  139. }
  140. if (FD->getIdentifier() == II_fseek) {
  141. Fseek(C, CE);
  142. return true;
  143. }
  144. if (FD->getIdentifier() == II_ftell) {
  145. Ftell(C, CE);
  146. return true;
  147. }
  148. if (FD->getIdentifier() == II_rewind) {
  149. Rewind(C, CE);
  150. return true;
  151. }
  152. if (FD->getIdentifier() == II_fgetpos) {
  153. Fgetpos(C, CE);
  154. return true;
  155. }
  156. if (FD->getIdentifier() == II_fsetpos) {
  157. Fsetpos(C, CE);
  158. return true;
  159. }
  160. if (FD->getIdentifier() == II_clearerr) {
  161. Clearerr(C, CE);
  162. return true;
  163. }
  164. if (FD->getIdentifier() == II_feof) {
  165. Feof(C, CE);
  166. return true;
  167. }
  168. if (FD->getIdentifier() == II_ferror) {
  169. Ferror(C, CE);
  170. return true;
  171. }
  172. if (FD->getIdentifier() == II_fileno) {
  173. Fileno(C, CE);
  174. return true;
  175. }
  176. return false;
  177. }
  178. void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) const {
  179. OpenFileAux(C, CE);
  180. }
  181. void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) const {
  182. OpenFileAux(C, CE);
  183. }
  184. void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) const {
  185. ProgramStateRef state = C.getState();
  186. SValBuilder &svalBuilder = C.getSValBuilder();
  187. const LocationContext *LCtx = C.getPredecessor()->getLocationContext();
  188. DefinedSVal RetVal = svalBuilder.conjureSymbolVal(0, CE, LCtx, C.blockCount())
  189. .castAs<DefinedSVal>();
  190. state = state->BindExpr(CE, C.getLocationContext(), RetVal);
  191. ConstraintManager &CM = C.getConstraintManager();
  192. // Bifurcate the state into two: one with a valid FILE* pointer, the other
  193. // with a NULL.
  194. ProgramStateRef stateNotNull, stateNull;
  195. llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, RetVal);
  196. if (SymbolRef Sym = RetVal.getAsSymbol()) {
  197. // if RetVal is not NULL, set the symbol's state to Opened.
  198. stateNotNull =
  199. stateNotNull->set<StreamMap>(Sym,StreamState::getOpened(CE));
  200. stateNull =
  201. stateNull->set<StreamMap>(Sym, StreamState::getOpenFailed(CE));
  202. C.addTransition(stateNotNull);
  203. C.addTransition(stateNull);
  204. }
  205. }
  206. void StreamChecker::Fclose(CheckerContext &C, const CallExpr *CE) const {
  207. ProgramStateRef state = CheckDoubleClose(CE, C.getState(), C);
  208. if (state)
  209. C.addTransition(state);
  210. }
  211. void StreamChecker::Fread(CheckerContext &C, const CallExpr *CE) const {
  212. ProgramStateRef state = C.getState();
  213. if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
  214. state, C))
  215. return;
  216. }
  217. void StreamChecker::Fwrite(CheckerContext &C, const CallExpr *CE) const {
  218. ProgramStateRef state = C.getState();
  219. if (!CheckNullStream(state->getSVal(CE->getArg(3), C.getLocationContext()),
  220. state, C))
  221. return;
  222. }
  223. void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) const {
  224. ProgramStateRef state = C.getState();
  225. if (!(state = CheckNullStream(state->getSVal(CE->getArg(0),
  226. C.getLocationContext()), state, C)))
  227. return;
  228. // Check the legality of the 'whence' argument of 'fseek'.
  229. SVal Whence = state->getSVal(CE->getArg(2), C.getLocationContext());
  230. Optional<nonloc::ConcreteInt> CI = Whence.getAs<nonloc::ConcreteInt>();
  231. if (!CI)
  232. return;
  233. int64_t x = CI->getValue().getSExtValue();
  234. if (x >= 0 && x <= 2)
  235. return;
  236. if (ExplodedNode *N = C.addTransition(state)) {
  237. if (!BT_illegalwhence)
  238. BT_illegalwhence.reset(new BuiltinBug("Illegal whence argument",
  239. "The whence argument to fseek() should be "
  240. "SEEK_SET, SEEK_END, or SEEK_CUR."));
  241. BugReport *R = new BugReport(*BT_illegalwhence,
  242. BT_illegalwhence->getDescription(), N);
  243. C.emitReport(R);
  244. }
  245. }
  246. void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) const {
  247. ProgramStateRef state = C.getState();
  248. if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
  249. state, C))
  250. return;
  251. }
  252. void StreamChecker::Rewind(CheckerContext &C, const CallExpr *CE) const {
  253. ProgramStateRef state = C.getState();
  254. if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
  255. state, C))
  256. return;
  257. }
  258. void StreamChecker::Fgetpos(CheckerContext &C, const CallExpr *CE) const {
  259. ProgramStateRef state = C.getState();
  260. if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
  261. state, C))
  262. return;
  263. }
  264. void StreamChecker::Fsetpos(CheckerContext &C, const CallExpr *CE) const {
  265. ProgramStateRef state = C.getState();
  266. if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
  267. state, C))
  268. return;
  269. }
  270. void StreamChecker::Clearerr(CheckerContext &C, const CallExpr *CE) const {
  271. ProgramStateRef state = C.getState();
  272. if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
  273. state, C))
  274. return;
  275. }
  276. void StreamChecker::Feof(CheckerContext &C, const CallExpr *CE) const {
  277. ProgramStateRef state = C.getState();
  278. if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
  279. state, C))
  280. return;
  281. }
  282. void StreamChecker::Ferror(CheckerContext &C, const CallExpr *CE) const {
  283. ProgramStateRef state = C.getState();
  284. if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
  285. state, C))
  286. return;
  287. }
  288. void StreamChecker::Fileno(CheckerContext &C, const CallExpr *CE) const {
  289. ProgramStateRef state = C.getState();
  290. if (!CheckNullStream(state->getSVal(CE->getArg(0), C.getLocationContext()),
  291. state, C))
  292. return;
  293. }
  294. ProgramStateRef StreamChecker::CheckNullStream(SVal SV, ProgramStateRef state,
  295. CheckerContext &C) const {
  296. Optional<DefinedSVal> DV = SV.getAs<DefinedSVal>();
  297. if (!DV)
  298. return 0;
  299. ConstraintManager &CM = C.getConstraintManager();
  300. ProgramStateRef stateNotNull, stateNull;
  301. llvm::tie(stateNotNull, stateNull) = CM.assumeDual(state, *DV);
  302. if (!stateNotNull && stateNull) {
  303. if (ExplodedNode *N = C.generateSink(stateNull)) {
  304. if (!BT_nullfp)
  305. BT_nullfp.reset(new BuiltinBug("NULL stream pointer",
  306. "Stream pointer might be NULL."));
  307. BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
  308. C.emitReport(R);
  309. }
  310. return 0;
  311. }
  312. return stateNotNull;
  313. }
  314. ProgramStateRef StreamChecker::CheckDoubleClose(const CallExpr *CE,
  315. ProgramStateRef state,
  316. CheckerContext &C) const {
  317. SymbolRef Sym =
  318. state->getSVal(CE->getArg(0), C.getLocationContext()).getAsSymbol();
  319. if (!Sym)
  320. return state;
  321. const StreamState *SS = state->get<StreamMap>(Sym);
  322. // If the file stream is not tracked, return.
  323. if (!SS)
  324. return state;
  325. // Check: Double close a File Descriptor could cause undefined behaviour.
  326. // Conforming to man-pages
  327. if (SS->isClosed()) {
  328. ExplodedNode *N = C.generateSink();
  329. if (N) {
  330. if (!BT_doubleclose)
  331. BT_doubleclose.reset(new BuiltinBug("Double fclose",
  332. "Try to close a file Descriptor already"
  333. " closed. Cause undefined behaviour."));
  334. BugReport *R = new BugReport(*BT_doubleclose,
  335. BT_doubleclose->getDescription(), N);
  336. C.emitReport(R);
  337. }
  338. return NULL;
  339. }
  340. // Close the File Descriptor.
  341. return state->set<StreamMap>(Sym, StreamState::getClosed(CE));
  342. }
  343. void StreamChecker::checkDeadSymbols(SymbolReaper &SymReaper,
  344. CheckerContext &C) const {
  345. // TODO: Clean up the state.
  346. for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
  347. E = SymReaper.dead_end(); I != E; ++I) {
  348. SymbolRef Sym = *I;
  349. ProgramStateRef state = C.getState();
  350. const StreamState *SS = state->get<StreamMap>(Sym);
  351. // TODO: Shouldn't we have a continue here?
  352. if (!SS)
  353. return;
  354. if (SS->isOpened()) {
  355. ExplodedNode *N = C.generateSink();
  356. if (N) {
  357. if (!BT_ResourceLeak)
  358. BT_ResourceLeak.reset(new BuiltinBug("Resource Leak",
  359. "Opened File never closed. Potential Resource leak."));
  360. BugReport *R = new BugReport(*BT_ResourceLeak,
  361. BT_ResourceLeak->getDescription(), N);
  362. C.emitReport(R);
  363. }
  364. }
  365. }
  366. }
  367. void ento::registerStreamChecker(CheckerManager &mgr) {
  368. mgr.registerChecker<StreamChecker>();
  369. }