From: Hans Wennborg Date: Thu, 4 Sep 2014 22:16:48 +0000 (+0000) Subject: Don't allow inline asm statements to reference parameters in naked functions X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e0e727e02529f654e4f8a1bfdd266149672dd9a9;p=clang Don't allow inline asm statements to reference parameters in naked functions Differential Revision: http://reviews.llvm.org/D5183 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@217200 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index cda5aa02ca..78f3eeb70c 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -6993,6 +6993,8 @@ def err_filter_expression_integral : Error< 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< diff --git a/lib/Sema/SemaStmtAsm.cpp b/lib/Sema/SemaStmtAsm.cpp index 47a7672ae1..989999f18d 100644 --- a/lib/Sema/SemaStmtAsm.cpp +++ b/lib/Sema/SemaStmtAsm.cpp @@ -405,6 +405,19 @@ ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, Result = CheckPlaceholderExpr(Result.get()); if (!Result.isUsable()) return Result; + // Referring to parameters is not allowed in naked functions. + if (DeclRefExpr *DRE = dyn_cast(Result.get())) { + if (ParmVarDecl *Parm = dyn_cast(DRE->getDecl())) { + if (FunctionDecl *Func = dyn_cast(Parm->getDeclContext())) { + if (Func->hasAttr()) { + Diag(Id.getLocStart(), diag::err_asm_naked_parm_ref); + Diag(Func->getAttr()->getLocation(), diag::note_attribute); + return ExprError(); + } + } + } + } + QualType T = Result.get()->getType(); // For now, reject dependent types. diff --git a/test/Sema/attr-naked.c b/test/Sema/attr-naked.c index c1ad52c454..f6c26b29fc 100644 --- a/test/Sema/attr-naked.c +++ b/test/Sema/attr-naked.c @@ -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"); +} diff --git a/test/Sema/ms-inline-asm.c b/test/Sema/ms-inline-asm.c index d4227027d7..358b0bd454 100644 --- a/test/Sema/ms-inline-asm.c +++ b/test/Sema/ms-inline-asm.c @@ -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 } +}