Skip to content

Commit ee9192b

Browse files
committed
add fsan_cov_group
1 parent a709c39 commit ee9192b

File tree

4 files changed

+85
-26
lines changed

4 files changed

+85
-26
lines changed

clang/include/clang/Options/Options.td

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ def hlsl_Group : OptionGroup<"<HLSL group>">, Group<f_Group>,
201201
DocName<"HLSL options">,
202202
Visibility<[ClangOption]>;
203203

204+
def fsan_cov_Group : OptionGroup<"<-fsanitize-coverage group>">,
205+
Group<f_clang_Group>,
206+
DocName<"Sanitizer Coverage options">;
207+
204208
// Feature groups - these take command line options that correspond directly to
205209
// target specific features and can be translated directly from command line
206210
// options.
@@ -2407,26 +2411,26 @@ def : Flag<["-"], "fno-sanitize-blacklist">,
24072411
Group<f_clang_Group>, Flags<[HelpHidden]>, Alias<fno_sanitize_ignorelist>;
24082412

24092413
def fsanitize_coverage : CommaJoined<["-"], "fsanitize-coverage=">,
2410-
Group<f_clang_Group>,
2414+
Group<fsan_cov_Group>,
24112415
HelpText<"Specify the type of coverage instrumentation for Sanitizers">;
24122416
def fno_sanitize_coverage : CommaJoined<["-"], "fno-sanitize-coverage=">,
2413-
Group<f_clang_Group>, Visibility<[ClangOption, CLOption]>,
2417+
Group<fsan_cov_Group>, Visibility<[ClangOption, CLOption]>,
24142418
HelpText<"Disable features of coverage instrumentation for Sanitizers">,
24152419
Values<"func,bb,edge,indirect-calls,trace-bb,trace-cmp,trace-div,trace-gep,"
24162420
"8bit-counters,trace-pc,trace-pc-guard,no-prune,inline-8bit-counters,"
24172421
"inline-bool-flag">;
24182422
def fsanitize_coverage_allowlist : Joined<["-"], "fsanitize-coverage-allowlist=">,
2419-
Group<f_clang_Group>, Visibility<[ClangOption, CLOption]>,
2423+
Group<fsan_cov_Group>, Visibility<[ClangOption, CLOption]>,
24202424
HelpText<"Restrict sanitizer coverage instrumentation exclusively to modules and functions that match the provided special case list, except the blocked ones">,
24212425
MarshallingInfoStringVector<CodeGenOpts<"SanitizeCoverageAllowlistFiles">>;
24222426
def fsanitize_coverage_ignorelist : Joined<["-"], "fsanitize-coverage-ignorelist=">,
2423-
Group<f_clang_Group>, Visibility<[ClangOption, CLOption]>,
2427+
Group<fsan_cov_Group>, Visibility<[ClangOption, CLOption]>,
24242428
HelpText<"Disable sanitizer coverage instrumentation for modules and functions "
24252429
"that match the provided special case list, even the allowed ones">,
24262430
MarshallingInfoStringVector<CodeGenOpts<"SanitizeCoverageIgnorelistFiles">>;
24272431
def fsanitize_coverage_stack_depth_callback_min_EQ
24282432
: Joined<["-"], "fsanitize-coverage-stack-depth-callback-min=">,
2429-
Group<f_clang_Group>,
2433+
Group<fsan_cov_Group>,
24302434
MetaVarName<"<M>">,
24312435
HelpText<"Use callback for max stack depth tracing with minimum stack "
24322436
"depth M">,
@@ -7901,70 +7905,87 @@ def linker_option : Joined<["--"], "linker-option=">,
79017905
HelpText<"Add linker option">,
79027906
MarshallingInfoStringVector<CodeGenOpts<"LinkerOptions">>;
79037907
def fsanitize_coverage_type : Joined<["-"], "fsanitize-coverage-type=">,
7908+
Group<fsan_cov_Group>,
79047909
HelpText<"Sanitizer coverage type">,
79057910
MarshallingInfoInt<CodeGenOpts<"SanitizeCoverageType">>;
79067911
def fsanitize_coverage_indirect_calls
79077912
: Flag<["-"], "fsanitize-coverage-indirect-calls">,
7913+
Group<fsan_cov_Group>,
79087914
HelpText<"Enable sanitizer coverage for indirect calls">,
79097915
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageIndirectCalls">>;
79107916
def fsanitize_coverage_trace_bb
79117917
: Flag<["-"], "fsanitize-coverage-trace-bb">,
7918+
Group<fsan_cov_Group>,
79127919
HelpText<"Enable basic block tracing in sanitizer coverage">,
79137920
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceBB">>;
79147921
def fsanitize_coverage_trace_cmp
79157922
: Flag<["-"], "fsanitize-coverage-trace-cmp">,
7923+
Group<fsan_cov_Group>,
79167924
HelpText<"Enable cmp instruction tracing in sanitizer coverage">,
79177925
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceCmp">>;
79187926
def fsanitize_coverage_trace_div
79197927
: Flag<["-"], "fsanitize-coverage-trace-div">,
7928+
Group<fsan_cov_Group>,
79207929
HelpText<"Enable div instruction tracing in sanitizer coverage">,
79217930
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceDiv">>;
79227931
def fsanitize_coverage_trace_gep
79237932
: Flag<["-"], "fsanitize-coverage-trace-gep">,
7933+
Group<fsan_cov_Group>,
79247934
HelpText<"Enable gep instruction tracing in sanitizer coverage">,
79257935
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceGep">>;
79267936
def fsanitize_coverage_8bit_counters
79277937
: Flag<["-"], "fsanitize-coverage-8bit-counters">,
7938+
Group<fsan_cov_Group>,
79287939
HelpText<"Enable frequency counters in sanitizer coverage">,
79297940
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverage8bitCounters">>;
79307941
def fsanitize_coverage_inline_8bit_counters
79317942
: Flag<["-"], "fsanitize-coverage-inline-8bit-counters">,
7943+
Group<fsan_cov_Group>,
79327944
HelpText<"Enable inline 8-bit counters in sanitizer coverage">,
79337945
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageInline8bitCounters">>;
79347946
def fsanitize_coverage_inline_bool_flag
79357947
: Flag<["-"], "fsanitize-coverage-inline-bool-flag">,
7948+
Group<fsan_cov_Group>,
79367949
HelpText<"Enable inline bool flag in sanitizer coverage">,
79377950
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageInlineBoolFlag">>;
79387951
def fsanitize_coverage_pc_table
79397952
: Flag<["-"], "fsanitize-coverage-pc-table">,
7953+
Group<fsan_cov_Group>,
79407954
HelpText<"Create a table of coverage-instrumented PCs">,
79417955
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoveragePCTable">>;
79427956
def fsanitize_coverage_control_flow
79437957
: Flag<["-"], "fsanitize-coverage-control-flow">,
7958+
Group<fsan_cov_Group>,
79447959
HelpText<"Collect control flow of function">,
79457960
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageControlFlow">>;
79467961
def fsanitize_coverage_trace_pc
79477962
: Flag<["-"], "fsanitize-coverage-trace-pc">,
7963+
Group<fsan_cov_Group>,
79487964
HelpText<"Enable PC tracing in sanitizer coverage">,
79497965
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTracePC">>;
79507966
def fsanitize_coverage_trace_pc_guard
79517967
: Flag<["-"], "fsanitize-coverage-trace-pc-guard">,
7968+
Group<fsan_cov_Group>,
79527969
HelpText<"Enable PC tracing with guard in sanitizer coverage">,
79537970
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTracePCGuard">>;
79547971
def fsanitize_coverage_no_prune
79557972
: Flag<["-"], "fsanitize-coverage-no-prune">,
7973+
Group<fsan_cov_Group>,
79567974
HelpText<"Disable coverage pruning (i.e. instrument all blocks/edges)">,
79577975
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageNoPrune">>;
79587976
def fsanitize_coverage_stack_depth
79597977
: Flag<["-"], "fsanitize-coverage-stack-depth">,
7978+
Group<fsan_cov_Group>,
79607979
HelpText<"Enable max stack depth tracing">,
79617980
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageStackDepth">>;
79627981
def fsanitize_coverage_trace_loads
79637982
: Flag<["-"], "fsanitize-coverage-trace-loads">,
7983+
Group<fsan_cov_Group>,
79647984
HelpText<"Enable tracing of loads">,
79657985
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceLoads">>;
79667986
def fsanitize_coverage_trace_stores
79677987
: Flag<["-"], "fsanitize-coverage-trace-stores">,
7988+
Group<fsan_cov_Group>,
79687989
HelpText<"Enable tracing of stores">,
79697990
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceStores">>;
79707991
def fexperimental_sanitize_metadata_EQ_covered

clang/lib/Driver/ToolChains/AMDGPU.h

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,49 @@ class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
155155
return SanitizerKind::Address;
156156
}
157157

158+
bool diagnoseUnsupportedOption(const llvm::opt::Arg *A,
159+
const llvm::opt::DerivedArgList &DAL,
160+
const llvm::opt::ArgList &DriverArgs,
161+
const char *Value = nullptr) const {
162+
auto &Diags = getDriver().getDiags();
163+
bool IsExplicitDevice =
164+
A->getBaseArg().getOption().matches(options::OPT_Xarch_device);
165+
166+
if (Value) {
167+
unsigned DiagID =
168+
IsExplicitDevice
169+
? clang::diag::err_drv_unsupported_option_part_for_target
170+
: clang::diag::warn_drv_unsupported_option_part_for_target;
171+
Diags.Report(DiagID) << Value << A->getAsString(DriverArgs)
172+
<< getTriple().str();
173+
} else {
174+
unsigned DiagID =
175+
IsExplicitDevice
176+
? clang::diag::err_drv_unsupported_option_for_target
177+
: clang::diag::warn_drv_unsupported_option_for_target;
178+
Diags.Report(DiagID) << A->getAsString(DAL) << getTriple().str();
179+
}
180+
return true;
181+
}
182+
158183
bool handleSanitizeOption(const ToolChain &TC, llvm::opt::DerivedArgList &DAL,
159184
const llvm::opt::ArgList &DriverArgs,
160185
StringRef TargetID, const llvm::opt::Arg *A) const {
161186
if (TargetID.empty())
162187
return false;
163-
// If this isn't a sanitizer option, don't handle it.
164-
if (!A->getOption().matches(options::OPT_fsanitize_EQ))
165-
return false;
166188
// If we shouldn't do sanitizing, skip it.
167189
if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
168190
options::OPT_fno_gpu_sanitize, true))
169191
return true;
170-
171-
auto &Diags = getDriver().getDiags();
172-
bool IsExplicitDevice =
173-
A->getBaseArg().getOption().matches(options::OPT_Xarch_device);
192+
const llvm::opt::Option &Opt = A->getOption();
193+
// Sanitizer coverage is currently not supported for AMDGPU, so warn/error
194+
// on every related option.
195+
if (Opt.matches(options::OPT_fsan_cov_Group)) {
196+
diagnoseUnsupportedOption(A, DAL, DriverArgs);
197+
}
198+
// If this isn't a sanitizer option, don't handle it.
199+
if (!Opt.matches(options::OPT_fsanitize_EQ))
200+
return false;
174201

175202
SmallVector<const char *, 4> SupportedSanitizers;
176203
SmallVector<const char *, 4> UnSupportedSanitizers;
@@ -185,25 +212,13 @@ class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
185212

186213
// If there are no supported sanitizers, drop the whole argument.
187214
if (SupportedSanitizers.empty()) {
188-
if (IsExplicitDevice) {
189-
Diags.Report(clang::diag::err_drv_unsupported_option_for_target)
190-
<< A->getAsString(DAL) << getTriple().str();
191-
} else {
192-
Diags.Report(clang::diag::warn_drv_unsupported_option_for_target)
193-
<< A->getAsString(DAL) << getTriple().str();
194-
}
215+
diagnoseUnsupportedOption(A, DAL, DriverArgs);
195216
return true;
196217
}
197218
// If only some sanitizers are unsupported, report each one individually.
198219
if (!UnSupportedSanitizers.empty()) {
199220
for (const char *Value : UnSupportedSanitizers) {
200-
if (IsExplicitDevice) {
201-
Diags.Report(clang::diag::err_drv_unsupported_option_part_for_target)
202-
<< Value << A->getAsString(DriverArgs) << getTriple().str();
203-
} else {
204-
Diags.Report(clang::diag::warn_drv_unsupported_option_part_for_target)
205-
<< Value << A->getAsString(DriverArgs) << getTriple().str();
206-
}
221+
diagnoseUnsupportedOption(A, DAL, DriverArgs, Value);
207222
}
208223
}
209224
// If we know the target arch, check if the sanitizer is supported for it.

clang/test/Driver/amdgpu-openmp-sanitize-options.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272
// RUN: %clang -no-canonical-prefixes -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=gfx908:xnack+,gfx900:xnack- -fsanitize=fuzzer,address -fsanitize=leak --rocm-path=%S/Inputs/rocm %s 2>&1 \
7373
// RUN: | FileCheck -check-prefixes=HOSTSANCOMBINATION2,NOTSUPPORTED-DAG,INVALIDCOMBINATION2 %s
7474

75+
// Check for -fsanitize-coverage options
76+
// RUN: %clang -no-canonical-prefixes -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=gfx908:xnack+ -fsanitize=address -fsanitize-coverage=inline-bool-flag --rocm-path=%S/Inputs/rocm %s 2>&1 \
77+
// RUN: | FileCheck -check-prefixes=WARNSANCOV %s
78+
7579
// Test -Xarch_device error scenario
7680

7781
// RUN: not %clang -no-canonical-prefixes -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=gfx908:xnack+ -Xarch_device -fsanitize=leak --rocm-path=%S/Inputs/rocm %s 2>&1 \
@@ -83,6 +87,10 @@
8387
// RUN: not %clang -no-canonical-prefixes -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=gfx908:xnack+ -Xarch_device -fsanitize=fuzzer,address --rocm-path=%S/Inputs/rocm %s 2>&1 \
8488
// RUN: | FileCheck -check-prefixes=INVALIDCOMBINATIONERROR %s
8589

90+
// RUN: not %clang -no-canonical-prefixes -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=gfx908:xnack+ -fsanitize=address -Xarch_device -fsanitize-coverage-stack-depth-callback-min=42 --rocm-path=%S/Inputs/rocm %s 2>&1 \
91+
// RUN: | FileCheck -check-prefixes=ERRSANCOV %s
92+
93+
8694
// INVALIDCOMBINATION1: warning: ignoring 'fuzzer' in '-fsanitize=address,fuzzer' option as it is not currently supported for target 'amdgcn-amd-amdhsa' [-Woption-ignored]
8795
// INVALIDCOMBINATION2: warning: ignoring 'fuzzer' in '-fsanitize=fuzzer,address' option as it is not currently supported for target 'amdgcn-amd-amdhsa' [-Woption-ignored]
8896

@@ -106,3 +114,6 @@
106114
// UNSUPPORTEDERROR: error: '-fsanitize=leak' option is not currently supported for target 'amdgcn-amd-amdhsa'
107115
// XNACKERROR: error: '-fsanitize=address' option for offload arch 'gfx908:xnack-' is not currently supported there. Use it with an offload arch containing 'xnack+' instead
108116
// INVALIDCOMBINATIONERROR: error: 'fuzzer' in '-fsanitize=fuzzer,address' option is not currently supported for target 'amdgcn-amd-amdhsa'
117+
118+
// WARNSANCOV: warning: ignoring '-fsanitize-coverage=inline-bool-flag' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
119+
// ERRSANCOV: error: '-fsanitize-coverage-stack-depth-callback-min=42' option is not currently supported for target 'amdgcn-amd-amdhsa'

clang/test/Driver/hip-sanitize-options.hip

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@
7474
// RUN: -fsanitize=fuzzer,address -fsanitize=leak --rocm-path=%S/Inputs/rocm %s 2>&1 \
7575
// RUN: | FileCheck -check-prefixes=MULT2,XNACK2 %s
7676

77+
// Check for -fsanitize-coverage options
78+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \
79+
// RUN: -fsanitize=address -fsanitize-coverage=inline-bool-flag --rocm-path=%S/Inputs/rocm %s 2>&1 \
80+
// RUN: | FileCheck -check-prefixes=WARNSANCOV %s
81+
7782
// Test -Xarch_device error scenario
7883

7984
// RUN: not %clang -### --target=x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \
@@ -88,6 +93,10 @@
8893
// RUN: -Xarch_device -fsanitize=fuzzer,address --rocm-path=%S/Inputs/rocm %s 2>&1 \
8994
// RUN: | FileCheck -check-prefixes=INVALIDCOMBINATIONERROR %s
9095

96+
// RUN: not %clang -### --target=x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \
97+
// RUN: -fsanitize=address -Xarch_device -fsanitize-coverage-stack-depth-callback-min=42 --rocm-path=%S/Inputs/rocm %s 2>&1 \
98+
// RUN: | FileCheck -check-prefixes=ERRSANCOV %s
99+
91100
// CHECK-NOT: {{"[^"]*clang[^"]*".* "-fcuda-is-device".* "-fsanitize=address"}}
92101
// CHECK-NOT: {{"[^"]*clang[^"]*".* "-fcuda-is-device".* "-mlink-bitcode-file" ".*asanrtl.bc"}}
93102
// CHECK-NOT: {{"[^"]*lld(\.exe){0,1}".* ".*hip.bc"}}
@@ -162,3 +171,6 @@
162171
// UNSUPPORTEDERROR: error: '-fsanitize=leak' option is not currently supported for target 'amdgcn-amd-amdhsa'
163172
// XNACKERROR: error: '-fsanitize=address' option for offload arch 'gfx900:xnack-' is not currently supported there. Use it with an offload arch containing 'xnack+' instead
164173
// INVALIDCOMBINATIONERROR: error: 'fuzzer' in '-fsanitize=fuzzer,address' option is not currently supported for target 'amdgcn-amd-amdhsa'
174+
175+
// WARNSANCOV: warning: ignoring '-fsanitize-coverage=inline-bool-flag' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
176+
// ERRSANCOV: error: '-fsanitize-coverage-stack-depth-callback-min=42' option is not currently supported for target 'amdgcn-amd-amdhsa'

0 commit comments

Comments
 (0)