Skip to content

Commit 53fe7b7

Browse files
committed
add fsan_cov_group
1 parent 0ee2fac commit 53fe7b7

File tree

4 files changed

+85
-26
lines changed

4 files changed

+85
-26
lines changed

clang/include/clang/Driver/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">,
@@ -7898,70 +7902,87 @@ def linker_option : Joined<["--"], "linker-option=">,
78987902
HelpText<"Add linker option">,
78997903
MarshallingInfoStringVector<CodeGenOpts<"LinkerOptions">>;
79007904
def fsanitize_coverage_type : Joined<["-"], "fsanitize-coverage-type=">,
7905+
Group<fsan_cov_Group>,
79017906
HelpText<"Sanitizer coverage type">,
79027907
MarshallingInfoInt<CodeGenOpts<"SanitizeCoverageType">>;
79037908
def fsanitize_coverage_indirect_calls
79047909
: Flag<["-"], "fsanitize-coverage-indirect-calls">,
7910+
Group<fsan_cov_Group>,
79057911
HelpText<"Enable sanitizer coverage for indirect calls">,
79067912
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageIndirectCalls">>;
79077913
def fsanitize_coverage_trace_bb
79087914
: Flag<["-"], "fsanitize-coverage-trace-bb">,
7915+
Group<fsan_cov_Group>,
79097916
HelpText<"Enable basic block tracing in sanitizer coverage">,
79107917
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceBB">>;
79117918
def fsanitize_coverage_trace_cmp
79127919
: Flag<["-"], "fsanitize-coverage-trace-cmp">,
7920+
Group<fsan_cov_Group>,
79137921
HelpText<"Enable cmp instruction tracing in sanitizer coverage">,
79147922
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceCmp">>;
79157923
def fsanitize_coverage_trace_div
79167924
: Flag<["-"], "fsanitize-coverage-trace-div">,
7925+
Group<fsan_cov_Group>,
79177926
HelpText<"Enable div instruction tracing in sanitizer coverage">,
79187927
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceDiv">>;
79197928
def fsanitize_coverage_trace_gep
79207929
: Flag<["-"], "fsanitize-coverage-trace-gep">,
7930+
Group<fsan_cov_Group>,
79217931
HelpText<"Enable gep instruction tracing in sanitizer coverage">,
79227932
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceGep">>;
79237933
def fsanitize_coverage_8bit_counters
79247934
: Flag<["-"], "fsanitize-coverage-8bit-counters">,
7935+
Group<fsan_cov_Group>,
79257936
HelpText<"Enable frequency counters in sanitizer coverage">,
79267937
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverage8bitCounters">>;
79277938
def fsanitize_coverage_inline_8bit_counters
79287939
: Flag<["-"], "fsanitize-coverage-inline-8bit-counters">,
7940+
Group<fsan_cov_Group>,
79297941
HelpText<"Enable inline 8-bit counters in sanitizer coverage">,
79307942
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageInline8bitCounters">>;
79317943
def fsanitize_coverage_inline_bool_flag
79327944
: Flag<["-"], "fsanitize-coverage-inline-bool-flag">,
7945+
Group<fsan_cov_Group>,
79337946
HelpText<"Enable inline bool flag in sanitizer coverage">,
79347947
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageInlineBoolFlag">>;
79357948
def fsanitize_coverage_pc_table
79367949
: Flag<["-"], "fsanitize-coverage-pc-table">,
7950+
Group<fsan_cov_Group>,
79377951
HelpText<"Create a table of coverage-instrumented PCs">,
79387952
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoveragePCTable">>;
79397953
def fsanitize_coverage_control_flow
79407954
: Flag<["-"], "fsanitize-coverage-control-flow">,
7955+
Group<fsan_cov_Group>,
79417956
HelpText<"Collect control flow of function">,
79427957
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageControlFlow">>;
79437958
def fsanitize_coverage_trace_pc
79447959
: Flag<["-"], "fsanitize-coverage-trace-pc">,
7960+
Group<fsan_cov_Group>,
79457961
HelpText<"Enable PC tracing in sanitizer coverage">,
79467962
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTracePC">>;
79477963
def fsanitize_coverage_trace_pc_guard
79487964
: Flag<["-"], "fsanitize-coverage-trace-pc-guard">,
7965+
Group<fsan_cov_Group>,
79497966
HelpText<"Enable PC tracing with guard in sanitizer coverage">,
79507967
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTracePCGuard">>;
79517968
def fsanitize_coverage_no_prune
79527969
: Flag<["-"], "fsanitize-coverage-no-prune">,
7970+
Group<fsan_cov_Group>,
79537971
HelpText<"Disable coverage pruning (i.e. instrument all blocks/edges)">,
79547972
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageNoPrune">>;
79557973
def fsanitize_coverage_stack_depth
79567974
: Flag<["-"], "fsanitize-coverage-stack-depth">,
7975+
Group<fsan_cov_Group>,
79577976
HelpText<"Enable max stack depth tracing">,
79587977
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageStackDepth">>;
79597978
def fsanitize_coverage_trace_loads
79607979
: Flag<["-"], "fsanitize-coverage-trace-loads">,
7980+
Group<fsan_cov_Group>,
79617981
HelpText<"Enable tracing of loads">,
79627982
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceLoads">>;
79637983
def fsanitize_coverage_trace_stores
79647984
: Flag<["-"], "fsanitize-coverage-trace-stores">,
7985+
Group<fsan_cov_Group>,
79657986
HelpText<"Enable tracing of stores">,
79667987
MarshallingInfoFlag<CodeGenOpts<"SanitizeCoverageTraceStores">>;
79677988
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)