clang 22.0.0git
UnsafeBufferUsage.h
Go to the documentation of this file.
1//===- UnsafeBufferUsage.h - Replace pointers with modern C++ ---*- 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 an analysis that aids replacing buffer accesses through
10// raw pointers with safer C++ abstractions such as containers and views/spans.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H
15#define LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H
16
18#include "clang/AST/Decl.h"
19#include "clang/AST/Expr.h"
20#include "clang/AST/Stmt.h"
22#include "llvm/Support/Debug.h"
23#include <set>
24
25namespace clang {
26
27using VarGrpTy = std::vector<const VarDecl *>;
29
31public:
33 virtual ~VariableGroupsManager() = default;
34 /// Returns the set of variables (including `Var`) that need to be fixed
35 /// together in one step.
36 ///
37 /// `Var` must be a variable that needs fix (so it must be in a group).
38 /// `HasParm` is an optional argument that will be set to true if the set of
39 /// variables, where `Var` is in, contains parameters.
40 virtual VarGrpRef getGroupOfVar(const VarDecl *Var,
41 bool *HasParm = nullptr) const =0;
42
43 /// Returns the non-empty group of variables that include parameters of the
44 /// analyzing function, if such a group exists. An empty group, otherwise.
45 virtual VarGrpRef getGroupOfParms() const =0;
46};
47
48// FixitStrategy is a map from variables to the way we plan to emit fixes for
49// these variables. It is figured out gradually by trying different fixes
50// for different variables depending on gadgets in which these variables
51// participate.
53public:
54 enum class Kind {
55 Wontfix, // We don't plan to emit a fixit for this variable.
56 Span, // We recommend replacing the variable with std::span.
57 Iterator, // We recommend replacing the variable with std::span::iterator.
58 Array, // We recommend replacing the variable with std::array.
59 Vector // We recommend replacing the variable with std::vector.
60 };
61
62private:
63 using MapTy = llvm::DenseMap<const VarDecl *, Kind>;
64
65 MapTy Map;
66
67public:
68 FixitStrategy() = default;
69 FixitStrategy(const FixitStrategy &) = delete; // Let's avoid copies.
73
74 void set(const VarDecl *VD, Kind K) { Map[VD] = K; }
75
76 Kind lookup(const VarDecl *VD) const {
77 auto I = Map.find(VD);
78 if (I == Map.end())
79 return Kind::Wontfix;
80
81 return I->second;
82 }
83};
84
85/// The interface that lets the caller handle unsafe buffer usage analysis
86/// results by overriding this class's handle... methods.
88#ifndef NDEBUG
89public:
90 // A self-debugging facility that you can use to notify the user when
91 // suggestions or fixits are incomplete.
92 // Uses std::function to avoid computing the message when it won't
93 // actually be displayed.
94 using DebugNote = std::pair<SourceLocation, std::string>;
95 using DebugNoteList = std::vector<DebugNote>;
96 using DebugNoteByVar = std::map<const VarDecl *, DebugNoteList>;
98#endif
99
100public:
102 virtual ~UnsafeBufferUsageHandler() = default;
103
104 /// This analyses produces large fixits that are organized into lists
105 /// of primitive fixits (individual insertions/removals/replacements).
107
108 /// Invoked when an unsafe operation over raw pointers is found.
109 virtual void handleUnsafeOperation(const Stmt *Operation,
110 bool IsRelatedToDecl, ASTContext &Ctx) = 0;
111
112 /// Invoked when a call to an unsafe libc function is found.
113 /// \param PrintfInfo
114 /// is 0 if the callee function is not a member of the printf family;
115 /// is 1 if the callee is `sprintf`;
116 /// is 2 if arguments of the call have `__size_by` relation but are not in a
117 /// safe pattern;
118 /// is 3 if string arguments do not guarantee null-termination
119 /// is 4 if the callee takes va_list
120 /// \param UnsafeArg one of the actual arguments that is unsafe, non-null
121 /// only when `2 <= PrintfInfo <= 3`
122 virtual void handleUnsafeLibcCall(const CallExpr *Call, unsigned PrintfInfo,
123 ASTContext &Ctx,
124 const Expr *UnsafeArg = nullptr) = 0;
125
126 /// Invoked when an unsafe operation with a std container is found.
127 virtual void handleUnsafeOperationInContainer(const Stmt *Operation,
128 bool IsRelatedToDecl,
129 ASTContext &Ctx) = 0;
130
131 /// Invoked when a fix is suggested against a variable. This function groups
132 /// all variables that must be fixed together (i.e their types must be changed
133 /// to the same target type to prevent type mismatches) into a single fixit.
134 ///
135 /// `D` is the declaration of the callable under analysis that owns `Variable`
136 /// and all of its group mates.
137 virtual void
139 const VariableGroupsManager &VarGrpMgr,
140 FixItList &&Fixes, const Decl *D,
141 const FixitStrategy &VarTargetTypes) = 0;
142
143 // Invoked when an array subscript operator[] is used on a
144 // std::unique_ptr<T[]>.
146 bool IsRelatedToDecl,
147 ASTContext &Ctx) = 0;
148
149#ifndef NDEBUG
150public:
152 DEBUG_WITH_TYPE("SafeBuffers", return true);
153 return false;
154 }
155
157 std::string Text) {
159 DebugNotesByVar[VD].push_back(std::make_pair(Loc, Text));
160 }
161
164 DebugNotesByVar.clear();
165 }
166#endif
167
168public:
169 /// \return true iff buffer safety is opt-out at `Loc`; false otherwise.
170 virtual bool isSafeBufferOptOut(const SourceLocation &Loc) const = 0;
171
172 /// \return true iff unsafe uses in containers should NOT be reported at
173 /// `Loc`; false otherwise.
174 virtual bool
176
177 /// \return true iff unsafe libc call should NOT be reported at `Loc`
178 virtual bool
180
181 virtual std::string
183 StringRef WSSuffix = "") const = 0;
184};
185
186// This function invokes the analysis and allows the caller to react to it
187// through the handler class.
189 bool EmitSuggestions);
190
191namespace internal {
192// Tests if any two `FixItHint`s in `FixIts` conflict. Two `FixItHint`s
193// conflict if they have overlapping source ranges.
195 const SourceManager &SM);
196} // namespace internal
197
198std::set<const Expr *> findUnsafePointers(const FunctionDecl *FD);
199} // end namespace clang
200
201#endif /* LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H */
#define SM(sm)
llvm::DenseMap< Stmt *, Stmt * > MapTy
Definition ParentMap.cpp:21
Defines the clang::SourceLocation class and associated facilities.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2877
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
A dynamically typed AST node container.
This represents one expression.
Definition Expr.h:112
FixitStrategy(FixitStrategy &&)=default
FixitStrategy & operator=(const FixitStrategy &)=delete
FixitStrategy & operator=(FixitStrategy &&)=default
FixitStrategy(const FixitStrategy &)=delete
Kind lookup(const VarDecl *VD) const
void set(const VarDecl *VD, Kind K)
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Stmt - This represents one statement.
Definition Stmt.h:85
The interface that lets the caller handle unsafe buffer usage analysis results by overriding this cla...
virtual void handleUnsafeUniquePtrArrayAccess(const DynTypedNode &Node, bool IsRelatedToDecl, ASTContext &Ctx)=0
void addDebugNoteForVar(const VarDecl *VD, SourceLocation Loc, std::string Text)
std::vector< DebugNote > DebugNoteList
virtual std::string getUnsafeBufferUsageAttributeTextAt(SourceLocation Loc, StringRef WSSuffix="") const =0
virtual bool isSafeBufferOptOut(const SourceLocation &Loc) const =0
std::map< const VarDecl *, DebugNoteList > DebugNoteByVar
std::pair< SourceLocation, std::string > DebugNote
llvm::SmallVectorImpl< FixItHint > FixItList
This analyses produces large fixits that are organized into lists of primitive fixits (individual ins...
virtual bool ignoreUnsafeBufferInContainer(const SourceLocation &Loc) const =0
virtual void handleUnsafeOperation(const Stmt *Operation, bool IsRelatedToDecl, ASTContext &Ctx)=0
Invoked when an unsafe operation over raw pointers is found.
virtual void handleUnsafeVariableGroup(const VarDecl *Variable, const VariableGroupsManager &VarGrpMgr, FixItList &&Fixes, const Decl *D, const FixitStrategy &VarTargetTypes)=0
Invoked when a fix is suggested against a variable.
virtual ~UnsafeBufferUsageHandler()=default
virtual void handleUnsafeOperationInContainer(const Stmt *Operation, bool IsRelatedToDecl, ASTContext &Ctx)=0
Invoked when an unsafe operation with a std container is found.
virtual bool ignoreUnsafeBufferInLibcCall(const SourceLocation &Loc) const =0
virtual void handleUnsafeLibcCall(const CallExpr *Call, unsigned PrintfInfo, ASTContext &Ctx, const Expr *UnsafeArg=nullptr)=0
Invoked when a call to an unsafe libc function is found.
Represents a variable declaration or definition.
Definition Decl.h:926
virtual ~VariableGroupsManager()=default
virtual VarGrpRef getGroupOfVar(const VarDecl *Var, bool *HasParm=nullptr) const =0
Returns the set of variables (including Var) that need to be fixed together in one step.
virtual VarGrpRef getGroupOfParms() const =0
Returns the non-empty group of variables that include parameters of the analyzing function,...
bool anyConflict(const llvm::SmallVectorImpl< FixItHint > &FixIts, const SourceManager &SM)
The JSON file list parser is used to communicate input to InstallAPI.
void checkUnsafeBufferUsage(const Decl *D, UnsafeBufferUsageHandler &Handler, bool EmitSuggestions)
std::vector< const VarDecl * > VarGrpTy
std::set< const Expr * > findUnsafePointers(const FunctionDecl *FD)
ArrayRef< const VarDecl * > VarGrpRef