Skip to content

Commit 4d6fb57

Browse files
committed
Revert "[analyzer] Introduce a simplified API for adding custom path notes."
This reverts commit r357323. ASan leaks found by a buildbot :) Differential Revision: https://reviews.llvm.org/D58367 llvm-svn: 357332
1 parent b55637b commit 4d6fb57

File tree

10 files changed

+40
-138
lines changed

10 files changed

+40
-138
lines changed

clang/include/clang/Analysis/ProgramPoint.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ class ProgramPointTag {
4242
virtual ~ProgramPointTag();
4343
virtual StringRef getTagDescription() const = 0;
4444

45+
protected:
4546
/// Used to implement 'isKind' in subclasses.
46-
const void *getTagKind() const { return TagKind; }
47+
const void *getTagKind() { return TagKind; }
4748

4849
private:
49-
const void *const TagKind;
50+
const void *TagKind;
5051
};
5152

5253
class SimpleProgramPointTag : public ProgramPointTag {

clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporter.h

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -592,60 +592,6 @@ class BugReporterContext {
592592
NodeMapClosure& getNodeResolver() { return NMC; }
593593
};
594594

595-
596-
/// The tag upon which the TagVisitor reacts. Add these in order to display
597-
/// additional PathDiagnosticEventPieces along the path.
598-
class NoteTag : public ProgramPointTag {
599-
public:
600-
using Callback =
601-
std::function<std::string(BugReporterContext &, BugReport &)>;
602-
603-
private:
604-
static int Kind;
605-
606-
const Callback Cb;
607-
608-
NoteTag(Callback &&Cb) : ProgramPointTag(&Kind), Cb(std::move(Cb)) {}
609-
610-
public:
611-
static bool classof(const ProgramPointTag *T) {
612-
return T->getTagKind() == &Kind;
613-
}
614-
615-
Optional<std::string> generateMessage(BugReporterContext &BRC,
616-
BugReport &R) const {
617-
std::string Msg = Cb(BRC, R);
618-
if (Msg.empty())
619-
return None;
620-
621-
return std::move(Msg);
622-
}
623-
624-
StringRef getTagDescription() const override {
625-
// TODO: Remember a few examples of generated messages
626-
// and display them in the ExplodedGraph dump by
627-
// returning them from this function.
628-
return "Note Tag";
629-
}
630-
631-
// Manage memory for NoteTag objects.
632-
class Factory {
633-
llvm::BumpPtrAllocator &Alloc;
634-
635-
public:
636-
Factory(llvm::BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
637-
638-
const NoteTag *makeNoteTag(Callback &&Cb) {
639-
// We cannot use make_unique because we cannot access the private
640-
// constructor from inside it.
641-
NoteTag *Tag = Alloc.Allocate<NoteTag>();
642-
return new (Tag) NoteTag(std::move(Cb));
643-
}
644-
};
645-
646-
friend class TagVisitor;
647-
};
648-
649595
} // namespace ento
650596

651597
} // namespace clang

clang/include/clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#ifndef LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGREPORTERVISITORS_H
1515
#define LLVM_CLANG_STATICANALYZER_CORE_BUGREPORTER_BUGREPORTERVISITORS_H
1616

17-
#include "clang/Analysis/ProgramPoint.h"
1817
#include "clang/Basic/LLVM.h"
1918
#include "clang/StaticAnalyzer/Core/PathSensitive/RangedConstraintManager.h"
2019
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
@@ -329,17 +328,6 @@ class FalsePositiveRefutationBRVisitor final : public BugReporterVisitor {
329328
BugReport &BR) override;
330329
};
331330

332-
333-
/// The visitor detects NoteTags and displays the event notes they contain.
334-
class TagVisitor : public BugReporterVisitor {
335-
public:
336-
void Profile(llvm::FoldingSetNodeID &ID) const override;
337-
338-
std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
339-
BugReporterContext &BRC,
340-
BugReport &R) override;
341-
};
342-
343331
namespace bugreporter {
344332

345333
/// Attempts to add visitors to track expression value back to its point of

clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,24 +219,6 @@ class CheckerContext {
219219
Eng.getBugReporter().emitReport(std::move(R));
220220
}
221221

222-
223-
/// Produce a program point tag that displays an additional path note
224-
/// to the user. This is a lightweight alternative to the
225-
/// BugReporterVisitor mechanism: instead of visiting the bug report
226-
/// node-by-node to restore the sequence of events that led to discovering
227-
/// a bug, you can add notes as you add your transitions.
228-
const NoteTag *getNoteTag(NoteTag::Callback &&Cb) {
229-
return Eng.getNoteTags().makeNoteTag(std::move(Cb));
230-
}
231-
232-
/// A shorthand version of getNoteTag that doesn't require you to accept
233-
/// the BugReporterContext arguments when you don't need it.
234-
const NoteTag *getNoteTag(std::function<std::string(BugReport &)> &&Cb) {
235-
return getNoteTag(
236-
[Cb](BugReporterContext &, BugReport &BR) { return Cb(BR); });
237-
}
238-
239-
240222
/// Returns the word that should be used to refer to the declaration
241223
/// in the report.
242224
StringRef getDeclDescription(const Decl *D);

clang/include/clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "clang/Analysis/ProgramPoint.h"
2323
#include "clang/Basic/LLVM.h"
2424
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
25-
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporterVisitors.h"
2625
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
2726
#include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
2827
#include "clang/StaticAnalyzer/Core/PathSensitive/FunctionSummary.h"
@@ -156,8 +155,6 @@ class ExprEngine : public SubEngine {
156155
/// The flag, which specifies the mode of inlining for the engine.
157156
InliningModes HowToInline;
158157

159-
NoteTag::Factory NoteTags;
160-
161158
public:
162159
ExprEngine(cross_tu::CrossTranslationUnitContext &CTU, AnalysisManager &mgr,
163160
SetOfConstDecls *VisitedCalleesIn,
@@ -399,8 +396,6 @@ class ExprEngine : public SubEngine {
399396
SymbolManager &getSymbolManager() { return SymMgr; }
400397
MemRegionManager &getRegionManager() { return MRMgr; }
401398

402-
NoteTag::Factory &getNoteTags() { return NoteTags; }
403-
404399

405400
// Functions for external checking of whether we have unfinished work
406401
bool wasBlocksExhausted() const { return Engine.wasBlocksExhausted(); }

clang/lib/StaticAnalyzer/Checkers/MIGChecker.cpp

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,43 @@ class MIGChecker : public Checker<check::PostCall, check::PreStmt<ReturnStmt>,
8080
checkReturnAux(RS, C);
8181
}
8282

83+
class Visitor : public BugReporterVisitor {
84+
public:
85+
void Profile(llvm::FoldingSetNodeID &ID) const {
86+
static int X = 0;
87+
ID.AddPointer(&X);
88+
}
89+
90+
std::shared_ptr<PathDiagnosticPiece> VisitNode(const ExplodedNode *N,
91+
BugReporterContext &BRC, BugReport &R);
92+
};
8393
};
8494
} // end anonymous namespace
8595

86-
REGISTER_TRAIT_WITH_PROGRAMSTATE(ReleasedParameter, bool)
96+
// FIXME: It's a 'const ParmVarDecl *' but there's no ready-made GDM traits
97+
// specialization for this sort of types.
98+
REGISTER_TRAIT_WITH_PROGRAMSTATE(ReleasedParameter, const void *)
99+
100+
std::shared_ptr<PathDiagnosticPiece>
101+
MIGChecker::Visitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
102+
BugReport &R) {
103+
const auto *NewPVD = static_cast<const ParmVarDecl *>(
104+
N->getState()->get<ReleasedParameter>());
105+
const auto *OldPVD = static_cast<const ParmVarDecl *>(
106+
N->getFirstPred()->getState()->get<ReleasedParameter>());
107+
if (OldPVD == NewPVD)
108+
return nullptr;
109+
110+
assert(NewPVD && "What is deallocated cannot be un-deallocated!");
111+
SmallString<64> Str;
112+
llvm::raw_svector_ostream OS(Str);
113+
OS << "Value passed through parameter '" << NewPVD->getName()
114+
<< "' is deallocated";
115+
116+
PathDiagnosticLocation Loc =
117+
PathDiagnosticLocation::create(N->getLocation(), BRC.getSourceManager());
118+
return std::make_shared<PathDiagnosticEventPiece>(Loc, OS.str());
119+
}
87120

88121
static const ParmVarDecl *getOriginParam(SVal V, CheckerContext &C) {
89122
SymbolRef Sym = V.getAsSymbol();
@@ -162,16 +195,7 @@ void MIGChecker::checkPostCall(const CallEvent &Call, CheckerContext &C) const {
162195
if (!PVD)
163196
return;
164197

165-
const NoteTag *T = C.getNoteTag([this, PVD](BugReport &BR) -> std::string {
166-
if (&BR.getBugType() != &BT)
167-
return "";
168-
SmallString<64> Str;
169-
llvm::raw_svector_ostream OS(Str);
170-
OS << "Value passed through parameter '" << PVD->getName()
171-
<< "\' is deallocated";
172-
return OS.str();
173-
});
174-
C.addTransition(C.getState()->set<ReleasedParameter>(true), T);
198+
C.addTransition(C.getState()->set<ReleasedParameter>(PVD));
175199
}
176200

177201
// Returns true if V can potentially represent a "successful" kern_return_t.
@@ -236,6 +260,7 @@ void MIGChecker::checkReturnAux(const ReturnStmt *RS, CheckerContext &C) const {
236260

237261
R->addRange(RS->getSourceRange());
238262
bugreporter::trackExpressionValue(N, RS->getRetValue(), *R, false);
263+
R->addVisitor(llvm::make_unique<Visitor>());
239264
C.emitReport(std::move(R));
240265
}
241266

clang/lib/StaticAnalyzer/Core/BugReporter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,6 @@ std::pair<BugReport*, std::unique_ptr<VisitorsDiagnosticsTy>> findValidReport(
26122612
R->addVisitor(llvm::make_unique<NilReceiverBRVisitor>());
26132613
R->addVisitor(llvm::make_unique<ConditionBRVisitor>());
26142614
R->addVisitor(llvm::make_unique<CXXSelfAssignmentBRVisitor>());
2615-
R->addVisitor(llvm::make_unique<TagVisitor>());
26162615

26172616
BugReporterContext BRC(Reporter, ErrorGraph.BackMap);
26182617

clang/lib/StaticAnalyzer/Core/BugReporterVisitors.cpp

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,30 +2472,6 @@ FalsePositiveRefutationBRVisitor::VisitNode(const ExplodedNode *N,
24722472
return nullptr;
24732473
}
24742474

2475-
int NoteTag::Kind = 0;
2476-
2477-
void TagVisitor::Profile(llvm::FoldingSetNodeID &ID) const {
2478-
static int Tag = 0;
2479-
ID.AddPointer(&Tag);
2480-
}
2481-
2482-
std::shared_ptr<PathDiagnosticPiece>
2483-
TagVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
2484-
BugReport &R) {
2485-
ProgramPoint PP = N->getLocation();
2486-
const NoteTag *T = dyn_cast_or_null<NoteTag>(PP.getTag());
2487-
if (!T)
2488-
return nullptr;
2489-
2490-
if (Optional<std::string> Msg = T->generateMessage(BRC, R)) {
2491-
PathDiagnosticLocation Loc =
2492-
PathDiagnosticLocation::create(PP, BRC.getSourceManager());
2493-
return std::make_shared<PathDiagnosticEventPiece>(Loc, *Msg);
2494-
}
2495-
2496-
return nullptr;
2497-
}
2498-
24992475
void FalsePositiveRefutationBRVisitor::Profile(
25002476
llvm::FoldingSetNodeID &ID) const {
25012477
static int Tag = 0;

clang/lib/StaticAnalyzer/Core/ExprEngine.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@ ExprEngine::ExprEngine(cross_tu::CrossTranslationUnitContext &CTU,
201201
svalBuilder(StateMgr.getSValBuilder()),
202202
ObjCNoRet(mgr.getASTContext()),
203203
BR(mgr, *this),
204-
VisitedCallees(VisitedCalleesIn),
205-
HowToInline(HowToInlineIn),
206-
NoteTags(G.getAllocator()) {
204+
VisitedCallees(VisitedCalleesIn), HowToInline(HowToInlineIn) {
207205
unsigned TrimInterval = mgr.options.GraphTrimInterval;
208206
if (TrimInterval != 0) {
209207
// Enable eager node reclamation when constructing the ExplodedGraph.

clang/test/Analysis/mig.mm

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,6 @@ kern_return_t release_twice(mach_port_name_t port, vm_address_t addr1, vm_addres
9191
// expected-note@-1{{MIG callback fails with error after deallocating argument value. This is a use-after-free vulnerability because the caller will try to deallocate it again}}
9292
}
9393

94-
MIG_SERVER_ROUTINE
95-
kern_return_t no_unrelated_notes(mach_port_name_t port, vm_address_t address, vm_size_t size) {
96-
vm_deallocate(port, address, size); // no-note
97-
1 / 0; // expected-warning{{Division by zero}}
98-
// expected-note@-1{{Division by zero}}
99-
return KERN_SUCCESS;
100-
}
101-
10294
// Make sure we find the bug when the object is destroyed within an
10395
// automatic destructor.
10496
MIG_SERVER_ROUTINE

0 commit comments

Comments
 (0)