Skip to content

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

Description

@alexcrichton

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!

Activity

  1. kennytm commented on Mar 12, 2018

    @kennytm
    Member

    Existing crates implementing offset_of:

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

  2. added
    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.
    on Mar 12, 2018
  3. oli-obk commented on Mar 13, 2018

    @oli-obk
    Contributor

    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

  4. alexcrichton commented on Mar 13, 2018

    @alexcrichton
    MemberAuthor

    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!

  5. steveklabnik commented on Oct 21, 2019

    @steveklabnik
    Contributor

    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

  6. Centril commented on Oct 21, 2019

    @Centril
    Contributor

    (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

  7. est31 commented on Nov 20, 2023

    @est31
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions