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 test ptr::slice_mut compliant with Tree Borrows #68

Merged
merged 1 commit into from
Aug 26, 2024

Conversation

JoJoDeveloping
Copy link
Contributor

@JoJoDeveloping JoJoDeveloping commented Aug 23, 2024

Tree Borrows is a new aliasing model for Rust, which features more precise tracking of pointer aliasing. The aim is to be more lenient than Stacked Borrows, while also having less dirty hacks than SB had.

It turns out that the test ptr::slice_mut here relied on one such hack.

The problematic code is here:

soa-derive/tests/ptr.rs

Lines 77 to 93 in ad918be

let mut slice = particles.as_mut_slice();
let ptr = slice.as_mut_ptr();
unsafe {
*ptr.as_mut().unwrap().name = String::from("Fe");
*ptr.as_mut().unwrap().mass = 42.0;
}
assert_eq!(slice.name[0], "Fe");
assert_eq!(slice.mass[0], 42.0);
unsafe {
let slice = ParticleSliceMut::from_raw_parts_mut(ptr, 2);
for mass in slice.mass {
*mass = -1.0;
}
There, a slice is created, and then a reference to it is obtained using slice::as_mut_ptr(), which (internally) creates a new mutable reference. This means that the resulting pointer can not be mixed with accesses to the original slice.

However, the test does so: It first uses the pointer, then the original slice, and then again the pointer. The proper solution is to re-derive the pointer when it's needed again, which is what this PR does.

Tree Borrows is a new aliasing model for Rust, which features more precise
tracking of pointer aliasing. The aim is to be more lenient than Stacked Borrows,
while also having less dirty hacks than SB had.

It turns out that the test ptr::slice_mut here relied on one such gross hack.

The problematic code is here:
https://github.com/lumol-org/soa-derive/blob/ad918be58f47ee6d38e9c83411180e756a3220c0/tests/ptr.rs#L77-L93
There, a slice is created, and then a reference to it is obtained using
`slice::as_mut_ptr()`, which (internally) creates a new mutable
reference. This means that the resulting pointer can not be mixed with
accesses to the original slice.

However, the test does so: It first uses the pointer, then the original
slice, and then again the pointer. The proper solution is to re-derive
the pointer when it's needed again, which is what this PR does.
@Luthaf
Copy link
Member

Luthaf commented Aug 26, 2024

Thanks for this contribution! Is this all that's required to comply with tree borrow in this project?

@Luthaf Luthaf merged commit c5fd944 into lumol-org:master Aug 26, 2024
5 checks passed
@JoJoDeveloping
Copy link
Contributor Author

Is this all that's required to comply with tree borrow in this project?

I hope so. I did not audit all code, but usually, code that complies with Stacked Borrows also complies with Tree Borrows. Also note that I only tested the deno_core package, the other tests all fail because they use features / have dependencies not supported by miri.

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.

2 participants