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

make vtable pointers entirely opaque #99420

Merged
merged 16 commits into from
Jul 22, 2022
Merged

make vtable pointers entirely opaque #99420

merged 16 commits into from
Jul 22, 2022

Conversation

RalfJung
Copy link
Member

This implements the scheme discussed in rust-lang/unsafe-code-guidelines#338: vtable pointers should be considered entirely opaque and not even readable by Rust code, similar to function pointers.

  • We have a new kind of GlobalAlloc that symbolically refers to a vtable.
  • Miri uses that kind of allocation when generating a vtable.
  • The codegen backends, upon encountering such an allocation, call vtable_allocation to obtain an actually dataful allocation for this vtable.
  • We need new intrinsics to obtain the size and align from a vtable (for some ptr::metadata APIs), since direct accesses are UB now.

I had to touch quite a bit of code that I am not very familiar with, so some of this might not make much sense...
r? @oli-obk

@rustbot rustbot added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 18, 2022
@rustbot
Copy link
Collaborator

rustbot commented Jul 18, 2022

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

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 @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. 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

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 18, 2022
@@ -240,6 +240,13 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
}
Scalar::Ptr(ptr, _size) => {
let (alloc_id, offset) = ptr.into_parts();
// For vtables, get the underlying data allocation.
let alloc_id = match self.tcx.global_alloc(alloc_id) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the alternative would be to move the body of the GlobalAlloc::Memory match arm below into a new function, and also call that from the GlobalAlloc::Vtable arm. Not sure which is better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, I was going to ask about that when I read the 3 copies of this snippet in the various codegen backends. Another alternative is to add a codegen_alloc function on TyCtxt that does what this snippet here does. I think I may actually prefer that instead of splitting the logic out into a function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I think a function to codegen a &Allocation would be nicer. It would make sense to have a "follow the symbolic allocation" operation (which follows statics and vtables), but for statics we don't want to follow them in codegen, so that is just the wrong operation. Following only vtables seems rather ad-hoc.

In fact can we make the vtable_allocation provider return an &Allocation rather than an AllocId? And maybe not even have an AllocId for the "materialized" vtable? That would be best IMO.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preliminary review, will dig into it more later this week

compiler/rustc_const_eval/src/interpret/memory.rs Outdated Show resolved Hide resolved
library/core/src/ptr/metadata.rs Outdated Show resolved Hide resolved
library/core/src/ptr/metadata.rs Show resolved Hide resolved
library/core/src/ptr/metadata.rs Show resolved Hide resolved
library/core/src/intrinsics.rs Show resolved Hide resolved
@oli-obk
Copy link
Contributor

oli-obk commented Jul 18, 2022

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 18, 2022
@bors
Copy link
Contributor

bors commented Jul 18, 2022

⌛ Trying commit 88c84b2 with merge 02e4d4082990eb37f535b13a7373b6ba6d7f8250...

@bors
Copy link
Contributor

bors commented Jul 18, 2022

☀️ Try build successful - checks-actions
Build commit: 02e4d4082990eb37f535b13a7373b6ba6d7f8250 (02e4d4082990eb37f535b13a7373b6ba6d7f8250)

@rust-timer
Copy link
Collaborator

Queued 02e4d4082990eb37f535b13a7373b6ba6d7f8250 with parent 144227d, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (02e4d4082990eb37f535b13a7373b6ba6d7f8250): comparison url.

Instruction count

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

Max RSS (memory usage)

Results
  • Primary benchmarks: no relevant changes found
  • Secondary benchmarks: mixed results
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
2.3% 2.5% 4
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
-2.3% -3.1% 3
All 😿🎉 (primary) N/A N/A 0

Cycles

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

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

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: +S-waiting-on-review -S-waiting-on-perf -perf-regression

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 18, 2022
@rustbot
Copy link
Collaborator

rustbot commented Jul 20, 2022

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with outstanding comments resolved

compiler/rustc_middle/src/mir/interpret/mod.rs Outdated Show resolved Hide resolved
library/core/src/ptr/metadata.rs Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors commented Jul 21, 2022

☀️ Try build successful - checks-actions
Build commit: 1e72cb883abe838e32fdd99537aeeda0fe0723b9 (1e72cb883abe838e32fdd99537aeeda0fe0723b9)

@RalfJung
Copy link
Member Author

When I download that toolchain, and check out commit 3de6c04269a7d315f7e9864b9013451cd9580a08 of xsv (the thing that failed), everything passes. I also ran a local build of this branch, and ran ./x.py test src/tools/cargotest --stage 0. I cannot reproduce the problem.

So maybe it is random, after all?
@bors r=oli-obk

@bors
Copy link
Contributor

bors commented Jul 22, 2022

📌 Commit d46dfa2 has been approved by oli-obk

It is now in the queue for this repository.

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 22, 2022
@bors
Copy link
Contributor

bors commented Jul 22, 2022

⌛ Testing commit d46dfa2 with merge aa01891...

@bors
Copy link
Contributor

bors commented Jul 22, 2022

☀️ Test successful - checks-actions
Approved by: oli-obk
Pushing aa01891 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 22, 2022
@bors bors merged commit aa01891 into rust-lang:master Jul 22, 2022
@rustbot rustbot added this to the 1.64.0 milestone Jul 22, 2022
@rust-highfive
Copy link
Collaborator

📣 Toolstate changed by #99420!

Tested on commit aa01891.
Direct link to PR: #99420

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

rust-highfive added a commit to rust-lang-nursery/rust-toolstate that referenced this pull request Jul 22, 2022
Tested on commit rust-lang/rust@aa01891.
Direct link to PR: <rust-lang/rust#99420>

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

Finished benchmarking commit (aa01891): comparison url.

Instruction count

  • Primary benchmarks: no relevant changes found
  • Secondary benchmarks: 😿 relevant regressions found
mean1 max count2
Regressions 😿
(primary)
N/A N/A 0
Regressions 😿
(secondary)
0.4% 0.5% 2
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
N/A N/A 0
All 😿🎉 (primary) N/A N/A 0

Max RSS (memory usage)

Results
  • Primary benchmarks: 😿 relevant regression found
  • Secondary benchmarks: 😿 relevant regressions found
mean1 max count2
Regressions 😿
(primary)
2.8% 2.8% 1
Regressions 😿
(secondary)
2.5% 2.6% 2
Improvements 🎉
(primary)
N/A N/A 0
Improvements 🎉
(secondary)
N/A N/A 0
All 😿🎉 (primary) 2.8% 2.8% 1

Cycles

Results
  • Primary benchmarks: mixed results
  • Secondary benchmarks: 😿 relevant regression found
mean1 max count2
Regressions 😿
(primary)
3.5% 3.5% 1
Regressions 😿
(secondary)
2.7% 2.7% 1
Improvements 🎉
(primary)
-2.3% -2.3% 1
Improvements 🎉
(secondary)
N/A N/A 0
All 😿🎉 (primary) 0.6% 3.5% 2

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 3

  2. number of relevant changes 2 3

bors added a commit to rust-lang/miri that referenced this pull request Jul 22, 2022
adjust for symbolic vtables

The Miri side of rust-lang/rust#99420
bors added a commit to rust-lang/miri that referenced this pull request Jul 22, 2022
adjust for symbolic vtables

The Miri side of rust-lang/rust#99420
@RalfJung RalfJung deleted the vtable branch July 22, 2022 12:51
celinval added a commit to celinval/kani-dev that referenced this pull request Aug 8, 2022
celinval added a commit to celinval/kani-dev that referenced this pull request Aug 15, 2022
celinval added a commit to model-checking/kani that referenced this pull request Aug 17, 2022
* Fix compilation errors

Regression is still failing. Related changes:

- rust-lang/rust#99420
- rust-lang/rust#99495
- rust-lang/rust#99844
- rust-lang/rust#99058

* Change test to expect compilation failure

The compiler has reverted their fix to Opaque types due to performance
degradation.

* Fix VTable handling now that it has an Opaque type

 - Add an implementation for vtable_size and vtable_align intrinsics.
 - Change how we handled Foreign types. Even though they are unsized, a
   pointer to foreign types is a thin pointer.

Co-authored-by: Daniel Schwartz-Narbonne <danielsn@users.noreply.github.com>
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Aug 19, 2022
…enkov

make NOP dyn casts not require anything about the vtable

As suggested [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/144729-t-types/topic/dyn-upcasting.20stabilization/near/292151439). This matches what the codegen backends already do, and what Miri did do until rust-lang#99420 when I made it super extra paranoid.
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this pull request Aug 24, 2022
make NOP dyn casts not require anything about the vtable

As suggested [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/144729-t-types/topic/dyn-upcasting.20stabilization/near/292151439). This matches what the codegen backends already do, and what Miri did do until rust-lang/rust#99420 when I made it super extra paranoid.
matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this pull request Mar 7, 2023
make vtable pointers entirely opaque

This implements the scheme discussed in rust-lang/unsafe-code-guidelines#338: vtable pointers should be considered entirely opaque and not even readable by Rust code, similar to function pointers.

- We have a new kind of `GlobalAlloc` that symbolically refers to a vtable.
- Miri uses that kind of allocation when generating a vtable.
- The codegen backends, upon encountering such an allocation, call `vtable_allocation` to obtain an actually dataful allocation for this vtable.
- We need new intrinsics to obtain the size and align from a vtable (for some `ptr::metadata` APIs), since direct accesses are UB now.

I had to touch quite a bit of code that I am not very familiar with, so some of this might not make much sense...
r? `@oli-obk`
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-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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.