ir: Skip phantom anonymous members from inside attribute expressions#3392
Open
armoha wants to merge 1 commit into
Open
ir: Skip phantom anonymous members from inside attribute expressions#3392armoha wants to merge 1 commit into
armoha wants to merge 1 commit into
Conversation
Clang exposes anonymous structs declared inside the parent struct's
attribute expressions (e.g. `__alignof__(struct{int aaa;})` inside
`__attribute__((aligned(...)))`) as child cursors of the surrounding
struct. The existing `is_anonymous() && kind != EnumDecl` check in
`CompInfo::from_ty` promoted them to anonymous members, corrupting
the parent struct's layout.
Filter them out by checking that the child's source range lies fully
within the parent's source range via a new `Cursor::extent_contains`
helper.
Fixes rust-lang#3295
Co-authored-by: barry3406 <duan3406@gmail.com>
05d0dd3 to
adad51c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersede #3361
Fixes #3295
CompInfo::from_tyunconditionally promotes any anonymous struct or union child cursor as a member, adding phantom members to corrupt the struct's layout. Clang exposes anonymous structs declared inside__alignof__expressions within__attribute__((aligned(...)))as child cursors of the surrounding struct.Cursor::extent_containschecks the cursor's source location to find phantom member whose source ranges are outside the parent's body. This PR is based on PR #3361 by @barry3406 and extends it to check the end offset. It isn't valid C syntax for child cursor to start inside but somehow extend past the parent's body but stricter check is just added for safety.Alternatives
Filtering by cursor kind is not sufficient because anonymous members are represented as
StructDeclcursors, notFieldDecl. Walking up the AST and semantic analysis would be more complex.Limitations
I think it would be extremely rare but this PR doesn't work for macros and templates defining both a struct member and an aligned attribute with a nested anonymous struct: