# Volatile Wrapping Structs

**URL:** <https://internals.rust-lang.org/t/volatile-wrapping-structs/10273>\
**Category:** libs\
**Created:** [May 25, 2019, 4:45pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273 "2019-05-25T16:45:36Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [May 25, 2019, 4:45pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/1 "2019-05-25T16:45:36Z")

</div>

Having read a lot of the previous threads on volatile reads and writes using the provided functions on pointers. I came to think if it was possible to have `VolatileCellRw<T>`, `VolatileCellRo<T>`, and `VolatileCellWo<T>`. Where they were respectively implement `Deref` and `DerefMut` which could be used with the provided functions (or even better not need then at all).

Would this be enough to guarantee no spurious reads or writes?

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [May 25, 2019, 8:07pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/2 "2019-05-25T20:07:27Z")

</div>

- `Deref` can produce `&T`, which doesn’t have volatile reads.

- `DerefMut` implies `Deref` so `VolatileCellWo` is impossible.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [May 25, 2019, 8:16pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/3 "2019-05-25T20:16:36Z")

</div>

So it would only work if they had their own methods on them which isn’t much better than using pointers directly is it?

---

<div class="post-metadata">

**Author:** ![hyeonu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hyeonu/32/2162_2.png) [@hyeonu](https://internals.rust-lang.org/u/hyeonu)\
**Post date:** [May 26, 2019, 5:30am UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/4 "2019-05-26T05:30:10Z")

</div>

Note that `std::cell::Cell` doesn’t `impl Deref` for the reason @ExpHP mentioned. Imo VolatileCell\* families should follow the interface of `std::cell::Cell`

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [May 26, 2019, 5:56am UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/5 "2019-05-26T05:56:10Z")

</div>

So, as a point of reference, would something like this work?

I tried to model the `Cell` interface as much as possible

[https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e8b787d07bf75da9aa694e8a0d74421c](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e8b787d07bf75da9aa694e8a0d74421c)

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [May 26, 2019, 8:18am UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/6 "2019-05-26T08:18:46Z")

</div>

Would that work if T was a pointer or would that have to be a different struct?

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [May 26, 2019, 8:29am UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/7 "2019-05-26T08:29:32Z")

</div>

I don’t know what you are trying to say.

Do you mean is `VolatileCell<*const Foo>` valid? I don’t see why not.

---

<div class="post-metadata">

**Author:** ![skade](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skade/32/3333_2.png) [@skade](https://internals.rust-lang.org/u/skade)\
**Post date:** [May 26, 2019, 9:28am UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/8 "2019-05-26T09:28:08Z")

</div>

This exists as:

[https://github.com/japaric/vcell](https://github.com/japaric/vcell) \< A cell where all operations are volatile

[https://github.com/japaric/volatile-register](https://github.com/japaric/volatile-register) \< Volatile registers with read-write, read-only and write-only actions

Because everything is volatile, they expect `T: Copy` on the inner type and allow no borrows.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [May 26, 2019, 2:31pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/9 "2019-05-26T14:31:32Z")

</div>

Unfortunately, “volatile cell” does not work _because you are still using references_. A `&VolatileCell<T>` is still marked as `dereferencable` and thus the compiler is allowed to introduce spurious reads. As such, the vcell crate is unfortunately not solving this problem.

To [solve the problem with volatile references](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/49), we need either a wrapper around a raw pointer – something like `VolatileRef<'a, T>` which is basically a newtype around `NonNull<T>`; or we need a language change to no longer allow spurious dereferences of _all_ references. Note that this does not just affect volatile accesses, we also have [a case in libstd that is in conflict with this](https://github.com/rust-lang/rust/issues/55005).

Also see [this UCG discussion](https://github.com/rust-lang/unsafe-code-guidelines/issues/33).

---

<div class="post-metadata">

**Author:** ![dhm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dhm/32/4879_2.png) [@dhm](https://internals.rust-lang.org/u/dhm)\
**Post date:** [May 27, 2019, 11:23am UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/10 "2019-05-27T11:23:05Z")

</div>

> [@RalfJung](#):
>
> A `&VolatileCell<T>` is still marked as `dereferencable` and thus the compiler is allowed to introduce spurious reads. As such, the vcell crate is unfortunately not solving this problem.

So this happens even though `VolatileCell<T>` is wrapping an `UnsafeCell<T>`? In other words, `UnsafeCell<T>` prevents the aliasing analysis of a `&UnsafeCell<T>` to cross the wrapper boundary and be applied to the wrappee, but it does not prevent the `dereferenceable` attribute from crossing the wrapper boundaries, is that it?

In that case, a new `#[lang]` wrapper seems to be needed, to act as a barrier on the `dereferenceable` attribute, is it not?

I mean, I don't think that `VolatileRef[Mut]<'a, T>` can suffice for all use cases, although this one can indeed [be already implemented in Rust (comments on the code are welcome)](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8fa1d2c518210c3d0f2a1480a877fa99), even if it is very hard to get a value in Rust that has not been a reference before (need for the `&raw` operator, here).

For instance, how valid is the example currently provided by the documentation?

> [@https://doc.rust-lang.org/1.35.0/core/ptr/fn.read\_volatile.html](#):
>
> # [Examples](https://doc.rust-lang.org/1.35.0/core/ptr/fn.read_volatile.html#examples)
> 
> Basic usage:
> 
> ```rust
> let x = 12;
> let y = &x as *const i32;
> 
> unsafe {
> assert_eq!(std::ptr::read_volatile(y), 12);
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [May 27, 2019, 11:37am UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/11 "2019-05-27T11:37:39Z")

</div>

> [@dhm](#):
>
> So this happens even though `VolatileCell<T>` is wrapping an `UnsafeCell<T>` ? In other words, `UnsafeCell<T>` prevents the aliasing analysis of a `&UnsafeCell<T>` to cross the wrapper boundary and be applied to the wrappee, but it does not prevent the `dereferenceable` attribute from crossing the wrapper boundaries, is that it?

Correct.

> [@dhm](#):
>
> In that case, a new `#[lang]` wrapper seems to be needed, to act as a barrier on the `dereferenceable` attribute, is it not?

If we want to use references for this, yes. The alternative is to make `UnsafeCell` that wrapper, i.e. to also make it remove the `dereferencable` attribute.

> [@dhm](#):
>
> For instance, how valid is the example currently provided by the documentation?

That example is valid as in there is no UB, but it allows spurious reads. `x` is not an MMIO location so that's not a problem.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [May 27, 2019, 5:52pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/12 "2019-05-27T17:52:07Z")

</div>

Does `MaybeUninit` remove the `dereferencable` attribute? If so, we can use that.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [May 27, 2019, 6:03pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/13 "2019-05-27T18:03:09Z")

</div>

No it does not. It does not have any effect on aliasing either – `&MaybeUninit<i32>` is just as much a read-only reference as `&i32`.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [May 27, 2019, 6:40pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/14 "2019-05-27T18:40:46Z")

</div>

I was thinking `UnsafeCell<MaybeUninit<T>>`, but since that won’t work either, I guess we do need either `&raw` or a lang item.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [August 25, 2019, 6:46pm UTC](https://internals.rust-lang.org/t/volatile-wrapping-structs/10273/15 "2019-08-25T18:46:53Z")

</div>

Closed on August 25, 2019, 6:46pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
