Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/NonnullGlobalConstantsChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This checker adds an assumption that constant globals of certain types* are
10
//  non-null, as otherwise they generally do not convey any useful information.
11
//  The assumption is useful, as many framework use e. g. global const strings,
12
//  and the analyzer might not be able to infer the global value if the
13
//  definition is in a separate translation unit.
14
//  The following types (and their typedef aliases) are considered to be
15
//  non-null:
16
//   - `char* const`
17
//   - `const CFStringRef` from CoreFoundation
18
//   - `NSString* const` from Foundation
19
//   - `CFBooleanRef` from Foundation
20
//
21
//===----------------------------------------------------------------------===//
22
23
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
24
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
25
#include "clang/StaticAnalyzer/Core/Checker.h"
26
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
27
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
28
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
29
30
using namespace clang;
31
using namespace ento;
32
33
namespace {
34
35
class NonnullGlobalConstantsChecker : public Checker<check::Location> {
36
  mutable IdentifierInfo *NSStringII = nullptr;
37
  mutable IdentifierInfo *CFStringRefII = nullptr;
38
  mutable IdentifierInfo *CFBooleanRefII = nullptr;
39
40
public:
41
675
  NonnullGlobalConstantsChecker() {}
42
43
  void checkLocation(SVal l, bool isLoad, const Stmt *S,
44
                     CheckerContext &C) const;
45
46
private:
47
  void initIdentifierInfo(ASTContext &Ctx) const;
48
49
  bool isGlobalConstString(SVal V) const;
50
51
  bool isNonnullType(QualType Ty) const;
52
};
53
54
} // namespace
55
56
/// Lazily initialize cache for required identifier information.
57
175k
void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
58
175k
  if (NSStringII)
59
174k
    return;
60
602
61
602
  NSStringII = &Ctx.Idents.get("NSString");
62
602
  CFStringRefII = &Ctx.Idents.get("CFStringRef");
63
602
  CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
64
602
}
65
66
/// Add an assumption that const string-like globals are non-null.
67
void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
68
                                                 const Stmt *S,
69
175k
                                                 CheckerContext &C) const {
70
175k
  initIdentifierInfo(C.getASTContext());
71
175k
  if (!isLoad || 
!location.isValid()145k
)
72
30.1k
    return;
73
145k
74
145k
  ProgramStateRef State = C.getState();
75
145k
76
145k
  if (isGlobalConstString(location)) {
77
39
    SVal V = State->getSVal(location.castAs<Loc>());
78
39
    Optional<DefinedOrUnknownSVal> Constr = V.getAs<DefinedOrUnknownSVal>();
79
39
80
39
    if (Constr) {
81
39
82
39
      // Assume that the variable is non-null.
83
39
      ProgramStateRef OutputState = State->assume(*Constr, true);
84
39
      C.addTransition(OutputState);
85
39
    }
86
39
  }
87
145k
}
88
89
/// \param V loaded lvalue.
90
/// \return whether {@code val} is a string-like const global.
91
145k
bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
92
145k
  Optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
93
145k
  if (!RegionVal)
94
181
    return false;
95
145k
  auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
96
145k
  if (!Region)
97
41.4k
    return false;
98
103k
  const VarDecl *Decl = Region->getDecl();
99
103k
100
103k
  if (!Decl->hasGlobalStorage())
101
97.2k
    return false;
102
6.31k
103
6.31k
  QualType Ty = Decl->getType();
104
6.31k
  bool HasConst = Ty.isConstQualified();
105
6.31k
  if (isNonnullType(Ty) && 
HasConst4.60k
)
106
33
    return true;
107
6.28k
108
6.28k
  // Look through the typedefs.
109
6.54k
  
while (const Type *6.28k
T = Ty.getTypePtr()) {
110
6.54k
    if (const auto *TT = dyn_cast<TypedefType>(T)) {
111
270
      Ty = TT->getDecl()->getUnderlyingType();
112
270
      // It is sufficient for any intermediate typedef
113
270
      // to be classified const.
114
270
      HasConst = HasConst || 
Ty.isConstQualified()96
;
115
270
      if (isNonnullType(Ty) && 
HasConst4
)
116
4
        return true;
117
6.27k
    } else if (const auto *AT = dyn_cast<AttributedType>(T)) {
118
2
      if (AT->getAttrKind() == attr::TypeNonNull)
119
2
        return true;
120
0
      Ty = AT->getModifiedType();
121
6.27k
    } else {
122
6.27k
      return false;
123
6.27k
    }
124
6.54k
  }
125
6.28k
  
return false0
;
126
6.28k
}
127
128
/// \return whether {@code type} is extremely unlikely to be null
129
6.58k
bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
130
6.58k
131
6.58k
  if (Ty->isPointerType() && 
Ty->getPointeeType()->isCharType()5.20k
)
132
4.54k
    return true;
133
2.03k
134
2.03k
  if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
135
59
    return T->getInterfaceDecl() &&
136
59
      
T->getInterfaceDecl()->getIdentifier() == NSStringII54
;
137
1.97k
  } else if (auto *T = dyn_cast<TypedefType>(Ty)) {
138
298
    IdentifierInfo* II = T->getDecl()->getIdentifier();
139
298
    return II == CFStringRefII || 
II == CFBooleanRefII270
;
140
298
  }
141
1.67k
  return false;
142
1.67k
}
143
144
675
void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
145
675
  Mgr.registerChecker<NonnullGlobalConstantsChecker>();
146
675
}
147
148
676
bool ento::shouldRegisterNonnullGlobalConstantsChecker(const LangOptions &LO) {
149
676
  return true;
150
676
}