Index: clang/include/clang/Basic/DiagnosticDriverKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticDriverKinds.td +++ clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -368,6 +368,9 @@ def err_drv_ropi_incompatible_with_cxx : Error< "ROPI is not compatible with c++">; +def err_stack_tagging_requires_hardware_feature : Error< + "'-fsanitize=memtag' requires hardware support (+memtag)">; + def warn_target_unsupported_nan2008 : Warning< "ignoring '-mnan=2008' option because the '%0' architecture does not support it">, InGroup; Index: clang/lib/Driver/SanitizerArgs.cpp =================================================================== --- clang/lib/Driver/SanitizerArgs.cpp +++ clang/lib/Driver/SanitizerArgs.cpp @@ -869,6 +869,18 @@ CmdArgs.push_back(Args.MakeArgString(LinkerOptionFlag)); } +static bool hasTargetFeatureMTE(const llvm::opt::ArgStringList &CmdArgs) { + for (auto Start = CmdArgs.begin(), End = CmdArgs.end(); Start != End; ++Start) { + auto It = std::find(Start, End, StringRef("+mte")); + if (It == End) + break; + if (It > Start && *std::prev(It) == StringRef("-target-feature")) + return true; + Start = It; + } + return false; +} + void SanitizerArgs::addArgs(const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, types::ID InputType) const { @@ -1036,6 +1048,9 @@ Sanitizers.Mask & CFIClasses) << "-fvisibility="; } + + if (Sanitizers.has(SanitizerKind::MemTag) && !hasTargetFeatureMTE(CmdArgs)) + TC.getDriver().Diag(diag::err_stack_tagging_requires_hardware_feature); } SanitizerMask parseArgValues(const Driver &D, const llvm::opt::Arg *A, Index: clang/test/Driver/fsanitize.c =================================================================== --- clang/test/Driver/fsanitize.c +++ clang/test/Driver/fsanitize.c @@ -191,6 +191,16 @@ // RUN: %clang -target x86_64-linux-android -fsanitize=memtag -fno-rtti %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-BAD-ARCH // CHECK-SANMT-BAD-ARCH: unsupported option '-fsanitize=memtag' for target +// RUN: %clang -target aarch64-linux -fsanitize=memtag -march=armv8-a+memtag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-MT +// CHECK-SANMT-MT: "-target-feature" "+mte" +// CHECK-SANMT-MT-SAME: "-fsanitize=memtag" + +// RUN: %clang -target aarch64-linux -fsanitize=memtag %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-NOMT-0 +// CHECK-SANMT-NOMT-0: '-fsanitize=memtag' requires hardware support (+memtag) + +// RUN: %clang -target aarch64-linux -fsanitize=memtag -I +mte %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-SANMT-NOMT-1 +// CHECK-SANMT-NOMT-1: '-fsanitize=memtag' requires hardware support (+memtag) + // RUN: %clang -target x86_64-linux-gnu -fsanitize=address -fsanitize-address-use-after-scope %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-USE-AFTER-SCOPE // RUN: %clang_cl --target=x86_64-windows -fsanitize=address -fsanitize-address-use-after-scope -### -- %s 2>&1 | FileCheck %s --check-prefix=CHECK-USE-AFTER-SCOPE // CHECK-USE-AFTER-SCOPE: -cc1{{.*}}-fsanitize-address-use-after-scope