Skip to content

TB: Refine protector end semantics #3732

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

Merged

Conversation

JoJoDeveloping
Copy link
Contributor

@JoJoDeveloping JoJoDeveloping commented Jul 4, 2024

Tree Borrows has protector end tag semantics, namely that protectors ending cause a special implicit read on all locations protected by that protector that have actually been accessed. See also #3067.

While this is enough for ensuring protectors allow adding/reordering reads, it does not prove that one can reorder writes. For this, we need to make this stronger, by making this implicit read be a write in cases when there was a write to the location protected by that protector, i.e. if the permission is Active.

There is a test that shows why this behavior is necessary, see tests/fail/tree_borrows/protector-write-lazy.rs.

@JoJoDeveloping JoJoDeveloping force-pushed the tree-borrows-protector-end-write branch from 62336b4 to 71f6c4f Compare July 4, 2024 09:42
Comment on lines 12 to 18
let funky_ref_lazy_on_fst_elem;
{
// this creates a reference that is Reserved Lazy on the first element (offset 0).
// It does so by doing a proper retag on the second element (offset 1), which is fine
// since nothing else happens at that offset, but the lazy init mechanism means it's
// also reserved at offset 0, but not initialized.
funky_ref_lazy_on_fst_elem =
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason you did not make this let funky_ref_lazy_on_fst_elem = ...?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No reason, just being derpy. 🙂

funky_ref_lazy_on_fst_elem =
unsafe { (&mut *(ptr_to_vec.wrapping_add(1))) as *mut i32 }.wrapping_sub(1);
}
return funky_ref_lazy_on_fst_elem;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return funky_ref_lazy_on_fst_elem;
// If we write to `ref_to_fst_elem` here, then any further access to `funky_ref_lazy_on_fst_elem` would
// definitely be UB. Since the compiler ought to be able to reorder the write of `42` above down to
// here, that means we want this program to also be UB.
return funky_ref_lazy_on_fst_elem;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added!

@@ -28,7 +28,7 @@ impl fmt::Display for AccessCause {
Self::Explicit(kind) => write!(f, "{kind}"),
Self::Reborrow => write!(f, "reborrow"),
Self::Dealloc => write!(f, "deallocation"),
Self::FnExit => write!(f, "protector release"),
Self::FnExit(kind) => write!(f, "protector release ({kind})"),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Self::FnExit(kind) => write!(f, "protector release ({kind})"),
// These are dead code, since the protector release access itself can never
// cause UB (while the protector is active, if some other access invalidates
// further use of the protected tag, that is immediate UB).
// Describing the cause of UB is the only time this function is called.
Self::FnExit(_) => unreachable!("protector accesses can never be the source of UB"),

This way if someone out there happens to somehow trigger this, we'll know. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added it, and all tests still passed.

@JoJoDeveloping JoJoDeveloping force-pushed the tree-borrows-protector-end-write branch from 71f6c4f to 5d11037 Compare July 4, 2024 10:20
@RalfJung
Copy link
Member

RalfJung commented Jul 4, 2024

Looks good, thanks. :)

@bors r+

@bors
Copy link
Contributor

bors commented Jul 4, 2024

📌 Commit 5d11037 has been approved by RalfJung

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jul 4, 2024

⌛ Testing commit 5d11037 with merge 89c5442...

@bors
Copy link
Contributor

bors commented Jul 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants