1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
//===- ObjCAutoreleaseWriteChecker.cpp ----------------------------*- C++ -*-==//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines ObjCAutoreleaseWriteChecker which warns against writes
// into autoreleased out parameters which are likely to cause crashes.
// An example of a problematic write is a write to {@code error} in the example
// below:
//
// - (BOOL) mymethod:(NSError *__autoreleasing *)error list:(NSArray*) list {
// [list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// NSString *myString = obj;
// if ([myString isEqualToString:@"error"] && error)
// *error = [NSError errorWithDomain:@"MyDomain" code:-1];
// }];
// return false;
// }
//
// Such code is very likely to crash due to the other queue autorelease pool
// begin able to free the error object.
//
//===----------------------------------------------------------------------===//
#include "ClangSACheckers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
#include "llvm/ADT/Twine.h"
using namespace clang;
using namespace ento;
using namespace ast_matchers;
namespace {
const char *ProblematicWriteBind = "problematicwrite";
const char *ParamBind = "parambind";
const char *MethodBind = "methodbind";
class ObjCAutoreleaseWriteChecker : public Checker<check::ASTCodeBody> {
public:
void checkASTCodeBody(const Decl *D,
AnalysisManager &AM,
BugReporter &BR) const;
private:
std::vector<std::string> SelectorsWithAutoreleasingPool = {
// Common to NSArray, NSSet, NSOrderedSet
"enumerateObjectsUsingBlock:",
"enumerateObjectsWithOptions:usingBlock:",
// Common to NSArray and NSOrderedSet
"enumerateObjectsAtIndexes:options:usingBlock:",
"indexOfObjectAtIndexes:options:passingTest:",
"indexesOfObjectsAtIndexes:options:passingTest:",
"indexOfObjectPassingTest:",
"indexOfObjectWithOptions:passingTest:",
"indexesOfObjectsPassingTest:",
"indexesOfObjectsWithOptions:passingTest:",
// NSDictionary
"enumerateKeysAndObjectsUsingBlock:",
"enumerateKeysAndObjectsWithOptions:usingBlock:",
"keysOfEntriesPassingTest:",
"keysOfEntriesWithOptions:passingTest:",
// NSSet
"objectsPassingTest:",
"objectsWithOptions:passingTest:",
"enumerateIndexPathsWithOptions:usingBlock:",
// NSIndexSet
"enumerateIndexesWithOptions:usingBlock:",
"enumerateIndexesUsingBlock:",
"enumerateIndexesInRange:options:usingBlock:",
"enumerateRangesUsingBlock:",
"enumerateRangesWithOptions:usingBlock:",
"enumerateRangesInRange:options:usingBlock:",
"indexPassingTest:",
"indexesPassingTest:",
"indexWithOptions:passingTest:",
"indexesWithOptions:passingTest:",
"indexInRange:options:passingTest:",
"indexesInRange:options:passingTest:"
};
std::vector<std::string> FunctionsWithAutoreleasingPool = {
"dispatch_async", "dispatch_group_async", "dispatch_barrier_async"};
};
}
static inline std::vector<llvm::StringRef> toRefs(std::vector<std::string> V) {
return std::vector<llvm::StringRef>(V.begin(), V.end());
}
static auto callsNames(std::vector<std::string> FunctionNames)
-> decltype(callee(functionDecl())) {
return callee(functionDecl(hasAnyName(toRefs(FunctionNames))));
}
static void emitDiagnostics(BoundNodes &Match, const Decl *D, BugReporter &BR,
AnalysisManager &AM,
const ObjCAutoreleaseWriteChecker *Checker) {
AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
const auto *PVD = Match.getNodeAs<ParmVarDecl>(ParamBind);
assert(PVD);
QualType Ty = PVD->getType();
if (Ty->getPointeeType().getObjCLifetime() != Qualifiers::OCL_Autoreleasing)
return;
const auto *SW = Match.getNodeAs<Expr>(ProblematicWriteBind);
bool IsMethod = Match.getNodeAs<ObjCMethodDecl>(MethodBind) != nullptr;
const char *Name = IsMethod ? "method" : "function";
assert(SW);
BR.EmitBasicReport(
ADC->getDecl(), Checker,
/*Name=*/"Write to autoreleasing out parameter inside autorelease pool",
/*Category=*/"Memory",
(llvm::Twine("Write to autoreleasing out parameter inside ") +
"autorelease pool that may exit before " + Name + " returns; consider "
"writing first to a strong local variable declared outside of the block")
.str(),
PathDiagnosticLocation::createBegin(SW, BR.getSourceManager(), ADC),
SW->getSourceRange());
}
void ObjCAutoreleaseWriteChecker::checkASTCodeBody(const Decl *D,
AnalysisManager &AM,
BugReporter &BR) const {
// Write into a binded object, e.g. *ParamBind = X.
auto WritesIntoM = binaryOperator(
hasLHS(unaryOperator(
hasOperatorName("*"),
hasUnaryOperand(
ignoringParenImpCasts(
declRefExpr(to(parmVarDecl(equalsBoundNode(ParamBind))))))
)),
hasOperatorName("=")
).bind(ProblematicWriteBind);
// WritesIntoM happens inside a block passed as an argument.
auto WritesInBlockM = hasAnyArgument(allOf(
hasType(hasCanonicalType(blockPointerType())),
forEachDescendant(WritesIntoM)
));
auto CallsAsyncM = stmt(anyOf(
callExpr(allOf(
callsNames(FunctionsWithAutoreleasingPool), WritesInBlockM)),
objcMessageExpr(allOf(
hasAnySelector(toRefs(SelectorsWithAutoreleasingPool)),
WritesInBlockM))
));
auto DoublePointerParamM =
parmVarDecl(hasType(pointerType(
pointee(hasCanonicalType(objcObjectPointerType())))))
.bind(ParamBind);
auto HasParamAndWritesAsyncM = allOf(
hasAnyParameter(DoublePointerParamM),
forEachDescendant(CallsAsyncM));
auto MatcherM = decl(anyOf(
objcMethodDecl(HasParamAndWritesAsyncM).bind(MethodBind),
functionDecl(HasParamAndWritesAsyncM)));
auto Matches = match(MatcherM, *D, AM.getASTContext());
for (BoundNodes Match : Matches)
emitDiagnostics(Match, D, BR, AM, this);
}
void ento::registerAutoreleaseWriteChecker(CheckerManager &Mgr) {
Mgr.registerChecker<ObjCAutoreleaseWriteChecker>();
}
|