Skip to content

Commit 21d25d2

Browse files
authored
[ubsan] Suppression by type for -fsanitize=enum (#114754)
Similar to #107332.
1 parent 2c95fb9 commit 21d25d2

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -872,10 +872,9 @@ Sanitizers
872872
This new flag should allow those projects to enable integer sanitizers with
873873
less noise.
874874

875-
- Arithmetic overflow sanitizers ``-fsanitize=signed-integer-overflow`` and
876-
``-fsanitize=unsigned-integer-overflow`` as well as the implicit integer
877-
truncation sanitizers ``-fsanitize=implicit-signed-integer-truncation`` and
878-
``-fsanitize=implicit-unsigned-integer-truncation`` now properly support the
875+
- ``-fsanitize=signed-integer-overflow``, ``-fsanitize=unsigned-integer-overflow``,
876+
``-fsanitize=implicit-signed-integer-truncation``, ``-fsanitize=implicit-unsigned-integer-truncation``,
877+
``-fsanitize=enum`` now properly support the
879878
"type" prefix within `Sanitizer Special Case Lists (SSCL)
880879
<https://clang.llvm.org/docs/SanitizerSpecialCaseList.html>`_. See that link
881880
for examples.

clang/docs/SanitizerSpecialCaseList.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ Goal and usage
1616
==============
1717

1818
Users of sanitizer tools, such as :doc:`AddressSanitizer`,
19-
:doc:`ThreadSanitizer`, :doc:`MemorySanitizer` or :doc:`UndefinedBehaviorSanitizer`
20-
may want to disable or alter some checks for certain source-level entities to:
19+
:doc:`HardwareAssistedAddressSanitizerDesign`, :doc:`ThreadSanitizer`,
20+
:doc:`MemorySanitizer` or :doc:`UndefinedBehaviorSanitizer` may want to disable
21+
or alter some checks for certain source-level entities to:
2122

2223
* speedup hot function, which is known to be correct;
2324
* ignore a function that does some low-level magic (e.g. walks through the
@@ -51,11 +52,10 @@ Example
5152
Usage with UndefinedBehaviorSanitizer
5253
=====================================
5354

54-
The arithmetic overflow sanitizers ``unsigned-integer-overflow`` and
55-
``signed-integer-overflow`` as well as the implicit integer truncation
56-
sanitizers ``implicit-signed-integer-truncation`` and
57-
``implicit-unsigned-integer-truncation`` support the ability to adjust
58-
instrumentation based on type.
55+
``unsigned-integer-overflow``, ``signed-integer-overflow``,
56+
``implicit-signed-integer-truncation``,
57+
``implicit-unsigned-integer-truncation``, and ``enum`` sanitizers support the
58+
ability to adjust instrumentation based on type.
5959

6060
By default, supported sanitizers will have their instrumentation disabled for
6161
types specified within an ignorelist.

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,10 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
19411941
cast<llvm::IntegerType>(Value->getType())->getBitWidth() == 1)
19421942
return false;
19431943

1944+
if (NeedsEnumCheck &&
1945+
getContext().isTypeIgnoredBySanitizer(SanitizerKind::Enum, Ty))
1946+
return false;
1947+
19441948
llvm::APInt Min, End;
19451949
if (!getRangeForType(*this, Ty, Min, End, /*StrictEnums=*/true, IsBool))
19461950
return true;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=enum -fsanitize-ignorelist=%t/ignorelist -emit-llvm %t/test.cpp -o - | FileCheck %s --implicit-check-not="call void @__ubsan_handle"
5+
6+
//--- ignorelist
7+
[enum]
8+
type:IgnoreEnum
9+
10+
//--- test.cpp
11+
enum IgnoreEnum {
12+
A,
13+
B,
14+
C,
15+
};
16+
17+
// CHECK-LABEL: define dso_local noundef i32 @_Z6ignore10IgnoreEnum
18+
int ignore(IgnoreEnum v) {
19+
return v;
20+
}
21+
22+
23+
enum CheckEnum {
24+
X,
25+
Y,
26+
Z,
27+
};
28+
29+
// CHECK-LABEL: define dso_local noundef i32 @_Z5check9CheckEnum
30+
// CHECK: call void @__ubsan_handle_load_invalid_value_abort
31+
int check(CheckEnum v) {
32+
return v;
33+
}

0 commit comments

Comments
 (0)