Skip to content

Commit

Permalink
[AArch64] Avoid crashing on invalid -Wa,-march= values
Browse files Browse the repository at this point in the history
As reported in https://bugs.freebsd.org/260078, the gnutls Makefiles
pass -Wa,-march=all to compile a number of assembly files. Clang does
not support this -march value, but because of a mistake in handling
the arguments, an unitialized Arg pointer is dereferenced, which can
cause a segfault.

Work around this by adding a check if the local WaMArch variable is
initialized, and if so, using its value in the diagnostic message.

Reviewed By: tschuett

Differential Revision: https://reviews.llvm.org/D114677

(cherry picked from commit df08b2f)
  • Loading branch information
DimitryAndric authored and tstellar committed Jan 5, 2022
1 parent 67b5bc2 commit d31f8cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 11 additions & 4 deletions clang/lib/Driver/ToolChains/Arch/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
bool success = true;
// Enable NEON by default.
Features.push_back("+neon");
llvm::StringRef WaMArch = "";
llvm::StringRef WaMArch;
if (ForAS)
for (const auto *A :
Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler))
Expand All @@ -201,7 +201,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
// Call getAArch64ArchFeaturesFromMarch only if "-Wa,-march=" or
// "-Xassembler -march" is detected. Otherwise it may return false
// and causes Clang to error out.
if (WaMArch.size())
if (!WaMArch.empty())
success = getAArch64ArchFeaturesFromMarch(D, WaMArch, Args, Features);
else if ((A = Args.getLastArg(options::OPT_march_EQ)))
success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
Expand All @@ -222,8 +222,15 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
success = getAArch64MicroArchFeaturesFromMcpu(
D, getAArch64TargetCPU(Args, Triple, A), Args, Features);

if (!success)
D.Diag(diag::err_drv_clang_unsupported) << A->getAsString(Args);
if (!success) {
auto Diag = D.Diag(diag::err_drv_clang_unsupported);
// If "-Wa,-march=" is used, 'WaMArch' will contain the argument's value,
// while 'A' is uninitialized. Only dereference 'A' in the other case.
if (!WaMArch.empty())
Diag << "-march=" + WaMArch.str();
else
Diag << A->getAsString(Args);
}

if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
Features.push_back("-fp-armv8");
Expand Down
9 changes: 9 additions & 0 deletions clang/test/Driver/aarch64-target-as-march.s
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@
// TARGET-FEATURE-3-NOT: "-target-feature" "+v8.4a"
// TARGET-FEATURE-4: "-target-feature" "+v8.4a"
// TARGET-FEATURE-4-NOT: "-target-feature" "+v8.3a"

// Invalid -march settings
// RUN: %clang --target=aarch64-linux-gnueabi -### -c -Wa,-march=all %s 2>&1 | \
// RUN: FileCheck --check-prefix=INVALID-ARCH-1 %s
// RUN: %clang --target=aarch64-linux-gnueabi -### -c -Wa,-march=foobar %s 2>&1 | \
// RUN: FileCheck --check-prefix=INVALID-ARCH-2 %s

// INVALID-ARCH-1: error: the clang compiler does not support '-march=all'
// INVALID-ARCH-2: error: the clang compiler does not support '-march=foobar'

0 comments on commit d31f8cc

Please sign in to comment.