Skip to content

Commit f4261e1

Browse files
frobtechpetrhosek
authored andcommitted
[Clang] Enable -fsanitize=leak on Fuchsia targets
This required some fixes to the generic code for two issues: 1. -fsanitize=safe-stack is default on x86_64-fuchsia and is *not* incompatible with -fsanitize=leak on Fuchisa 2. -fsanitize=leak and other static-only runtimes must not be omitted under -shared-libsan (which is the default on Fuchsia) Patch By: mcgrathr Differential Revision: https://reviews.llvm.org/D73397
1 parent d59e342 commit f4261e1

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
lines changed

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,11 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
412412
SanitizerKind::Leak | SanitizerKind::Thread |
413413
SanitizerKind::Memory | SanitizerKind::KernelAddress),
414414
std::make_pair(SanitizerKind::SafeStack,
415-
SanitizerKind::Address | SanitizerKind::HWAddress |
416-
SanitizerKind::Leak | SanitizerKind::Thread |
417-
SanitizerKind::Memory | SanitizerKind::KernelAddress),
415+
(TC.getTriple().isOSFuchsia() ? SanitizerMask()
416+
: SanitizerKind::Leak) |
417+
SanitizerKind::Address | SanitizerKind::HWAddress |
418+
SanitizerKind::Thread | SanitizerKind::Memory |
419+
SanitizerKind::KernelAddress),
418420
std::make_pair(SanitizerKind::KernelHWAddress,
419421
SanitizerKind::Address | SanitizerKind::HWAddress |
420422
SanitizerKind::Leak | SanitizerKind::Thread |
@@ -831,8 +833,9 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
831833
}
832834

833835
if (AllAddedKinds & SanitizerKind::SafeStack) {
834-
// SafeStack runtime is built into the system on Fuchsia.
835-
SafeStackRuntime = !TC.getTriple().isOSFuchsia();
836+
// SafeStack runtime is built into the system on Android and Fuchsia.
837+
SafeStackRuntime =
838+
!TC.getTriple().isAndroid() && !TC.getTriple().isOSFuchsia();
836839
}
837840

838841
LinkRuntimes =

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -642,17 +642,21 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
642642
StaticRuntimes.push_back("stats_client");
643643

644644
// Collect static runtimes.
645-
if (Args.hasArg(options::OPT_shared) || SanArgs.needsSharedRt()) {
646-
// Don't link static runtimes into DSOs or if -shared-libasan.
645+
if (Args.hasArg(options::OPT_shared)) {
646+
// Don't link static runtimes into DSOs.
647647
return;
648648
}
649-
if (SanArgs.needsAsanRt() && SanArgs.linkRuntimes()) {
649+
650+
// Each static runtime that has a DSO counterpart above is excluded below,
651+
// but runtimes that exist only as static are not affected by needsSharedRt.
652+
653+
if (!SanArgs.needsSharedRt() && SanArgs.needsAsanRt() && SanArgs.linkRuntimes()) {
650654
StaticRuntimes.push_back("asan");
651655
if (SanArgs.linkCXXRuntimes())
652656
StaticRuntimes.push_back("asan_cxx");
653657
}
654658

655-
if (SanArgs.needsHwasanRt() && SanArgs.linkRuntimes()) {
659+
if (!SanArgs.needsSharedRt() && SanArgs.needsHwasanRt() && SanArgs.linkRuntimes()) {
656660
StaticRuntimes.push_back("hwasan");
657661
if (SanArgs.linkCXXRuntimes())
658662
StaticRuntimes.push_back("hwasan_cxx");
@@ -671,7 +675,7 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
671675
if (SanArgs.linkCXXRuntimes())
672676
StaticRuntimes.push_back("tsan_cxx");
673677
}
674-
if (SanArgs.needsUbsanRt() && SanArgs.linkRuntimes()) {
678+
if (!SanArgs.needsSharedRt() && SanArgs.needsUbsanRt() && SanArgs.linkRuntimes()) {
675679
if (SanArgs.requiresMinimalRuntime()) {
676680
StaticRuntimes.push_back("ubsan_minimal");
677681
} else {
@@ -684,18 +688,20 @@ collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
684688
NonWholeStaticRuntimes.push_back("safestack");
685689
RequiredSymbols.push_back("__safestack_init");
686690
}
687-
if (SanArgs.needsCfiRt() && SanArgs.linkRuntimes())
688-
StaticRuntimes.push_back("cfi");
689-
if (SanArgs.needsCfiDiagRt() && SanArgs.linkRuntimes()) {
690-
StaticRuntimes.push_back("cfi_diag");
691-
if (SanArgs.linkCXXRuntimes())
692-
StaticRuntimes.push_back("ubsan_standalone_cxx");
691+
if (!(SanArgs.needsSharedRt() && SanArgs.needsUbsanRt() && SanArgs.linkRuntimes())) {
692+
if (SanArgs.needsCfiRt() && SanArgs.linkRuntimes())
693+
StaticRuntimes.push_back("cfi");
694+
if (SanArgs.needsCfiDiagRt() && SanArgs.linkRuntimes()) {
695+
StaticRuntimes.push_back("cfi_diag");
696+
if (SanArgs.linkCXXRuntimes())
697+
StaticRuntimes.push_back("ubsan_standalone_cxx");
698+
}
693699
}
694700
if (SanArgs.needsStatsRt() && SanArgs.linkRuntimes()) {
695701
NonWholeStaticRuntimes.push_back("stats");
696702
RequiredSymbols.push_back("__sanitizer_stats_register");
697703
}
698-
if (SanArgs.needsScudoRt() && SanArgs.linkRuntimes()) {
704+
if (!SanArgs.needsSharedRt() && SanArgs.needsScudoRt() && SanArgs.linkRuntimes()) {
699705
if (SanArgs.requiresMinimalRuntime()) {
700706
StaticRuntimes.push_back("scudo_minimal");
701707
if (SanArgs.linkCXXRuntimes())

clang/lib/Driver/ToolChains/Fuchsia.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ SanitizerMask Fuchsia::getSupportedSanitizers() const {
340340
Res |= SanitizerKind::PointerSubtract;
341341
Res |= SanitizerKind::Fuzzer;
342342
Res |= SanitizerKind::FuzzerNoLink;
343+
Res |= SanitizerKind::Leak;
343344
Res |= SanitizerKind::SafeStack;
344345
Res |= SanitizerKind::Scudo;
345346
return Res;

clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/aarch64-fuchsia/libclang_rt.lsan.a

Whitespace-only changes.

clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/x86_64-fuchsia/libclang_rt.lsan.a

Whitespace-only changes.

clang/test/Driver/fuchsia.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,35 @@
174174
// CHECK-SCUDO-SHARED: "-fsanitize=safe-stack,scudo"
175175
// CHECK-SCUDO-SHARED: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-fuchsia{{/|\\\\}}libclang_rt.scudo.so"
176176

177+
// RUN: %clang %s -### --target=aarch64-fuchsia \
178+
// RUN: -fsanitize=leak 2>&1 \
179+
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
180+
// RUN: -fuse-ld=lld \
181+
// RUN: | FileCheck %s -check-prefix=CHECK-LSAN-AARCH64
182+
// CHECK-LSAN-AARCH64: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
183+
// CHECK-LSAN-AARCH64: "-fsanitize=leak,shadow-call-stack"
184+
// CHECK-LSAN-AARCH64: "-pie"
185+
// CHECK-LSAN-AARCH64: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}aarch64-fuchsia{{/|\\\\}}libclang_rt.lsan.a"
186+
187+
// RUN: %clang %s -### --target=x86_64-fuchsia \
188+
// RUN: -fsanitize=leak 2>&1 \
189+
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
190+
// RUN: -fuse-ld=lld \
191+
// RUN: | FileCheck %s -check-prefix=CHECK-LSAN-X86
192+
// CHECK-LSAN-X86: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
193+
// CHECK-LSAN-X86: "-fsanitize=leak,safe-stack"
194+
// CHECK-LSAN-X86: "-pie"
195+
// CHECK-LSAN-X86: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}x86_64-fuchsia{{/|\\\\}}libclang_rt.lsan.a"
196+
197+
// RUN: %clang %s -### --target=aarch64-fuchsia \
198+
// RUN: -fsanitize=leak -fPIC -shared 2>&1 \
199+
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \
200+
// RUN: -fuse-ld=lld \
201+
// RUN: | FileCheck %s -check-prefix=CHECK-LSAN-SHARED
202+
// CHECK-LSAN-SHARED: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]"
203+
// CHECK-LSAN-SHARED: "-fsanitize=leak,shadow-call-stack"
204+
// CHECK-LSAN-SHARED-NOT: "[[RESOURCE_DIR]]{{/|\\\\}}lib{{/|\\\\}}aarch64-fuchsia{{/|\\\\}}libclang_rt.lsan.a"
205+
177206
// RUN: %clang %s -### --target=x86_64-fuchsia \
178207
// RUN: -fxray-instrument -fxray-modes=xray-basic \
179208
// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \

0 commit comments

Comments
 (0)