[clang] [Clang] Fix crash for incompatible types in inline assembly (PR #119098)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 7 16:18:13 PST 2024


https://github.com/AdUhTkJm updated https://github.com/llvm/llvm-project/pull/119098

>From 26bb776fcd750df11a4940899869f59035f78a79 Mon Sep 17 00:00:00 2001
From: AdUhTkJm <2292398666 at qq.com>
Date: Sun, 8 Dec 2024 08:07:59 +0800
Subject: [PATCH] [Clang] Fix crash for incompatible types in inline assembly

---
 clang/lib/Sema/SemaStmtAsm.cpp | 10 +++++++++-
 clang/test/Sema/PR119098.c     | 23 +++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Sema/PR119098.c

diff --git a/clang/lib/Sema/SemaStmtAsm.cpp b/clang/lib/Sema/SemaStmtAsm.cpp
index 0b272b806391c4..f3e4d08761ea07 100644
--- a/clang/lib/Sema/SemaStmtAsm.cpp
+++ b/clang/lib/Sema/SemaStmtAsm.cpp
@@ -664,11 +664,19 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
       SmallerValueMentioned |= OutSize < InSize;
     }
 
+    // If the input is in an integer register while the output is floating point,
+    // there is no way we can extend and we must reject it.
+    bool FPExtendFromInt = false;
+    if (InputDomain != AD_Float && OutputDomain == AD_Float) {
+      FPExtendFromInt = true;
+    }
+
     // If the smaller value wasn't mentioned in the asm string, and if the
     // output was a register, just extend the shorter one to the size of the
     // larger one.
-    if (!SmallerValueMentioned && InputDomain != AD_Other &&
+    if (!SmallerValueMentioned && !FPExtendFromInt && InputDomain != AD_Other &&
         OutputConstraintInfos[TiedTo].allowsRegister()) {
+
       // FIXME: GCC supports the OutSize to be 128 at maximum. Currently codegen
       // crash when the size larger than the register size. So we limit it here.
       if (OutTy->isStructureType() &&
diff --git a/clang/test/Sema/PR119098.c b/clang/test/Sema/PR119098.c
new file mode 100644
index 00000000000000..48c41cb1bea042
--- /dev/null
+++ b/clang/test/Sema/PR119098.c
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -triple x86_64 -fsyntax-only -verify
+
+// expected-warning at +2 {{incompatible redeclaration of library function 'fabs'}}
+// expected-note at +1 {{'fabs' is a builtin with type 'double (double)'}}
+extern __inline  double
+fabs (char  __x)
+{
+  register double __value;
+  __asm __volatile__
+    ("fabs"
+     : "=t" (__value) : "0" (__x));  // expected-error {{unsupported inline asm: input with type 'char' matching output with type 'double'}}
+  return __value;
+}
+int
+foo ()
+{
+  int i, j, k;
+  double x = 0, y = ((i == j) ? 1 : 0);
+  for (i = 0; i < 10; i++)
+    ;
+  fabs (x - y);
+  return 0;
+}
\ No newline at end of file



More information about the cfe-commits mailing list