clang
24.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/Analyses/LifetimeSafety/Utils.h
"
27
#include "
clang/Analysis/AnalysisDeclContext.h
"
28
#include "
clang/Analysis/CFG.h
"
29
#include "llvm/ADT/FoldingSet.h"
30
#include "llvm/Support/Debug.h"
31
32
namespace
clang::lifetimes::internal
{
33
34
using
CausingFactType
=
35
::llvm::PointerUnion<const UseFact *, const OriginEscapesFact *>;
36
37
enum class
LivenessKind
:
uint8_t
{
38
Dead
,
// Not alive
39
Maybe
,
// Live on some path but not all paths (may-be-live)
40
Must
// Live on all paths (must-be-live)
41
};
42
43
/// Information about why an origin is live at a program point.
44
struct
LivenessInfo
{
45
/// The use that makes the origin live. If liveness is propagated from
46
/// multiple uses along different paths, this will point to the use appearing
47
/// earlier in the translation unit.
48
/// This is 'null' when the origin is not live.
49
CausingFactType
CausingFact
;
50
51
/// The kind of liveness of the origin.
52
/// `Must`: The origin is live on all control-flow paths from the current
53
/// point to the function's exit (i.e. the current point is dominated by a set
54
/// of uses).
55
/// `Maybe`: indicates it is live on some but not all paths.
56
///
57
/// This determines the diagnostic's confidence level.
58
/// `Must`-be-alive at expiration implies a definite use-after-free,
59
/// while `Maybe`-be-alive suggests a potential one on some paths.
60
LivenessKind
Kind
;
61
62
LivenessInfo
() :
CausingFact
(
nullptr
),
Kind
(
LivenessKind
::
Dead
) {}
63
LivenessInfo
(
CausingFactType
CF,
LivenessKind
K) :
CausingFact
(CF),
Kind
(K) {}
64
65
bool
operator==
(
const
LivenessInfo
&
Other
)
const
{
66
return
CausingFact
==
Other
.CausingFact &&
Kind
==
Other
.Kind;
67
}
68
bool
operator!=
(
const
LivenessInfo
&
Other
)
const
{
return
!(*
this
==
Other
); }
69
70
void
Profile
(llvm::FoldingSetNodeID &IDBuilder)
const
{
71
IDBuilder.AddPointer(
CausingFact
.getOpaqueValue());
72
IDBuilder.Add(
Kind
);
73
}
74
};
75
76
using
LivenessMap
=
utils::MapTy<OriginID, LivenessInfo>
;
77
78
/// The origins that are live at a program point.
79
///
80
/// Origins confined to a single basic block are tracked separately from those
81
/// referenced by more than one, so that only the latter take part in the joins
82
/// at block boundaries. Both halves are live, so consumers must visit both:
83
///
84
/// for (const LivenessMap &Live : {Origins.Persistent, Origins.BlockLocal})
85
/// for (auto &[OID, Info] : Live)
86
struct
LiveOriginSet
{
87
LivenessMap
Persistent
;
88
LivenessMap
BlockLocal
;
89
};
90
91
class
LiveOriginsAnalysis
{
92
public
:
93
LiveOriginsAnalysis
(
const
CFG
&
C
,
AnalysisDeclContext
&AC,
FactManager
&F,
94
LivenessMap::Factory &SF);
95
~LiveOriginsAnalysis
();
96
97
/// Returns the set of origins that are live at a specific program point,
98
/// along with the the details of the liveness.
99
LiveOriginSet
getLiveOriginsAt
(
ProgramPoint
P)
const
;
100
101
// Dump liveness values on all test points in the program.
102
void
dump
(llvm::raw_ostream &OS,
103
const
llvm::StringMap<ProgramPoint> &TestPoints)
const
;
104
105
private
:
106
class
Impl
;
107
std::unique_ptr<Impl> PImpl;
108
};
109
110
}
// namespace clang::lifetimes::internal
111
112
#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...
Utils.h
CFG.h
Facts.h
Origins.h
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Definition
AnalysisDeclContext.h:71
clang::CFG
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition
CFG.h:1271
clang::lifetimes::internal::FactManager
Definition
Facts.h:348
clang::lifetimes::internal::LiveOriginsAnalysis::Impl
Definition
LiveOrigins.cpp:254
clang::lifetimes::internal::LiveOriginsAnalysis::getLiveOriginsAt
LiveOriginSet 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:267
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:258
clang::lifetimes::internal::LiveOriginsAnalysis::dump
void dump(llvm::raw_ostream &OS, const llvm::StringMap< ProgramPoint > &TestPoints) const
Definition
LiveOrigins.cpp:271
clang::lifetimes::internal::utils::MapTy
llvm::ImmutableMap< KeyT, ValT, llvm::ImutKeyValueInfo< KeyT, ValT >, false > MapTy
Definition
Utils.h:44
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:98
clang::lifetimes::internal::CausingFactType
::llvm::PointerUnion< const UseFact *, const OriginEscapesFact * > CausingFactType
Definition
LiveOrigins.h:34
clang::lifetimes::internal::LivenessMap
utils::MapTy< OriginID, LivenessInfo > LivenessMap
Definition
LiveOrigins.h:76
clang::lifetimes::internal::LivenessKind
LivenessKind
Definition
LiveOrigins.h:37
clang::lifetimes::internal::LivenessKind::Dead
@ Dead
Definition
LiveOrigins.h:38
clang::lifetimes::internal::LivenessKind::Maybe
@ Maybe
Definition
LiveOrigins.h:39
clang::lifetimes::internal::LivenessKind::Must
@ Must
Definition
LiveOrigins.h:40
clang::LinkageSpecLanguageIDs::C
@ C
Definition
DeclCXX.h:3032
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:1774
uint8_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
Definition
riscv_packed_simd.h:323
clang::lifetimes::internal::LiveOriginSet
The origins that are live at a program point.
Definition
LiveOrigins.h:86
clang::lifetimes::internal::LiveOriginSet::BlockLocal
LivenessMap BlockLocal
Definition
LiveOrigins.h:88
clang::lifetimes::internal::LiveOriginSet::Persistent
LivenessMap Persistent
Definition
LiveOrigins.h:87
clang::lifetimes::internal::LivenessInfo::LivenessInfo
LivenessInfo(CausingFactType CF, LivenessKind K)
Definition
LiveOrigins.h:63
clang::lifetimes::internal::LivenessInfo::LivenessInfo
LivenessInfo()
Definition
LiveOrigins.h:62
clang::lifetimes::internal::LivenessInfo::Kind
LivenessKind Kind
The kind of liveness of the origin.
Definition
LiveOrigins.h:60
clang::lifetimes::internal::LivenessInfo::CausingFact
CausingFactType CausingFact
The use that makes the origin live.
Definition
LiveOrigins.h:49
clang::lifetimes::internal::LivenessInfo::operator==
bool operator==(const LivenessInfo &Other) const
Definition
LiveOrigins.h:65
clang::lifetimes::internal::LivenessInfo::Profile
void Profile(llvm::FoldingSetNodeID &IDBuilder) const
Definition
LiveOrigins.h:70
clang::lifetimes::internal::LivenessInfo::operator!=
bool operator!=(const LivenessInfo &Other) const
Definition
LiveOrigins.h:68
Generated on
for clang by
1.14.0