Skip to content

Commit 3f742c1

Browse files
committed
ignore sanitizers selectively
1 parent 3a126e0 commit 3f742c1

File tree

6 files changed

+67
-29
lines changed

6 files changed

+67
-29
lines changed

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,29 +1073,32 @@ ROCMToolChain::getCommonDeviceLibNames(
10731073
getSanitizerArgs(DriverArgs).needsAsanRt());
10741074
}
10751075

1076-
bool AMDGPUToolChain::shouldSkipSanitizeOption(
1076+
std::optional<std::string> AMDGPUToolChain::filterSanitizeOption(
10771077
const ToolChain &TC, const llvm::opt::ArgList &DriverArgs,
10781078
StringRef TargetID, const llvm::opt::Arg *A) const {
10791079
// For actions without targetID, do nothing.
10801080
if (TargetID.empty())
1081-
return false;
1081+
return std::nullopt;
10821082
Option O = A->getOption();
10831083

10841084
if (!O.matches(options::OPT_fsanitize_EQ))
1085-
return false;
1085+
return std::nullopt;
10861086

10871087
if (!DriverArgs.hasFlag(options::OPT_fgpu_sanitize,
10881088
options::OPT_fno_gpu_sanitize, true))
1089-
return true;
1089+
return "";
10901090

10911091
auto &Diags = TC.getDriver().getDiags();
10921092

1093-
// For simplicity, we only allow -fsanitize=address
1093+
// We only allow the address sanitizer and ignore all other sanitizers.
1094+
SmallVector<std::string, 4> SupportedSanitizers;
10941095
for (const char *Value : A->getValues()) {
10951096
SanitizerMask K = parseSanitizerValue(Value, /*AllowGroups=*/false);
1096-
if (K != SanitizerKind::Address)
1097-
return true;
1097+
if (K == SanitizerKind::Address)
1098+
SupportedSanitizers.push_back(std::string(Value));
10981099
}
1100+
if (SupportedSanitizers.empty())
1101+
return "";
10991102

11001103
llvm::StringMap<bool> FeatureMap;
11011104
auto OptionalGpuArch = parseTargetID(TC.getTriple(), TargetID, &FeatureMap);
@@ -1107,7 +1110,8 @@ bool AMDGPUToolChain::shouldSkipSanitizeOption(
11071110
Diags.Report(
11081111
clang::diag::warn_drv_unsupported_option_for_offload_arch_req_feature)
11091112
<< A->getAsString(DriverArgs) << TargetID << "xnack+";
1110-
return true;
1113+
return "";
11111114
}
1112-
return false;
1115+
1116+
return llvm::join(SupportedSanitizers, ",");
11131117
}

clang/lib/Driver/ToolChains/AMDGPU.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUToolChain : public Generic_ELF {
101101
/// Needed for translating LTO options.
102102
const char *getDefaultLinker() const override { return "ld.lld"; }
103103

104-
/// Should skip sanitize options.
105-
bool shouldSkipSanitizeOption(const ToolChain &TC,
106-
const llvm::opt::ArgList &DriverArgs,
107-
StringRef TargetID,
108-
const llvm::opt::Arg *A) const;
104+
/// Filter supported sanitizers from the sanitize option and return them. If
105+
/// there should be no filtering and Arg should be kept as-is, return
106+
/// std::nullopt. If no sanitizers are supported, return an empty string.
107+
std::optional<std::string>
108+
filterSanitizeOption(const ToolChain &TC,
109+
const llvm::opt::ArgList &DriverArgs, StringRef TargetID,
110+
const llvm::opt::Arg *A) const;
109111

110112
/// Uses amdgpu-arch tool to get arch of the system GPU. Will return error
111113
/// if unable to find one.
@@ -164,8 +166,8 @@ class LLVM_LIBRARY_VISIBILITY ROCMToolChain : public AMDGPUToolChain {
164166
for (const char *Value : A->getValues()) {
165167
SanitizerMask K = parseSanitizerValue(Value, /*Allow Groups*/ false);
166168
if (K != SanitizerKind::Address)
167-
Diags.Report(clang::diag::warn_drv_unsupported_option_for_target)
168-
<< A->getAsString(Args) << getTriple().str();
169+
Diags.Report(clang::diag::warn_drv_unsupported_option_part_for_target)
170+
<< Value << A->getAsString(Args) << getTriple().str();
169171
}
170172
}
171173
}

clang/lib/Driver/ToolChains/AMDGPUOpenMP.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,20 @@ llvm::opt::DerivedArgList *AMDGPUOpenMPToolChain::TranslateArgs(
6666

6767
const OptTable &Opts = getDriver().getOpts();
6868

69-
// Skip sanitize options passed from the HostTC. Claim them early.
69+
// Skip sanitize options passed from the HostTC. Remove them early.
7070
// The decision to sanitize device code is computed only by
7171
// 'shouldSkipSanitizeOption'.
7272
if (DAL->hasArg(options::OPT_fsanitize_EQ))
73-
DAL->claimAllArgs(options::OPT_fsanitize_EQ);
73+
DAL->eraseArg(options::OPT_fsanitize_EQ);
7474

75-
for (Arg *A : Args)
76-
if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A) &&
77-
!llvm::is_contained(*DAL, A))
75+
for (Arg *A : Args) {
76+
std::optional<std::string> SupportedSanitizers =
77+
filterSanitizeOption(*this, Args, BoundArch, A);
78+
if (!SupportedSanitizers)
7879
DAL->append(A);
80+
else if (!SupportedSanitizers->empty())
81+
DAL->AddJoinedArg(A, A->getOption(), *SupportedSanitizers);
82+
}
7983

8084
if (!BoundArch.empty()) {
8185
DAL->eraseArg(options::OPT_march_EQ);

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,19 @@ HIPAMDToolChain::TranslateArgs(const llvm::opt::DerivedArgList &Args,
291291

292292
const OptTable &Opts = getDriver().getOpts();
293293

294+
// Skip sanitize options passed from the HostTC. Remove them early.
295+
// The decision to sanitize device code is computed only by
296+
// 'shouldSkipSanitizeOption'.
297+
if (DAL->hasArg(options::OPT_fsanitize_EQ))
298+
DAL->eraseArg(options::OPT_fsanitize_EQ);
299+
294300
for (Arg *A : Args) {
295-
if (!shouldSkipSanitizeOption(*this, Args, BoundArch, A))
301+
std::optional<std::string> SupportedSanitizers =
302+
filterSanitizeOption(*this, Args, BoundArch, A);
303+
if (!SupportedSanitizers)
296304
DAL->append(A);
305+
else if (!SupportedSanitizers->empty())
306+
DAL->AddJoinedArg(A, A->getOption(), *SupportedSanitizers);
297307
}
298308

299309
if (!BoundArch.empty()) {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,21 @@
4848
// RUN: %clang -no-canonical-prefixes -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=gfx908:xnack+ --offload-arch=gfx900:xnack+ -fsanitize=address -fno-gpu-sanitize --rocm-path=%S/Inputs/rocm %s 2>&1 \
4949
// RUN: | FileCheck -check-prefixes=HOSTSAN,NOGPUSAN,SAN %s
5050

51-
// Catch invalid combination of sanitizers regardless of their order.
52-
// The address sanitizer enables the device sanitizer pipeline. The fuzzer
51+
// Catch invalid combination of sanitizers regardless of their order and ignore
52+
// them selectively.
53+
// (The address sanitizer enables the device sanitizer pipeline. The fuzzer
5354
// implicitly turns on LLVMs SanitizerCoverage, which the driver then forwards
54-
// to the device cc1. SanitizerCoverage is not supported on amdgcn
55+
// to the device cc1. SanitizerCoverage is not supported on amdgcn.)
5556
// RUN: %clang -no-canonical-prefixes -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=gfx908:xnack+ -fsanitize=address,fuzzer --rocm-path=%S/Inputs/rocm %s 2>&1 \
5657
// RUN: | FileCheck -check-prefixes=HOSTSANCOMBINATION,INVALIDCOMBINATION1 %s
5758
// RUN: %clang -no-canonical-prefixes -### --target=x86_64-unknown-linux-gnu -fopenmp=libomp --offload-arch=gfx908:xnack+ -fsanitize=fuzzer,address --rocm-path=%S/Inputs/rocm %s 2>&1 \
5859
// RUN: | FileCheck -check-prefixes=HOSTSANCOMBINATION,INVALIDCOMBINATION2 %s
5960

60-
// INVALIDCOMBINATION1: warning: ignoring '-fsanitize=address,fuzzer' option as it is not currently supported for target 'amdgcn-amd-amdhsa' [-Woption-ignored]
61-
// INVALIDCOMBINATION2: warning: ignoring '-fsanitize=fuzzer,address' option as it is not currently supported for target 'amdgcn-amd-amdhsa' [-Woption-ignored]
61+
// INVALIDCOMBINATION1: warning: ignoring 'fuzzer' in '-fsanitize=address,fuzzer' option as it is not currently supported for target 'amdgcn-amd-amdhsa' [-Woption-ignored]
62+
// INVALIDCOMBINATION2: warning: ignoring 'fuzzer' in '-fsanitize=fuzzer,address' option as it is not currently supported for target 'amdgcn-amd-amdhsa' [-Woption-ignored]
6263

6364
// FAIL-DAG: error: cannot find ROCm device library for ABI version 5; provide its path via '--rocm-path' or '--rocm-device-lib-path', or pass '-nogpulib' to build without ROCm device library
64-
// NOTSUPPORTED-DAG: warning: ignoring '-fsanitize=leak' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
65+
// NOTSUPPORTED-DAG: warning: ignoring 'leak' in '-fsanitize=leak' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
6566

6667
// NOXNACK: warning: ignoring '-fsanitize=address' option for offload arch 'gfx908' as it is not currently supported there. Use it with an offload arch containing 'xnack+' instead
6768
// XNACKNEG: warning: ignoring '-fsanitize=address' option for offload arch 'gfx908:xnack-' as it is not currently supported there. Use it with an offload arch containing 'xnack+' instead

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@
4747
// RUN: -fsanitize=leak -nogpuinc --rocm-path=%S/Inputs/rocm \
4848
// RUN: %s 2>&1 | FileCheck -check-prefixes=NOGPUNEG %s
4949

50+
// Catch invalid combination of sanitizers regardless of their order and ignore
51+
// them selectively.
52+
// (The address sanitizer enables the device sanitizer pipeline. The fuzzer
53+
// implicitly turns on LLVMs SanitizerCoverage, which the driver then forwards
54+
// to the device cc1. SanitizerCoverage is not supported on amdgcn.)
55+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \
56+
// RUN: -fsanitize=address,fuzzer --rocm-path=%S/Inputs/rocm %s 2>&1 \
57+
// RUN: | FileCheck -check-prefixes=INVALIDCOMBINATION,INVALIDCOMBINATION1 %s
58+
// RUN: %clang -### --target=x86_64-unknown-linux-gnu --offload-arch=gfx900:xnack+ \
59+
// RUN: -fsanitize=fuzzer,address --rocm-path=%S/Inputs/rocm %s 2>&1 \
60+
// RUN: | FileCheck -check-prefixes=INVALIDCOMBINATION,INVALIDCOMBINATION2 %s
61+
5062
// CHECK-NOT: {{"[^"]*clang[^"]*".* "-fcuda-is-device".* "-fsanitize=address"}}
5163
// CHECK-NOT: {{"[^"]*clang[^"]*".* "-fcuda-is-device".* "-mlink-bitcode-file" ".*asanrtl.bc"}}
5264
// CHECK-NOT: {{"[^"]*lld(\.exe){0,1}".* ".*hip.bc"}}
@@ -62,7 +74,7 @@
6274

6375
// FAIL: error: cannot find ROCm device library for ABI version 5; provide its path via '--rocm-path' or '--rocm-device-lib-path', or pass '-nogpulib' to build without ROCm device library
6476

65-
// XNACK-DAG: warning: ignoring '-fsanitize=leak' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
77+
// XNACK-DAG: warning: ignoring 'leak' in '-fsanitize=leak' option as it is not currently supported for target 'amdgcn-amd-amdhsa'
6678
// XNACK-DAG: warning: ignoring '-fsanitize=address' option for offload arch 'gfx900:xnack-' as it is not currently supported there. Use it with an offload arch containing 'xnack+' instead
6779
// XNACK-DAG: warning: ignoring '-fsanitize=address' option for offload arch 'gfx906' as it is not currently supported there. Use it with an offload arch containing 'xnack+' instead
6880
// XNACK-DAG: {{"[^"]*clang[^"]*".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-target-cpu" "gfx900".* "-target-feature" "\+xnack".* "-fsanitize=address"}}
@@ -96,3 +108,8 @@
96108
// NOGPUNEG-NOT: {{"[^"]*clang[^"]*".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-target-cpu" "gfx900".* "-target-feature" "-xnack"}}
97109
// NOGPUNEG-NOT: {{"[^"]*clang[^"]*".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-target-cpu" "gfx906"}}
98110
// NOGPUNEG-NOT: {{"[^"]*lld(\.exe){0,1}".* ".*hip.bc"}}
111+
112+
// INVALIDCOMBINATION1-DAG: warning: ignoring 'fuzzer' in '-fsanitize=address,fuzzer' option as it is not currently supported for target 'amdgcn-amd-amdhsa' [-Woption-ignored]
113+
// INVALIDCOMBINATION2-DAG: warning: ignoring 'fuzzer' in '-fsanitize=fuzzer,address' option as it is not currently supported for target 'amdgcn-amd-amdhsa' [-Woption-ignored]
114+
// INVALIDCOMBINATION-DAG: {{"[^"]*clang[^"]*".* "-mlink-bitcode-file" ".*asanrtl.bc".* "-target-cpu" "gfx900".* "-target-feature" "\+xnack".* "-fsanitize=address"}}
115+
// INVALIDCOMBINATION-DAG: {{"[^"]*clang[^"]*".* "-triple" "x86_64-unknown-linux-gnu".* "-fsanitize=address,fuzzer,fuzzer-no-link"}}

0 commit comments

Comments
 (0)