This is an archive of the discontinued LLVM Phabricator instance.

[Clang] reject bit-fields as instruction operands in Microsoft style inline asm blocks.
ClosedPublic

Authored by tahonermann on Oct 7 2022, 4:35 PM.

Details

Summary

MSVC allows bit-fields to be specified as instruction operands in inline asm blocks. Such references are resolved to the address of the allocation unit that the named bitfield is a part of. The result is that reads and writes of such operands will read or mutate nearby bit-fields stored in the same allocation unit. This is a surprising behavior for which MSVC issues warning C4401, "'<identifier>': member is bit field". Intel's icc compiler also allows such bit-field references, but does not issue a diagnostic.

Prior to this change, Clang fails the following assertion when a bit-field is referenced in such instructions:

clang/lib/CodeGen/CGValue.h:338: llvm::Value* clang::CodeGen::LValue::getPointer(clang::CodeGen::CodeGenFunction&) const: Assertion `isSimple()' failed.

In non-assert enabled builds, Clang's behavior appears to match the behavior of the MSVC and icc compilers, though it is unknown if that is by design or happenstance.

Following this change, attempts to use a bit-field as an instruction operand in Microsoft style asm blocks is diagnosed as an error due to the risk of unintentionally reading or writing nearby bit-fields.

Fixes https://github.com/llvm/llvm-project/issues/57791

Diff Detail

Event Timeline

tahonermann created this revision.Oct 7 2022, 4:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 7 2022, 4:35 PM
tahonermann requested review of this revision.Oct 7 2022, 4:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 7 2022, 4:35 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
erichkeane accepted this revision.Oct 10 2022, 7:16 AM

1 nit, otherwise LGTM.

clang/lib/Sema/SemaStmtAsm.cpp
936–937

There is enough repetition of Exprs[I] I suspect there is value to splitting them up as:

Expr *CurExpr = Exprs[I];

for readability purposes.

This revision is now accepted and ready to land.Oct 10 2022, 7:16 AM
aaron.ballman accepted this revision.Oct 10 2022, 8:24 AM

LGTM as well with a diagnostic wording suggestion; please also add a release note for the fix.

clang/include/clang/Basic/DiagnosticCommonKinds.td
283

(Not strongly tied to the new wording, but we try to write diagnostics in singular form when possible and that led me to this rewording.)

Rebased and addressed code review feedback.

This revision was landed with ongoing or failed builds.Oct 10 2022, 12:45 PM
This revision was automatically updated to reflect the committed changes.
tahonermann marked 2 inline comments as done.Oct 10 2022, 1:14 PM
tahonermann added inline comments.
clang/lib/Sema/SemaStmtAsm.cpp
936–937

Thank you, this was a good suggestion!