Skip to content

Commit fb57f4e

Browse files
authored
Allow multiple sanitizers on baremetal targets. (#72933)
Baremetal targets tend to implement their own runtime support for sanitizers. Clang driver gatekeeping of allowed sanitizer types is counter productive. This change allows anything that does not crash and burn in compilation, and leaves any potential runtime issues for the user to figure out.
1 parent 752c21b commit fb57f4e

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,29 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
491491
JA, *this, ResponseFileSupport::AtFileCurCP(),
492492
Args.MakeArgString(TC.GetLinkerPath()), CmdArgs, Inputs, Output));
493493
}
494+
495+
// BareMetal toolchain allows all sanitizers where the compiler generates valid
496+
// code, ignoring all runtime library support issues on the assumption that
497+
// baremetal targets typically implement their own runtime support.
498+
SanitizerMask BareMetal::getSupportedSanitizers() const {
499+
const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
500+
const bool IsAArch64 = getTriple().getArch() == llvm::Triple::aarch64 ||
501+
getTriple().getArch() == llvm::Triple::aarch64_be;
502+
const bool IsRISCV64 = getTriple().getArch() == llvm::Triple::riscv64;
503+
SanitizerMask Res = ToolChain::getSupportedSanitizers();
504+
Res |= SanitizerKind::Address;
505+
Res |= SanitizerKind::KernelAddress;
506+
Res |= SanitizerKind::PointerCompare;
507+
Res |= SanitizerKind::PointerSubtract;
508+
Res |= SanitizerKind::Fuzzer;
509+
Res |= SanitizerKind::FuzzerNoLink;
510+
Res |= SanitizerKind::Vptr;
511+
Res |= SanitizerKind::SafeStack;
512+
Res |= SanitizerKind::Thread;
513+
Res |= SanitizerKind::Scudo;
514+
if (IsX86_64 || IsAArch64 || IsRISCV64) {
515+
Res |= SanitizerKind::HWAddress;
516+
Res |= SanitizerKind::KernelHWAddress;
517+
}
518+
return Res;
519+
}

clang/lib/Driver/ToolChains/BareMetal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class LLVM_LIBRARY_VISIBILITY BareMetal : public ToolChain {
7272
void AddLinkRuntimeLib(const llvm::opt::ArgList &Args,
7373
llvm::opt::ArgStringList &CmdArgs) const;
7474
std::string computeSysRoot() const override;
75+
SanitizerMask getSupportedSanitizers() const override;
7576

7677
private:
7778
using OrderedMultilibs =

clang/test/Driver/fsanitize.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,11 +973,58 @@
973973
// RUN: not %clang --target=x86_64-sie-ps5 -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI
974974
// RUN: not %clang --target=x86_64-sie-ps5 -fsanitize=function -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI --check-prefix=CHECK-UBSAN-FUNCTION
975975
// RUN: %clang --target=x86_64-sie-ps5 -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-UNDEFINED
976+
// CHECK-UBSAN-UNDEFINED: "-fsanitize={{((alignment|array-bounds|bool|builtin|enum|float-cast-overflow|integer-divide-by-zero|nonnull-attribute|null|pointer-overflow|return|returns-nonnull-attribute|shift-base|shift-exponent|signed-integer-overflow|unreachable|vla-bound),?){17}"}}
976977

977978
// RUN: not %clang --target=armv6t2-eabi -mexecute-only -fsanitize=function %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-FUNCTION
978979
// RUN: not %clang --target=armv6t2-eabi -mexecute-only -fsanitize=kcfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-KCFI
979-
// RUN: %clang --target=armv6t2-eabi -mexecute-only -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-UNDEFINED
980+
// RUN: %clang --target=armv6t2-eabi -mexecute-only -fsanitize=undefined %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-UNDEFINED-VPTR
980981

981982
// CHECK-UBSAN-KCFI-DAG: error: invalid argument '-fsanitize=kcfi' not allowed with {{('x86_64-sie-ps5'|'armv6t2-unknown-unknown-eabi')}}
982983
// CHECK-UBSAN-FUNCTION-DAG: error: invalid argument '-fsanitize=function' not allowed with {{('x86_64-sie-ps5'|'armv6t2-unknown-unknown-eabi')}}
983-
// CHECK-UBSAN-UNDEFINED: "-fsanitize={{((alignment|array-bounds|bool|builtin|enum|float-cast-overflow|integer-divide-by-zero|nonnull-attribute|null|pointer-overflow|return|returns-nonnull-attribute|shift-base|shift-exponent|signed-integer-overflow|unreachable|vla-bound),?){17}"}}
984+
// CHECK-UBSAN-UNDEFINED-VPTR: "-fsanitize={{((alignment|array-bounds|bool|builtin|enum|float-cast-overflow|integer-divide-by-zero|nonnull-attribute|null|pointer-overflow|return|returns-nonnull-attribute|shift-base|shift-exponent|signed-integer-overflow|unreachable|vla-bound|vptr),?){18}"}}
985+
986+
// * Test BareMetal toolchain sanitizer support *
987+
988+
// RUN: %clang --target=arm-arm-non-eabi -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=ADDRESS-BAREMETAL
989+
// RUN: %clang --target=aarch64-none-elf -fsanitize=address %s -### 2>&1 | FileCheck %s -check-prefix=ADDRESS-BAREMETAL
990+
// ADDRESS-BAREMETAL: "-fsanitize=address"
991+
992+
// RUN: %clang --target=arm-arm-none-eabi -fsanitize=kernel-address %s -### 2>&1 | FileCheck %s -check-prefix=KERNEL-ADDRESS-BAREMETAL
993+
// RUN: %clang --target=aarch64-none-elf -fsanitize=kernel-address %s -### 2>&1 | FileCheck %s -check-prefix=KERNEL-ADDRESS-BAREMETAL
994+
// KERNEL-ADDRESS-BAREMETAL: "-fsanitize=kernel-address"
995+
996+
// RUN: %clang --target=aarch64-none-elf -fsanitize=hwaddress %s -### 2>&1 | FileCheck %s -check-prefix=HWADDRESS-BAREMETAL
997+
// HWADDRESS-BAREMETAL: "-fsanitize=hwaddress"
998+
999+
// RUN: %clang --target=aarch64-none-elf -fsanitize=kernel-hwaddress %s -### 2>&1 | FileCheck %s -check-prefix=KERNEL-HWADDRESS-BAREMETAL
1000+
// KERNEL-HWADDRESS-BAREMETAL: "-fsanitize=kernel-hwaddress"
1001+
1002+
// RUN: %clang --target=aarch64-none-elf -fsanitize=thread %s -### 2>&1 | FileCheck %s -check-prefix=THREAD-BAREMETAL
1003+
// THREAD-BAREMETAL: "-fsanitize=thread"
1004+
1005+
// RUN: %clang --target=arm-arm-none-eabi -fsanitize=vptr %s -### 2>&1 | FileCheck %s -check-prefix=VPTR-BAREMETAL
1006+
// RUN: %clang --target=aarch64-none-elf -fsanitize=vptr %s -### 2>&1 | FileCheck %s -check-prefix=VPTR-BAREMETAL
1007+
// VPTR-BAREMETAL: "-fsanitize=vptr"
1008+
1009+
// RUN: %clang --target=arm-arm-none-eabi -fsanitize=safe-stack %s -### 2>&1 | FileCheck %s -check-prefix=SAFESTACK-BAREMETAL
1010+
// RUN: %clang --target=aarch64-none-elf -fsanitize=safe-stack %s -### 2>&1 | FileCheck %s -check-prefix=SAFESTACK-BAREMETAL
1011+
// SAFESTACK-BAREMETAL: "-fsanitize=safe-stack"
1012+
1013+
// RUN: %clang --target=arm-arm-none-eabi -fsanitize=undefined %s -### 2>&1 | FileCheck %s -check-prefix=UNDEFINED-BAREMETAL
1014+
// RUN: %clang --target=aarch64-none-elf -fsanitize=undefined %s -### 2>&1 | FileCheck %s -check-prefix=UNDEFINED-BAREMETAL
1015+
// UNDEFINED-BAREMETAL: "-fsanitize={{.*}}
1016+
1017+
// RUN: %clang --target=arm-arm-none-eabi -fsanitize=scudo %s -### 2>&1 | FileCheck %s -check-prefix=SCUDO-BAREMETAL
1018+
// RUN: %clang --target=aarch64-none-elf -fsanitize=scudo %s -### 2>&1 | FileCheck %s -check-prefix=SCUDO-BAREMETAL
1019+
// SCUDO-BAREMETAL: "-fsanitize=scudo"
1020+
1021+
// RUN: %clang --target=arm-arm-none-eabi -fsanitize=function %s -### 2>&1 | FileCheck %s -check-prefix=FUNCTION-BAREMETAL
1022+
// RUN: %clang --target=aarch64-none-elf -fsanitize=function %s -### 2>&1 | FileCheck %s -check-prefix=FUNCTION-BAREMETAL
1023+
// FUNCTION-BAREMETAL: "-fsanitize=function"
1024+
1025+
// RUN: not %clang --target=aarch64-none-elf -fsanitize=memory %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
1026+
// RUN: not %clang --target=aarch64-none-elf -fsanitize=kernel-memory %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
1027+
// RUN: not %clang --target=aarch64-none-elf -fsanitize=leak %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
1028+
// RUN: not %clang --target=aarch64-none-elf -fsanitize=dataflow %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
1029+
// RUN: not %clang --target=arm-arm-none-eabi -fsanitize=shadow-call-stack %s -### 2>&1 | FileCheck %s -check-prefix=UNSUPPORTED-BAREMETAL
1030+
// UNSUPPORTED-BAREMETAL: unsupported option '-fsanitize={{.*}}' for target

0 commit comments

Comments
 (0)