Skip to content

Commit 76236fa

Browse files
[Clang] Overflow Pattern Exclusion - rename some patterns, enhance docs (#105709)
From @vitalybuka's review on #104889: - [x] remove unused variable in tests - [x] rename `post-decr-while` --> `unsigned-post-decr-while` - [x] split `add-overflow-test` into `add-unsigned-overflow-test` and `add-signed-overflow-test` - [x] be more clear about defaults within docs - [x] add table to docs Here's a screenshot of the rendered table so you don't have to build the html docs yourself to inspect the layout: ![image](https://github.com/user-attachments/assets/5d3497c4-5f5a-4579-b29b-96a0fd192faa) CCs: @vitalybuka --------- Signed-off-by: Justin Stitt <[email protected]> Co-authored-by: Vitaly Buka <[email protected]>
1 parent e185850 commit 76236fa

File tree

10 files changed

+87
-32
lines changed

10 files changed

+87
-32
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/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
}

clang/test/CodeGen/ignore-overflow-pattern-false-pos.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
// Check for potential false positives from patterns that _almost_ match classic overflow-dependent or overflow-prone code patterns
55
extern unsigned a, b, c;
6-
extern int u, v, w;
76

87
extern unsigned some(void);
98

clang/test/CodeGen/ignore-overflow-pattern.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-undefined-ignore-overflow-pattern=all %s -emit-llvm -o - | FileCheck %s
22
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-undefined-ignore-overflow-pattern=all -fwrapv %s -emit-llvm -o - | FileCheck %s
3-
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-undefined-ignore-overflow-pattern=add-overflow-test %s -emit-llvm -o - | FileCheck %s --check-prefix=ADD
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-undefined-ignore-overflow-pattern=add-signed-overflow-test,add-unsigned-overflow-test %s -emit-llvm -o - | FileCheck %s --check-prefix=ADD
44
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-undefined-ignore-overflow-pattern=negated-unsigned-const %s -emit-llvm -o - | FileCheck %s --check-prefix=NEGATE
5-
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-undefined-ignore-overflow-pattern=post-decr-while %s -emit-llvm -o - | FileCheck %s --check-prefix=WHILE
5+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow,unsigned-integer-overflow -fsanitize-undefined-ignore-overflow-pattern=unsigned-post-decr-while %s -emit-llvm -o - | FileCheck %s --check-prefix=WHILE
66

77
// Ensure some common overflow-dependent or overflow-prone code patterns don't
88
// trigger the overflow sanitizers. In many cases, overflow warnings caused by
@@ -25,6 +25,7 @@
2525
// CHECK-NOT: handle{{.*}}overflow
2626

2727
extern unsigned a, b, c;
28+
extern int u, v;
2829
extern unsigned some(void);
2930

3031
// ADD-LABEL: @basic_commutativity
@@ -50,6 +51,8 @@ void basic_commutativity(void) {
5051
c = 9;
5152
if (b > b + a)
5253
c = 9;
54+
if (u + v < u)
55+
c = 9;
5356
}
5457

5558
// ADD-LABEL: @arguments_and_commutativity

0 commit comments

Comments
 (0)