Skip to content

Commit a7438d6

Browse files
authored
[clang] [ubsan] add __has_feature for UBSan checks (#148310)
Before, we could only condition code on whether *any* UBSan check is enabled. Add separate features for each of them, now we can do e.g. __has_feature(array_bounds_sanitizer).
1 parent 6fea3da commit a7438d6

File tree

2 files changed

+207
-2
lines changed

2 files changed

+207
-2
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: 166 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,177 @@
11
// 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
2+
// RUN: %clang -E -fsanitize=alignment %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-ALIGNMENT %s
3+
// RUN: %clang -E -fsanitize=bool %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-BOOL %s
4+
// RUN: %clang -E -fsanitize=builtin %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-BUILTIN %s
5+
// RUN: %clang -E -fsanitize=array-bounds %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-ARRAY-BOUNDS %s
6+
// RUN: %clang -E -fsanitize=enum %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-ENUM %s
7+
// RUN: %clang -E -fsanitize=float-cast-overflow %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-FLOAT-CAST-OVERFLOW %s
8+
// RUN: %clang -E -fsanitize=integer-divide-by-zero %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-INTEGER-DIVIDE-BY-ZERO %s
9+
// RUN: %clang -E -fsanitize=nonnull-attribute %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-NONNULL-ATTRIBUTE %s
10+
// RUN: %clang -E -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 -O2 -fsanitize=object-size %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-OBJECT-SIZE %s
13+
// RUN: %clang -E -fsanitize=pointer-overflow %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-POINTER-OVERFLOW %s
14+
// RUN: %clang -E -fsanitize=return %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-RETURN %s
15+
// RUN: %clang -E -fsanitize=returns-nonnull-attribute %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-RETURNS-NONNULL-ATTRIBUTE %s
16+
// RUN: %clang -E -fsanitize=shift-base %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT-BASE,CHECK-SHIFT %s
17+
// RUN: %clang -E -fsanitize=shift-exponent %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT-EXPONENT,CHECK-SHIFT %s
18+
// RUN: %clang -E -fsanitize=shift %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SHIFT %s
19+
// RUN: %clang -E -fsanitize=signed-integer-overflow %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-SIGNED-INTEGER-OVERFLOW %s
20+
// RUN: %clang -E -fsanitize=unreachable %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-UNREACHABLE %s
21+
// RUN: %clang -E -fsanitize=vla-bound %s -o - | FileCheck --check-prefixes=CHECK-UBSAN,CHECK-VLA-BOUND %s
22+
// RUN: %clang -E -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+
#if !__has_feature(undefined_behavior_sanitizer_finegrained_feature_checks)
27+
#error "Missing undefined_behavior_sanitizer_finegrained_feature_checks"
28+
#endif
29+
530
#if __has_feature(undefined_behavior_sanitizer)
631
int UBSanEnabled();
732
#else
833
int UBSanDisabled();
934
#endif
1035

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

0 commit comments

Comments
 (0)