StreamChecker.cpp 15 KB

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