# Volatile and sensitive memory

**URL:** <https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188>\
**Category:** language design\
**Created:** [February 18, 2016, 8:37pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188 "2016-02-18T20:37:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 8:37pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/1 "2016-02-18T20:37:53Z")

</div>

In Rust, there is no `volatile` modifier. Instead, there are unstable “volatile read” and “volatile write” primitives. And, it seems like those will be stabilized soon. So, since there is no `volatile` modifier, does that mean that the Rust compiler must assume that all memory is volatile by default? In particular, in general given `x: &u64`, the compiler won’t know if x is a reference to value stored in volatile memory, right? Thus, it will never be able to do speculative reads of `x` and in general it must make sure it uses `ACCESS_ONCE` [1] style access to every such value, unless it can prove that `x` is not a reference to volatile memory, which is in general very hard. Is that right?

It seems like it can’t be right, because this seems like it would be horrible for performance. But, I can’t figure out how the proposed “volatile read” and “volatile write” functions work otherwise. In particular, “volatile read” and “volatile write” seem to only make sense under the assumption that there are no speculative reads/writes to the memory they are writing to, but there’s no way to indicate to rustc that no speculative reads/writes are allowed.

[1] [https://lwn.net/Articles/508991/](https://lwn.net/Articles/508991/)

---

<div class="post-metadata">

**Author:** ![notriddle](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/notriddle/32/14082_2.png) [@notriddle](https://internals.rust-lang.org/u/notriddle)\
**Post date:** [February 18, 2016, 9:32pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/2 "2016-02-18T21:32:12Z")

</div>

You cannot `volatile_store` or `volatile_load` an object that is pointed to by a reference; that would be a violation of the aliasing rules, just like having an exclusive reference and a shared reference to the same object is.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 9:46pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/3 "2016-02-18T21:46:08Z")

</div>

I’m not sure what you mean. Here’s some code. Is the compiler allowed rewrite `easy` into `easy_optimized`? Is it allowed to rewrite `tricky` into `tricky_extra_load`?

```rust
#![feature(core_intrinsics)]

fn easy(x: &u64, y: &u64) -> u64 {
    if *x != 0 {
        *x
    } else {
        *y
    }
}

fn easy_optimized(x: &u64, y: &u64) -> u64 {
    // The compiler replaces two loads of |*x| 
    let ret = *x;
    if ret != 0 {
        ret
    } else {
        *y
    }
}

fn tricky(x: &u64, y: &u64) -> u64 {
    if unsafe { std::intrinsics::volatile_load(x as *const u64) } != 0 {
        unsafe { std::intrinsics::volatile_load(x as *const u64) }
    } else {
        *y
    }
}

fn tricky_extra_load(x: &u64, y: &u64) -> u64 {
    // The compiler adds a spurious read of |*x| when compiling `tricky`.
    let _ = *x;
    if unsafe { std::intrinsics::volatile_load(x as *const u64) } != 0 {
        unsafe { std::intrinsics::volatile_load(x as *const u64) }
    } else {
        *y
    }
}

fn main() {
  let x = 1;
  let y = 2;
  easy(&x, &y);
  easy_optimized(&x, &y);
  tricky(&x, &y);
  tricky_extra_load(&x, &y);
}

```

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 18, 2016, 9:47pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/4 "2016-02-18T21:47:42Z")

</div>

I think the assumption is that if memory is volatile, it will _only_ be used via the volatile read/write primitives – and if you mix accesses, we are free to do whatever we want to the other accesses.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 9:48pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/5 "2016-02-18T21:48:48Z")

</div>

Niko, does that mean that the Rust compiler will never insert a read anywhere where the Rust code doesn’t put one? I.e. no speculative loads?

---

<div class="post-metadata">

**Author:** ![huon](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/huon/32/3698_2.png) [@huon](https://internals.rust-lang.org/u/huon)\
**Post date:** [February 18, 2016, 9:54pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/6 "2016-02-18T21:54:16Z")

</div>

I don’t think “volatile memory” is a concept in the C spec. According to ([the last draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) of) the C11 spec, the `volatile` modifier on a variable/memory location means that expressions involving it are evaluated strictly as required by the abstract machine. That is, as far as I can tell, it just controls uses of the variable, not the actual storage itself.

This is exactly what `{read,write}_volatile` offer, explicit control over the uses of a variable. These allow/are designed for building an abstraction like, which is fairly similar to the `volatile` qualifier:

```rust
struct VolatileCell<T> {
     x: T
}
impl<T> VolatileCell<T> {
    fn get(&self) -> T {
        ptr::read_volatile(&self.x)
    }
    fn set(&mut self, x: T) {
       ptr::write_volatile(&mut self.x, x)
    }
}

```

(In practice it may make more sense to have this use `UnsafeCell` internally and `&self` for `set`.)

---

<div class="post-metadata">

**Author:** ![Amanieu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/amanieu/32/4095_2.png) [@Amanieu](https://internals.rust-lang.org/u/Amanieu)\
**Post date:** [February 18, 2016, 9:58pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/7 "2016-02-18T21:58:11Z")

</div>

Note that in your example, the compiler is allowed to insert a spurious read of `*x` because `&T` and `&mut T` parameters to a function are marked as `dereferenceable` in LLVM IR, which means they can be safely dereferenced. If you had used `*const T` or `*mut T` instead then the compiler would not have been allowed to add a spurious read.

---

<div class="post-metadata">

**Author:** ![nikomatsakis](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nikomatsakis/32/5410_2.png) [@nikomatsakis](https://internals.rust-lang.org/u/nikomatsakis)\
**Post date:** [February 18, 2016, 9:59pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/8 "2016-02-18T21:59:57Z")

</div>

> [@briansmith](#):
>
> Niko, does that mean that the Rust compiler will never insert a read anywhere where the Rust code doesn't put one? I.e. no speculative loads?

No, that's not what I mean. It means that if you have a **volatile load** , it won't be speculatively loaded in advance. And if you want to ensure no speculative loads etc are done for some memory, you should only be using volatile accesses. As @huon said, _addresses_ are not volatile -- accesses are. Declaring a variable as volatile is really just a way of saying "all accesses to this variable are volatile". I believe LLVM and C have roughly this same model. Here are [LLVM's docs](http://llvm.org/docs/LangRef.html#volatile-memory-accesses), for example. (I'd be interested to see counterexamples though.)

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 10:02pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/9 "2016-02-18T22:02:22Z")

</div>

Let’s back up. Consider this:

```rust
fn foo(_x: &u64) { }

```

In _Rust_, Is the compiler allowed to rewrite it to this?:

```rust
fn foo(x: &u64) {
   let _ = *x;
}

```

---

<div class="post-metadata">

**Author:** ![Amanieu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/amanieu/32/4095_2.png) [@Amanieu](https://internals.rust-lang.org/u/Amanieu)\
**Post date:** [February 18, 2016, 10:10pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/10 "2016-02-18T22:10:24Z")

</div>

> [@briansmith](#):
>
> Let's back up. Consider this:
> 
> ```rust
> fn foo(_x: &u64) { }
> 
> ```
> 
> In Rust, Is the compiler allowed to rewrite it to this?:
> 
> ```rust
> fn foo(x: &u64) {
> let _ = *x;
> }
> 
> ```

Yes it is, _because_ you are using a `&u64`, which guarantees that it points to a valid instance of `u64`. Now, this isn't actually specified in the documentation, but Rust (at least the way it is currently implemented) will also assume that it points to normal memory where reading the value multiple times will not result in any side-effects and will always return the same value.

Raw pointers do not make any of these assumptions, so Rust would _not_ be allowed to rewrite this:

```rust
fn foo(_x: *const u64) { }

```

into this:

```rust
fn foo(x: *const u64) {
   let _ = unsafe { *x };
}

```

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 10:15pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/11 "2016-02-18T22:15:04Z")

</div>

> [@nikomatsakis](#):
>
> (I'd be interested to see counterexamples though.

> **[Compiler Explorer](https://gcc.godbolt.org/#%7B%22version%22%3A3%2C%22filterAsm%22%3A%7B%22labels%22%3Atrue%2C%22directives%22%3Atrue%2C%22commentOnly%22%3Atrue%7D%2C%22compilers%22%3A%5B%7B%22sourcez%22%3A%22JYOwLgBAtghqAUBKCBvAUBTEBuB7ANjGMPgKYSiQi5gD6ATqTPvgJ616HFkDcGWnIiXKUIAKgAOEALwQAZNTqNmbDgSG9%2BmeHmAATRJL5YIjMAFd6ICAAY%2BAXzRAAA%3D%3D%22%2C%22compiler%22%3A%22clang371%22%2C%22options%22%3A%22-O3%22%7D%5D%7D)**
>
> Compiler Explorer is an interactive online compiler which shows the assembly output of compiled C++, Rust, Go (and many more) code.

```C
int main() {
    volatile int not_really_volatile;
    volatile int *p = &not_really_volatile;
    (void)*p;
    return 0;
}

```

This gets compiled to:

```rust
main: # @main
        xorl %eax, %eax
        retq

```

Even though `not_really_volatile` is declared volatile, the C compiler is allowed to use its intrinsic knowledge of the fact that it allocated it in non-volatile storage to optimize away the load.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 10:27pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/12 "2016-02-18T22:27:26Z")

</div>

[quote="Amanieu, post:10, topic:3188"] Yes it is, because you are using a &u64, which guarantees that it points to a valid instance of u64. Now, this isn't actually specified in the documentation, but Rust (at least the way it is currently implemented) will also assume that it points to normal memory where reading the value multiple times will not result in any side-effects and will always return the same value.[/quote]

So, would it be fair to say that a Rust reference is equivalent to a non-NULL pointer-to-non-volatile object?

> [@](#):
>
> Raw pointers do not make any of these assumptions

So, would it be fair to say that a Rust pointer is equivalent to a C pointer to volatile object?

In particular, if this is all true, it means that no function could ever safely use reference types and then later cast them to pointers for the purpose of calling `volatile_load` or `volatile_store`, as the compiler may insert speculative loads ahead of the cast from reference to pointer. This has the pretty amazing consequence, AFAICT, that one cannot create a safe (doesn't require the use of `unsafe`) API that ever uses volatile memory.

If so, that actually sounds pretty OK to me for the time being. However, it would be nice to also have a safe (i.e. doesn't require the use of `unsafe`) API for volatile memory in Rust, because pointers are too unsafe. AFAICT, that would require an extension to the type system, or at least the creation of volatile types analogous to Rust's atomic types.

Also, IMO it is very important that it is clearly documented in the Rust reference that references are non-volatile and pointers are volatile, if this is all true.

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [February 18, 2016, 10:35pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/13 "2016-02-18T22:35:42Z")

</div>

> [@briansmith](#):
>
> ```rust
> int main() {
> volatile int not_really_volatile;
> volatile int *p = &not_really_volatile;
> (void)*p;
> return 0;
> }
> 
> ```

Dereferencing doesn't imply reading ("lvalue-to-rvalue conversion") in C/C++, try to use something like `int unused = *p;` and you'll observe a read operation generated.

Edit: Or maybe not, there's some pedantry involved, I need to check in the standard if conversions to `void` invoke lvalue-to-rvalue conversions.

Update: As it often happens with C++, everything is more interesting - the rules have changed between C++03 and C++11. `*p` invokes lvalue-to-rvalue conversion for volatile glvalue expressions (Clause 5 Expressions [expr]). You can observe the difference by running Clang with different `-std` options on your snippet. I haven't checked various versions of the C standard, they may differ from each other and from C++ as well.

Update 2: But my point is that Clang certainly doesn't "use its intrinsic knowledge of the fact that it allocated it in non-volatile storage to optimize away the load".

---

<div class="post-metadata">

**Author:** ![Amanieu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/amanieu/32/4095_2.png) [@Amanieu](https://internals.rust-lang.org/u/Amanieu)\
**Post date:** [February 18, 2016, 10:47pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/14 "2016-02-18T22:47:46Z")

</div>

> [@petrochenkov](#):
>
> Dereferencing doesn't imply reading ("lvalue-to-rvalue conversion") in C/C++, try to use something like int unused = \*p; and you'll observe a read operation generated.
> 
> Edit: Or maybe not, there's some pedantry involved, I need to check in the standard if conversions to void invoke lvalue-to-rvalue conversions.

You are actually correct, the code above generates this warning:

```rust
warning: expression result unused; assign into a variable to force a volatile load [-Wunused-volatile-lvalue]

```

---

<div class="post-metadata">

**Author:** ![notriddle](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/notriddle/32/14082_2.png) [@notriddle](https://internals.rust-lang.org/u/notriddle)\
**Post date:** [February 18, 2016, 10:56pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/15 "2016-02-18T22:56:00Z")

</div>

> [@briansmith](#):
>
> Is the compiler allowed rewrite easy into easy\_optimized?

Yes.

> [@briansmith](#):
>
> Is it allowed to rewrite tricky into tricky\_extra\_load?

Yes.

---

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [February 18, 2016, 11:01pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/16 "2016-02-18T23:01:38Z")

</div>

@Amanieu

I’ve updated my comment, everything is more interesting.

---

<div class="post-metadata">

**Author:** ![notriddle](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/notriddle/32/14082_2.png) [@notriddle](https://internals.rust-lang.org/u/notriddle)\
**Post date:** [February 18, 2016, 11:01pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/17 "2016-02-18T23:01:52Z")

</div>

> [@briansmith](#):
>
> So, would it be fair to say that a Rust reference is equivalent to a non-NULL pointer-to-non-volatile object?

Yes.

> [@briansmith](#):
>
> So, would it be fair to say that a Rust pointer is equivalent to a C pointer to volatile object?

Not quite. Regular reads and writes are not volatile, so you'll need to use the volatile functions if that's what you want.

> [@briansmith](#):
>
> In particular, if this is all true, it means that no function could ever safely use reference types and then later cast them to pointers for the purpose of calling volatile\_load or volatile\_store, as the compiler may insert speculative loads ahead of the cast from reference to pointer. This has the pretty amazing consequence, AFAICT, that one cannot create a safe (doesn't require the use of unsafe) API that ever uses volatile memory.

How do you figure? I can have a reference to a raw pointer, and the aliasing rules on the reference don't affect the raw pointer's aliasing rules at all. Similarly, I can have a newtype around a raw pointer, have it use volatile access under the hood, and none of that will be affected by the fact that accesses to the newtype are non-volatile.

---

<div class="post-metadata">

**Author:** ![Amanieu](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/amanieu/32/4095_2.png) [@Amanieu](https://internals.rust-lang.org/u/Amanieu)\
**Post date:** [February 18, 2016, 11:03pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/18 "2016-02-18T23:03:51Z")

</div>

> [@briansmith](#):
>
> So, would it be fair to say that a Rust reference is equivalent to a non-NULL pointer-to-non-volatile object?

Yes

> [@briansmith](#):
>
> So, would it be fair to say that a Rust pointer is equivalent to a C pointer to volatile object?

No, a Rust pointer is equivalent to a plain C pointer. A C compiler is not allowed to insert a spurious load if a pointer is never dereferenced (volatile dereferences don't count). This is because, in C, dereferencing a (non-volatile) pointer requires the pointer to point to a valid object in normal memory, otherwise undefined behavior happens. Since the compiler assumes undefined behavior never happens it can assume that the pointer is safely dereferencable. Rust pointers works the same way.

> [@briansmith](#):
>
> In particular, if this is all true, it means that no function could ever safely use reference types and then later cast them to pointers for the purpose of calling volatile\_load or volatile\_store, as the compiler may insert speculative loads ahead of the cast from reference to pointer. This has the pretty amazing consequence, AFAICT, that one cannot create a safe (doesn't require the use of unsafe) API that ever uses volatile memory.
> 
> If so, that actually sounds pretty OK to me for the time being. However, it would be nice to also have a safe (i.e. doesn't require the use of unsafe) API for volatile memory in Rust, because pointers are too unsafe. AFAICT, that would require an extension to the type system, or at least the creation of volatile types analogous to Rust's atomic types.

The only use for volatile is to access memory-mapped I/O, which is always unsafe since you're writing to an arbitrary memory location outside of Rust's control. You can create a safe wrapper around a raw pointer to allow safe access to a specific I/O memory location, but the actual access will still need to use unsafe code.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 18, 2016, 11:39pm UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/19 "2016-02-18T23:39:13Z")

</div>

OK, let me make sure I understand:

Given this declaration of a C function:

```rust
uint64_t volatile *f();

```

Is this the proper Rust FFI declaration for this function?:

```rust
extern {
    fn f() -> *mut u64;
}

```

Can we assume that as long as Rust code never attempts to load or store from the result of `f`, the compiler will never attempt any loads or stores?

Let’s say I want to allocate a buffer that Rust will never load or store from. Is there a way to do that? For example, consider:

```rust
struct Foo {
   buffer: std::cell::UnsafeCell<[u8; 64]>
}
impl Foo {
   fn new() -> Foo {
     Foo {
       buffer: std::cell::UnsafeCell::new([0u8; 64])
     }
   }
}

fn main() {
   let _ = Foo::new();
}

```

It seems like the compiler is allowed to insert spurious loads and stores into the buffer. Is there any way to avoid that? In particular, I would like to pass this buffer to some non-Rust code so that it can use it for whatever it wants, but I want Rust to be responsible for the allocation. Is this possible? In C, we would use `volatile uint8_t buffer[64]`.

---

<div class="post-metadata">

**Author:** ![briansmith](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/briansmith/32/1306_2.png) [@briansmith](https://internals.rust-lang.org/u/briansmith)\
**Post date:** [February 19, 2016, 12:23am UTC](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188/20 "2016-02-19T00:23:44Z")

</div>

Perhaps this is a better way of phrasing the second part: What is the proper FFI declaration for this?:

```rust
struct Foo {
    volatile uint8_t buffer[64];
};

```

```rust
#[repr(C)]
struct Foo {
    buffer: ???
}

```

[Next page](https://internals.rust-lang.org/t/volatile-and-sensitive-memory/3188.md?page=2)
