Skip to content

Commit d31f8cc

Browse files
DimitryAndrictstellar
authored andcommitted
[AArch64] Avoid crashing on invalid -Wa,-march= values
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)
1 parent 67b5bc2 commit d31f8cc

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

clang/lib/Driver/ToolChains/Arch/AArch64.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
191191
bool success = true;
192192
// Enable NEON by default.
193193
Features.push_back("+neon");
194-
llvm::StringRef WaMArch = "";
194+
llvm::StringRef WaMArch;
195195
if (ForAS)
196196
for (const auto *A :
197197
Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler))
@@ -201,7 +201,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
201201
// Call getAArch64ArchFeaturesFromMarch only if "-Wa,-march=" or
202202
// "-Xassembler -march" is detected. Otherwise it may return false
203203
// and causes Clang to error out.
204-
if (WaMArch.size())
204+
if (!WaMArch.empty())
205205
success = getAArch64ArchFeaturesFromMarch(D, WaMArch, Args, Features);
206206
else if ((A = Args.getLastArg(options::OPT_march_EQ)))
207207
success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
@@ -222,8 +222,15 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
222222
success = getAArch64MicroArchFeaturesFromMcpu(
223223
D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
224224

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

228235
if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
229236
Features.push_back("-fp-armv8");

clang/test/Driver/aarch64-target-as-march.s

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@
4444
// TARGET-FEATURE-3-NOT: "-target-feature" "+v8.4a"
4545
// TARGET-FEATURE-4: "-target-feature" "+v8.4a"
4646
// TARGET-FEATURE-4-NOT: "-target-feature" "+v8.3a"
47+
48+
// Invalid -march settings
49+
// RUN: %clang --target=aarch64-linux-gnueabi -### -c -Wa,-march=all %s 2>&1 | \
50+
// RUN: FileCheck --check-prefix=INVALID-ARCH-1 %s
51+
// RUN: %clang --target=aarch64-linux-gnueabi -### -c -Wa,-march=foobar %s 2>&1 | \
52+
// RUN: FileCheck --check-prefix=INVALID-ARCH-2 %s
53+
54+
// INVALID-ARCH-1: error: the clang compiler does not support '-march=all'
55+
// INVALID-ARCH-2: error: the clang compiler does not support '-march=foobar'

0 commit comments

Comments
 (0)