Skip to content

Commit 5f38436

Browse files
authored
[Driver] -fsanitize=undefined: don't expand to signed-integer-overflow if -fwrapv (#85501)
Linux kernel uses -fwrapv to change signed integer overflows from undefined behaviors to defined behaviors. However, the security folks still want -fsanitize=signed-integer-overflow diagnostics. Their intention can be expressed with -fwrapv -fsanitize=signed-integer-overflow (#80089). This mode by default reports recoverable errors while still making signed integer overflows defined (most UBSan checks are recoverable by default: you get errors in stderr, but the program is not halted). -fsanitize=undefined -fwrapv users likely want to suppress signed-integer-overflow, unless signed-integer-overflow is explicitly enabled. Implement this suppression.
1 parent b9a41b9 commit 5f38436

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,14 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
487487
Add &= ~NotAllowedWithExecuteOnly;
488488
if (CfiCrossDso)
489489
Add &= ~SanitizerKind::CFIMFCall;
490+
// -fsanitize=undefined does not expand to signed-integer-overflow in
491+
// -fwrapv (implied by -fno-strict-overflow) mode.
492+
if (Add & SanitizerKind::UndefinedGroup) {
493+
bool S = Args.hasFlagNoClaim(options::OPT_fno_strict_overflow,
494+
options::OPT_fstrict_overflow, false);
495+
if (Args.hasFlagNoClaim(options::OPT_fwrapv, options::OPT_fno_wrapv, S))
496+
Add &= ~SanitizerKind::SignedIntegerOverflow;
497+
}
490498
Add &= Supported;
491499

492500
if (Add & SanitizerKind::Fuzzer)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// When -fwrapv (implied by -fno-strict-overflow) is enabled,
2+
/// -fsanitize=undefined does not expand to signed-integer-overflow.
3+
/// -fsanitize=signed-integer-overflow is unaffected by -fwrapv.
4+
5+
// RUN: %clang -### --target=x86_64-linux -fwrapv -fsanitize=signed-integer-overflow %s 2>&1 | FileCheck %s
6+
// CHECK: -fsanitize=signed-integer-overflow
7+
// CHECK: -fsanitize-recover=signed-integer-overflow
8+
9+
// RUN: %clang -### --target=x86_64-linux -fno-strict-overflow -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=EXCLUDE
10+
// RUN: %clang -### --target=x86_64-linux -fstrict-overflow -fwrapv -fsanitize=undefined %s 2>&1 | FileCheck %s --check-prefix=EXCLUDE
11+
// EXCLUDE: -fsanitize=alignment,array-bounds,
12+
// EXCLUDE-NOT: signed-integer-overflow,
13+
// EXCLUDE: -fsanitize-recover=alignment,array-bounds,
14+
// EXCLUDE-SAME: signed-integer-overflow
15+
16+
// RUN: %clang -### --target=x86_64-linux -fwrapv -fsanitize=undefined -fsanitize=signed-integer-overflow %s 2>&1 | FileCheck %s --check-prefix=INCLUDE
17+
// RUN: %clang -### --target=x86_64-linux -fno-strict-overflow -fno-sanitize=signed-integer-overflow -fsanitize=undefined -fsanitize=signed-integer-overflow %s 2>&1 | FileCheck %s --check-prefix=INCLUDE
18+
// INCLUDE: -fsanitize=alignment,array-bounds,
19+
// INCLUDE-SAME: signed-integer-overflow
20+
// INCLUDE: -fsanitize-recover=alignment,array-bounds,
21+
// INCLUDE-SAME: signed-integer-overflow
22+
23+
/// -fsanitize-trap=undefined expands to signed-integer-overflow regardless of -fwrapv.
24+
// RUN: %clang -### --target=x86_64-linux -fwrapv -fsanitize=undefined -fsanitize=signed-integer-overflow -fsanitize-trap=undefined %s 2>&1 | FileCheck %s --check-prefix=INCLUDE-TRAP
25+
// INCLUDE-TRAP: -fsanitize=alignment,array-bounds,
26+
// INCLUDE-TRAP-SAME: signed-integer-overflow
27+
// INCLUDE-TRAP: -fsanitize-trap=alignment,array-bounds,
28+
// INCLUDE-TRAP-SAME: signed-integer-overflow

0 commit comments

Comments
 (0)