-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Incorrect thread safety analysis warning on accessing union member. #78131
Comments
@llvm/issue-subscribers-clang-frontend Author: Haojian Wu (hokein)
See [godbolt](https://godbolt.org/z/bc81b64ej)
|
Based on 5ff1644 it looks like we have precise diagnostics controlled So this feels like expected behavior. |
Thanks for the link!
Yeah, I guess we could restruct the union structure to
It looks like analysis not working well on aliases is a known issue/limitation, and is probably hard to fix. (I keep this issue opened, as this is a false positive for aliases, it would be great to have it fixed). |
I believe the analysis simply does not handle members of the anonymous structs and unions well. E.g. there is no aliasing involved in this example: class Test {
Mutex mu;
struct {
int a GUARDED_BY(mu);
int b GUARDED_BY(mu);
};
void test1() {
mu.Lock();
a = 0; // unexpected warning: writing variable 'a' requires holding mutex 'mu' exclusively
mu.Unlock();
}
}; https://godbolt.org/z/8fdxrxzdr It is (roughly) equivalent to this code that does not show any warnings: class Test {
Mutex mu;
int a GUARDED_BY(mu);
int b GUARDED_BY(mu);
void test1() {
mu.Lock();
a = 0; // unexpected warning: writing variable 'a' requires holding mutex 'mu' exclusively
mu.Unlock();
}
}; |
Good catch. Even for regular nested structures, it doesn't work.
Interestingly, If we moved everything to namespace scope rather than |
I'm calling this function when investigating the issue (llvm#78131), and I'm surprised to see the definition is commented out. I think it makes sense to provide the definition even though the implementation is not stable.
The nested union has no way of referring to non-static members of the parent class, because semantically a nested record type doesn't get access to We could special-case implicit fields of anonymous record types and allow them access to their parent's
If there is any way to extract such a nested record (possibly by reference) from its parent, we get into trouble because we lose the parent's fields. Furthermore, we might need to invent an operation to go from What I'd favor instead is putting the attribute on the (possibly implicit) struct member in the parent class. I think we should do this:
For now we can focus on 2. First question would be: is that good enough for you, or do you see a use case for annotating individual nested record members individually? The second question: currently we warn on union {
int a;
int b;
} GUARDED_BY(mu); that the "'guarded_by' attribute only applies to non-static data members and global variables [-Wignored-attributes]". Can we syntactically attach it to the (implicit) non-static data member? Maybe @AaronBallman can chime on this. |
See also CWG2767, which specifies that for struct A {
[[some_attr]] union { ... };
}; the attribute appertains to the implicit unnamed non-static data member of union type. That doesn't necessarily imply that's how GNU attributes should behave, but that seems logical. |
I called this function when investigating the issue (#78131), and I was surprised to see the definition is commented out. I think it makes sense to provide the definition even though the implementation is not stable.
This makes sense to me, especially due to the direction CWG2767 is potentially going.
I think that also seems logical, but it would make me more comfortable if GCC was planning to do the same as well. CC @erichkeane in case his opinion differs. |
See godbolt
The text was updated successfully, but these errors were encountered: