From: Alexander Musman Date: Fri, 5 Jun 2015 13:40:59 +0000 (+0000) Subject: Fix for PR14269: Clang crashes when a bit field is used as inline assembler X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=27a3de8cace2484d2f8099ef6e7743dde2b8a987;p=clang Fix for PR14269: Clang crashes when a bit field is used as inline assembler input / output with memory constraint. One generally can't get address of a bit field, so the general solution is to error on such cases. GCC does the same. Patch by Andrey Bokhanko Differential Revision: http://reviews.llvm.org/D10086 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@239153 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index d79aeb2e8f..7f313d3055 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -6338,6 +6338,9 @@ let CategoryName = "Inline Assembly Issue" in { "remove the cast or build with -fheinous-gnu-extensions">; def err_invalid_asm_value_for_constraint : Error <"value '%0' out of range for constraint '%1'">; + def err_asm_bitfield_in_memory_constraint + : Error <"reference to a bit-field in asm " + "%select{input|output}0 with a memory constraint '%1'">; def warn_asm_label_on_auto_decl : Warning< "ignored asm label '%0' on automatic variable">; diff --git a/lib/Sema/SemaStmtAsm.cpp b/lib/Sema/SemaStmtAsm.cpp index 9f48616ea5..d19d8819d8 100644 --- a/lib/Sema/SemaStmtAsm.cpp +++ b/lib/Sema/SemaStmtAsm.cpp @@ -154,6 +154,14 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, if (CheckNakedParmReference(OutputExpr, *this)) return StmtError(); + // Bitfield can't be referenced with a pointer. + if (Info.allowsMemory() && OutputExpr->refersToBitField()) + return StmtError(Diag(OutputExpr->getLocStart(), + diag::err_asm_bitfield_in_memory_constraint) + << 1 + << Info.getConstraintStr() + << OutputExpr->getSourceRange()); + OutputConstraintInfos.push_back(Info); // If this is dependent, just continue. @@ -230,6 +238,14 @@ StmtResult Sema::ActOnGCCAsmStmt(SourceLocation AsmLoc, bool IsSimple, if (CheckNakedParmReference(InputExpr, *this)) return StmtError(); + // Bitfield can't be referenced with a pointer. + if (Info.allowsMemory() && InputExpr->refersToBitField()) + return StmtError(Diag(InputExpr->getLocStart(), + diag::err_asm_bitfield_in_memory_constraint) + << 0 + << Info.getConstraintStr() + << InputExpr->getSourceRange()); + // Only allow void types for memory constraints. if (Info.allowsMemory() && !Info.allowsRegister()) { if (CheckAsmLValue(InputExpr, *this)) diff --git a/test/Sema/asm.c b/test/Sema/asm.c index 6c6f3f398e..1a1e02993a 100644 --- a/test/Sema/asm.c +++ b/test/Sema/asm.c @@ -204,3 +204,20 @@ void fn6() { : "=rm"(a), "=rm"(a) : "11m"(a)) // expected-error {{invalid input constraint '11m' in asm}} } + +// PR14269 +typedef struct test16_foo { + unsigned int field1 : 1; + unsigned int field2 : 2; + unsigned int field3 : 3; +} test16_foo; +test16_foo x; +void test16() +{ + __asm__("movl $5, %0" + : "=rm" (x.field2)); // expected-error {{reference to a bit-field in asm output with a memory constraint '=rm'}} + __asm__("movl $5, %0" + : + : "m" (x.field3)); // expected-error {{reference to a bit-field in asm input with a memory constraint 'm'}} +} +