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

The offset_of! macro may need to be a compiler intrinsic #48956

Closed
alexcrichton opened this issue Mar 12, 2018 · 6 comments
Closed

The offset_of! macro may need to be a compiler intrinsic #48956

alexcrichton opened this issue Mar 12, 2018 · 6 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@alexcrichton
Copy link
Member

This came up a few weeks ago in libs triage specifically related to #48493 where we were wondering if there were other locations in the standard library that needed to be adjusted to handle uninhabited enums being handled in generic locations. While not many locations in the standard library leapt to mind one thing we thought of was the offset_of! macro.

For example if we've got something like:

struct Foo<T> { a: T, b: usize }

do existing offset_of! macros handle this?

offset_of!(Foo<Void>, b)

or is there "technical UB that rustc may paper over today" somewhere in there?

cc @dtolnay, I think you had better ideas about examples here than I!

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Mar 12, 2018
@kennytm
Copy link
Member

kennytm commented Mar 12, 2018

Existing crates implementing offset_of:

(Also, there should be an offset_of_val! in additional to offset_of! due to DST field.)

@kennytm kennytm added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Mar 12, 2018
@oli-obk
Copy link
Contributor

oli-obk commented Mar 13, 2018

Why do we even need a memory location for that? Can't we just create a raw pointer from the ether and operate on that?

https://play.rust-lang.org/?gist=79229d888ce819235d5350241a037a78&version=stable

@alexcrichton
Copy link
Member Author

Perhaps! I recall some discussion in the past about how valid the temporary reference is there, but I'd naively assume that such a construction was reasonable!

@steveklabnik
Copy link
Member

I guess @rust-lang/lang would have to answer this, but I think the answer is "yes". We've had some things around this area like https://internals.rust-lang.org/t/pre-rfc-add-a-new-offset-of-macro-to-core-mem/9273

@Centril
Copy link
Contributor

Centril commented Oct 21, 2019

(It's currently not possible to implement offset_of! in a sound manner with Rust. The memoffset crate, while being unsound, has been improved to make the UB less exploitable.)

cc #64490
cc @RalfJung

@est31
Copy link
Member

est31 commented Nov 20, 2023

Given that we have had rust-lang/rfcs#3308 and now have the offset_of! macro (#106655), I think this can be closed.

@est31 est31 closed this as completed Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants