clang 23.0.0git
LifetimeSafety.cpp
Go to the documentation of this file.
1//===- LifetimeSafety.cpp - C++ Lifetime Safety Analysis -*--------- 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 file implements the main LifetimeSafetyAnalysis class, which coordinates
10// the various components (fact generation, loan propagation, live origins
11// analysis, and checking) to detect lifetime safety violations in C++ code.
12//
13//===----------------------------------------------------------------------===//
15#include "clang/AST/Decl.h"
16#include "clang/AST/Expr.h"
17#include "clang/AST/Type.h"
26#include "clang/Analysis/CFG.h"
27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/TimeProfiler.h"
31#include <memory>
32
33namespace clang::lifetimes {
34namespace internal {
35
36#ifndef NDEBUG
37static void DebugOnlyFunction(AnalysisDeclContext &AC, const CFG &Cfg,
38 FactManager &FactMgr) {
39 std::string Name;
40 if (const Decl *D = AC.getDecl()) {
41 if (const auto *ND = dyn_cast<NamedDecl>(D))
42 Name = ND->getQualifiedNameAsString();
43 };
44 DEBUG_WITH_TYPE(Name.c_str(), AC.getDecl()->dumpColor());
45 DEBUG_WITH_TYPE(Name.c_str(), Cfg.dump(AC.getASTContext().getLangOpts(),
46 /*ShowColors=*/true));
47 DEBUG_WITH_TYPE(Name.c_str(), FactMgr.dump(Cfg, AC));
48}
49#endif
50
53 const LifetimeSafetyOpts &LSOpts)
54 : AC(AC), SemaHelper(SemaHelper), LSOpts(LSOpts) {}
55
57 llvm::TimeTraceScope TimeProfile("LifetimeSafetyAnalysis");
58
59 const CFG &Cfg = *AC.getCFG();
60 if (LSOpts.MaxCFGBlocks > 0 && Cfg.getNumBlockIDs() > LSOpts.MaxCFGBlocks) {
61 DEBUG_WITH_TYPE(
62 "LifetimeSafety", std::string FuncName = "<unknown>";
63 if (const Decl *D = AC.getDecl()) if (const auto *ND =
64 dyn_cast<NamedDecl>(D))
65 FuncName = ND->getQualifiedNameAsString();
66 llvm::dbgs() << "LifetimeSafety: Skipping function " << FuncName
67 << "due to large CFG: " << Cfg.getNumBlockIDs()
68 << " blocks (threshold: " << LSOpts.MaxCFGBlocks << ")\n");
69 return;
70 }
71
72 FactMgr = std::make_unique<FactManager>(AC, Cfg);
73
74 FactsGenerator FactGen(*FactMgr, AC);
75 FactGen.run();
76
77 /// TODO(opt): Consider optimizing individual blocks before running the
78 /// dataflow analysis.
79 /// 1. Expression Origins: These are assigned once and read at most once,
80 /// forming simple chains. These chains can be compressed into a single
81 /// assignment.
82 /// 2. Block-Local Loans: Origins of expressions are never read by other
83 /// blocks; only Decls are visible. Therefore, loans in a block that
84 /// never reach an Origin associated with a Decl can be safely dropped by
85 /// the analysis.
86 /// 3. Collapse ExpireFacts belonging to same source location into a single
87 /// Fact.
88 LoanPropagation = std::make_unique<LoanPropagationAnalysis>(
89 Cfg, AC, *FactMgr, Factory.OriginMapFactory, Factory.LoanSetFactory);
90
91 LiveOrigins = std::make_unique<LiveOriginsAnalysis>(
92 Cfg, AC, *FactMgr, Factory.LivenessMapFactory);
93
94 MovedLoans = std::make_unique<MovedLoansAnalysis>(
95 Cfg, AC, *FactMgr, *LoanPropagation, *LiveOrigins, FactMgr->getLoanMgr(),
96 Factory.MovedLoansMapFactory);
97
98 runLifetimeChecker(*LoanPropagation, *MovedLoans, *LiveOrigins, *FactMgr, AC,
99 SemaHelper);
100
101 DEBUG_WITH_TYPE("PrintCFG", Cfg.dump(AC.getASTContext().getLangOpts(),
102 /*ShowColors=*/true));
103
104 DEBUG_WITH_TYPE("LifetimeFacts", FactMgr->dump(Cfg, AC));
105
106 // Debug print facts for a specific function using
107 // -debug-only=EnableFilterByFunctionName,YourFunctionNameFoo
108 DEBUG_WITH_TYPE("EnableFilterByFunctionName",
109 DebugOnlyFunction(AC, Cfg, *FactMgr));
110 DEBUG_WITH_TYPE("LiveOrigins",
111 LiveOrigins->dump(llvm::dbgs(), FactMgr->getTestPoints()));
112}
113
115 LifetimeSafetyStats &Stats) {
116 Stmt *FunctionBody = AC.getBody();
117 if (FunctionBody == nullptr)
118 return;
119 OM.collectMissingOrigins(*FunctionBody, Stats);
120}
121} // namespace internal
122
124 LifetimeSafetySemaHelper *SemaHelper,
125 LifetimeSafetyStats &Stats, bool CollectStats) {
126 LifetimeSafetyOpts LSOpts;
127 LSOpts.MaxCFGBlocks =
128 AC.getASTContext().getLangOpts().LifetimeSafetyMaxCFGBlocks;
129
130 internal::LifetimeSafetyAnalysis Analysis(AC, SemaHelper, LSOpts);
131 Analysis.run();
132 if (CollectStats)
133 collectLifetimeStats(AC, Analysis.getFactManager().getOriginMgr(), Stats);
134}
135} // namespace clang::lifetimes
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
C Language Family Type Representation.
const LangOptions & getLangOpts() const
Definition ASTContext.h:951
AnalysisDeclContext contains the context data for the function, method or block under analysis.
ASTContext & getASTContext() const
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1250
void dump(const LangOptions &LO, bool ShowColors) const
dump - A simple pretty printer of a CFG that outputs to stderr.
Definition CFG.cpp:6349
unsigned getNumBlockIDs() const
Returns the total number of BlockIDs allocated (which start at 0).
Definition CFG.h:1443
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
void dumpColor() const
Stmt - This represents one statement.
Definition Stmt.h:86
Abstract interface for operations requiring Sema access.
void dump(const CFG &Cfg, AnalysisDeclContext &AC) const
Definition Facts.cpp:124
Running the lifetime safety analysis and querying its results.
LifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetySemaHelper *SemaHelper, const LifetimeSafetyOpts &LSOpts)
Manages the creation, storage, and retrieval of origins for pointer-like variables and expressions.
Definition Origins.h:126
void collectMissingOrigins(Stmt &FunctionBody, LifetimeSafetyStats &LSStats)
Collects statistics about expressions that lack associated origins.
Definition Origins.cpp:233
void runLifetimeChecker(const LoanPropagationAnalysis &LoanPropagation, const MovedLoansAnalysis &MovedLoans, const LiveOriginsAnalysis &LiveOrigins, FactManager &FactMgr, AnalysisDeclContext &ADC, LifetimeSafetySemaHelper *SemaHelper)
Runs the lifetime checker, which detects use-after-free errors by examining loan expiration points an...
Definition Checker.cpp:388
static void DebugOnlyFunction(AnalysisDeclContext &AC, const CFG &Cfg, FactManager &FactMgr)
void collectLifetimeStats(AnalysisDeclContext &AC, OriginManager &OM, LifetimeSafetyStats &Stats)
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, LifetimeSafetySemaHelper *SemaHelper, LifetimeSafetyStats &Stats, bool CollectStats)
The main entry point for the analysis.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(const_cast< Expr * >(T -> getSizeExpr())))
size_t MaxCFGBlocks
Maximum number of CFG blocks to analyze.
A structure to hold the statistics related to LifetimeAnalysis.