Skip to content

Declare structs as anonymous within anonymous unions #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2022

Conversation

ZehMatt
Copy link
Contributor

@ZehMatt ZehMatt commented Apr 24, 2022

Declaring named types within anonymous unions is not allowed, GCC fails to build the code otherwise.

@athre0z
Copy link
Member

athre0z commented Apr 24, 2022

Under which conditions does this fail to build? It's compiling perfectly fine with gcc both on my local box and the CI. Removing the names will break the code-generation of various bindings -- that's why the structs are named in the first place.

@ZehMatt
Copy link
Contributor Author

ZehMatt commented Apr 24, 2022

Under which conditions does this fail to build? It's compiling perfectly fine with gcc both on my local box and the CI. Removing the names will break the code-generation of various bindings -- that's why the structs are named in the first place.

Are you building it as C11? The issue is including it from a cpp compilation unit. You'll run into
error: ‘struct ZydisDecodedOperand_::<unnamed union>::ZydisDecodedOperandReg_’ invalid; an anonymous union may only have public non-static data members [-fpermissive]

@ZehMatt
Copy link
Contributor Author

ZehMatt commented Apr 24, 2022

I can keep the struct names but that would require to move them out of the union.

@athre0z
Copy link
Member

athre0z commented Apr 24, 2022

Ah. Yeah, we switched to C11 to permit anonymous unions. Looks like this is an incompatibility between C++ and C11 then, meh. I think we might have to keep the structs named, but move them out of the nesting then instead of removing the names.

@ZehMatt
Copy link
Contributor Author

ZehMatt commented Apr 24, 2022

Also you mentioned of possibly breaking bindings, how do you bind a type that comes from an anonymous struct/union, is it in C11 accessible from the global scope? I'm not that good with C but I would imagine the type can't be accessed, at least not in Cpp.

@athre0z
Copy link
Member

athre0z commented Apr 24, 2022

I don't think it's possible in C11 either, but bindgens often essentially flatten nested types into global scope, so it works there. If the structs are unnamed, they'll essentially get something like a SHA1 hash as their name.

Thinking about it again, however, I remembered that we actually stopped using bindgens for the languages where it mattered, switching to hand-crafted struct types instead. Having these fields named thus doesn't actually matter too much anymore. While I do like having them named, I feel that unnesting them kind of makes discoverability of the struct a lot worse -- you have to follow all the "links" to find the fields you're interested in, so I guess your proposed solution here is preferable.

@ZehMatt
Copy link
Contributor Author

ZehMatt commented Apr 24, 2022

I'm glad you commented before I pushed onto this branch, the alternative would be: https://github.com/ZehMatt/zydis/tree/fix-anonymous-types-2

Copy link
Member

@athre0z athre0z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll say it's probably the lesser evil this way, but I'll wait for @flobernd to take a look as well before merging. Either way, thanks for bringing this to our attention and proposing the fix!

@athre0z athre0z requested a review from flobernd April 24, 2022 10:14
@flobernd
Copy link
Member

@athre0z @ZehMatt Just found some minutes to check this. I'd highly prefer option 2 just to be on the safe side. I remember having some problems when working on the C# bindings. If you are fine as well @athre0z, maybe @ZehMatt you can create a PR for your other solution instead?

@ZehMatt
Copy link
Contributor Author

ZehMatt commented Apr 26, 2022

@athre0z @ZehMatt Just found some minutes to check this. I'd highly prefer option 2 just to be on the safe side. I remember having some problems when working on the C# bindings. If you are fine as well @athre0z, maybe @ZehMatt you can create a PR for your other solution instead?

I can either open a new PR or push it onto this one, as you wish.

@flobernd
Copy link
Member

@ZehMatt Sure, pushing it to this one is even better, thanks! Let's just wait for @athre0z to agree.

@athre0z
Copy link
Member

athre0z commented Apr 26, 2022

Hmm, yeah, it's fine with me. I feel like the struct is easier to navigate when nested as-is, but if there are still bindings that depend on this, let's do it as suggested. I think it will also result in better documentation in doxygen, since it otherwise also generates hashed struct names.

@ZehMatt ZehMatt force-pushed the fix-anonymous-types branch 2 times, most recently from a6159b2 to 7109f84 Compare April 26, 2022 11:00
@ZehMatt
Copy link
Contributor Author

ZehMatt commented Apr 26, 2022

Alright pushed the other branch onto this one, the CI unfortunately won't run since this is my first PR.

@flobernd
Copy link
Member

Thanks! I started the CI.

@athre0z athre0z added C-bug Category: This is a bug (or a fix for a bug, when applied to PRs) A-decoder Area: Decoder P-medium Priority: Medium labels Apr 26, 2022
Copy link
Member

@flobernd flobernd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just a minor style issue, but we can as well solve later.

*/
ZydisRegister value;
} ZydisDecodedOperandReg;
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add some linebreaks (before all the struct comments).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I wasn't sure if I should do it or not since I copied it out as is, I addressed that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

@ZehMatt ZehMatt force-pushed the fix-anonymous-types branch from 7109f84 to 1f1c826 Compare April 26, 2022 13:58
Copy link
Member

@flobernd flobernd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@athre0z athre0z merged commit 6d83a27 into zyantific:master Apr 26, 2022
@ZehMatt ZehMatt deleted the fix-anonymous-types branch April 26, 2022 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-decoder Area: Decoder C-bug Category: This is a bug (or a fix for a bug, when applied to PRs) P-medium Priority: Medium
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants