blob: 926a1d7d8420e17449565273b50f91a410e605a9 [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"
Reka Kovacsd9f66ba2018-08-06 22:03:42 +000018#include "InterCheckerAPI.h"
Reka Kovacs18775fc2018-06-09 13:03:49 +000019#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
20#include "clang/StaticAnalyzer/Core/BugReporter/CommonBugCategories.h"
21#include "clang/StaticAnalyzer/Core/Checker.h"
22#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
23#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Reka Kovacs18775fc2018-06-09 13:03:49 +000024
25using namespace clang;
26using namespace ento;
27
Reka Kovacs5f70d9b2018-07-11 19:08:02 +000028using PtrSet = llvm::ImmutableSet<SymbolRef>;
29
30// Associate container objects with a set of raw pointer symbols.
31REGISTER_MAP_WITH_PROGRAMSTATE(RawPtrMap, const MemRegion *, PtrSet)
32
33// This is a trick to gain access to PtrSet's Factory.
34namespace clang {
35namespace ento {
Reka Kovacsc18ecc82018-07-19 15:10:06 +000036template <>
37struct ProgramStateTrait<PtrSet> : public ProgramStatePartialTrait<PtrSet> {
Reka Kovacs5f70d9b2018-07-11 19:08:02 +000038 static void *GDMIndex() {
39 static int Index = 0;
40 return &Index;
41 }
42};
43} // end namespace ento
44} // end namespace clang
Reka Kovacse453e602018-07-07 19:27:18 +000045
Reka Kovacs18775fc2018-06-09 13:03:49 +000046namespace {
47
Reka Kovacs88ad7042018-07-20 15:14:49 +000048class InnerPointerChecker
Reka Kovacse453e602018-07-07 19:27:18 +000049 : public Checker<check::DeadSymbols, check::PostCall> {
Reka Kovacsc18ecc82018-07-19 15:10:06 +000050
51 CallDescription AppendFn, AssignFn, ClearFn, CStrFn, DataFn, EraseFn,
52 InsertFn, PopBackFn, PushBackFn, ReplaceFn, ReserveFn, ResizeFn,
53 ShrinkToFitFn, SwapFn;
Reka Kovacs18775fc2018-06-09 13:03:49 +000054
55public:
Reka Kovacs88ad7042018-07-20 15:14:49 +000056 class InnerPointerBRVisitor : public BugReporterVisitor {
Reka Kovacse453e602018-07-07 19:27:18 +000057 SymbolRef PtrToBuf;
58
59 public:
Reka Kovacs88ad7042018-07-20 15:14:49 +000060 InnerPointerBRVisitor(SymbolRef Sym) : PtrToBuf(Sym) {}
Reka Kovacse453e602018-07-07 19:27:18 +000061
62 static void *getTag() {
63 static int Tag = 0;
64 return &Tag;
65 }
66
67 void Profile(llvm::FoldingSetNodeID &ID) const override {
68 ID.AddPointer(getTag());
69 }
70
71 std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
72 const ExplodedNode *PrevN,
73 BugReporterContext &BRC,
74 BugReport &BR) override;
75
76 // FIXME: Scan the map once in the visitor's constructor and do a direct
77 // lookup by region.
78 bool isSymbolTracked(ProgramStateRef State, SymbolRef Sym) {
79 RawPtrMapTy Map = State->get<RawPtrMap>();
80 for (const auto Entry : Map) {
Reka Kovacs5f70d9b2018-07-11 19:08:02 +000081 if (Entry.second.contains(Sym))
Reka Kovacse453e602018-07-07 19:27:18 +000082 return true;
83 }
84 return false;
85 }
86 };
87
Reka Kovacs88ad7042018-07-20 15:14:49 +000088 InnerPointerChecker()
Reka Kovacsc18ecc82018-07-19 15:10:06 +000089 : AppendFn("append"), AssignFn("assign"), ClearFn("clear"),
90 CStrFn("c_str"), DataFn("data"), EraseFn("erase"), InsertFn("insert"),
91 PopBackFn("pop_back"), PushBackFn("push_back"), ReplaceFn("replace"),
92 ReserveFn("reserve"), ResizeFn("resize"),
93 ShrinkToFitFn("shrink_to_fit"), SwapFn("swap") {}
94
Reka Kovacsc74cfc42018-07-30 15:43:45 +000095 /// Check if the object of this member function call is a `basic_string`.
96 bool isCalledOnStringObject(const CXXInstanceCall *ICall) const;
Reka Kovacs18775fc2018-06-09 13:03:49 +000097
Reka Kovacsc74cfc42018-07-30 15:43:45 +000098 /// Check whether the called member function potentially invalidates
99 /// pointers referring to the container object's inner buffer.
100 bool isInvalidatingMemberFunction(const CallEvent &Call) const;
101
102 /// Mark pointer symbols associated with the given memory region released
103 /// in the program state.
104 void markPtrSymbolsReleased(const CallEvent &Call, ProgramStateRef State,
105 const MemRegion *ObjRegion,
106 CheckerContext &C) const;
107
108 /// Standard library functions that take a non-const `basic_string` argument by
109 /// reference may invalidate its inner pointers. Check for these cases and
110 /// mark the pointers released.
111 void checkFunctionArguments(const CallEvent &Call, ProgramStateRef State,
112 CheckerContext &C) const;
113
114 /// Record the connection between raw pointers referring to a container
115 /// object's inner buffer and the object's memory region in the program state.
116 /// Mark potentially invalidated pointers released.
Reka Kovacs18775fc2018-06-09 13:03:49 +0000117 void checkPostCall(const CallEvent &Call, CheckerContext &C) const;
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000118
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000119 /// Clean up the program state map.
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000120 void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const;
Reka Kovacs18775fc2018-06-09 13:03:49 +0000121};
122
123} // end anonymous namespace
124
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000125bool InnerPointerChecker::isCalledOnStringObject(
126 const CXXInstanceCall *ICall) const {
127 const auto *ObjRegion =
128 dyn_cast_or_null<TypedValueRegion>(ICall->getCXXThisVal().getAsRegion());
129 if (!ObjRegion)
130 return false;
131
132 QualType ObjTy = ObjRegion->getValueType();
Reka Kovacs7d36e922018-08-02 22:19:57 +0000133 if (ObjTy.isNull())
134 return false;
135
136 CXXRecordDecl *Decl = ObjTy->getAsCXXRecordDecl();
Reka Kovacsbfd9cfd2018-08-03 20:42:02 +0000137 return Decl && Decl->getName() == "basic_string";
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000138}
139
140bool InnerPointerChecker::isInvalidatingMemberFunction(
141 const CallEvent &Call) const {
Reka Kovacsc18ecc82018-07-19 15:10:06 +0000142 if (const auto *MemOpCall = dyn_cast<CXXMemberOperatorCall>(&Call)) {
143 OverloadedOperatorKind Opc = MemOpCall->getOriginExpr()->getOperator();
144 if (Opc == OO_Equal || Opc == OO_PlusEqual)
145 return true;
146 return false;
147 }
148 return (isa<CXXDestructorCall>(Call) || Call.isCalled(AppendFn) ||
149 Call.isCalled(AssignFn) || Call.isCalled(ClearFn) ||
150 Call.isCalled(EraseFn) || Call.isCalled(InsertFn) ||
151 Call.isCalled(PopBackFn) || Call.isCalled(PushBackFn) ||
152 Call.isCalled(ReplaceFn) || Call.isCalled(ReserveFn) ||
153 Call.isCalled(ResizeFn) || Call.isCalled(ShrinkToFitFn) ||
154 Call.isCalled(SwapFn));
155}
156
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000157void InnerPointerChecker::markPtrSymbolsReleased(const CallEvent &Call,
158 ProgramStateRef State,
159 const MemRegion *MR,
160 CheckerContext &C) const {
161 if (const PtrSet *PS = State->get<RawPtrMap>(MR)) {
162 const Expr *Origin = Call.getOriginExpr();
163 for (const auto Symbol : *PS) {
164 // NOTE: `Origin` may be null, and will be stored so in the symbol's
165 // `RefState` in MallocChecker's `RegionState` program state map.
166 State = allocation_state::markReleased(State, Symbol, Origin);
167 }
168 State = State->remove<RawPtrMap>(MR);
169 C.addTransition(State);
170 return;
171 }
172}
173
174void InnerPointerChecker::checkFunctionArguments(const CallEvent &Call,
175 ProgramStateRef State,
176 CheckerContext &C) const {
177 if (const auto *FC = dyn_cast<AnyFunctionCall>(&Call)) {
178 const FunctionDecl *FD = FC->getDecl();
179 if (!FD || !FD->isInStdNamespace())
180 return;
181
182 for (unsigned I = 0, E = FD->getNumParams(); I != E; ++I) {
183 QualType ParamTy = FD->getParamDecl(I)->getType();
184 if (!ParamTy->isReferenceType() ||
185 ParamTy->getPointeeType().isConstQualified())
186 continue;
187
188 // In case of member operator calls, `this` is counted as an
189 // argument but not as a parameter.
190 bool isaMemberOpCall = isa<CXXMemberOperatorCall>(FC);
191 unsigned ArgI = isaMemberOpCall ? I+1 : I;
192
193 SVal Arg = FC->getArgSVal(ArgI);
194 const auto *ArgRegion =
195 dyn_cast_or_null<TypedValueRegion>(Arg.getAsRegion());
196 if (!ArgRegion)
197 continue;
198
199 markPtrSymbolsReleased(Call, State, ArgRegion, C);
200 }
201 }
202}
203
204// [string.require]
205//
206// "References, pointers, and iterators referring to the elements of a
207// basic_string sequence may be invalidated by the following uses of that
208// basic_string object:
209//
210// -- As an argument to any standard library function taking a reference
211// to non-const basic_string as an argument. For example, as an argument to
212// non-member functions swap(), operator>>(), and getline(), or as an argument
213// to basic_string::swap().
214//
215// -- Calling non-const member functions, except operator[], at, front, back,
216// begin, rbegin, end, and rend."
217
Reka Kovacs88ad7042018-07-20 15:14:49 +0000218void InnerPointerChecker::checkPostCall(const CallEvent &Call,
219 CheckerContext &C) const {
Reka Kovacs18775fc2018-06-09 13:03:49 +0000220 ProgramStateRef State = C.getState();
221
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000222 if (const auto *ICall = dyn_cast<CXXInstanceCall>(&Call)) {
223 if (isCalledOnStringObject(ICall)) {
224 const auto *ObjRegion = dyn_cast_or_null<TypedValueRegion>(
225 ICall->getCXXThisVal().getAsRegion());
226
227 if (Call.isCalled(CStrFn) || Call.isCalled(DataFn)) {
228 SVal RawPtr = Call.getReturnValue();
229 if (SymbolRef Sym = RawPtr.getAsSymbol(/*IncludeBaseRegions=*/true)) {
230 // Start tracking this raw pointer by adding it to the set of symbols
231 // associated with this container object in the program state map.
232
233 PtrSet::Factory &F = State->getStateManager().get_context<PtrSet>();
234 const PtrSet *SetPtr = State->get<RawPtrMap>(ObjRegion);
235 PtrSet Set = SetPtr ? *SetPtr : F.getEmptySet();
236 assert(C.wasInlined || !Set.contains(Sym));
237 Set = F.add(Set, Sym);
238
239 State = State->set<RawPtrMap>(ObjRegion, Set);
240 C.addTransition(State);
241 }
242 return;
243 }
244
245 // Check [string.require] / second point.
246 if (isInvalidatingMemberFunction(Call)) {
247 markPtrSymbolsReleased(Call, State, ObjRegion, C);
248 return;
249 }
Reka Kovacs18775fc2018-06-09 13:03:49 +0000250 }
Reka Kovacs18775fc2018-06-09 13:03:49 +0000251 }
252
Reka Kovacsc74cfc42018-07-30 15:43:45 +0000253 // Check [string.require] / first point.
254 checkFunctionArguments(Call, State, C);
Reka Kovacs18775fc2018-06-09 13:03:49 +0000255}
256
Reka Kovacs88ad7042018-07-20 15:14:49 +0000257void InnerPointerChecker::checkDeadSymbols(SymbolReaper &SymReaper,
258 CheckerContext &C) const {
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000259 ProgramStateRef State = C.getState();
Reka Kovacs5f70d9b2018-07-11 19:08:02 +0000260 PtrSet::Factory &F = State->getStateManager().get_context<PtrSet>();
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000261 RawPtrMapTy RPM = State->get<RawPtrMap>();
262 for (const auto Entry : RPM) {
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000263 if (!SymReaper.isLiveRegion(Entry.first)) {
Reka Kovacs5f70d9b2018-07-11 19:08:02 +0000264 // Due to incomplete destructor support, some dead regions might
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000265 // remain in the program state map. Clean them up.
266 State = State->remove<RawPtrMap>(Entry.first);
267 }
Reka Kovacs5f70d9b2018-07-11 19:08:02 +0000268 if (const PtrSet *OldSet = State->get<RawPtrMap>(Entry.first)) {
269 PtrSet CleanedUpSet = *OldSet;
270 for (const auto Symbol : Entry.second) {
271 if (!SymReaper.isLive(Symbol))
272 CleanedUpSet = F.remove(CleanedUpSet, Symbol);
273 }
274 State = CleanedUpSet.isEmpty()
Reka Kovacsc18ecc82018-07-19 15:10:06 +0000275 ? State->remove<RawPtrMap>(Entry.first)
276 : State->set<RawPtrMap>(Entry.first, CleanedUpSet);
Reka Kovacs5f70d9b2018-07-11 19:08:02 +0000277 }
Reka Kovacs7ff6a8a2018-06-09 21:08:27 +0000278 }
279 C.addTransition(State);
280}
281
Reka Kovacsbb2749a2018-08-10 23:56:57 +0000282namespace clang {
283namespace ento {
284namespace allocation_state {
285
286std::unique_ptr<BugReporterVisitor> getInnerPointerBRVisitor(SymbolRef Sym) {
287 return llvm::make_unique<InnerPointerChecker::InnerPointerBRVisitor>(Sym);
288}
289
290const MemRegion *getContainerObjRegion(ProgramStateRef State, SymbolRef Sym) {
291 RawPtrMapTy Map = State->get<RawPtrMap>();
292 for (const auto Entry : Map) {
293 if (Entry.second.contains(Sym)) {
294 return Entry.first;
295 }
296 }
297 return nullptr;
298}
299
300} // end namespace allocation_state
301} // end namespace ento
302} // end namespace clang
303
Reka Kovacse453e602018-07-07 19:27:18 +0000304std::shared_ptr<PathDiagnosticPiece>
Reka Kovacs88ad7042018-07-20 15:14:49 +0000305InnerPointerChecker::InnerPointerBRVisitor::VisitNode(const ExplodedNode *N,
306 const ExplodedNode *PrevN,
307 BugReporterContext &BRC,
308 BugReport &BR) {
Reka Kovacse453e602018-07-07 19:27:18 +0000309 if (!isSymbolTracked(N->getState(), PtrToBuf) ||
310 isSymbolTracked(PrevN->getState(), PtrToBuf))
311 return nullptr;
312
313 const Stmt *S = PathDiagnosticLocation::getStmt(N);
314 if (!S)
315 return nullptr;
316
Reka Kovacsbb2749a2018-08-10 23:56:57 +0000317 const MemRegion *ObjRegion =
318 allocation_state::getContainerObjRegion(N->getState(), PtrToBuf);
319 const auto *TypedRegion = cast<TypedValueRegion>(ObjRegion);
320 QualType ObjTy = TypedRegion->getValueType();
321
Reka Kovacse453e602018-07-07 19:27:18 +0000322 SmallString<256> Buf;
323 llvm::raw_svector_ostream OS(Buf);
Reka Kovacsbb2749a2018-08-10 23:56:57 +0000324 OS << "Pointer to inner buffer of '" << ObjTy.getAsString()
325 << "' obtained here";
Reka Kovacse453e602018-07-07 19:27:18 +0000326 PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
327 N->getLocationContext());
328 return std::make_shared<PathDiagnosticEventPiece>(Pos, OS.str(), true,
329 nullptr);
330}
331
Reka Kovacs88ad7042018-07-20 15:14:49 +0000332void ento::registerInnerPointerChecker(CheckerManager &Mgr) {
Reka Kovacsd9f66ba2018-08-06 22:03:42 +0000333 registerInnerPointerCheckerAux(Mgr);
Reka Kovacs88ad7042018-07-20 15:14:49 +0000334 Mgr.registerChecker<InnerPointerChecker>();
Reka Kovacs18775fc2018-06-09 13:03:49 +0000335}