Skip to content

[-Wunsafe-buffer-usage] cherry-picks #7779

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
bfccf7c
[-Wunsafe-buffer-usage] Fix a fallthrough case in UPCStandalonePointe…
t-rasmud Jul 25, 2023
a811a8b
[-Wunsafe-buffer-usage] Ignore the FixableGadgets that will not be fi…
ziqingluo-90 Jul 25, 2023
fb2657c
[-Wunsafe-buffer-usage] Add a facility for debugging low fixit coverage
t-rasmud Jul 26, 2023
a86f10b
[clang] UnsafeBufferUsage.cpp - fix MSVC "not all control paths retur…
RKSimon Jul 27, 2023
0f3c80e
[clang][Analysis][NFC] Remove indented empty lines (fix format checking)
danix800 Jul 28, 2023
a8fd8fc
Fix the linting problems in UnsafeBufferUsage.cpp
AMP999 Aug 1, 2023
47fdcde
[-Wunsafe-buffer-usage] Replace assert that declarations are always f…
t-rasmud Aug 8, 2023
75c818e
[-Wunsafe-buffer-usage] Refactor and improve for parameter fix-its
ziqingluo-90 Aug 17, 2023
2f1b646
[-Wunsafe-buffer-usage] Fix assertion failure in case of BindingDecl
t-rasmud Aug 17, 2023
e4738fc
[-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize the code
ziqingluo-90 Aug 17, 2023
f39e223
Revert "[-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize t…
ziqingluo-90 Aug 17, 2023
4647ed6
Removed whitespace that made "grep -rnI '[[:blank:]]$' clang/lib clan…
AntonRydahl Aug 18, 2023
0375bc0
Re-land "[-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize …
ziqingluo-90 Aug 18, 2023
de8159d
[-Wunsafe-buffer-usage][NFC] Refactor `getFixIts`---where fix-its are…
ziqingluo-90 Aug 18, 2023
d52b61b
[clang][NFC] Remove redundant whitespaces
zyn0217 Aug 19, 2023
07153f2
[-Wunsafe-buffer-usage] Refactor to let local variable fix-its and pa…
ziqingluo-90 Aug 21, 2023
eaebadd
[-Wunsafe-buffer-usage] Stop generating incorrect fix-its for variabl…
ziqingluo-90 Aug 21, 2023
427914b
[Analysis] Use DenseMap::lookup (NFC)
kazutakahirata Aug 27, 2023
79c4b46
[-Wunsafe-buffer-usage] Group parameter fix-its
ziqingluo-90 Sep 21, 2023
077ebe9
[-Wunsafe-buffer-usage] Use `Strategy` to determine whether to fix a …
ziqingluo-90 Sep 21, 2023
5512df0
[NFC][CLANG] Fix static analyzer bugs about unnecessary object copies…
smanna12 Sep 28, 2023
e43554a
[-Wunsafe-buffer-usage] Extract the included part in tests to separat…
ziqingluo-90 Sep 29, 2023
2c8d696
[-Wunsafe-buffer-usage] Add AST info to the unclaimed DRE debug notes…
ziqingluo-90 Oct 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 60 additions & 5 deletions clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,46 @@

#include "clang/AST/Decl.h"
#include "clang/AST/Stmt.h"
#include "llvm/Support/Debug.h"

namespace clang {

using DefMapTy = llvm::DenseMap<const VarDecl *, std::vector<const VarDecl *>>;
using VarGrpTy = std::vector<const VarDecl *>;
using VarGrpRef = ArrayRef<const VarDecl *>;

class VariableGroupsManager {
public:
VariableGroupsManager() = default;
virtual ~VariableGroupsManager() = default;
/// Returns the set of variables (including `Var`) that need to be fixed
/// together in one step.
///
/// `Var` must be a variable that needs fix (so it must be in a group).
/// `HasParm` is an optional argument that will be set to true if the set of
/// variables, where `Var` is in, contains parameters.
virtual VarGrpRef getGroupOfVar(const VarDecl *Var,
bool *HasParm = nullptr) const = 0;

/// Returns the non-empty group of variables that include parameters of the
/// analyzing function, if such a group exists. An empty group, otherwise.
virtual VarGrpRef getGroupOfParms() const = 0;
};

/// The interface that lets the caller handle unsafe buffer usage analysis
/// results by overriding this class's handle... methods.
class UnsafeBufferUsageHandler {
#ifndef NDEBUG
public:
// A self-debugging facility that you can use to notify the user when
// suggestions or fixits are incomplete.
// Uses std::function to avoid computing the message when it won't
// actually be displayed.
using DebugNote = std::pair<SourceLocation, std::string>;
using DebugNoteList = std::vector<DebugNote>;
using DebugNoteByVar = std::map<const VarDecl *, DebugNoteList>;
DebugNoteByVar DebugNotesByVar;
#endif

public:
UnsafeBufferUsageHandler() = default;
virtual ~UnsafeBufferUsageHandler() = default;
Expand All @@ -37,12 +69,35 @@ class UnsafeBufferUsageHandler {
bool IsRelatedToDecl) = 0;

/// Invoked when a fix is suggested against a variable. This function groups
/// all variables that must be fixed together (i.e their types must be changed to the
/// same target type to prevent type mismatches) into a single fixit.
/// all variables that must be fixed together (i.e their types must be changed
/// to the same target type to prevent type mismatches) into a single fixit.
///
/// `D` is the declaration of the callable under analysis that owns `Variable`
/// and all of its group mates.
virtual void handleUnsafeVariableGroup(const VarDecl *Variable,
const DefMapTy &VarGrpMap,
FixItList &&Fixes) = 0;
const VariableGroupsManager &VarGrpMgr,
FixItList &&Fixes, const Decl *D) = 0;

#ifndef NDEBUG
public:
bool areDebugNotesRequested() {
DEBUG_WITH_TYPE("SafeBuffers", return true);
return false;
}

void addDebugNoteForVar(const VarDecl *VD, SourceLocation Loc,
std::string Text) {
if (areDebugNotesRequested())
DebugNotesByVar[VD].push_back(std::make_pair(Loc, Text));
}

void clearDebugNotes() {
if (areDebugNotesRequested())
DebugNotesByVar.clear();
}
#endif

public:
/// Returns a reference to the `Preprocessor`:
virtual bool isSafeBufferOptOut(const SourceLocation &Loc) const = 0;

Expand Down
9 changes: 9 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -11965,8 +11965,17 @@ def note_unsafe_buffer_operation : Note<
"used%select{| in pointer arithmetic| in buffer access}0 here">;
def note_unsafe_buffer_variable_fixit_group : Note<
"change type of %0 to '%select{std::span|std::array|std::span::iterator}1' to preserve bounds information%select{|, and change %2 to '%select{std::span|std::array|std::span::iterator}1' to propagate bounds information between them}3">;
def note_unsafe_buffer_variable_fixit_together : Note<
"change type of %0 to '%select{std::span|std::array|std::span::iterator}1' to preserve bounds information"
"%select{|, and change %2 to safe types to make function %4 bounds-safe}3">;
def note_safe_buffer_usage_suggestions_disabled : Note<
"pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions">;
#ifndef NDEBUG
// Not a user-facing diagnostic. Useful for debugging false negatives in
// -fsafe-buffer-usage-suggestions (i.e. lack of -Wunsafe-buffer-usage fixits).
def note_safe_buffer_debug_mode : Note<"safe buffers debug: %0">;
#endif

def err_loongarch_builtin_requires_la32 : Error<
"this builtin requires target: loongarch32">;

Expand Down
Loading