]> granicus.if.org Git - clang/commitdiff
Check that the input size is correct for the given constraint.
authorBill Wendling <isanbard@gmail.com>
Mon, 12 Nov 2012 06:42:51 +0000 (06:42 +0000)
committerBill Wendling <isanbard@gmail.com>
Mon, 12 Nov 2012 06:42:51 +0000 (06:42 +0000)
The 'a', 'c', and 'd' constraints on i386 mean a 32-bit register. We cannot
place a 64-bit value into the 32-bit register. Error out instead of causing the
compiler to spew general badness.
<rdar://problem/12415959>

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@167717 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Basic/TargetInfo.h
lib/Basic/Targets.cpp
lib/Sema/SemaStmtAsm.cpp
test/CodeGen/x86_32-inline-asm.c [new file with mode: 0644]

index 0d64bf38ad7ceb368fcf6b0ca614671d82214aee..9110fa36fd51075d4b33b8b642db02a640a707f9 100644 (file)
@@ -5141,6 +5141,8 @@ let CategoryName = "Inline Assembly Issue" in {
     "%diff{$ matching output with type $|}0,1">;
   def err_asm_unknown_register_name : Error<"unknown register name '%0' in asm">;
   def err_asm_empty : Error<"__asm used with no assembly instructions">;
+  def err_asm_invalid_input_size : Error<
+    "invalid input size for constraint '%0'">;
   def err_invalid_asm_cast_lvalue : Error<
     "invalid use of a cast in a inline asm context requiring an l-value: "
     "remove the cast or build with -fheinous-gnu-extensions">;
index b0e58050d5d959b2668a14ef920756e3a89c7702..6774e8ed2c1587d37b96c5a130ed17a3c43049a0 100644 (file)
@@ -517,6 +517,10 @@ public:
   bool validateInputConstraint(ConstraintInfo *OutputConstraints,
                                unsigned NumOutputs,
                                ConstraintInfo &info) const;
+  virtual bool validateInputSize(StringRef /*Constraint*/,
+                                 unsigned /*Size*/) const {
+    return true;
+  }
   virtual bool validateConstraintModifier(StringRef /*Constraint*/,
                                           const char /*Modifier*/,
                                           unsigned /*Size*/) const {
index 26a4f41442d2ce38d6b5611702ca8377508aef00..ea4ac27a3580754ffe818a0d8f2b6a321874d76f 100644 (file)
@@ -2595,6 +2595,19 @@ public:
     if (RegNo == 1) return 2;
     return -1;
   }
+  virtual bool validateInputSize(StringRef Constraint,
+                                 unsigned Size) const {
+    switch (Constraint[0]) {
+    default: break;
+    case 'a':
+    case 'b':
+    case 'c':
+    case 'd':
+      return Size == 32;
+    }
+
+    return true;
+  }
 };
 } // end anonymous namespace
 
index 7c2c766e46153bc1d1a6032c60453366f99af5cd..e3b5dd851f0eebc77ff4b8d1111fbd746fa085d5 100644 (file)
@@ -179,6 +179,17 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple,
 
     Exprs[i] = Result.take();
     InputConstraintInfos.push_back(Info);
+
+    const Type *Ty = Exprs[i]->getType().getTypePtr();
+    if (Ty->isDependentType() || Ty->isIncompleteType())
+      continue;
+
+    unsigned Size = Context.getTypeSize(Ty);
+    if (!Context.getTargetInfo().validateInputSize(Literal->getString(),
+                                                   Size))
+      return StmtError(Diag(InputExpr->getLocStart(),
+                            diag::err_asm_invalid_input_size)
+                       << Info.getConstraintStr());
   }
 
   // Check that the clobbers are valid.
diff --git a/test/CodeGen/x86_32-inline-asm.c b/test/CodeGen/x86_32-inline-asm.c
new file mode 100644 (file)
index 0000000..0596ba4
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -triple i386-apple-darwin9 -verify %s
+// <rdar://problem/12415959>
+
+typedef unsigned int u_int32_t;
+typedef u_int32_t uint32_t;
+
+typedef unsigned long long u_int64_t;
+typedef u_int64_t uint64_t;
+
+int main () {
+  uint32_t msr = 0x8b;
+  uint64_t val = 0;
+  __asm__ volatile("wrmsr"
+                   :
+                   : "c" (msr),
+                     "a" ((val & 0xFFFFFFFFUL)), // expected-error {{invalid input size for constraint 'a'}}
+                     "d" (((val >> 32) & 0xFFFFFFFFUL)));
+}