Skip to content
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

[out.ptr.t][inout.ptr.t] The notes for operator void** might suggest violation of the strict aliasing rule #4701

Closed
frederick-vs-ja opened this issue Jun 16, 2021 · 6 comments · Fixed by #5158
Assignees

Comments

@frederick-vs-ja
Copy link
Contributor

The suggested implementation strategy can make the stored Pointer member subobject modified via a void* lvalue, which violates the strict aliasing rule ([basic.lval]/11), and might result in miscompilation on compilers that make use of TBAA.
A std::byte array member of appropriate size and alignment is suitable for the required type punning due to implicit object creation (the destructor can read p by std::bit_cast if the such strategy is used).

As the strategy using a byte array may be too delicate for a note, can we just remove the notes, or replace the wording with a less detailed one?
(E.g. Recommended practice: If possible, implementations should use no additional storage with respect to that for p in order to support both conversion functions.)

@jensmaurer
Copy link
Member

The TBAA violation was known when the paper was approved by LWG.

@jwakely , do you think there is anything we can do to this note to make it palatable?

@timsong-cpp
Copy link
Contributor

I think this is why we used "can be" rather than "is" - it can be viable on non-TBAA implementations. For some implementations it's not a viable implementation strategy, whether because of TBAA or something else (e.g., void* might not have the same size and alignment as Pointer).

@jwakely
Copy link
Member

jwakely commented Jun 17, 2021

The main motivation for that member is to interoperate with APIs doing disgusting aliasing violations. The motivating examples in the paper involve aliasing violations, but that doesn't happen inside the standard library. If users choose to pass the void** to something that misuses it, that's not the library's problem, and it's exactly what they'd be doing today without the std::out_ptr utility.

@timsong-cpp
Copy link
Contributor

The main motivation for that member is to interoperate with APIs doing disgusting aliasing violations. The motivating examples in the paper involve aliasing violations, but that doesn't happen inside the standard library. If users choose to pass the void** to something that misuses it, that's not the library's problem, and it's exactly what they'd be doing today without the std::out_ptr utility.

We intentionally worded this to make the behavior well-defined even for what would otherwise have been disgusting aliasing violations though - that's why we didn't just specify this as returning the result of reinterpret_cast.

@frederick-vs-ja
Copy link
Contributor Author

Can we add "for some compilers" to indicate that such strategy is not always viable?
Users may write analogies of these types, IMO it should be noted that the strategy is not portable.

@jensmaurer
Copy link
Member

We tend to avoid the term "compiler" in the standard; some of the existing uses would benefit from replacement by "implementation" or similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants