Skip to content

Commit 22b21f3

Browse files
authored
Reapply "[clang] [ubsan] add __has_feature for UBSan checks" (#148322) (#148323)
This reverts commit 102c15a.
1 parent 395643e commit 22b21f3

File tree

2 files changed

+210
-3
lines changed

2 files changed

+210
-3
lines changed

clang/include/clang/Basic/Features.def

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,47 @@ FEATURE(memtag_globals,
5454
FEATURE(xray_instrument, LangOpts.XRayInstrument)
5555
FEATURE(undefined_behavior_sanitizer,
5656
LangOpts.Sanitize.hasOneOf(SanitizerKind::Undefined))
57+
FEATURE(undefined_behavior_sanitizer_finegrained_feature_checks, true)
58+
// These are all part of undefined_behavior_sanitizer:
59+
FEATURE(alignment_sanitizer,
60+
LangOpts.Sanitize.has(SanitizerKind::Alignment))
61+
FEATURE(bool_sanitizer,
62+
LangOpts.Sanitize.has(SanitizerKind::Bool))
63+
FEATURE(builtin_sanitizer,
64+
LangOpts.Sanitize.has(SanitizerKind::Builtin))
65+
FEATURE(array_bounds_sanitizer,
66+
LangOpts.Sanitize.has(SanitizerKind::ArrayBounds))
67+
FEATURE(enum_sanitizer,
68+
LangOpts.Sanitize.has(SanitizerKind::Enum))
69+
FEATURE(float_cast_overflow_sanitizer,
70+
LangOpts.Sanitize.has(SanitizerKind::FloatCastOverflow))
71+
FEATURE(integer_divide_by_zero_sanitizer,
72+
LangOpts.Sanitize.has(SanitizerKind::IntegerDivideByZero))
73+
FEATURE(nonnull_attribute_sanitizer,
74+
LangOpts.Sanitize.has(SanitizerKind::NonnullAttribute))
75+
FEATURE(null_sanitizer,
76+
LangOpts.Sanitize.has(SanitizerKind::Null))
77+
FEATURE(object_size_sanitizer,
78+
LangOpts.Sanitize.has(SanitizerKind::ObjectSize))
79+
FEATURE(pointer_overflow_sanitizer,
80+
LangOpts.Sanitize.has(SanitizerKind::PointerOverflow))
81+
FEATURE(return_sanitizer,
82+
LangOpts.Sanitize.has(SanitizerKind::Return))
83+
FEATURE(returns_nonnull_attribute_sanitizer,
84+
LangOpts.Sanitize.has(SanitizerKind::ReturnsNonnullAttribute))
85+
FEATURE(shift_base_sanitizer, LangOpts.Sanitize.has(SanitizerKind::ShiftBase))
86+
FEATURE(shift_exponent_sanitizer, LangOpts.Sanitize.has(SanitizerKind::ShiftExponent))
87+
FEATURE(shift_sanitizer,
88+
LangOpts.Sanitize.hasOneOf(SanitizerKind::Shift))
89+
FEATURE(signed_integer_overflow_sanitizer,
90+
LangOpts.Sanitize.has(SanitizerKind::SignedIntegerOverflow))
91+
FEATURE(unreachable_sanitizer,
92+
LangOpts.Sanitize.has(SanitizerKind::Unreachable))
93+
FEATURE(vla_bound_sanitizer,
94+
LangOpts.Sanitize.has(SanitizerKind::VLABound))
95+
FEATURE(function_sanitizer,
96+
LangOpts.Sanitize.has(SanitizerKind::Function))
97+
5798
FEATURE(realtime_sanitizer,
5899
LangOpts.Sanitize.has(SanitizerKind::Realtime))
59100
FEATURE(coverage_sanitizer, LangOpts.SanitizeCoverage)
Lines changed: 169 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,179 @@
1-
// RUN: %clang -E -fsanitize=undefined %s -o - | FileCheck --check-prefix=CHECK-UBSAN %s
2-
// RUN: %clang -E -fsanitize=alignment %s -o - | FileCheck --check-prefix=CHECK-ALIGNMENT %s
1+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=undefined %s -o - | FileCheck --check-prefix=CHECK-UBSAN %s
2+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=alignment %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-ALIGNMENT %s
3+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=bool %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-BOOL %s
4+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=builtin %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-BUILTIN %s
5+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=array-bounds %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-ARRAY-BOUNDS %s
6+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=enum %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-ENUM %s
7+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=float-cast-overflow %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-FLOAT-CAST-OVERFLOW %s
8+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=integer-divide-by-zero %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-INTEGER-DIVIDE-BY-ZERO %s
9+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=nonnull-attribute %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-NONNULL-ATTRIBUTE %s
10+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=null %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-NULL %s
11+
// object-size is a no-op at O0.
12+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -O2 -fsanitize=object-size %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-OBJECT-SIZE %s
13+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=pointer-overflow %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-POINTER-OVERFLOW %s
14+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=return %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-RETURN %s
15+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=returns-nonnull-attribute %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-RETURNS-NONNULL-ATTRIBUTE %s
16+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=shift-base %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT-BASE,CHECK-SHIFT %s
17+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=shift-exponent %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT-EXPONENT,CHECK-SHIFT %s
18+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=shift %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT %s
19+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=signed-integer-overflow %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SIGNED-INTEGER-OVERFLOW %s
20+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=unreachable %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-UNREACHABLE %s
21+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=vla-bound %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-VLA-BOUND %s
22+
// RUN: %clang -E -target x86_64-unknown-linux-gnu -fsanitize=function %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-FUNCTION %s
23+
324
// RUN: %clang -E %s -o - | FileCheck --check-prefix=CHECK-NO-UBSAN %s
425

26+
// REQUIRES: x86-registered-target
27+
28+
#if !__has_feature(undefined_behavior_sanitizer_finegrained_feature_checks)
29+
#error "Missing undefined_behavior_sanitizer_finegrained_feature_checks"
30+
#endif
31+
532
#if __has_feature(undefined_behavior_sanitizer)
633
int UBSanEnabled();
734
#else
835
int UBSanDisabled();
936
#endif
1037

38+
#if __has_feature(alignment_sanitizer)
39+
int AlignmentSanitizerEnabled();
40+
#else
41+
int AlignmentSanitizerDisabled();
42+
#endif
43+
44+
#if __has_feature(bool_sanitizer)
45+
int BoolSanitizerEnabled();
46+
#else
47+
int BoolSanitizerDisabled();
48+
#endif
49+
50+
#if __has_feature(builtin_sanitizer)
51+
int BuiltinSanitizerEnabled();
52+
#else
53+
int BuiltinSanitizerDisabled();
54+
#endif
55+
56+
#if __has_feature(array_bounds_sanitizer)
57+
int ArrayBoundsSanitizerEnabled();
58+
#else
59+
int ArrayBoundsSanitizerDisabled();
60+
#endif
61+
62+
#if __has_feature(enum_sanitizer)
63+
int EnumSanitizerEnabled();
64+
#else
65+
int EnumSanitizerDisabled();
66+
#endif
67+
68+
#if __has_feature(float_cast_overflow_sanitizer)
69+
int FloatCastOverflowSanitizerEnabled();
70+
#else
71+
int FloatCastOverflowSanitizerDisabled();
72+
#endif
73+
74+
#if __has_feature(integer_divide_by_zero_sanitizer)
75+
int IntegerDivideByZeroSanitizerEnabled();
76+
#else
77+
int IntegerDivideByZeroSanitizerDisabled();
78+
#endif
79+
80+
#if __has_feature(nonnull_attribute_sanitizer)
81+
int NonnullAttributeSanitizerEnabled();
82+
#else
83+
int NonnullAttributeSanitizerDisabled();
84+
#endif
85+
86+
#if __has_feature(null_sanitizer)
87+
int NullSanitizerEnabled();
88+
#else
89+
int NullSanitizerDisabled();
90+
#endif
91+
92+
#if __has_feature(object_size_sanitizer)
93+
int ObjectSizeSanitizerEnabled();
94+
#else
95+
int ObjectSizeSanitizerDisabled();
96+
#endif
97+
98+
#if __has_feature(pointer_overflow_sanitizer)
99+
int PointerOverflowSanitizerEnabled();
100+
#else
101+
int PointerOverflowSanitizerDisabled();
102+
#endif
103+
104+
#if __has_feature(return_sanitizer)
105+
int ReturnSanitizerEnabled();
106+
#else
107+
int ReturnSanitizerDisabled();
108+
#endif
109+
110+
#if __has_feature(returns_nonnull_attribute_sanitizer)
111+
int ReturnsNonnullAttributeSanitizerEnabled();
112+
#else
113+
int ReturnsNonnullAttributeSanitizerDisabled();
114+
#endif
115+
116+
#if __has_feature(shift_base_sanitizer)
117+
int ShiftBaseSanitizerEnabled();
118+
#else
119+
int ShiftBaseSanitizerDisabled();
120+
#endif
121+
122+
#if __has_feature(shift_exponent_sanitizer)
123+
int ShiftExponentSanitizerEnabled();
124+
#else
125+
int ShiftExponentSanitizerDisabled();
126+
#endif
127+
128+
#if __has_feature(shift_sanitizer)
129+
int ShiftSanitizerEnabled();
130+
#else
131+
int ShiftSanitizerDisabled();
132+
#endif
133+
134+
#if __has_feature(signed_integer_overflow_sanitizer)
135+
int SignedIntegerOverflowSanitizerEnabled();
136+
#else
137+
int SignedIntegerOverflowSanitizerDisabled();
138+
#endif
139+
140+
#if __has_feature(unreachable_sanitizer)
141+
int UnreachableSanitizerEnabled();
142+
#else
143+
int UnreachableSanitizerDisabled();
144+
#endif
145+
146+
#if __has_feature(vla_bound_sanitizer)
147+
int VLABoundSanitizerEnabled();
148+
#else
149+
int VLABoundSanitizerDisabled();
150+
#endif
151+
152+
#if __has_feature(function_sanitizer)
153+
int FunctionSanitizerEnabled();
154+
#else
155+
int FunctionSanitizerDisabled();
156+
#endif
157+
11158
// CHECK-UBSAN: UBSanEnabled
12-
// CHECK-ALIGNMENT: UBSanEnabled
159+
// CHECK-ALIGNMENT: AlignmentSanitizerEnabled
160+
// CHECK-BOOL: BoolSanitizerEnabled
161+
// CHECK-BUILTIN: BuiltinSanitizerEnabled
162+
// CHECK-ARRAY-BOUNDS: ArrayBoundsSanitizerEnabled
163+
// CHECK-ENUM: EnumSanitizerEnabled
164+
// CHECK-FLOAT-CAST-OVERFLOW: FloatCastOverflowSanitizerEnabled
165+
// CHECK-INTEGER-DIVIDE-BY-ZERO: IntegerDivideByZeroSanitizerEnabled
166+
// CHECK-NONNULL-ATTRIBUTE: NonnullAttributeSanitizerEnabled
167+
// CHECK-NULL: NullSanitizerEnabled
168+
// CHECK-OBJECT-SIZE: ObjectSizeSanitizerEnabled
169+
// CHECK-POINTER-OVERFLOW: PointerOverflowSanitizerEnabled
170+
// CHECK-RETURN: ReturnSanitizerEnabled
171+
// CHECK-RETURNS-NONNULL-ATTRIBUTE: ReturnsNonnullAttributeSanitizerEnabled
172+
// CHECK-SHIFT-BASE: ShiftBaseSanitizerEnabled
173+
// CHECK-SHIFT-EXPONENT: ShiftExponentSanitizerEnabled
174+
// CHECK-SHIFT: ShiftSanitizerEnabled
175+
// CHECK-SIGNED-INTEGER-OVERFLOW: SignedIntegerOverflowSanitizerEnabled
176+
// CHECK-UNREACHABLE: UnreachableSanitizerEnabled
177+
// CHECK-VLA-BOUND: VLABoundSanitizerEnabled
178+
// CHECK-FUNCTION: FunctionSanitizerEnabled
13179
// CHECK-NO-UBSAN: UBSanDisabled

0 commit comments

Comments
 (0)