clang 22.0.0git
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
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
33
34enum 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.
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.
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.
58
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
74using LivenessMap = llvm::ImmutableMap<OriginID, LivenessInfo>;
75
77public:
79 LivenessMap::Factory &SF);
81
82 /// Returns the set of origins that are live at a specific program point,
83 /// along with the the details of the liveness.
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
90private:
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
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1222
void dump(llvm::raw_ostream &OS, llvm::StringMap< ProgramPoint > TestPoints) const
LiveOriginsAnalysis(const CFG &C, AnalysisDeclContext &AC, FactManager &F, LivenessMap::Factory &SF)
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...
const Fact * ProgramPoint
A ProgramPoint identifies a location in the CFG by pointing to a specific Fact.
Definition Facts.h:74
llvm::ImmutableMap< OriginID, LivenessInfo > LivenessMap
Definition LiveOrigins.h:74
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Other
Other implicit parameter.
Definition Decl.h:1746
LivenessInfo(const UseFact *UF, LivenessKind K)
Definition LiveOrigins.h:60
LivenessKind Kind
The kind of liveness of the origin.
Definition LiveOrigins.h:57
const UseFact * CausingUseFact
The use that makes the origin live.
Definition LiveOrigins.h:46
bool operator==(const LivenessInfo &Other) const
Definition LiveOrigins.h:63
void Profile(llvm::FoldingSetNodeID &IDBuilder) const
Definition LiveOrigins.h:68
bool operator!=(const LivenessInfo &Other) const
Definition LiveOrigins.h:66