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

Optional semantics for Unique #2936

Merged
merged 2 commits into from
Jun 28, 2023
Merged

Optional semantics for Unique #2936

merged 2 commits into from
Jun 28, 2023

Conversation

Vanille-N
Copy link
Contributor

Use with -Zmiri-unique-is-unique, makes core::ptr::Unique get a reborrow with its own permissions.

src/bin/miri.rs Outdated Show resolved Hide resolved
@oli-obk
Copy link
Contributor

oli-obk commented Jun 19, 2023

did some discussions related to this happen somewhere? I'm not against this change, just curious since it came out of nowhere from my perspective :D

@RalfJung
Copy link
Member

No, this is an experiment Neven and me discussed.

Unique having semantics came up in rust-lang/unsafe-code-guidelines#384, rust-lang/unsafe-code-guidelines#326.

Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Very nice, just some nits :)

//@compile-flags: -Zmiri-tree-borrows
//@[uniq]compile-flags: -Zmiri-unique-is-unique
Copy link
Member

Choose a reason for hiding this comment

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

Nothing here actually uses unique, or does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are Vecs and Boxes in the test.

Copy link
Member

Choose a reason for hiding this comment

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

Fair.

Copy link
Member

Choose a reason for hiding this comment

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

Will we get any test breakage if libs decides to remove ptr::Unique from Vec? Or would that be just okay?

Copy link
Member

Choose a reason for hiding this comment

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

We will get test breakage but that seems fine for me, we can then just adjust the tests.

tests/pass/tree_borrows/unique.rs Show resolved Hide resolved
@Vanille-N
Copy link
Contributor Author

Rebased at README: -Zmiri-unique-is-unique

Tests will fail.

@RalfJung
Copy link
Member

If you rebase over latest Miri master (and run ./miri toolchain) you should get a rustc with your retag PR.

@Vanille-N
Copy link
Contributor Author

Vanille-N commented Jun 22, 2023

The problem here is that Unique::as_ptr reborrows and thus does not hand out the actual pointer inside the Unique.

Problem

The following diff illustrates the issue:

--- tests/pass/tree_borrows/unique.uniq.stderr
 | Act|      └─┬──<TAG=base>
 | Act|        └─┬──<TAG=raw>
-| Act|          └────<TAG=uniq, uniq>
+|?Dis|          ├────<TAG=uniq>
+|?Dis|          └────<TAG=uniq>

uniq was named through _.as_ptr(), and independent invocations of as_ptr hand out cousin pointers.

This part of the diff shows that the wrong pointer is being named, since it gets invalidated when the pointer inside Unique should not be.

-| Act|
+|?Dis|

This part of the diff shows that pointers obtained from as_ptr are cousins instead of being identical.

- └────<TAG=uniq, uniq>
+ ├────<TAG=uniq>
+ └────<TAG=uniq>

Solution

In the long term, if we find out that Unique cannot be used for Vec or that there is too much breakage from -Zmiri-unique-is-unique, this is likely to be a cause.
We will have to decide (1) whether that is indented behavior, and (2) if it is then is it the fault of TB or the fault of the implementation of as_ptr ?

Right now, this illustrates that miri_pointer_name is not capable of naming the tags that we want to name without breaking upon -Zmiri-unique-is-unique, see #2939 for an idea of a solution.

@RalfJung
Copy link
Member

Isn't this a bigger problem than just the ui tests? This means we cannot do the equivalent of addr_of_mut!(*mutable_ref), i.e. create a raw pointer with the tag of the noalias pointer (or an equivalent tag).

I guess if the tests still pass we can keep going with the experiment, but eventually we might want a new borrow state for this, one where alias requirements are only enforced while the tag is protected.

@Vanille-N
Copy link
Contributor Author

Yes this would make Unique more strict than &mut which is unexpected. I'm looking for a test that shows that uses of &mut can alternate with uses of addr_of_mut (which apparently wouldn't be possible here with Unique), and if we don't have one yet it's a good occasion to add it.

@Vanille-N Vanille-N marked this pull request as draft June 26, 2023 13:34
@Vanille-N Vanille-N marked this pull request as ready for review June 28, 2023 07:03
@RalfJung
Copy link
Member

marked this pull request as ready for review

FWIW github doesn't send notification for this so it's pretty much useless. Thankfully we have a bot that helps: @rustbot ready. :)

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Jun 28, 2023
tests/fail/tree_borrows/kill-copies.rs Outdated Show resolved Hide resolved
tests/fail/tree_borrows/kill-copies.rs Outdated Show resolved Hide resolved
//@compile-flags: -Zmiri-tree-borrows
//@[uniq]compile-flags: -Zmiri-unique-is-unique
Copy link
Member

Choose a reason for hiding this comment

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

We will get test breakage but that seems fine for me, we can then just adjust the tests.

tests/pass/tree_borrows/unique.rs Outdated Show resolved Hide resolved
tests/pass/tree_borrows/vec_unique.rs Outdated Show resolved Hide resolved
Copy link
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Some tweaks for the comments -- please apply the same changes to all copies of these comments.

Then please squash the commits and we can land this. :)

tests/pass/tree_borrows/vec_unique.rs Outdated Show resolved Hide resolved
tests/pass/tree_borrows/vec_unique.rs Outdated Show resolved Hide resolved
@RalfJung
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 28, 2023

📌 Commit 71f0db4 has been approved by RalfJung

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jun 28, 2023

⌛ Testing commit 71f0db4 with merge a646140...

@bors
Copy link
Collaborator

bors commented Jun 28, 2023

☀️ Test successful - checks-actions
Approved by: RalfJung
Pushing a646140 to master...

@bors bors merged commit a646140 into rust-lang:master Jun 28, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Waiting for a review to complete
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants