Skip to content

Commit df08b2f

Browse files
committed
[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
1 parent 3608e18 commit df08b2f

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
@@ -225,7 +225,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
225225
bool success = true;
226226
// Enable NEON by default.
227227
Features.push_back("+neon");
228-
llvm::StringRef WaMArch = "";
228+
llvm::StringRef WaMArch;
229229
if (ForAS)
230230
for (const auto *A :
231231
Args.filtered(options::OPT_Wa_COMMA, options::OPT_Xassembler))
@@ -235,7 +235,7 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
235235
// Call getAArch64ArchFeaturesFromMarch only if "-Wa,-march=" or
236236
// "-Xassembler -march" is detected. Otherwise it may return false
237237
// and causes Clang to error out.
238-
if (WaMArch.size())
238+
if (!WaMArch.empty())
239239
success = getAArch64ArchFeaturesFromMarch(D, WaMArch, Args, Features);
240240
else if ((A = Args.getLastArg(options::OPT_march_EQ)))
241241
success = getAArch64ArchFeaturesFromMarch(D, A->getValue(), Args, Features);
@@ -259,8 +259,15 @@ void aarch64::getAArch64TargetFeatures(const Driver &D,
259259
success = getAArch64MicroArchFeaturesFromMcpu(
260260
D, getAArch64TargetCPU(Args, Triple, A), Args, Features);
261261

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

265272
if (Args.getLastArg(options::OPT_mgeneral_regs_only)) {
266273
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)