Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:24

0001 //===- UnsafeBufferUsage.h - Replace pointers with modern C++ ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines an analysis that aids replacing buffer accesses through
0010 //  raw pointers with safer C++ abstractions such as containers and views/spans.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H
0015 #define LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H
0016 
0017 #include "clang/AST/Decl.h"
0018 #include "clang/AST/Expr.h"
0019 #include "clang/AST/Stmt.h"
0020 #include "clang/Basic/SourceLocation.h"
0021 #include "llvm/Support/Debug.h"
0022 
0023 namespace clang {
0024 
0025 using VarGrpTy = std::vector<const VarDecl *>;
0026 using VarGrpRef = ArrayRef<const VarDecl *>;
0027 
0028 class VariableGroupsManager {
0029 public:
0030   VariableGroupsManager() = default;
0031   virtual ~VariableGroupsManager() = default;
0032   /// Returns the set of variables (including `Var`) that need to be fixed
0033   /// together in one step.
0034   ///
0035   /// `Var` must be a variable that needs fix (so it must be in a group).
0036   /// `HasParm` is an optional argument that will be set to true if the set of
0037   /// variables, where `Var` is in, contains parameters.
0038   virtual VarGrpRef getGroupOfVar(const VarDecl *Var,
0039                                   bool *HasParm = nullptr) const =0;
0040 
0041   /// Returns the non-empty group of variables that include parameters of the
0042   /// analyzing function, if such a group exists.  An empty group, otherwise.
0043   virtual VarGrpRef getGroupOfParms() const =0;
0044 };
0045 
0046 // FixitStrategy is a map from variables to the way we plan to emit fixes for
0047 // these variables. It is figured out gradually by trying different fixes
0048 // for different variables depending on gadgets in which these variables
0049 // participate.
0050 class FixitStrategy {
0051 public:
0052   enum class Kind {
0053     Wontfix,  // We don't plan to emit a fixit for this variable.
0054     Span,     // We recommend replacing the variable with std::span.
0055     Iterator, // We recommend replacing the variable with std::span::iterator.
0056     Array,    // We recommend replacing the variable with std::array.
0057     Vector    // We recommend replacing the variable with std::vector.
0058   };
0059 
0060 private:
0061   using MapTy = llvm::DenseMap<const VarDecl *, Kind>;
0062 
0063   MapTy Map;
0064 
0065 public:
0066   FixitStrategy() = default;
0067   FixitStrategy(const FixitStrategy &) = delete; // Let's avoid copies.
0068   FixitStrategy &operator=(const FixitStrategy &) = delete;
0069   FixitStrategy(FixitStrategy &&) = default;
0070   FixitStrategy &operator=(FixitStrategy &&) = default;
0071 
0072   void set(const VarDecl *VD, Kind K) { Map[VD] = K; }
0073 
0074   Kind lookup(const VarDecl *VD) const {
0075     auto I = Map.find(VD);
0076     if (I == Map.end())
0077       return Kind::Wontfix;
0078 
0079     return I->second;
0080   }
0081 };
0082 
0083 /// The interface that lets the caller handle unsafe buffer usage analysis
0084 /// results by overriding this class's handle... methods.
0085 class UnsafeBufferUsageHandler {
0086 #ifndef NDEBUG
0087 public:
0088   // A self-debugging facility that you can use to notify the user when
0089   // suggestions or fixits are incomplete.
0090   // Uses std::function to avoid computing the message when it won't
0091   // actually be displayed.
0092   using DebugNote = std::pair<SourceLocation, std::string>;
0093   using DebugNoteList = std::vector<DebugNote>;
0094   using DebugNoteByVar = std::map<const VarDecl *, DebugNoteList>;
0095   DebugNoteByVar DebugNotesByVar;
0096 #endif
0097 
0098 public:
0099   UnsafeBufferUsageHandler() = default;
0100   virtual ~UnsafeBufferUsageHandler() = default;
0101 
0102   /// This analyses produces large fixits that are organized into lists
0103   /// of primitive fixits (individual insertions/removals/replacements).
0104   using FixItList = llvm::SmallVectorImpl<FixItHint>;
0105 
0106   /// Invoked when an unsafe operation over raw pointers is found.
0107   virtual void handleUnsafeOperation(const Stmt *Operation,
0108                                      bool IsRelatedToDecl, ASTContext &Ctx) = 0;
0109 
0110   /// Invoked when a call to an unsafe libc function is found.
0111   /// \param PrintfInfo
0112   ///  is 0 if the callee function is not a member of the printf family;
0113   ///  is 1 if the callee is `sprintf`;
0114   ///  is 2 if arguments of the call have `__size_by` relation but are not in a
0115   ///  safe pattern;
0116   ///  is 3 if string arguments do not guarantee null-termination
0117   ///  is 4 if the callee takes va_list
0118   /// \param UnsafeArg one of the actual arguments that is unsafe, non-null
0119   /// only when `2 <= PrintfInfo <= 3`
0120   virtual void handleUnsafeLibcCall(const CallExpr *Call, unsigned PrintfInfo,
0121                                     ASTContext &Ctx,
0122                                     const Expr *UnsafeArg = nullptr) = 0;
0123 
0124   /// Invoked when an unsafe operation with a std container is found.
0125   virtual void handleUnsafeOperationInContainer(const Stmt *Operation,
0126                                                 bool IsRelatedToDecl,
0127                                                 ASTContext &Ctx) = 0;
0128 
0129   /// Invoked when a fix is suggested against a variable. This function groups
0130   /// all variables that must be fixed together (i.e their types must be changed
0131   /// to the same target type to prevent type mismatches) into a single fixit.
0132   ///
0133   /// `D` is the declaration of the callable under analysis that owns `Variable`
0134   /// and all of its group mates.
0135   virtual void
0136   handleUnsafeVariableGroup(const VarDecl *Variable,
0137                             const VariableGroupsManager &VarGrpMgr,
0138                             FixItList &&Fixes, const Decl *D,
0139                             const FixitStrategy &VarTargetTypes) = 0;
0140 
0141 #ifndef NDEBUG
0142 public:
0143   bool areDebugNotesRequested() {
0144     DEBUG_WITH_TYPE("SafeBuffers", return true);
0145     return false;
0146   }
0147 
0148   void addDebugNoteForVar(const VarDecl *VD, SourceLocation Loc,
0149                           std::string Text) {
0150     if (areDebugNotesRequested())
0151       DebugNotesByVar[VD].push_back(std::make_pair(Loc, Text));
0152   }
0153 
0154   void clearDebugNotes() {
0155     if (areDebugNotesRequested())
0156       DebugNotesByVar.clear();
0157   }
0158 #endif
0159 
0160 public:
0161   /// \return true iff buffer safety is opt-out at `Loc`; false otherwise.
0162   virtual bool isSafeBufferOptOut(const SourceLocation &Loc) const = 0;
0163 
0164   /// \return true iff unsafe uses in containers should NOT be reported at
0165   /// `Loc`; false otherwise.
0166   virtual bool
0167   ignoreUnsafeBufferInContainer(const SourceLocation &Loc) const = 0;
0168 
0169   /// \return true iff unsafe libc call should NOT be reported at `Loc`
0170   virtual bool
0171   ignoreUnsafeBufferInLibcCall(const SourceLocation &Loc) const = 0;
0172 
0173   virtual std::string
0174   getUnsafeBufferUsageAttributeTextAt(SourceLocation Loc,
0175                                       StringRef WSSuffix = "") const = 0;
0176 };
0177 
0178 // This function invokes the analysis and allows the caller to react to it
0179 // through the handler class.
0180 void checkUnsafeBufferUsage(const Decl *D, UnsafeBufferUsageHandler &Handler,
0181                             bool EmitSuggestions);
0182 
0183 namespace internal {
0184 // Tests if any two `FixItHint`s in `FixIts` conflict.  Two `FixItHint`s
0185 // conflict if they have overlapping source ranges.
0186 bool anyConflict(const llvm::SmallVectorImpl<FixItHint> &FixIts,
0187                  const SourceManager &SM);
0188 } // namespace internal
0189 } // end namespace clang
0190 
0191 #endif /* LLVM_CLANG_ANALYSIS_ANALYSES_UNSAFEBUFFERUSAGE_H */