deno_core icon indicating copy to clipboard operation
deno_core copied to clipboard

Fix aliasing bugs revealed by Tree Borrows

Open JoJoDeveloping opened this issue 5 months ago • 2 comments

This PR fixes some aliasing bugs caused by improper usage of shared and mutable references intermixed with raw pointer accesses. These have been discovered by running the testsuite under Tree Borrows, a new aliasing model for Rust and a potential replacement for Stacked Borrows. While TB is in general more lenient than SB, there are some cases where it is more strict due to SB relying on gross hacks, which have been removed from TB.

Turns out that deno_core relied on such hacks. The problem is with the DynFutureInfoErased, which has arbitrary data that is being modified by a future while other references to it are held in the runtime. To prevent this, that is put into an UnsafeCell which makes such modifications allowed.

But UnsafeCell only ensures that shared references can be modified without causing UB, it does not permit mutable references to be aliased. Thus, one should take care not to create mutable references here. The easiest way of doing this is to remove the implementation of DerefMut and AsMut for ArenaBox<T>, and work around the few cases where that leads to breakage (by using raw pointers, which is also more robust in terms of the aliasing model).

This fixes #884. See that issue for more information.

I have not audited the entire crate, so it is possible that other TB bugs are lurking elsewhere; especially in the parts that Miri can currently not test properly. But this at least makes all the test that could work, do work.

JoJoDeveloping avatar Aug 29 '24 09:08 JoJoDeveloping