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

implement ptr.addr() via transmute #97710

Merged
merged 1 commit into from Jun 6, 2022
Merged

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented Jun 3, 2022

As per the discussion in rust-lang/unsafe-code-guidelines#286, the semantics for ptr-to-int transmutes that we are going with for now is to make them strip provenance without exposing it. That's exactly what ptr.addr() does! So we can implement ptr.addr() via transmute. This also means that once #97684 lands, Miri can distinguish ptr.addr() from ptr.expose_addr(), and the following code will correctly be called out as having UB (if permissive provenance mode is enabled, which will become the default once the implementation is complete):

fn main() {
    let x: i32 = 3;
    let x_ptr = &x as *const i32;

    let x_usize: usize = x_ptr.addr();
    // Cast back an address that did *not* get exposed.
    let ptr = std::ptr::from_exposed_addr::<i32>(x_usize);
    assert_eq!(unsafe { *ptr }, 3); //~ ERROR Undefined Behavior: dereferencing pointer failed
}

This completes the Miri implementation of the new distinctions introduced by strict provenance. :)

Cc @Gankra -- for now I left in your FIXME(strict_provenance_magic) saying these should be intrinsics, but I do not necessarily agree that they should be. Or if we have an intrinsic, I think it should behave exactly like the transmute does, which makes one wonder why the intrinsic should be needed.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jun 3, 2022
@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @thomcc

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 3, 2022
@thomcc
Copy link
Member

thomcc commented Jun 5, 2022

Seems reasonable. I'm going to mark it as rollup=never out of an over-abundance of caution around the difference between a transmute and a cast in terms of performance, since this is a pretty fundamental thing that gets done in a lot of code (or maybe not as much as I think?)

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Jun 5, 2022

📌 Commit 4291332 has been approved by thomcc

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 5, 2022
@bors
Copy link
Contributor

bors commented Jun 6, 2022

⌛ Testing commit 4291332 with merge 760237f...

@bors
Copy link
Contributor

bors commented Jun 6, 2022

☀️ Test successful - checks-actions
Approved by: thomcc
Pushing 760237f to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 6, 2022
@bors bors merged commit 760237f into rust-lang:master Jun 6, 2022
@rustbot rustbot added this to the 1.63.0 milestone Jun 6, 2022
@rust-highfive
Copy link
Collaborator

📣 Toolstate changed by #97710!

Tested on commit 760237f.
Direct link to PR: #97710

💔 miri on linux: test-pass → test-fail (cc @RalfJung @eddyb @oli-obk).

rust-highfive added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Jun 6, 2022
Tested on commit rust-lang/rust@760237f.
Direct link to PR: <rust-lang/rust#97710>

💔 miri on linux: test-pass → test-fail (cc @RalfJung @eddyb @oli-obk).
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (760237f): comparison url.

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results
  • Primary benchmarks: 🎉 relevant improvements found
  • Secondary benchmarks: 🎉 relevant improvement found
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
N/A N/A 0
Improvements 🎉
(primary)
-1.6% -3.1% 2
Improvements 🎉
(secondary)
-4.4% -4.4% 1
All 😿🎉 (primary) -1.6% -3.1% 2

Cycles

Results
  • Primary benchmarks: 🎉 relevant improvement found
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
4.1% 4.1% 1
Improvements 🎉
(primary)
-2.0% -2.0% 1
Improvements 🎉
(secondary)
-3.5% -3.5% 1
All 😿🎉 (primary) -2.0% -2.0% 1

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

@RalfJung RalfJung deleted the ptr-addr branch June 6, 2022 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants