blob: b253e67cffdeae6d4775a75d2bb3c80ff762b413 [file] [log] [blame]
Reka Kovacs88ad7042018-07-20 15:14:49 +00001//=== InnerPointerChecker.cpp -------------------------------------*- C++ -*--//
Reka Kovacs18775fc2018-06-09 13:03:49 +00002//
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//
Reka Kovacse453e602018-07-07 19:27:18 +000010// This file defines a check that marks a raw pointer to a C++ container's
11// inner buffer released when the object is destroyed. This information can
12// be used by MallocChecker to detect use-after-free problems.
Reka Kovacs18775fc2018-06-09 13:03:49 +000013//
14//===----------------------------------------------------------------------===//
15
Reka Kovacse453e602018-07-07 19:27:18 +000016#include "AllocationState.h"
Reka Kovacs18775fc2018-06-09 13:03:49 +000017#include "ClangSACheckers.h"
18#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19#include "clang/StaticAnalyzer/Core/BugReporter/CommonBugCategories.h"
20#include "clang/StaticAnalyzer/Core/Checker.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Reka Kovacs18775fc2018-06-09 13:03:49 +000023
24using namespace clang;
25using namespace ento;
26
Reka Kovacs5f70d9b2018-07-11 19:08:02 +000027using PtrSet = llvm::ImmutableSet<SymbolRef>;
28
29// Associate container objects with a set of raw pointer symbols.
30REGISTER_MAP_WITH_PROGRAMSTATE(RawPtrMap, const MemRegion *, PtrSet)
31
32// This is a trick to gain access to PtrSet's Factory.
33namespace clang {
34namespace ento {
Reka Kovacsc18ecc82018-07-19 15:10:06 +000035template <>
36struct ProgramStateTrait<PtrSet> : public ProgramStatePartialTrait<PtrSet> {
Reka Kovacs5f70d9b2018-07-11 19:08:02 +000037 static void *GDMIndex() {
38 static int Index = 0;
39 return &Index;
40 }
41};
42} // end namespace ento
43} // end namespace clang
Reka Kovacse453e602018-07-07 19:27:18 +000044
Reka Kovacs18775fc2018-06-09 13:03:49 +000045namespace {
46
Reka Kovacs88ad7042018-07-20 15:14:49 +000047class InnerPointerChecker
Reka Kovacse453e602018-07-07 19:27:18 +000048 : public Checker<check::DeadSymbols, check::PostCall> {
Reka Kovacsc18ecc82018-07-19 15:10:06 +000049
50 CallDescription AppendFn, AssignFn, ClearFn, CStrFn, DataFn, EraseFn,
51 InsertFn, PopBackFn, PushBackFn, ReplaceFn, ReserveFn, ResizeFn,
52 ShrinkToFitFn, SwapFn;
Reka Kovacs18775fc2018-06-09 13:03:49 +000053
54public:
Reka Kovacs88ad7042018-07-20 15:14:49 +000055 class InnerPointerBRVisitor : public BugReporterVisitor {
Reka Kovacse453e602018-07-07 19:27:18 +000056 SymbolRef PtrToBuf;
57
58 public:
Reka Kovacs88ad7042018-07-20 15:14:49 +000059 InnerPointerBRVisitor(SymbolRef Sym) : PtrToBuf(Sym) {}
Reka Kovacse453e602018-07-07 19:27:18 +000060
61 static void *getTag() {
62 static int Tag = 0;
63 return &Tag;
64 }
65
66 void Profile(llvm::FoldingSetNodeID &ID) const override {
67 ID.AddPointer(getTag());
68 }
69
70 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
71 const ExplodedNode *PrevN,
72 BugReporterContext &BRC,
73 BugReport &BR) override;
74
75 // FIXME: Scan the map once in the visitor's constructor and do a direct
76 // lookup by region.
77 bool isSymbolTracked(ProgramStateRef State, SymbolRef Sym) {
78 RawPtrMapTy Map = State->get<RawPtrMap>();
79 for (const auto Entry : Map) {
Reka Kovacs5f70d9b2018-07-11 19:08:02 +000080 if (Entry.second.contains(Sym))
Reka Kovacse453e602018-07-07 19:27:18 +000081 return true;
82 }
83 return false;
84 }
85 };
86
Reka Kovacs88ad7042018-07-20 15:14:49 +000087 InnerPointerChecker()
Reka Kovacsc18ecc82018-07-19 15:10:06 +000088 : AppendFn("append"), AssignFn("assign"), ClearFn("clear"),
89 CStrFn("c_str"), DataFn("data"), EraseFn("erase"), InsertFn("insert"),
90 PopBackFn("pop_back"), PushBackFn("push_back"), ReplaceFn("replace"),
91 ReserveFn("reserve"), ResizeFn("resize"),
92 ShrinkToFitFn("shrink_to_fit"), SwapFn("swap") {}
93
Reka Kovacsc74cfc42018-07-30 15:43:45 +000094 /// Check if the object of this member function call is a `basic_string`.
95 bool isCalledOnStringObject(const CXXInstanceCall *ICall) const;
Reka Kovacs18775fc2018-06-09 13:03:49 +000096
Reka Kovacsc74cfc42018-07-30 15:43:45 +000097 /// Check whether the called member function potentially invalidates
98 /// pointers referring to the container object's inner buffer.
99 bool isInvalidatingMemberFunction(const CallEvent &Call) const;
100
101 /// Mark pointer symbols associated with the given memory region released
102 /// in the program state.
103 void markPtrSymbolsReleased(const CallEvent &Call, ProgramStateRef State,
104 const MemRegion *ObjRegion,
105 CheckerContext &C) const;
106
107 /// Standard library functions that take a non-const `basic_string` argument by
108 /// reference may invalidate its inner pointers. Check for these cases and
109 /// mark the pointers released.
110 void checkFunctionArguments(const CallEvent &Call, ProgramStateRef State,
111 CheckerContext &C) const;
112
113 /// Record the connection between raw pointers referring to a container
114 /// object's inner buffer and the object's memory region in the program state.
115 /// Mark potentially invalidated pointers released.
Reka Kovacs18775fc2018-06-09 13:03:49 +0000116 void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000117
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000118 /// Clean up the program state map.
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000119 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Reka Kovacs18775fc2018-06-09 13:03:49 +0000120};
121
122} // end anonymous namespace
123
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000124bool InnerPointerChecker::isCalledOnStringObject(
125 const CXXInstanceCall *ICall) const {
126 const auto *ObjRegion =
127 dyn_cast_or_null<TypedValueRegion>(ICall->getCXXThisVal().getAsRegion());
128 if (!ObjRegion)
129 return false;
130
131 QualType ObjTy = ObjRegion->getValueType();
Reka Kovacs7d36e922018-08-02 22:19:57 +0000132 if (ObjTy.isNull())
133 return false;
134
135 CXXRecordDecl *Decl = ObjTy->getAsCXXRecordDecl();
136 if (!Decl || Decl->getName() != "basic_string")
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000137 return false;
138
139 return true;
140}
141
142bool InnerPointerChecker::isInvalidatingMemberFunction(
143 const CallEvent &Call) const {
Reka Kovacsc18ecc82018-07-19 15:10:06 +0000144 if (const auto *MemOpCall = dyn_cast<CXXMemberOperatorCall>(&Call)) {
145 OverloadedOperatorKind Opc = MemOpCall->getOriginExpr()->getOperator();
146 if (Opc == OO_Equal || Opc == OO_PlusEqual)
147 return true;
148 return false;
149 }
150 return (isa<CXXDestructorCall>(Call) || Call.isCalled(AppendFn) ||
151 Call.isCalled(AssignFn) || Call.isCalled(ClearFn) ||
152 Call.isCalled(EraseFn) || Call.isCalled(InsertFn) ||
153 Call.isCalled(PopBackFn) || Call.isCalled(PushBackFn) ||
154 Call.isCalled(ReplaceFn) || Call.isCalled(ReserveFn) ||
155 Call.isCalled(ResizeFn) || Call.isCalled(ShrinkToFitFn) ||
156 Call.isCalled(SwapFn));
157}
158
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000159void InnerPointerChecker::markPtrSymbolsReleased(const CallEvent &Call,
160 ProgramStateRef State,
161 const MemRegion *MR,
162 CheckerContext &C) const {
163 if (const PtrSet *PS = State->get<RawPtrMap>(MR)) {
164 const Expr *Origin = Call.getOriginExpr();
165 for (const auto Symbol : *PS) {
166 // NOTE: `Origin` may be null, and will be stored so in the symbol's
167 // `RefState` in MallocChecker's `RegionState` program state map.
168 State = allocation_state::markReleased(State, Symbol, Origin);
169 }
170 State = State->remove<RawPtrMap>(MR);
171 C.addTransition(State);
172 return;
173 }
174}
175
176void InnerPointerChecker::checkFunctionArguments(const CallEvent &Call,
177 ProgramStateRef State,
178 CheckerContext &C) const {
179 if (const auto *FC = dyn_cast<AnyFunctionCall>(&Call)) {
180 const FunctionDecl *FD = FC->getDecl();
181 if (!FD || !FD->isInStdNamespace())
182 return;
183
184 for (unsigned I = 0, E = FD->getNumParams(); I != E; ++I) {
185 QualType ParamTy = FD->getParamDecl(I)->getType();
186 if (!ParamTy->isReferenceType() ||
187 ParamTy->getPointeeType().isConstQualified())
188 continue;
189
190 // In case of member operator calls, `this` is counted as an
191 // argument but not as a parameter.
192 bool isaMemberOpCall = isa<CXXMemberOperatorCall>(FC);
193 unsigned ArgI = isaMemberOpCall ? I+1 : I;
194
195 SVal Arg = FC->getArgSVal(ArgI);
196 const auto *ArgRegion =
197 dyn_cast_or_null<TypedValueRegion>(Arg.getAsRegion());
198 if (!ArgRegion)
199 continue;
200
201 markPtrSymbolsReleased(Call, State, ArgRegion, C);
202 }
203 }
204}
205
206// [string.require]
207//
208// "References, pointers, and iterators referring to the elements of a
209// basic_string sequence may be invalidated by the following uses of that
210// basic_string object:
211//
212// -- As an argument to any standard library function taking a reference
213// to non-const basic_string as an argument. For example, as an argument to
214// non-member functions swap(), operator>>(), and getline(), or as an argument
215// to basic_string::swap().
216//
217// -- Calling non-const member functions, except operator[], at, front, back,
218// begin, rbegin, end, and rend."
219
Reka Kovacs88ad7042018-07-20 15:14:49 +0000220void InnerPointerChecker::checkPostCall(const CallEvent &Call,
221 CheckerContext &C) const {
Reka Kovacs18775fc2018-06-09 13:03:49 +0000222 ProgramStateRef State = C.getState();
223
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000224 if (const auto *ICall = dyn_cast<CXXInstanceCall>(&Call)) {
225 if (isCalledOnStringObject(ICall)) {
226 const auto *ObjRegion = dyn_cast_or_null<TypedValueRegion>(
227 ICall->getCXXThisVal().getAsRegion());
228
229 if (Call.isCalled(CStrFn) || Call.isCalled(DataFn)) {
230 SVal RawPtr = Call.getReturnValue();
231 if (SymbolRef Sym = RawPtr.getAsSymbol(/*IncludeBaseRegions=*/true)) {
232 // Start tracking this raw pointer by adding it to the set of symbols
233 // associated with this container object in the program state map.
234
235 PtrSet::Factory &F = State->getStateManager().get_context<PtrSet>();
236 const PtrSet *SetPtr = State->get<RawPtrMap>(ObjRegion);
237 PtrSet Set = SetPtr ? *SetPtr : F.getEmptySet();
238 assert(C.wasInlined || !Set.contains(Sym));
239 Set = F.add(Set, Sym);
240
241 State = State->set<RawPtrMap>(ObjRegion, Set);
242 C.addTransition(State);
243 }
244 return;
245 }
246
247 // Check [string.require] / second point.
248 if (isInvalidatingMemberFunction(Call)) {
249 markPtrSymbolsReleased(Call, State, ObjRegion, C);
250 return;
251 }
Reka Kovacs18775fc2018-06-09 13:03:49 +0000252 }
Reka Kovacs18775fc2018-06-09 13:03:49 +0000253 }
254
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000255 // Check [string.require] / first point.
256 checkFunctionArguments(Call, State, C);
Reka Kovacs18775fc2018-06-09 13:03:49 +0000257}
258
Reka Kovacs88ad7042018-07-20 15:14:49 +0000259void InnerPointerChecker::checkDeadSymbols(SymbolReaper &SymReaper,
260 CheckerContext &C) const {
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000261 ProgramStateRef State = C.getState();
Reka Kovacs5f70d9b2018-07-11 19:08:02 +0000262 PtrSet::Factory &F = State->getStateManager().get_context<PtrSet>();
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000263 RawPtrMapTy RPM = State->get<RawPtrMap>();
264 for (const auto Entry : RPM) {
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000265 if (!SymReaper.isLiveRegion(Entry.first)) {
Reka Kovacs5f70d9b2018-07-11 19:08:02 +0000266 // Due to incomplete destructor support, some dead regions might
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000267 // remain in the program state map. Clean them up.
268 State = State->remove<RawPtrMap>(Entry.first);
269 }
Reka Kovacs5f70d9b2018-07-11 19:08:02 +0000270 if (const PtrSet *OldSet = State->get<RawPtrMap>(Entry.first)) {
271 PtrSet CleanedUpSet = *OldSet;
272 for (const auto Symbol : Entry.second) {
273 if (!SymReaper.isLive(Symbol))
274 CleanedUpSet = F.remove(CleanedUpSet, Symbol);
275 }
276 State = CleanedUpSet.isEmpty()
Reka Kovacsc18ecc82018-07-19 15:10:06 +0000277 ? State->remove<RawPtrMap>(Entry.first)
278 : State->set<RawPtrMap>(Entry.first, CleanedUpSet);
Reka Kovacs5f70d9b2018-07-11 19:08:02 +0000279 }
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000280 }
281 C.addTransition(State);
282}
283
Reka Kovacse453e602018-07-07 19:27:18 +0000284std::shared_ptr<PathDiagnosticPiece>
Reka Kovacs88ad7042018-07-20 15:14:49 +0000285InnerPointerChecker::InnerPointerBRVisitor::VisitNode(const ExplodedNode *N,
286 const ExplodedNode *PrevN,
287 BugReporterContext &BRC,
288 BugReport &BR) {
Reka Kovacse453e602018-07-07 19:27:18 +0000289 if (!isSymbolTracked(N->getState(), PtrToBuf) ||
290 isSymbolTracked(PrevN->getState(), PtrToBuf))
291 return nullptr;
292
293 const Stmt *S = PathDiagnosticLocation::getStmt(N);
294 if (!S)
295 return nullptr;
296
297 SmallString<256> Buf;
298 llvm::raw_svector_ostream OS(Buf);
Reka Kovacsc18ecc82018-07-19 15:10:06 +0000299 OS << "Dangling inner pointer obtained here";
Reka Kovacse453e602018-07-07 19:27:18 +0000300 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
301 N->getLocationContext());
302 return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true,
303 nullptr);
304}
305
306namespace clang {
307namespace ento {
308namespace allocation_state {
309
Reka Kovacs88ad7042018-07-20 15:14:49 +0000310std::unique_ptr<BugReporterVisitor> getInnerPointerBRVisitor(SymbolRef Sym) {
311 return llvm::make_unique<InnerPointerChecker::InnerPointerBRVisitor>(Sym);
Reka Kovacse453e602018-07-07 19:27:18 +0000312}
313
314} // end namespace allocation_state
315} // end namespace ento
316} // end namespace clang
317
Reka Kovacs88ad7042018-07-20 15:14:49 +0000318void ento::registerInnerPointerChecker(CheckerManager &Mgr) {
Reka Kovacs18775fc2018-06-09 13:03:49 +0000319 registerNewDeleteChecker(Mgr);
Reka Kovacs88ad7042018-07-20 15:14:49 +0000320 Mgr.registerChecker<InnerPointerChecker>();
Reka Kovacs18775fc2018-06-09 13:03:49 +0000321}