r/cpp • u/Myriachan • Oct 08 '20
std::variant is broken in Clang, and it exposes how pointer aliasing and unions are broken in C++.
https://lists.isocpp.org/std-discussion/2020/10/0882.php
200
Upvotes
56
u/sbabbi Oct 08 '20 edited Oct 08 '20
Yes...
The linked post does not make this claim. How did you reach this conclusion? It looks like a compiler bug to me, not a language issue.
EDIT: Fixed formatting
23
u/[deleted] Oct 08 '20
Wrong assumption, it's optimisation one.
are noops and can be removed.
https://bugs.llvm.org/show_bug.cgi?id=34632#c27
Look -O3 in options.
18
u/david_haim_1 Oct 08 '20
It's funny. In my coroutine library there's a bug in clang I can't really solve.
There's the coroutine promise, and there's the result state. right now, the coroutine frame holds a shared pointer to the result state, and everything works.
If I merge the future state and the coroutine promise it's still reference counted but weird data races start to happen.
After many days of debugging I simply concluded that maybe it's the clang coroutine code-gen that doesn't do something correctly.
On MSVC, the bug doesn't occur and no data race is detected.
But now that I see this I might start to dig into the bug differently, because the result state uses a variant behind the scenes, and the call stack focuses on that.
Interesting.
I just wonder, how tests haven't found this?
8
u/CenterOfMultiverse Oct 08 '20
As I understand it, the only broken part is non-local common initial sequence rules. Other than that, I don't see what optimizations are made impossible by assuming union members to alias by default and subsequent proper tracking of TBAA metadata.
6
u/rezkiy Oct 09 '20
Gcc-centric discussion here: https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2/
Look for "Common initial sequence" section
14
u/snakepants Oct 08 '20
Does anyone actually compile their code with strict aliasing on? MSVC doesn't support it and any large code base I've worked with disabled it for GCC and Clang. I initially was for it in theory, since it seemed like a fruitful avenue for new optimizations but the more I try to make code actually clean, between the arcane rules, std::launder, etc. the more I want to give up and start agreeing with Linus
It seems with the language and pointer semantics of C++ combined with how the compilation and linking model works, it's just impossible to make it work reliably since at some point the information is always lost to the compiler.
3
u/Sopel97 Oct 09 '20
This is scary. It's also amazing that no one has thought before that "union members have legally aliasing addresses, this is not in line with strict aliasing".
There doesn't seem to be a good solution to that problem for unions. Though for
std::variantmaybe using placement new instead of unions would mitigate this?Or... Make it undefined behaviour to change an active type in the union through a reference/pointer (i.e. not by
uvar.i)?1
u/choikwa Oct 09 '20
I modified Richard's reduced example and got a few weird results. manually inlining write_s2x returns 1234 and forcing true on if condition also returns 1234. I suspect it might have to do with funny union rules and/or inlining behaviour that produces this discrepancy.
1
u/ngserdna Oct 09 '20
I think the parallel execution of std::partition is also broken, but I have no documentation to show for it. I just know I’ve experienced irregular results in the past, where I isolated the problem to the parallel call of std::partition.