blob: 6fdad5d2a40781e9f74d0e416c08a1878aa8a2b2 [file] [log] [blame]
Ted Kremenekbc3a0212009-10-30 17:24:47 +00001//== NullDerefChecker.cpp - Null dereference checker ------------*- 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 defines NullDerefChecker, a builtin check in GRExprEngine that performs
11// checks for null pointers at loads and stores.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekdc998c12009-11-03 18:41:06 +000015#include "clang/Analysis/PathSensitive/Checkers/DereferenceChecker.h"
Ted Kremenekb4b817d2009-11-11 03:26:34 +000016#include "clang/Analysis/PathSensitive/Checker.h"
Ted Kremenekbc3a0212009-10-30 17:24:47 +000017#include "clang/Analysis/PathSensitive/GRExprEngine.h"
18#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekb4b817d2009-11-11 03:26:34 +000019#include "GRExprEngineInternalChecks.h"
Ted Kremenekbc3a0212009-10-30 17:24:47 +000020
21using namespace clang;
22
Ted Kremenekb4b817d2009-11-11 03:26:34 +000023namespace {
24class VISIBILITY_HIDDEN DereferenceChecker : public Checker {
25 BuiltinBug *BT_null;
26 BuiltinBug *BT_undef;
27 llvm::SmallVector<ExplodedNode*, 2> ImplicitNullDerefNodes;
28public:
29 DereferenceChecker() : BT_null(0), BT_undef(0) {}
30 static void *getTag() { static int tag = 0; return &tag; }
31 void VisitLocation(CheckerContext &C, const Stmt *S, SVal location);
32
33 std::pair<ExplodedNode * const*, ExplodedNode * const*>
34 getImplicitNodes() const {
35 return std::make_pair(ImplicitNullDerefNodes.data(),
36 ImplicitNullDerefNodes.data() +
37 ImplicitNullDerefNodes.size());
38 }
39};
40} // end anonymous namespace
41
42void clang::RegisterDereferenceChecker(GRExprEngine &Eng) {
43 Eng.registerCheck(new DereferenceChecker());
Ted Kremenekbc3a0212009-10-30 17:24:47 +000044}
45
Ted Kremenekb4b817d2009-11-11 03:26:34 +000046std::pair<ExplodedNode * const *, ExplodedNode * const *>
47clang::GetImplicitNullDereferences(GRExprEngine &Eng) {
48 DereferenceChecker *checker = Eng.getChecker<DereferenceChecker>();
49 if (!checker)
50 return std::make_pair((ExplodedNode * const *) 0,
51 (ExplodedNode * const *) 0);
52 return checker->getImplicitNodes();
53}
54
55void DereferenceChecker::VisitLocation(CheckerContext &C, const Stmt *S,
56 SVal l) {
57 // Check for dereference of an undefined value.
58 if (l.isUndef()) {
59 ExplodedNode *N = C.GenerateNode(S, true);
Ted Kremenekbc3a0212009-10-30 17:24:47 +000060 if (N) {
Ted Kremenekb4b817d2009-11-11 03:26:34 +000061 if (!BT_undef)
62 BT_undef = new BuiltinBug("Dereference of undefined pointer value");
Ted Kremenekbc3a0212009-10-30 17:24:47 +000063
Ted Kremenekb4b817d2009-11-11 03:26:34 +000064 EnhancedBugReport *report =
65 new EnhancedBugReport(*BT_undef, BT_undef->getDescription().c_str(), N);
66 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
67 bugreporter::GetDerefExpr(N));
68 C.EmitReport(report);
69 }
70 return;
71 }
72
73 DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(l);
74
75 // Check for null dereferences.
76 if (!isa<Loc>(location))
77 return;
78
79 const GRState *state = C.getState();
80 const GRState *notNullState, *nullState;
81 llvm::tie(notNullState, nullState) = state->Assume(location);
82
83 // The explicit NULL case.
84 if (nullState) {
85 // Generate an error node.
86 ExplodedNode *N = C.GenerateNode(S, nullState, true);
87 if (N) {
88 if (!notNullState) {
89 // We know that 'location' cannot be non-null. This is what
90 // we call an "explicit" null dereference.
91 if (!BT_null)
92 BT_null = new BuiltinBug("Null pointer dereference",
93 "Dereference of null pointer");
Ted Kremenekbc3a0212009-10-30 17:24:47 +000094
Ted Kremenekb4b817d2009-11-11 03:26:34 +000095 EnhancedBugReport *report =
96 new EnhancedBugReport(*BT_null, BT_null->getDescription().c_str(), N);
97 report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue,
98 bugreporter::GetDerefExpr(N));
99
100 C.EmitReport(report);
101 return;
102 }
Ted Kremenek2c791bd2009-11-06 00:44:32 +0000103
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000104 // Otherwise, we have the case where the location could either be
105 // null or not-null. Record the error node as an "implicit" null
106 // dereference.
107 ImplicitNullDerefNodes.push_back(N);
Ted Kremenekbc3a0212009-10-30 17:24:47 +0000108 }
109 }
110
Ted Kremenekb4b817d2009-11-11 03:26:34 +0000111 // From this point forward, we know that the location is not null.
112 assert(notNullState);
113 C.addTransition(state != nullState ? C.GenerateNode(S, notNullState) :
114 C.getPredecessor());
Ted Kremenekbc3a0212009-10-30 17:24:47 +0000115}