clang
22.0.0git
include
clang
Analysis
Analyses
LifetimeSafety
LiveOrigins.h
Go to the documentation of this file.
1
//===- LiveOrigins.h - Live Origins 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 defines the LiveOriginAnalysis, a backward dataflow analysis that
10
// determines which origins are "live" at each program point. An origin is
11
// "live" at a program point if there's a potential future use of a pointer it
12
// is associated with. Liveness is "generated" by a use of an origin (e.g., a
13
// `UseFact` from a read of a pointer) and is "killed" (i.e., it stops being
14
// live) when the origin is replaced by flowing a different origin into it
15
// (e.g., an OriginFlow from an assignment that kills the destination).
16
//
17
// This information is used for detecting use-after-free errors, as it allows us
18
// to check if a live origin holds a loan to an object that has already expired.
19
//
20
//===----------------------------------------------------------------------===//
21
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
22
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
23
24
#include "
clang/Analysis/Analyses/LifetimeSafety/Facts.h
"
25
#include "
clang/Analysis/Analyses/LifetimeSafety/Origins.h
"
26
#include "
clang/Analysis/AnalysisDeclContext.h
"
27
#include "
clang/Analysis/CFG.h
"
28
#include "llvm/ADT/FoldingSet.h"
29
#include "llvm/ADT/ImmutableMap.h"
30
#include "llvm/Support/Debug.h"
31
32
namespace
clang::lifetimes::internal
{
33
34
enum class
LivenessKind
: uint8_t {
35
Dead
,
// Not alive
36
Maybe
,
// Live on some path but not all paths (may-be-live)
37
Must
// Live on all paths (must-be-live)
38
};
39
40
/// Information about why an origin is live at a program point.
41
struct
LivenessInfo
{
42
/// The use that makes the origin live. If liveness is propagated from
43
/// multiple uses along different paths, this will point to the use appearing
44
/// earlier in the translation unit.
45
/// This is 'null' when the origin is not live.
46
const
UseFact
*
CausingUseFact
;
47
48
/// The kind of liveness of the origin.
49
/// `Must`: The origin is live on all control-flow paths from the current
50
/// point to the function's exit (i.e. the current point is dominated by a set
51
/// of uses).
52
/// `Maybe`: indicates it is live on some but not all paths.
53
///
54
/// This determines the diagnostic's confidence level.
55
/// `Must`-be-alive at expiration implies a definite use-after-free,
56
/// while `Maybe`-be-alive suggests a potential one on some paths.
57
LivenessKind
Kind
;
58
59
LivenessInfo
() :
CausingUseFact
(
nullptr
),
Kind
(
LivenessKind
::
Dead
) {}
60
LivenessInfo
(
const
UseFact
*UF,
LivenessKind
K)
61
:
CausingUseFact
(UF),
Kind
(K) {}
62
63
bool
operator==
(
const
LivenessInfo
&
Other
)
const
{
64
return
CausingUseFact
==
Other
.CausingUseFact &&
Kind
==
Other
.Kind;
65
}
66
bool
operator!=
(
const
LivenessInfo
&
Other
)
const
{
return
!(*
this
==
Other
); }
67
68
void
Profile
(llvm::FoldingSetNodeID &IDBuilder)
const
{
69
IDBuilder.AddPointer(
CausingUseFact
);
70
IDBuilder.Add(
Kind
);
71
}
72
};
73
74
using
LivenessMap
= llvm::ImmutableMap<OriginID, LivenessInfo>;
75
76
class
LiveOriginsAnalysis
{
77
public
:
78
LiveOriginsAnalysis
(
const
CFG
&
C
,
AnalysisDeclContext
&AC,
FactManager
&F,
79
LivenessMap::Factory &SF);
80
~LiveOriginsAnalysis
();
81
82
/// Returns the set of origins that are live at a specific program point,
83
/// along with the the details of the liveness.
84
LivenessMap
getLiveOriginsAt
(
ProgramPoint
P)
const
;
85
86
// Dump liveness values on all test points in the program.
87
void
dump
(llvm::raw_ostream &OS,
88
llvm::StringMap<ProgramPoint> TestPoints)
const
;
89
90
private
:
91
class
Impl
;
92
std::unique_ptr<Impl> PImpl;
93
};
94
95
}
// namespace clang::lifetimes::internal
96
97
#endif
// LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
AnalysisDeclContext.h
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
CFG.h
Facts.h
Origins.h
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Definition
AnalysisDeclContext.h:72
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition
CFG.h:1222
clang::lifetimes::internal::FactManager
Definition
Facts.h:185
clang::lifetimes::internal::LiveOriginsAnalysis::Impl
Definition
LiveOrigins.cpp:159
clang::lifetimes::internal::LiveOriginsAnalysis::dump
void dump(llvm::raw_ostream &OS, llvm::StringMap< ProgramPoint > TestPoints) const
Definition
LiveOrigins.cpp:176
clang::lifetimes::internal::LiveOriginsAnalysis::~LiveOriginsAnalysis
~LiveOriginsAnalysis()
clang::lifetimes::internal::LiveOriginsAnalysis::LiveOriginsAnalysis
LiveOriginsAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, LivenessMap::Factory &SF)
Definition
LiveOrigins.cpp:163
clang::lifetimes::internal::LiveOriginsAnalysis::getLiveOriginsAt
LivenessMap getLiveOriginsAt(ProgramPoint P) const
Returns the set of origins that are live at a specific program point, along with the the details of t...
Definition
LiveOrigins.cpp:172
clang::lifetimes::internal::UseFact
Definition
Facts.h:145
clang::lifetimes::internal
Definition
Checker.h:23
clang::lifetimes::internal::ProgramPoint
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
Definition
Facts.h:74
clang::lifetimes::internal::LivenessMap
llvm::ImmutableMap< OriginID, LivenessInfo > LivenessMap
Definition
LiveOrigins.h:74
clang::lifetimes::internal::LivenessKind
LivenessKind
Definition
LiveOrigins.h:34
clang::lifetimes::internal::LivenessKind::Dead
@ Dead
Definition
LiveOrigins.h:35
clang::lifetimes::internal::LivenessKind::Maybe
@ Maybe
Definition
LiveOrigins.h:36
clang::lifetimes::internal::LivenessKind::Must
@ Must
Definition
LiveOrigins.h:37
clang::LinkageSpecLanguageIDs::C
@ C
Definition
DeclCXX.h:3007
clang::nullptr
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
Definition
StmtOpenACC.h:141
clang::ImplicitParamKind::Other
@ Other
Other implicit parameter.
Definition
Decl.h:1746
clang::lifetimes::internal::LivenessInfo::LivenessInfo
LivenessInfo()
Definition
LiveOrigins.h:59
clang::lifetimes::internal::LivenessInfo::LivenessInfo
LivenessInfo(const UseFact *UF, LivenessKind K)
Definition
LiveOrigins.h:60
clang::lifetimes::internal::LivenessInfo::Kind
LivenessKind Kind
The kind of liveness of the origin.
Definition
LiveOrigins.h:57
clang::lifetimes::internal::LivenessInfo::CausingUseFact
const UseFact * CausingUseFact
The use that makes the origin live.
Definition
LiveOrigins.h:46
clang::lifetimes::internal::LivenessInfo::operator==
bool operator==(const LivenessInfo &Other) const
Definition
LiveOrigins.h:63
clang::lifetimes::internal::LivenessInfo::Profile
void Profile(llvm::FoldingSetNodeID &IDBuilder) const
Definition
LiveOrigins.h:68
clang::lifetimes::internal::LivenessInfo::operator!=
bool operator!=(const LivenessInfo &Other) const
Definition
LiveOrigins.h:66
Generated on
for clang by
1.14.0