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
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
= llvm::ImmutableMap<OriginID, LivenessInfo>;
77
78
class
LiveOriginsAnalysis
{
79
public
:
80
LiveOriginsAnalysis
(
const
CFG
&
C
,
AnalysisDeclContext
&AC,
FactManager
&F,
81
LivenessMap::Factory &SF);
82
~LiveOriginsAnalysis
();
83
84
/// Returns the set of origins that are live at a specific program point,
85
/// along with the the details of the liveness.
86
LivenessMap
getLiveOriginsAt
(
ProgramPoint
P)
const
;
87
88
// Dump liveness values on all test points in the program.
89
void
dump
(llvm::raw_ostream &OS,
90
llvm::StringMap<ProgramPoint> TestPoints)
const
;
91
92
private
:
93
class
Impl
;
94
std::unique_ptr<Impl> PImpl;
95
};
96
97
}
// namespace clang::lifetimes::internal
98
99
#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:195
clang::lifetimes::internal::LiveOriginsAnalysis::Impl
Definition
LiveOrigins.cpp:177
clang::lifetimes::internal::LiveOriginsAnalysis::dump
void dump(llvm::raw_ostream &OS, llvm::StringMap< ProgramPoint > TestPoints) const
Definition
LiveOrigins.cpp:194
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:181
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:190
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:82
clang::lifetimes::internal::CausingFactType
::llvm::PointerUnion< const UseFact *, const OriginEscapesFact * > CausingFactType
Definition
LiveOrigins.h:34
clang::lifetimes::internal::LivenessMap
llvm::ImmutableMap< 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: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(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