Skip to content

Commit 64ebbeb

Browse files
committed
Apply changes from code browser
Apply changes from code browser
1 parent 0448831 commit 64ebbeb

File tree

11 files changed

+224
-107
lines changed

11 files changed

+224
-107
lines changed

clang/include/clang/Analysis/Analyses/LifetimeSafety/Checker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
#include "clang/AST/Decl.h"
1919
#include "clang/AST/Expr.h"
2020
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
21-
#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h"
2221
#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
2322
#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
2423
#include "clang/Analysis/Analyses/LifetimeSafety/Loans.h"
24+
#include "clang/Analysis/Analyses/LifetimeSafety/Reporter.h"
2525
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
2626
#include "clang/Analysis/AnalysisDeclContext.h"
2727
#include "clang/Analysis/CFG.h"

clang/include/clang/Analysis/Analyses/LifetimeSafety/Dataflow.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
//===- Dataflow.h - Generic Dataflow Analysis Framework --------*- 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 a generic, policy-based driver for dataflow analyses.
10+
// It provides a flexible framework that combines the dataflow runner and
11+
// transfer functions, allowing derived classes to implement specific analyses
12+
// by defining their lattice, join, and transfer functions.
13+
//
14+
//===----------------------------------------------------------------------===//
115
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_DATAFLOW_H
216
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_DATAFLOW_H
317

@@ -13,10 +27,6 @@
1327
namespace clang::lifetimes {
1428
namespace internal {
1529

16-
// ========================================================================= //
17-
// Generic Dataflow Analysis
18-
// ========================================================================= //
19-
2030
enum class Direction { Forward, Backward };
2131

2232
/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific

clang/include/clang/Analysis/Analyses/LifetimeSafety/Facts.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
//===- Facts.h - Lifetime Analysis Facts and Fact Generation ---*- 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 Facts, which are atomic lifetime-relevant events (such as
10+
// loan issuance, loan expiration, origin flow, and use), and the FactGenerator,
11+
// which traverses the AST to generate these facts from CFG statements.
12+
//
13+
//===----------------------------------------------------------------------===//
114
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTS_H
215
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_FACTS_H
316

417
#include "clang/AST/StmtVisitor.h"
518
#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h"
6-
#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h"
719
#include "clang/Analysis/Analyses/LifetimeSafety/Loans.h"
820
#include "clang/Analysis/Analyses/LifetimeSafety/Origins.h"
921
#include "clang/Analysis/Analyses/PostOrderCFGView.h"
@@ -64,6 +76,13 @@ class Fact {
6476
}
6577
};
6678

79+
/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
80+
/// `Fact`. identified by a lifetime-related event (`Fact`).
81+
///
82+
/// A `ProgramPoint` has "after" semantics: it represents the location
83+
/// immediately after its corresponding `Fact`.
84+
using ProgramPoint = const Fact *;
85+
6786
class IssueFact : public Fact {
6887
LoanID LID;
6988
OriginID OID;

clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -17,94 +17,26 @@
1717
//===----------------------------------------------------------------------===//
1818
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H
1919
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H
20+
21+
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
22+
#include "clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h"
23+
#include "clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h"
24+
#include "clang/Analysis/Analyses/LifetimeSafety/Reporter.h"
2025
#include "clang/Analysis/AnalysisDeclContext.h"
2126
#include "clang/Analysis/CFG.h"
22-
#include "clang/Basic/SourceLocation.h"
23-
#include "llvm/ADT/DenseMapInfo.h"
24-
#include "llvm/ADT/ImmutableMap.h"
25-
#include "llvm/ADT/ImmutableSet.h"
2627
#include "llvm/ADT/StringMap.h"
2728
#include <memory>
2829

2930
namespace clang::lifetimes {
3031

31-
/// Enum to track the confidence level of a potential error.
32-
enum class Confidence : uint8_t {
33-
None,
34-
Maybe, // Reported as a potential error (-Wlifetime-safety-strict)
35-
Definite // Reported as a definite error (-Wlifetime-safety-permissive)
36-
};
37-
38-
enum class LivenessKind : uint8_t {
39-
Dead, // Not alive
40-
Maybe, // Live on some path but not all paths (may-be-live)
41-
Must // Live on all paths (must-be-live)
42-
};
43-
44-
class LifetimeSafetyReporter {
45-
public:
46-
LifetimeSafetyReporter() = default;
47-
virtual ~LifetimeSafetyReporter() = default;
48-
49-
virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr,
50-
SourceLocation FreeLoc,
51-
Confidence Confidence) {}
52-
};
53-
5432
/// The main entry point for the analysis.
5533
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC,
5634
LifetimeSafetyReporter *Reporter);
5735

5836
namespace internal {
5937
// Forward declarations of internal types.
60-
class Fact;
61-
class FactManager;
62-
class LoanPropagationAnalysis;
63-
class ExpiredLoansAnalysis;
64-
class LiveOriginAnalysis;
6538
struct LifetimeFactory;
6639

67-
/// A generic, type-safe wrapper for an ID, distinguished by its `Tag` type.
68-
/// Used for giving ID to loans and origins.
69-
template <typename Tag> struct ID {
70-
uint32_t Value = 0;
71-
72-
bool operator==(const ID<Tag> &Other) const { return Value == Other.Value; }
73-
bool operator!=(const ID<Tag> &Other) const { return !(*this == Other); }
74-
bool operator<(const ID<Tag> &Other) const { return Value < Other.Value; }
75-
ID<Tag> operator++(int) {
76-
ID<Tag> Tmp = *this;
77-
++Value;
78-
return Tmp;
79-
}
80-
void Profile(llvm::FoldingSetNodeID &IDBuilder) const {
81-
IDBuilder.AddInteger(Value);
82-
}
83-
};
84-
85-
using LoanID = ID<struct LoanTag>;
86-
using OriginID = ID<struct OriginTag>;
87-
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, LoanID ID) {
88-
return OS << ID.Value;
89-
}
90-
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {
91-
return OS << ID.Value;
92-
}
93-
94-
// Using LLVM's immutable collections is efficient for dataflow analysis
95-
// as it avoids deep copies during state transitions.
96-
// TODO(opt): Consider using a bitset to represent the set of loans.
97-
using LoanSet = llvm::ImmutableSet<LoanID>;
98-
using OriginSet = llvm::ImmutableSet<OriginID>;
99-
using OriginLoanMap = llvm::ImmutableMap<OriginID, LoanSet>;
100-
101-
/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific
102-
/// `Fact`. identified by a lifetime-related event (`Fact`).
103-
///
104-
/// A `ProgramPoint` has "after" semantics: it represents the location
105-
/// immediately after its corresponding `Fact`.
106-
using ProgramPoint = const Fact *;
107-
10840
/// Running the lifetime safety analysis and querying its results. It
10941
/// encapsulates the various dataflow analyses.
11042
class LifetimeSafetyAnalysis {
@@ -159,25 +91,4 @@ class LifetimeSafetyAnalysis {
15991
} // namespace internal
16092
} // namespace clang::lifetimes
16193

162-
namespace llvm {
163-
template <typename Tag>
164-
struct DenseMapInfo<clang::lifetimes::internal::ID<Tag>> {
165-
using ID = clang::lifetimes::internal::ID<Tag>;
166-
167-
static inline ID getEmptyKey() {
168-
return {DenseMapInfo<uint32_t>::getEmptyKey()};
169-
}
170-
171-
static inline ID getTombstoneKey() {
172-
return {DenseMapInfo<uint32_t>::getTombstoneKey()};
173-
}
174-
175-
static unsigned getHashValue(const ID &Val) {
176-
return DenseMapInfo<uint32_t>::getHashValue(Val.Value);
177-
}
178-
179-
static bool isEqual(const ID &LHS, const ID &RHS) { return LHS == RHS; }
180-
};
181-
} // namespace llvm
182-
18394
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H

clang/include/clang/Analysis/Analyses/LifetimeSafety/LiveOrigins.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
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 live
11+
// if there's a potential future use of the pointer it represents. This
12+
// information is used to detect use-after-free errors by checking if live
13+
// origins hold loans to objects that have already expired.
14+
//
15+
//===----------------------------------------------------------------------===//
116
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
217
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LIVE_ORIGINS_H
318

4-
#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h"
519
#include "clang/AST/Decl.h"
620
#include "clang/AST/Expr.h"
721
#include "clang/AST/Type.h"
@@ -19,6 +33,14 @@
1933
namespace clang::lifetimes {
2034
namespace internal {
2135

36+
using OriginSet = llvm::ImmutableSet<OriginID>;
37+
38+
enum class LivenessKind : uint8_t {
39+
Dead, // Not alive
40+
Maybe, // Live on some path but not all paths (may-be-live)
41+
Must // Live on all paths (must-be-live)
42+
};
43+
2244
// ========================================================================= //
2345
// Live Origins Analysis
2446
// ========================================================================= //
@@ -200,11 +222,12 @@ class LiveOriginAnalysis
200222
}
201223

202224
// Dump liveness values on all test points in the program.
203-
void dump(llvm::raw_ostream &OS, const LifetimeSafetyAnalysis &LSA) const {
225+
void dump(llvm::raw_ostream &OS,
226+
llvm::StringMap<ProgramPoint> TestPoints) const {
204227
llvm::dbgs() << "==========================================\n";
205228
llvm::dbgs() << getAnalysisName() << " results:\n";
206229
llvm::dbgs() << "==========================================\n";
207-
for (const auto &Entry : LSA.getTestPoints()) {
230+
for (const auto &Entry : TestPoints) {
208231
OS << "TestPoint: " << Entry.getKey() << "\n";
209232
getState(Entry.getValue()).dump(OS, FactMgr.getOriginMgr());
210233
}

clang/include/clang/Analysis/Analyses/LifetimeSafety/LoanPropagation.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
//===- LoanPropagation.h - Loan Propagation 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 LoanPropagationAnalysis, a forward dataflow analysis
10+
// that tracks which loans each origin holds at each program point. Loans
11+
// represent borrows of storage locations and are propagated through the
12+
// program as pointers are copied or assigned.
13+
//
14+
//===----------------------------------------------------------------------===//
115
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOAN_PROPAGATION_H
216
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOAN_PROPAGATION_H
317

418
#include "clang/Analysis/Analyses/LifetimeSafety/Dataflow.h"
519
#include "clang/Analysis/Analyses/LifetimeSafety/Facts.h"
6-
#include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h"
720
#include "clang/Analysis/Analyses/LifetimeSafety/Utils.h"
821
#include "clang/Analysis/AnalysisDeclContext.h"
922
#include "clang/Analysis/CFG.h"
@@ -14,6 +27,12 @@
1427
namespace clang::lifetimes {
1528
namespace internal {
1629

30+
// Using LLVM's immutable collections is efficient for dataflow analysis
31+
// as it avoids deep copies during state transitions.
32+
// TODO(opt): Consider using a bitset to represent the set of loans.
33+
using LoanSet = llvm::ImmutableSet<LoanID>;
34+
using OriginLoanMap = llvm::ImmutableMap<OriginID, LoanSet>;
35+
1736
// ========================================================================= //
1837
// Loan Propagation Analysis
1938
// ========================================================================= //

clang/include/clang/Analysis/Analyses/LifetimeSafety/Loans.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
1+
//===- Loans.h - Loan and Access Path Definitions --------------*- 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 Loan and AccessPath structures, which represent
10+
// borrows of storage locations, and the LoanManager, which manages the
11+
// creation and retrieval of loans during lifetime analysis.
12+
//
13+
//===----------------------------------------------------------------------===//
114
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
215
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_LOANS_H
3-
#include "LifetimeSafety.h"
16+
17+
#include "clang/AST/Decl.h"
18+
#include "clang/Analysis/Analyses/LifetimeSafety/Utils.h"
19+
#include "llvm/Support/raw_ostream.h"
420

521
namespace clang::lifetimes {
622
namespace internal {
723

24+
using LoanID = utils::ID<struct LoanTag>;
25+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, LoanID ID) {
26+
return OS << ID.Value;
27+
}
28+
829
/// Represents the storage location being borrowed, e.g., a specific stack
930
/// variable.
1031
/// TODO: Model access paths of other types, e.g., s.field, heap and globals.

clang/include/clang/Analysis/Analyses/LifetimeSafety/Origins.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
1+
//===- Origins.h - Origin and Origin Management ----------------*- 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 Origins, which represent the set of possible loans a
10+
// pointer-like object could hold, and the OriginManager, which manages the
11+
// creation, storage, and retrieval of origins for variables and expressions.
12+
//
13+
//===----------------------------------------------------------------------===//
114
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_ORIGINS_H
215
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_ORIGINS_H
316

4-
#include "LifetimeSafety.h"
17+
#include "clang/AST/Decl.h"
18+
#include "clang/AST/Expr.h"
19+
#include "clang/Analysis/Analyses/LifetimeSafety/Utils.h"
520

621
namespace clang::lifetimes {
722
namespace internal {
823

24+
using OriginID = utils::ID<struct OriginTag>;
25+
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, OriginID ID) {
26+
return OS << ID.Value;
27+
}
28+
929
/// An Origin is a symbolic identifier that represents the set of possible
1030
/// loans a pointer-like object could hold at any given time.
1131
/// TODO: Enhance the origin model to handle complex types, pointer
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===- Reporter.h - TODO: FILL ME -*----------- 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+
// TODO: FILLME
9+
//===----------------------------------------------------------------------===//
10+
#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_REPORTER_H
11+
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_REPORTER_H
12+
13+
#include "clang/AST/Expr.h"
14+
#include "clang/Basic/SourceLocation.h"
15+
16+
namespace clang::lifetimes {
17+
18+
/// Enum to track the confidence level of a potential error.
19+
enum class Confidence : uint8_t {
20+
None,
21+
Maybe, // Reported as a potential error (-Wlifetime-safety-strict)
22+
Definite // Reported as a definite error (-Wlifetime-safety-permissive)
23+
};
24+
25+
class LifetimeSafetyReporter {
26+
public:
27+
LifetimeSafetyReporter() = default;
28+
virtual ~LifetimeSafetyReporter() = default;
29+
30+
virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr,
31+
SourceLocation FreeLoc,
32+
Confidence Confidence) {}
33+
};
34+
} // namespace clang::lifetimes
35+
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_REPORTER_H

0 commit comments

Comments
 (0)