-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[LifetimeSafety] Implement a basic use-after-free diagnostic #149731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
usx95
wants to merge
1
commit into
main
Choose a base branch
from
users/usx95/07-20-basic_error_report_for_use_after_free
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+579
−61
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,14 +19,35 @@ | |
#define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H | ||
#include "clang/Analysis/AnalysisDeclContext.h" | ||
#include "clang/Analysis/CFG.h" | ||
#include "clang/Basic/SourceLocation.h" | ||
#include "llvm/ADT/DenseMapInfo.h" | ||
#include "llvm/ADT/ImmutableMap.h" | ||
#include "llvm/ADT/ImmutableSet.h" | ||
#include "llvm/ADT/StringMap.h" | ||
#include <memory> | ||
|
||
namespace clang::lifetimes { | ||
|
||
/// Enum to track the confidence level of a potential error. | ||
enum class Confidence { | ||
None, | ||
Maybe, // Reported as a potential error (-Wlifetime-safety-strict) | ||
Definite // Reported as a definite error (-Wlifetime-safety-permissive) | ||
}; | ||
|
||
class LifetimeSafetyReporter { | ||
public: | ||
LifetimeSafetyReporter() = default; | ||
virtual ~LifetimeSafetyReporter() = default; | ||
|
||
virtual void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr, | ||
SourceLocation FreeLoc, | ||
Confidence Confidence) {} | ||
}; | ||
|
||
/// The main entry point for the analysis. | ||
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC); | ||
void runLifetimeSafetyAnalysis(AnalysisDeclContext &AC, | ||
LifetimeSafetyReporter *Reporter); | ||
|
||
namespace internal { | ||
// Forward declarations of internal types. | ||
|
@@ -53,6 +74,7 @@ template <typename Tag> struct ID { | |
IDBuilder.AddInteger(Value); | ||
} | ||
}; | ||
|
||
template <typename Tag> | ||
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, ID<Tag> ID) { | ||
return OS << ID.Value; | ||
|
@@ -66,6 +88,7 @@ using OriginID = ID<struct OriginTag>; | |
// TODO(opt): Consider using a bitset to represent the set of loans. | ||
using LoanSet = llvm::ImmutableSet<LoanID>; | ||
using OriginSet = llvm::ImmutableSet<OriginID>; | ||
using ExpiredLoanMap = llvm::ImmutableMap<LoanID, const Fact *>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe comment? including -- what kind of facts do we expect as values? |
||
|
||
/// A `ProgramPoint` identifies a location in the CFG by pointing to a specific | ||
/// `Fact`. identified by a lifetime-related event (`Fact`). | ||
|
@@ -78,7 +101,8 @@ using ProgramPoint = const Fact *; | |
/// encapsulates the various dataflow analyses. | ||
class LifetimeSafetyAnalysis { | ||
public: | ||
LifetimeSafetyAnalysis(AnalysisDeclContext &AC); | ||
LifetimeSafetyAnalysis(AnalysisDeclContext &AC, | ||
LifetimeSafetyReporter *Reporter); | ||
~LifetimeSafetyAnalysis(); | ||
|
||
void run(); | ||
|
@@ -87,7 +111,7 @@ class LifetimeSafetyAnalysis { | |
LoanSet getLoansAtPoint(OriginID OID, ProgramPoint PP) const; | ||
|
||
/// Returns the set of loans that have expired at a specific program point. | ||
LoanSet getExpiredLoansAtPoint(ProgramPoint PP) const; | ||
ExpiredLoanMap getExpiredLoansAtPoint(ProgramPoint PP) const; | ||
|
||
/// Finds the OriginID for a given declaration. | ||
/// Returns a null optional if not found. | ||
|
@@ -110,6 +134,7 @@ class LifetimeSafetyAnalysis { | |
|
||
private: | ||
AnalysisDeclContext &AC; | ||
LifetimeSafetyReporter *Reporter; | ||
std::unique_ptr<LifetimeFactory> Factory; | ||
std::unique_ptr<FactManager> FactMgr; | ||
std::unique_ptr<LoanPropagationAnalysis> LoanPropagation; | ||
|
@@ -118,4 +143,25 @@ class LifetimeSafetyAnalysis { | |
} // namespace internal | ||
} // namespace clang::lifetimes | ||
|
||
namespace llvm { | ||
template <typename Tag> | ||
struct DenseMapInfo<clang::lifetimes::internal::ID<Tag>> { | ||
using ID = clang::lifetimes::internal::ID<Tag>; | ||
|
||
static inline ID getEmptyKey() { | ||
return {DenseMapInfo<uint32_t>::getEmptyKey()}; | ||
} | ||
|
||
static inline ID getTombstoneKey() { | ||
return {DenseMapInfo<uint32_t>::getTombstoneKey()}; | ||
} | ||
|
||
static unsigned getHashValue(const ID &Val) { | ||
return DenseMapInfo<uint32_t>::getHashValue(Val.Value); | ||
} | ||
|
||
static bool isEqual(const ID &LHS, const ID &RHS) { return LHS == RHS; } | ||
}; | ||
} // namespace llvm | ||
|
||
#endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMESAFETY_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion here but a potential alternative is to just use the regular diagnostics interface of Sema directly and if we want to do testing we can use a different diagnostic consumer like
TextDiagnosticBuffer
orIgnoringDiagConsumer
.