From: Hans Wennborg Date: Wed, 8 Oct 2014 01:58:02 +0000 (+0000) Subject: Disallow using function parameters in extended asm inputs or outputs in naked functio... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9e077ef0c26fb864b7c8794455d5690e0d021c26;p=clang Disallow using function parameters in extended asm inputs or outputs in naked functions (PR21178) Clang won't emit any prologues for such functions, so it would assert trying to codegen the parameter references. This patch makes Clang check the extended asm inputs and outputs for references to function parameters. Differential Revision: http://reviews.llvm.org/D5640 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@219272 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaStmtAsm.cpp b/lib/Sema/SemaStmtAsm.cpp index dc50e87b58..7f79a05b46 100644 --- a/lib/Sema/SemaStmtAsm.cpp +++ b/lib/Sema/SemaStmtAsm.cpp @@ -75,6 +75,32 @@ static bool isOperandMentioned(unsigned OpNo, return false; } +static bool CheckNakedParmReference(Expr *E, Sema &S) { + FunctionDecl *Func = dyn_cast(S.CurContext); + if (!Func) + return false; + if (!Func->hasAttr()) + return false; + + SmallVector WorkList; + WorkList.push_back(E); + while (WorkList.size()) { + Expr *E = WorkList.pop_back_val(); + if (DeclRefExpr *DRE = dyn_cast(E)) { + if (isa(DRE->getDecl())) { + S.Diag(DRE->getLocStart(), diag::err_asm_naked_parm_ref); + S.Diag(Func->getAttr()->getLocation(), diag::note_attribute); + return true; + } + } + for (Stmt *Child : E->children()) { + if (Expr *E = dyn_cast_or_null(Child)) + WorkList.push_back(E); + } + } + return false; +} + StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, bool IsVolatile, unsigned NumOutputs, unsigned NumInputs, IdentifierInfo **Names, @@ -117,6 +143,10 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, diag::err_asm_invalid_lvalue_in_output) << OutputExpr->getSourceRange()); + // Referring to parameters is not allowed in naked functions. + if (CheckNakedParmReference(OutputExpr, *this)) + return StmtError(); + if (RequireCompleteType(OutputExpr->getLocStart(), Exprs[i]->getType(), diag::err_dereference_incomplete_type)) return StmtError(); @@ -160,6 +190,10 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, Expr *InputExpr = Exprs[i]; + // Referring to parameters is not allowed in naked functions. + if (CheckNakedParmReference(InputExpr, *this)) + return StmtError(); + // Only allow void types for memory constraints. if (Info.allowsMemory() && !Info.allowsRegister()) { if (CheckAsmLValue(InputExpr, *this)) @@ -421,17 +455,8 @@ ExprResult Sema::LookupInlineAsmIdentifier(CXXScopeSpec &SS, 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(); - } - } - } - } + if (CheckNakedParmReference(Result.get(), *this)) + return ExprError(); QualType T = Result.get()->getType(); diff --git a/test/Sema/attr-naked.c b/test/Sema/attr-naked.c index fcae842775..6b1344a986 100644 --- a/test/Sema/attr-naked.c +++ b/test/Sema/attr-naked.c @@ -32,3 +32,19 @@ __attribute__((naked)) void t7() { asm("movl $42, %eax"); ; } + +extern int x, y; + +__attribute__((naked)) void t8(int z) { // expected-note{{attribute is here}} + __asm__ ("movl $42, %1" + : "=r"(x), + "=r"(z) // expected-error{{parameter references not allowed in naked functions}} + ); +} + +__attribute__((naked)) void t9(int z) { // expected-note{{attribute is here}} + __asm__ ("movl %eax, %1" + : : "r"(x), + "r"(z) // expected-error{{parameter references not allowed in naked functions}} + ); +}