blob: 1fd72b5ce259c1883d51dbea5ccccb9493a0645a [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();
Zhongxing Xu70154852010-06-16 05:52:03 +000083 DefinedSVal RetVal = cast<DefinedSVal>(ValMgr.getConjuredSymbolVal(0, CE,
84 Count));
Zhongxing Xuc1960952010-06-16 05:38:05 +000085 state = state->BindExpr(CE, RetVal);
86
87 ConstraintManager &CM = C.getConstraintManager();
88 // Bifurcate the state into two: one with a valid FILE* pointer, the other
89 // with a NULL.
90 const GRState *stateNotNull, *stateNull;
Zhongxing Xu70154852010-06-16 05:52:03 +000091 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, RetVal);
Zhongxing Xuc1960952010-06-16 05:38:05 +000092
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.
Zhongxing Xub3f40312010-06-16 05:56:39 +0000101 SVal StreamVal = state->getSVal(CE->getArg(3));
Zhongxing Xuc1960952010-06-16 05:38:05 +0000102
Zhongxing Xub3f40312010-06-16 05:56:39 +0000103 if (const DefinedSVal *DV = cast<DefinedSVal>(&StreamVal)) {
104 ConstraintManager &CM = C.getConstraintManager();
105 const GRState *stateNotNull, *stateNull;
106 llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, *DV);
Zhongxing Xuc1960952010-06-16 05:38:05 +0000107
Zhongxing Xub3f40312010-06-16 05:56:39 +0000108 if (!stateNotNull && stateNull) {
109 if (ExplodedNode *N = C.GenerateSink(stateNull)) {
110 if (!BT_nullfp)
111 BT_nullfp = new BuiltinBug("NULL stream pointer",
112 "Stream pointer might be NULL.");
113 BugReport *R =new BugReport(*BT_nullfp, BT_nullfp->getDescription(), N);
114 C.EmitReport(R);
115 }
Zhongxing Xuc1960952010-06-16 05:38:05 +0000116 }
117 }
118}