You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Which creates 2 &mut x references and uses the first after the construction of the second, leading to UB absent in the original C code.
But it could use translate as:
This translation would prevent the UB from creating multiple &mut to the same static, and moreover makes the definition of x itself completely safe. It would even work with pointer typed static __thread variables because unlike normal statics, #[thread_local]static does not need to be Sync.
The text was updated successfully, but these errors were encountered:
What we should be doing, more generally as well, is always using addr_of_mut! instead of &mut so that we never create temporary references where we don't need to. Replacing the &muts here with addr_of_mut!s gets rid of the UB, as per miri. See #301 for more on this.
That said, a translation using Cell seems quite smart here, as then accessing the static is fully safe, as using .as_ptr() only doesn't incur the T: Copy restriction .get() adds. Most of this is due to {Cell,UnsafeCell}::as_ptr being basically the same as addr_of_mut!. We can see if this approach works.
Thread locals are currently translated as
static mut
which isunsafe
, but more importantly are accessed by&mut
which adds UB compared to C.As a minimal example this:
Currently translates on the website (https://c2rust.com) as:
Which creates 2
&mut x
references and uses the first after the construction of the second, leading to UB absent in the original C code.But it could use translate as:
This translation would prevent the UB from creating multiple
&mut
to the same static, and moreover makes the definition ofx
itself completely safe. It would even work with pointer typedstatic __thread
variables because unlike normal statics,#[thread_local]
static
does not need to beSync
.The text was updated successfully, but these errors were encountered: