r217200 - Don't allow inline asm statements to reference parameters in naked functions

Hans Wennborg hans at hanshq.net
Thu Sep 4 15:16:48 PDT 2014


Author: hans
Date: Thu Sep  4 17:16:48 2014
New Revision: 217200

URL: http://llvm.org/viewvc/llvm-project?rev=217200&view=rev
Log:
Don't allow inline asm statements to reference parameters in naked functions

Differential Revision: http://reviews.llvm.org/D5183

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaStmtAsm.cpp
    cfe/trunk/test/Sema/attr-naked.c
    cfe/trunk/test/Sema/ms-inline-asm.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=217200&r1=217199&r2=217200&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Sep  4 17:16:48 2014
@@ -6993,6 +6993,8 @@ def err_filter_expression_integral : Err
 
 def err_non_asm_stmt_in_naked_function : Error<
   "non-ASM statement in naked function is not supported">;
+def err_asm_naked_parm_ref : Error<
+  "parameter references not allowed in naked functions">;
 
 // OpenCL warnings and errors.
 def err_invalid_astype_of_different_size : Error<

Modified: cfe/trunk/lib/Sema/SemaStmtAsm.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaStmtAsm.cpp?rev=217200&r1=217199&r2=217200&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaStmtAsm.cpp (original)
+++ cfe/trunk/lib/Sema/SemaStmtAsm.cpp Thu Sep  4 17:16:48 2014
@@ -405,6 +405,19 @@ ExprResult Sema::LookupInlineAsmIdentifi
   Result = CheckPlaceholderExpr(Result.get());
   if (!Result.isUsable()) return Result;
 
+  // Referring to parameters is not allowed in naked functions.
+  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Result.get())) {
+    if (ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
+      if (FunctionDecl *Func = dyn_cast<FunctionDecl>(Parm->getDeclContext())) {
+        if (Func->hasAttr<NakedAttr>()) {
+          Diag(Id.getLocStart(), diag::err_asm_naked_parm_ref);
+          Diag(Func->getAttr<NakedAttr>()->getLocation(), diag::note_attribute);
+          return ExprError();
+        }
+      }
+    }
+  }
+
   QualType T = Result.get()->getType();
 
   // For now, reject dependent types.

Modified: cfe/trunk/test/Sema/attr-naked.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-naked.c?rev=217200&r1=217199&r2=217200&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-naked.c (original)
+++ cfe/trunk/test/Sema/attr-naked.c Thu Sep  4 17:16:48 2014
@@ -18,3 +18,8 @@ __attribute__((naked)) int t4() {
   asm("movl $42, %eax");
   asm("retl");
 }
+
+__attribute__((naked)) int t5(int x) {
+  asm("movl x, %eax");
+  asm("retl");
+}

Modified: cfe/trunk/test/Sema/ms-inline-asm.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/ms-inline-asm.c?rev=217200&r1=217199&r2=217200&view=diff
==============================================================================
--- cfe/trunk/test/Sema/ms-inline-asm.c (original)
+++ cfe/trunk/test/Sema/ms-inline-asm.c Thu Sep  4 17:16:48 2014
@@ -103,3 +103,14 @@ void t4() {
 void test_operand_size() {
   __asm { call word t4 } // expected-error {{Expected 'PTR' or 'ptr' token!}}
 }
+
+__declspec(naked) int t5(int x) { // expected-note {{attribute is here}}
+  asm { movl eax, x } // expected-error {{parameter references not allowed in naked functions}}
+  asm { retl }
+}
+
+int y;
+__declspec(naked) int t6(int x) {
+  asm { mov eax, y } // No error.
+  asm { ret }
+}





More information about the cfe-commits mailing list