blob: 80c29a131e0d50a4cd4cc6cf9165c0c7e4e8af97 [file] [log] [blame]
Zhongxing Xuc1960952010-06-16 05:38:05 +00001//===-- 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
14#include "GRExprEngineExperimentalChecks.h"
15#include "clang/Checker/BugReporter/BugType.h"
16#include "clang/Checker/PathSensitive/CheckerVisitor.h"
17#include "clang/Checker/PathSensitive/GRState.h"
18#include "clang/Checker/PathSensitive/GRStateTrait.h"
19#include "clang/Checker/PathSensitive/SymbolManager.h"
20#include "llvm/ADT/ImmutableMap.h"
21
22using namespace clang;
23
24namespace {
25
26class StreamChecker : public CheckerVisitor<StreamChecker> {
27 IdentifierInfo *II_fopen, *II_fread;
28 BuiltinBug *BT_nullfp;
29
30public:
31 StreamChecker() : II_fopen(0), II_fread(0), BT_nullfp(0) {}
32
33 static void *getTag() {
34 static int x;
35 return &x;
36 }
37
38 virtual bool EvalCallExpr(CheckerContext &C, const CallExpr *CE);
39
40private:
41 void FOpen(CheckerContext &C, const CallExpr *CE);
42 void FRead(CheckerContext &C, const CallExpr *CE);
43};
44
45}
46
47void clang::RegisterStreamChecker(GRExprEngine &Eng) {
48 Eng.registerCheck(new StreamChecker());
49}
50
51bool StreamChecker::EvalCallExpr(CheckerContext &C, const CallExpr *CE) {
52 const GRState *state = C.getState();
53 const Expr *Callee = CE->getCallee();
54 SVal L = state->getSVal(Callee);
55 const FunctionDecl *FD = L.getAsFunctionDecl();
56 if (!FD)
57 return false;
58
59 ASTContext &Ctx = C.getASTContext();
60 if (!II_fopen)
61 II_fopen = &Ctx.Idents.get("fopen");
62
63 if (!II_fread)
64 II_fread = &Ctx.Idents.get("fread");
65
66 if (FD->getIdentifier() == II_fopen) {
67 FOpen(C, CE);
68 return true;
69 }
70
71 if (FD->getIdentifier() == II_fread) {
72 FRead(C, CE);
73 return true;
74 }
75
76 return false;
77}
78
79void StreamChecker::FOpen(CheckerContext &C, const CallExpr *CE) {
80 const GRState *state = C.getState();
81 unsigned Count = C.getNodeBuilder().getCurrentBlockCount();
82 ValueManager &ValMgr = C.getValueManager();
83 SVal RetVal = ValMgr.getConjuredSymbolVal(0, CE, Count);
84 state = state->BindExpr(CE, RetVal);
85
86 ConstraintManager &CM = C.getConstraintManager();
87 // Bifurcate the state into two: one with a valid FILE* pointer, the other
88 // with a NULL.
89 const GRState *stateNotNull, *stateNull;
90 llvm::tie(stateNotNull, stateNull)
91 = CM.AssumeDual(state, cast<DefinedSVal>(RetVal));
92
93 C.addTransition(stateNotNull);
94 C.addTransition(stateNull);
95}
96
97void StreamChecker::FRead(CheckerContext &C, const CallExpr *CE) {
98 const GRState *state = C.getState();
99
100 // Assume CallAndMessageChecker has been run.
101 const DefinedSVal &StreamVal=cast<DefinedSVal>(state->getSVal(CE->getArg(3)));
102
103 ConstraintManager &CM = C.getConstraintManager();
104 const GRState *stateNotNull, *stateNull;
105 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, StreamVal);
106
107 if (!stateNotNull && stateNull) {
108 if (ExplodedNode *N = C.GenerateSink(stateNull)) {
109 if (!BT_nullfp)
110 BT_nullfp = new BuiltinBug("NULL stream pointer",
111 "Stream pointer might be NULL.");
112 BugReport *R = new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
113 C.EmitReport(R);
114 }
115 }
116}