Skip to content

Commit fee3baa

Browse files
SC llvm teamSC llvm team
SC llvm team
authored and
SC llvm team
committed
Merged main:99b85cae628c1cc5641944290712cd84ccf1f6c8 into amd-gfx:f09ee8740921
Local branch amd-gfx f09ee87 Merged main:c81d6665601d648c1a5349b665ee6019f3786352 into amd-gfx:80a4987ce388 Remote branch main 99b85ca [clang][bytecode][NFC] Add an additional assertion (llvm#105927)
2 parents f09ee87 + 99b85ca commit fee3baa

24 files changed

+170
-149
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,28 +466,34 @@ Sanitizers
466466

467467
- Added the ``-fsanitize-undefined-ignore-overflow-pattern`` flag which can be
468468
used to disable specific overflow-dependent code patterns. The supported
469-
patterns are: ``add-overflow-test``, ``negated-unsigned-const``, and
470-
``post-decr-while``. The sanitizer instrumentation can be toggled off for all
471-
available patterns by specifying ``all``. Conversely, you can disable all
472-
exclusions with ``none``.
469+
patterns are: ``add-signed-overflow-test``, ``add-unsigned-overflow-test``,
470+
``negated-unsigned-const``, and ``unsigned-post-decr-while``. The sanitizer
471+
instrumentation can be toggled off for all available patterns by specifying
472+
``all``. Conversely, you may disable all exclusions with ``none`` which is
473+
the default.
473474

474475
.. code-block:: c++
475476

476-
/// specified with ``-fsanitize-undefined-ignore-overflow-pattern=add-overflow-test``
477+
/// specified with ``-fsanitize-undefined-ignore-overflow-pattern=add-unsigned-overflow-test``
477478
int common_overflow_check_pattern(unsigned base, unsigned offset) {
478479
if (base + offset < base) { /* ... */ } // The pattern of `a + b < a`, and other re-orderings, won't be instrumented
479480
}
480481
482+
/// specified with ``-fsanitize-undefined-ignore-overflow-pattern=add-signed-overflow-test``
483+
int common_overflow_check_pattern_signed(signed int base, signed int offset) {
484+
if (base + offset < base) { /* ... */ } // The pattern of `a + b < a`, and other re-orderings, won't be instrumented
485+
}
486+
481487
/// specified with ``-fsanitize-undefined-ignore-overflow-pattern=negated-unsigned-const``
482488
void negation_overflow() {
483489
unsigned long foo = -1UL; // No longer causes a negation overflow warning
484490
unsigned long bar = -2UL; // and so on...
485491
}
486492

487-
/// specified with ``-fsanitize-undefined-ignore-overflow-pattern=post-decr-while``
493+
/// specified with ``-fsanitize-undefined-ignore-overflow-pattern=unsigned-post-decr-while``
488494
void while_post_decrement() {
489495
unsigned char count = 16;
490-
while (count--) { /* ... */} // No longer causes unsigned-integer-overflow sanitizer to trip
496+
while (count--) { /* ... */ } // No longer causes unsigned-integer-overflow sanitizer to trip
491497
}
492498
493499
Many existing projects have a large amount of these code patterns present.

clang/docs/UndefinedBehaviorSanitizer.rst

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,26 +314,49 @@ Currently, this option supports three overflow-dependent code idioms:
314314
unsigned long foo = -1UL; // No longer causes a negation overflow warning
315315
unsigned long bar = -2UL; // and so on...
316316

317-
``post-decr-while``
317+
``unsigned-post-decr-while``
318318

319319
.. code-block:: c++
320320

321-
/// -fsanitize-undefined-ignore-overflow-pattern=post-decr-while
321+
/// -fsanitize-undefined-ignore-overflow-pattern=unsigned-post-decr-while
322322
unsigned char count = 16;
323323
while (count--) { /* ... */ } // No longer causes unsigned-integer-overflow sanitizer to trip
324324
325-
``add-overflow-test``
325+
``add-signed-overflow-test,add-unsigned-overflow-test``
326326

327327
.. code-block:: c++
328328

329-
/// -fsanitize-undefined-ignore-overflow-pattern=add-overflow-test
329+
/// -fsanitize-undefined-ignore-overflow-pattern=add-(signed|unsigned)-overflow-test
330330
if (base + offset < base) { /* ... */ } // The pattern of `a + b < a`, and other re-orderings,
331-
// won't be instrumented (same for signed types)
331+
// won't be instrumented (signed or unsigned types)
332+
333+
.. list-table:: Overflow Pattern Types
334+
:widths: 30 50
335+
:header-rows: 1
336+
337+
* - Pattern
338+
- Sanitizer
339+
* - negated-unsigned-const
340+
- unsigned-integer-overflow
341+
* - unsigned-post-decr-while
342+
- unsigned-integer-overflow
343+
* - add-unsigned-overflow-test
344+
- unsigned-integer-overflow
345+
* - add-signed-overflow-test
346+
- signed-integer-overflow
347+
348+
349+
350+
Note: ``add-signed-overflow-test`` suppresses only the check for Undefined
351+
Behavior. Eager Undefined Behavior optimizations are still possible. One may
352+
remedy this with ``-fwrapv`` or ``-fno-strict-overflow``.
332353

333354
You can enable all exclusions with
334355
``-fsanitize-undefined-ignore-overflow-pattern=all`` or disable all exclusions
335-
with ``-fsanitize-undefined-ignore-overflow-pattern=none``. Specifying ``none``
336-
has precedence over other values.
356+
with ``-fsanitize-undefined-ignore-overflow-pattern=none``. If
357+
``-fsanitize-undefined-ignore-overflow-pattern`` is not specified ``none`` is
358+
implied. Specifying ``none`` alongside other values also implies ``none`` as
359+
``none`` has precedence over other values -- including ``all``.
337360

338361
Issue Suppression
339362
=================

clang/include/clang/Basic/LangOptions.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,13 @@ class LangOptionsBase {
375375
/// Exclude all overflow patterns (below)
376376
All = 1 << 1,
377377
/// if (a + b < a)
378-
AddOverflowTest = 1 << 2,
378+
AddSignedOverflowTest = 1 << 2,
379+
/// if (a + b < a)
380+
AddUnsignedOverflowTest = 1 << 3,
379381
/// -1UL
380-
NegUnsignedConst = 1 << 3,
382+
NegUnsignedConst = 1 << 4,
381383
/// while (count--)
382-
PostDecrInWhile = 1 << 4,
384+
PostDecrInWhile = 1 << 5,
383385
};
384386

385387
enum class DefaultVisiblityExportMapping {

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2570,7 +2570,7 @@ defm sanitize_stats : BoolOption<"f", "sanitize-stats",
25702570
def fsanitize_undefined_ignore_overflow_pattern_EQ : CommaJoined<["-"], "fsanitize-undefined-ignore-overflow-pattern=">,
25712571
HelpText<"Specify the overflow patterns to exclude from artihmetic sanitizer instrumentation">,
25722572
Visibility<[ClangOption, CC1Option]>,
2573-
Values<"none,all,add-overflow-test,negated-unsigned-const,post-decr-while">,
2573+
Values<"none,all,add-unsigned-overflow-test,add-signed-overflow-test,negated-unsigned-const,unsigned-post-decr-while">,
25742574
MarshallingInfoStringVector<LangOpts<"OverflowPatternExclusionValues">>;
25752575
def fsanitize_thread_memory_access : Flag<["-"], "fsanitize-thread-memory-access">,
25762576
Group<f_clang_Group>,

clang/lib/AST/ByteCode/IntegralAP.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ template <bool Signed> class IntegralAP final {
153153

154154
unsigned countLeadingZeros() const { return V.countl_zero(); }
155155

156-
void print(llvm::raw_ostream &OS) const { OS << V; }
156+
void print(llvm::raw_ostream &OS) const { V.print(OS, Signed);}
157157
std::string toDiagnosticString(const ASTContext &Ctx) const {
158158
std::string NameStr;
159159
llvm::raw_string_ostream OS(NameStr);

clang/lib/AST/ByteCode/Interp.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,9 +2608,11 @@ inline bool Call(InterpState &S, CodePtr OpPC, const Function *Func,
26082608
// the function we're about to call is a lambda call operator,
26092609
// skip the CheckInvoke, since the ThisPtr is a null pointer
26102610
// anyway.
2611-
if (!(S.Current->getFunction() &&
2612-
S.Current->getFunction()->isLambdaStaticInvoker() &&
2613-
Func->isLambdaCallOperator())) {
2611+
if (S.Current->getFunction() &&
2612+
S.Current->getFunction()->isLambdaStaticInvoker() &&
2613+
Func->isLambdaCallOperator()) {
2614+
assert(ThisPtr.isZero());
2615+
} else {
26142616
if (!CheckInvoke(S, OpPC, ThisPtr))
26152617
return false;
26162618
}

clang/lib/AST/Expr.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4806,6 +4806,26 @@ getOverflowPatternBinOp(const BinaryOperator *E) {
48064806
return {};
48074807
}
48084808

4809+
/// Compute and set the OverflowPatternExclusion bit based on whether the
4810+
/// BinaryOperator expression matches an overflow pattern being ignored by
4811+
/// -fsanitize-undefined-ignore-overflow-pattern=add-signed-overflow-test or
4812+
/// -fsanitize-undefined-ignore-overflow-pattern=add-unsigned-overflow-test
4813+
static void computeOverflowPatternExclusion(const ASTContext &Ctx,
4814+
const BinaryOperator *E) {
4815+
std::optional<BinaryOperator *> Result = getOverflowPatternBinOp(E);
4816+
if (!Result.has_value())
4817+
return;
4818+
QualType AdditionResultType = Result.value()->getType();
4819+
4820+
if ((AdditionResultType->isSignedIntegerType() &&
4821+
Ctx.getLangOpts().isOverflowPatternExcluded(
4822+
LangOptions::OverflowPatternExclusionKind::AddSignedOverflowTest)) ||
4823+
(AdditionResultType->isUnsignedIntegerType() &&
4824+
Ctx.getLangOpts().isOverflowPatternExcluded(
4825+
LangOptions::OverflowPatternExclusionKind::AddUnsignedOverflowTest)))
4826+
Result.value()->setExcludedOverflowPattern(true);
4827+
}
4828+
48094829
BinaryOperator::BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
48104830
Opcode opc, QualType ResTy, ExprValueKind VK,
48114831
ExprObjectKind OK, SourceLocation opLoc,
@@ -4818,12 +4838,7 @@ BinaryOperator::BinaryOperator(const ASTContext &Ctx, Expr *lhs, Expr *rhs,
48184838
BinaryOperatorBits.ExcludedOverflowPattern = false;
48194839
SubExprs[LHS] = lhs;
48204840
SubExprs[RHS] = rhs;
4821-
if (Ctx.getLangOpts().isOverflowPatternExcluded(
4822-
LangOptions::OverflowPatternExclusionKind::AddOverflowTest)) {
4823-
std::optional<BinaryOperator *> Result = getOverflowPatternBinOp(this);
4824-
if (Result.has_value())
4825-
Result.value()->BinaryOperatorBits.ExcludedOverflowPattern = true;
4826-
}
4841+
computeOverflowPatternExclusion(Ctx, this);
48274842
BinaryOperatorBits.HasFPFeatures = FPFeatures.requiresTrailingStorage();
48284843
if (hasStoredFPFeatures())
48294844
setStoredFPFeatures(FPFeatures);

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2785,7 +2785,7 @@ static bool matchesPostDecrInWhile(const UnaryOperator *UO, bool isInc,
27852785
if (isInc || isPre)
27862786
return false;
27872787

2788-
// -fsanitize-undefined-ignore-overflow-pattern=post-decr-while
2788+
// -fsanitize-undefined-ignore-overflow-pattern=unsigned-post-decr-while
27892789
if (!Ctx.getLangOpts().isOverflowPatternExcluded(
27902790
LangOptions::OverflowPatternExclusionKind::PostDecrInWhile))
27912791
return false;

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,9 +1457,12 @@ static int parseOverflowPatternExclusionValues(const Driver &D,
14571457
llvm::StringSwitch<int>(Value)
14581458
.Case("none", LangOptionsBase::None)
14591459
.Case("all", LangOptionsBase::All)
1460-
.Case("add-overflow-test", LangOptionsBase::AddOverflowTest)
1460+
.Case("add-unsigned-overflow-test",
1461+
LangOptionsBase::AddUnsignedOverflowTest)
1462+
.Case("add-signed-overflow-test",
1463+
LangOptionsBase::AddSignedOverflowTest)
14611464
.Case("negated-unsigned-const", LangOptionsBase::NegUnsignedConst)
1462-
.Case("post-decr-while", LangOptionsBase::PostDecrInWhile)
1465+
.Case("unsigned-post-decr-while", LangOptionsBase::PostDecrInWhile)
14631466
.Default(0);
14641467
if (E == 0)
14651468
D.Diag(clang::diag::err_drv_unsupported_option_argument)

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4274,9 +4274,13 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
42744274
llvm::StringSwitch<unsigned>(A->getValue(i))
42754275
.Case("none", LangOptionsBase::None)
42764276
.Case("all", LangOptionsBase::All)
4277-
.Case("add-overflow-test", LangOptionsBase::AddOverflowTest)
4277+
.Case("add-unsigned-overflow-test",
4278+
LangOptionsBase::AddUnsignedOverflowTest)
4279+
.Case("add-signed-overflow-test",
4280+
LangOptionsBase::AddSignedOverflowTest)
42784281
.Case("negated-unsigned-const", LangOptionsBase::NegUnsignedConst)
4279-
.Case("post-decr-while", LangOptionsBase::PostDecrInWhile)
4282+
.Case("unsigned-post-decr-while",
4283+
LangOptionsBase::PostDecrInWhile)
42804284
.Default(0);
42814285
}
42824286
}

0 commit comments

Comments
 (0)