Skip to content

Commit

Permalink
[𝘀𝗽𝗿] changes introduced through rebase
Browse files Browse the repository at this point in the history
Created using spr 1.3.5-bogner

[skip ci]
  • Loading branch information
bogner committed Aug 15, 2024
2 parents e1b20f2 + 8107810 commit 4613ca7
Show file tree
Hide file tree
Showing 180 changed files with 4,543 additions and 1,522 deletions.
2 changes: 0 additions & 2 deletions clang-tools-extra/include-cleaner/lib/WalkAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,13 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
// specialized template. Implicit ones are filtered out by RAV.
bool
VisitClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *CTSD) {
// if (CTSD->isExplicitSpecialization())
if (clang::isTemplateExplicitInstantiationOrSpecialization(
CTSD->getTemplateSpecializationKind()))
report(CTSD->getLocation(),
CTSD->getSpecializedTemplate()->getTemplatedDecl());
return true;
}
bool VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *VTSD) {
// if (VTSD->isExplicitSpecialization())
if (clang::isTemplateExplicitInstantiationOrSpecialization(
VTSD->getTemplateSpecializationKind()))
report(VTSD->getLocation(),
Expand Down
41 changes: 41 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,20 @@ C++ Specific Potentially Breaking Changes
`-Wno-enum-constexpr-conversion`, to allow for a transition period for users.
Now, in Clang 20, **it is no longer possible to suppress the diagnostic**.

- Extraneous template headers are now ill-formed by default.
This error can be disable with ``-Wno-error=extraneous-template-head``.

.. code-block:: c++

template <> // error: extraneous template head
template <typename T>
void f();

ABI Changes in This Version
---------------------------

- Fixed Microsoft name mangling of placeholder, auto and decltype(auto), return types for MSVC 1920+. This change resolves incompatibilities with code compiled by MSVC 1920+ but will introduce incompatibilities with code compiled by earlier versions of Clang unless such code is built with the compiler option -fms-compatibility-version=19.14 to imitate the MSVC 1914 mangling behavior.

AST Dumping Potentially Breaking Changes
----------------------------------------

Expand Down Expand Up @@ -383,6 +394,36 @@ Moved checkers
Sanitizers
----------

- Added the ``-fsanitize-overflow-pattern-exclusion=`` flag which can be used
to disable specific overflow-dependent code patterns. The supported patterns
are: ``add-overflow-test``, ``negated-unsigned-const``, and
``post-decr-while``. The sanitizer instrumentation can be toggled off for all
available patterns by specifying ``all``. Conversely, you can disable all
exclusions with ``none``.

.. code-block:: c++

/// specified with ``-fsanitize-overflow-pattern-exclusion=add-overflow-test``
int common_overflow_check_pattern(unsigned base, unsigned offset) {
if (base + offset < base) { /* ... */ } // The pattern of `a + b < a`, and other re-orderings, won't be instrumented
}
/// specified with ``-fsanitize-overflow-pattern-exclusion=negated-unsigned-const``
void negation_overflow() {
unsigned long foo = -1UL; // No longer causes a negation overflow warning
unsigned long bar = -2UL; // and so on...
}

/// specified with ``-fsanitize-overflow-pattern-exclusion=post-decr-while``
void while_post_decrement() {
unsigned char count = 16;
while (count--) { /* ... */} // No longer causes unsigned-integer-overflow sanitizer to trip
}
Many existing projects have a large amount of these code patterns present.
This new flag should allow those projects to enable integer sanitizers with
less noise.

Python Binding Changes
----------------------
- Fixed an issue that led to crashes when calling ``Type.get_exception_specification_kind``.
Expand Down
42 changes: 42 additions & 0 deletions clang/docs/UndefinedBehaviorSanitizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,48 @@ To silence reports from unsigned integer overflow, you can set
``-fsanitize-recover=unsigned-integer-overflow``, is particularly useful for
providing fuzzing signal without blowing up logs.

Disabling instrumentation for common overflow patterns
------------------------------------------------------

There are certain overflow-dependent or overflow-prone code patterns which
produce a lot of noise for integer overflow/truncation sanitizers. Negated
unsigned constants, post-decrements in a while loop condition and simple
overflow checks are accepted and pervasive code patterns. However, the signal
received from sanitizers instrumenting these code patterns may be too noisy for
some projects. To disable instrumentation for these common patterns one should
use ``-fsanitize-overflow-pattern-exclusion=``.

Currently, this option supports three overflow-dependent code idioms:

``negated-unsigned-const``

.. code-block:: c++

/// -fsanitize-overflow-pattern-exclusion=negated-unsigned-const
unsigned long foo = -1UL; // No longer causes a negation overflow warning
unsigned long bar = -2UL; // and so on...

``post-decr-while``

.. code-block:: c++

/// -fsanitize-overflow-pattern-exclusion=post-decr-while
unsigned char count = 16;
while (count--) { /* ... */ } // No longer causes unsigned-integer-overflow sanitizer to trip
``add-overflow-test``

.. code-block:: c++

/// -fsanitize-overflow-pattern-exclusion=add-overflow-test
if (base + offset < base) { /* ... */ } // The pattern of `a + b < a`, and other re-orderings,
// won't be instrumented (same for signed types)
You can enable all exclusions with
``-fsanitize-overflow-pattern-exclusion=all`` or disable all exclusions with
``-fsanitize-overflow-pattern-exclusion=none``. Specifying ``none`` has
precedence over other values.

Issue Suppression
=================

Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,10 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
/// Set the C++11 in-class initializer for this member.
void setInClassInitializer(Expr *NewInit);

/// Find the FieldDecl specified in a FAM's "counted_by" attribute. Returns
/// \p nullptr if either the attribute or the field doesn't exist.
const FieldDecl *findCountedByField() const;

private:
void setLazyInClassInitializer(LazyDeclStmtPtr NewInit);

Expand Down
9 changes: 9 additions & 0 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -4043,6 +4043,15 @@ class BinaryOperator : public Expr {
void setHasStoredFPFeatures(bool B) { BinaryOperatorBits.HasFPFeatures = B; }
bool hasStoredFPFeatures() const { return BinaryOperatorBits.HasFPFeatures; }

/// Set and get the bit that informs arithmetic overflow sanitizers whether
/// or not they should exclude certain BinaryOperators from instrumentation
void setExcludedOverflowPattern(bool B) {
BinaryOperatorBits.ExcludedOverflowPattern = B;
}
bool hasExcludedOverflowPattern() const {
return BinaryOperatorBits.ExcludedOverflowPattern;
}

/// Get FPFeatures from trailing storage
FPOptionsOverride getStoredFPFeatures() const {
assert(hasStoredFPFeatures());
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ class alignas(void *) Stmt {
LLVM_PREFERRED_TYPE(bool)
unsigned HasFPFeatures : 1;

/// Whether or not this BinaryOperator should be excluded from integer
/// overflow sanitization.
LLVM_PREFERRED_TYPE(bool)
unsigned ExcludedOverflowPattern : 1;

SourceLocation OpLoc;
};

Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -5428,7 +5428,8 @@ def err_template_spec_extra_headers : Error<
"extraneous template parameter list in template specialization or "
"out-of-line template definition">;
def ext_template_spec_extra_headers : ExtWarn<
"extraneous template parameter list in template specialization">;
"extraneous template parameter list in template specialization">,
InGroup<DiagGroup<"extraneous-template-head">>, DefaultError;
def note_explicit_template_spec_does_not_need_header : Note<
"'template<>' header not required for explicitly-specialized class %0 "
"declared here">;
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ VALUE_LANGOPT(TrivialAutoVarInitMaxSize, 32, 0,
"stop trivial automatic variable initialization if var size exceeds the specified size (in bytes). Must be greater than 0.")
ENUM_LANGOPT(SignedOverflowBehavior, SignedOverflowBehaviorTy, 2, SOB_Undefined,
"signed integer overflow handling")
LANGOPT(IgnoreNegationOverflow, 1, 0, "ignore overflow caused by negation")
LANGOPT(SanitizeOverflowIdioms, 1, 1, "enable instrumentation for common overflow idioms")
ENUM_LANGOPT(ThreadModel , ThreadModelKind, 2, ThreadModelKind::POSIX, "Thread Model")

BENIGN_LANGOPT(ArrowDepth, 32, 256,
Expand Down
28 changes: 28 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,21 @@ class LangOptionsBase {
PerThread,
};

/// Exclude certain code patterns from being instrumented by arithmetic
/// overflow sanitizers
enum OverflowPatternExclusionKind {
/// Don't exclude any overflow patterns from sanitizers
None = 1 << 0,
/// Exclude all overflow patterns (below)
All = 1 << 1,
/// if (a + b < a)
AddOverflowTest = 1 << 2,
/// -1UL
NegUnsignedConst = 1 << 3,
/// while (count--)
PostDecrInWhile = 1 << 4,
};

enum class DefaultVisiblityExportMapping {
None,
/// map only explicit default visibilities to exported
Expand Down Expand Up @@ -555,6 +570,11 @@ class LangOptions : public LangOptionsBase {
/// The default stream kind used for HIP kernel launching.
GPUDefaultStreamKind GPUDefaultStream;

/// Which overflow patterns should be excluded from sanitizer instrumentation
unsigned OverflowPatternExclusionMask = 0;

std::vector<std::string> OverflowPatternExclusionValues;

/// The seed used by the randomize structure layout feature.
std::string RandstructSeed;

Expand Down Expand Up @@ -630,6 +650,14 @@ class LangOptions : public LangOptionsBase {
return MSCompatibilityVersion >= MajorVersion * 100000U;
}

bool isOverflowPatternExcluded(OverflowPatternExclusionKind Kind) const {
if (OverflowPatternExclusionMask & OverflowPatternExclusionKind::None)
return false;
if (OverflowPatternExclusionMask & OverflowPatternExclusionKind::All)
return true;
return OverflowPatternExclusionMask & Kind;
}

/// Reset all of the options that are not considered when building a
/// module.
void resetNonModularOptions();
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -2565,6 +2565,11 @@ defm sanitize_stats : BoolOption<"f", "sanitize-stats",
"Disable">,
BothFlags<[], [ClangOption], " sanitizer statistics gathering.">>,
Group<f_clang_Group>;
def fsanitize_overflow_pattern_exclusion_EQ : CommaJoined<["-"], "fsanitize-overflow-pattern-exclusion=">,
HelpText<"Specify the overflow patterns to exclude from artihmetic sanitizer instrumentation">,
Visibility<[ClangOption, CC1Option]>,
Values<"none,all,add-overflow-test,negated-unsigned-const,post-decr-while">,
MarshallingInfoStringVector<LangOpts<"OverflowPatternExclusionValues">>;
def fsanitize_thread_memory_access : Flag<["-"], "fsanitize-thread-memory-access">,
Group<f_clang_Group>,
HelpText<"Enable memory access instrumentation in ThreadSanitizer (default)">;
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Driver/SanitizerArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SanitizerArgs {
std::vector<std::string> BinaryMetadataIgnorelistFiles;
int CoverageFeatures = 0;
int BinaryMetadataFeatures = 0;
int OverflowPatternExclusions = 0;
int MsanTrackOrigins = 0;
bool MsanUseAfterDtor = true;
bool MsanParamRetval = true;
Expand Down
13 changes: 13 additions & 0 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4678,6 +4678,19 @@ void FieldDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const {
DeclaratorDecl::printName(OS, Policy);
}

const FieldDecl *FieldDecl::findCountedByField() const {
const auto *CAT = getType()->getAs<CountAttributedType>();
if (!CAT)
return nullptr;

const auto *CountDRE = cast<DeclRefExpr>(CAT->getCountExpr());
const auto *CountDecl = CountDRE->getDecl();
if (const auto *IFD = dyn_cast<IndirectFieldDecl>(CountDecl))
CountDecl = IFD->getAnonField();

return dyn_cast<FieldDecl>(CountDecl);
}

//===----------------------------------------------------------------------===//
// TagDecl Implementation
//===----------------------------------------------------------------------===//
Expand Down
54 changes: 54 additions & 0 deletions clang/lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4759,6 +4759,53 @@ ParenListExpr *ParenListExpr::CreateEmpty(const ASTContext &Ctx,
return new (Mem) ParenListExpr(EmptyShell(), NumExprs);
}

/// Certain overflow-dependent code patterns can have their integer overflow
/// sanitization disabled. Check for the common pattern `if (a + b < a)` and
/// return the resulting BinaryOperator responsible for the addition so we can
/// elide overflow checks during codegen.
static std::optional<BinaryOperator *>
getOverflowPatternBinOp(const BinaryOperator *E) {
Expr *Addition, *ComparedTo;
if (E->getOpcode() == BO_LT) {
Addition = E->getLHS();
ComparedTo = E->getRHS();
} else if (E->getOpcode() == BO_GT) {
Addition = E->getRHS();
ComparedTo = E->getLHS();
} else {
return {};
}

const Expr *AddLHS = nullptr, *AddRHS = nullptr;
BinaryOperator *BO = dyn_cast<BinaryOperator>(Addition);

if (BO && BO->getOpcode() == clang::BO_Add) {
// now store addends for lookup on other side of '>'
AddLHS = BO->getLHS();
AddRHS = BO->getRHS();
}

if (!AddLHS || !AddRHS)
return {};

const Decl *LHSDecl, *RHSDecl, *OtherDecl;

LHSDecl = AddLHS->IgnoreParenImpCasts()->getReferencedDeclOfCallee();
RHSDecl = AddRHS->IgnoreParenImpCasts()->getReferencedDeclOfCallee();
OtherDecl = ComparedTo->IgnoreParenImpCasts()->getReferencedDeclOfCallee();

if (!OtherDecl)
return {};

if (!LHSDecl && !RHSDecl)
return {};

if ((LHSDecl && LHSDecl == OtherDecl && LHSDecl != RHSDecl) ||
(RHSDecl && RHSDecl == OtherDecl && RHSDecl != LHSDecl))
return BO;
return {};
}

BinaryOperator::BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
Opcode opc, QualType ResTy, ExprValueKind VK,
ExprObjectKind OK, SourceLocation opLoc,
Expand All @@ -4768,8 +4815,15 @@ BinaryOperator::BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
assert(!isCompoundAssignmentOp() &&
"Use CompoundAssignOperator for compound assignments");
BinaryOperatorBits.OpLoc = opLoc;
BinaryOperatorBits.ExcludedOverflowPattern = 0;
SubExprs[LHS] = lhs;
SubExprs[RHS] = rhs;
if (Ctx.getLangOpts().isOverflowPatternExcluded(
LangOptions::OverflowPatternExclusionKind::AddOverflowTest)) {
std::optional<BinaryOperator *> Result = getOverflowPatternBinOp(this);
if (Result.has_value())
Result.value()->BinaryOperatorBits.ExcludedOverflowPattern = 1;
}
BinaryOperatorBits.HasFPFeatures = FPFeatures.requiresTrailingStorage();
if (hasStoredFPFeatures())
setStoredFPFeatures(FPFeatures);
Expand Down
Loading

0 comments on commit 4613ca7

Please sign in to comment.