# Is it possible to read uninitialized memory without invoking UB?

**URL:** <https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092>\
**Category:** help\
**Created:** [August 2, 2021, 9:42pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092 "2021-08-02T21:42:15Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Alex\_Addy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alex_addy/32/17300_2.png) [@Alex\_Addy](https://users.rust-lang.org/u/Alex_Addy)\
**Post date:** [August 18, 2021, 12:53pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/21 "2021-08-18T12:53:10Z")

</div>

Thanks for everyone's responses. There is a lot of great information here, but it seems like reading uninitialized memory will always be UB. The closest I can get is to read effectively uninitialized memory returned from system calls.

I was sort of hoping that something like [LLVM's freeze instruction](https://llvm.org/docs/LangRef.html#freeze-instruction) could be exposed, but I couldn't find any work to make that happen.

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)\
**Post date:** [August 18, 2021, 2:24pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/22 "2021-08-18T14:24:26Z")

</div>

There have been some discussions about `freeze`, though no real proposals yet for how to integrate it into Rust:

- [API to acquire arbitrarily initialized buffer? - Rust Internals](https://internals.rust-lang.org/t/api-to-acquire-arbitrarily-initialized-buffer/13213)
- [WHAT-IF: Reading uninit RAM was not UB? - language design - Rust Internals](https://internals.rust-lang.org/t/what-if-reading-uninit-ram-was-not-ub/13231)

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [August 18, 2021, 3:30pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/23 "2021-08-18T15:30:57Z")

</div>

> [@Alex\_Addy](#):
>
> `assert!(page.assume_init().iter().sum(), 0); // definitely UB`

There are plenty of ways that page can be full of random byte values that sum to zero.

---

<div class="post-metadata">

**Author:** ![trentj](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/trentj/32/5690_2.png) [@trentj](https://users.rust-lang.org/u/trentj)\
**Post date:** [August 18, 2021, 4:44pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/24 "2021-08-18T16:44:08Z")

</div>

Calling `assume_init` on data that _has not been initialized_ is the source of UB here. So if you assume that that line will do something like "follow this pointer, add a bunch of things and compare their sum against zero" you are already mistaken. `page.assume_init()` is, at least conceptually, instant UB by itself.

Naturally, the compiler will almost certainly emit some code here that does do something like that. But even if it does, "panicking" is also not UB, so the result of the comparison still has no bearing on whether the code as a whole is UB or not.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [August 18, 2021, 6:06pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/25 "2021-08-18T18:06:27Z")

</div>

> [@Alex\_Addy](#):
>
> I was sort of hoping that something like [LLVM's freeze instruction](https://llvm.org/docs/LangRef.html#freeze-instruction) could be exposed, but I couldn't find any work to make that happen.

Note that `freeze` still doesn't solve your problem. It just replaces your UB with an arbitrary value. So if the page is not initialized, you're still just testing `if arbitrary_compiler_chosen_value() == 0`, which doesn't give you any usable information -- it could still look like it's zero-initialized even though it's `uninit`.

---

<div class="post-metadata">

**Author:** ![Alex\_Addy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alex_addy/32/17300_2.png) [@Alex\_Addy](https://users.rust-lang.org/u/Alex_Addy)\
**Post date:** [August 18, 2021, 7:20pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/26 "2021-08-18T19:20:32Z")

</div>

As long as I can read uninitialized memory everything else is a detail. For example, I could be testing a memory allocator that is supposed to reuse memory pages after zeroing them; so I would allocate, check page, modify, free a bunch of times. But I can't do that if even looking at the page before writing is UB. Freeze would solve my problem because it allows me to write this code without modifying the allocated page.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [August 18, 2021, 7:37pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/27 "2021-08-18T19:37:34Z")

</div>

> [@Alex\_Addy](#):
>
> For example, I could be testing a memory allocator that is supposed to reuse memory pages after zeroing them; so I would allocate, check page, modify, free a bunch of times.

That only works so long as it doesn't return uninitialized pages. If there's a bug that returns them uninit, then the code you write using `freeze` could say they they have the expected value because the compiler optimized out the check.

`freeze` just removes the UB; it doesn't let you actually "read" it.

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/newpavlov/32/6207_2.png) [@newpavlov](https://users.rust-lang.org/u/newpavlov)\
**Post date:** [August 18, 2021, 7:54pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/28 "2021-08-18T19:54:47Z")

</div>

BTW what about the following code? IIUC it does count as UB and compiler is not allowed to replace reads from the resulting `page` with something arbitrary

```rust
extern "C" {
    // does nothing with data behind the pointer,
    // but works as an "optimization barrier"
    fn touch_pointer(p: *mut [u8; 4096]);
}

let mut page = Box::<[u8; 4096]>::new_uninit();
let page = unsafe {
    touch_pointer(page.as_mut_ptr());
    page.assume_init()
};
// we now can legally read data from `page` since compiler
// can not make any assumptions about `touch_pointer`

```

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [August 18, 2021, 7:58pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/29 "2021-08-18T19:58:17Z")

</div>

That code has UB, but UB allows anything to happen, and "does what I wanted" is included in "anything". You should just be aware that changes to the behavior are not considered breaking even if they break your code with UB.

---

<div class="post-metadata">

**Author:** ![Alex\_Addy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alex_addy/32/17300_2.png) [@Alex\_Addy](https://users.rust-lang.org/u/Alex_Addy)\
**Post date:** [August 18, 2021, 7:59pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/30 "2021-08-18T19:59:16Z")

</div>

Why would it not allow me to read it? `freeze` says that the page will have an **arbitrary** but **unknown** pattern, which would mean that the compiler **can't** remove checks based upon knowledge of the value.

---

<div class="post-metadata">

**Author:** ![Alex\_Addy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alex_addy/32/17300_2.png) [@Alex\_Addy](https://users.rust-lang.org/u/Alex_Addy)\
**Post date:** [August 18, 2021, 8:01pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/31 "2021-08-18T20:01:51Z")

</div>

I really want to avoid solutions that rely on the compiler being too dumb, cause eventually it will figure it out. Sort of the opposite of the "sufficiently smart compiler".

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/newpavlov/32/6207_2.png) [@newpavlov](https://users.rust-lang.org/u/newpavlov)\
**Post date:** [August 18, 2021, 8:04pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/32 "2021-08-18T20:04:24Z")

</div>

Assuming `touch_pointer` comes from a shared library, compiler in principle is not allowed to make any assumptions about it and peeking into its implementation. But yes, it may break with static linking and LTO.

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [August 18, 2021, 8:09pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/33 "2021-08-18T20:09:51Z")

</div>

I think the point that @scottmcm is trying to make is that the compiler may optimize the code below to always print hello world without actually reading the underlying value.

```rust
let mut uninit = MaybeUninit::<u8>::uninit();

let ptr: *mut u8 = uninit.as_mut_ptr();
freeze(ptr);

if *ptr == 0 {
    println!("hello world!");
}

```

This is because as described in the [LLVM reference](https://llvm.org/docs/LangRef.html#freeze-instruction), the resulting value must be _fixed_, but can be arbitrary. The value 0 for the memory location is valid under those constraints.

---

<div class="post-metadata">

**Author:** ![Alex\_Addy](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alex_addy/32/17300_2.png) [@Alex\_Addy](https://users.rust-lang.org/u/Alex_Addy)\
**Post date:** [August 18, 2021, 8:31pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/34 "2021-08-18T20:31:11Z")

</div>

According to my reading of the reference, it should be a _fixed_ but _unknown_ value. The "arbitrary" part refers to fact that it could be anything, so the compiler can't optimize it by assuming that it is some _particular_ value. The examples further support this, `%z = add i32 %x, %x` being even but `%cmp = icmp eq i32 %x, %x2` being true or false with the compiler not being able to tell.

Of course, I am a novice and this is a really difficult subject so I would appreciate any other references supporting or refuting this stance.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [August 18, 2021, 8:45pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/35 "2021-08-18T20:45:39Z")

</div>

> [@Alex\_Addy](#):
>
> I really want to avoid solutions that rely on the compiler being too dumb, cause eventually it will figure it out.

The problem here is that if the compiler is ever smart enough to know that the memory is `undef`, then there's nothing you can do to detect that. At best you can use `freeze` to make your checks useless instead of UB.

All the approaches of things like the "optimization barriers" or "would mean that the compiler can't remove checks" are about forcing the compiler to be insufficiently smart to be able to actually know anything useful -- which makes those things also "work" fine without the freeze.

> [@Alex\_Addy](#):
>
> The "arbitrary" part refers to fact that it could be anything, so the compiler can't optimize it by assuming that it is some _particular_ value.

Make sure to go through [https://llvm.org/docs/LangRef.html#undefined-values](https://llvm.org/docs/LangRef.html#undefined-values) really carefully. In particular, note the difference between `freeze(under)` and just `undef`.

If you have `%A = undef` and `%B = freeze(undef)`, `icmp %A, %A` is `undef` but `icmp %B, %B` is `i1 1`. It's that _repeated_ use where the `freeze` is relevant. If the _only_ use of `%B` is in `icmp %B, 0`, then it can be optimized to just `freeze(undef)` -- it'll have a consistent answer, but the optimizer can pick whichever answer it would prefer.

(At least, that's my understanding. I'm no Ralf, though 🙃)

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [August 18, 2021, 8:48pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/36 "2021-08-18T20:48:24Z")

</div>

I would generally consider the word arbitrary to refer to the guarantees that are given to us, the authors of the code, and not to be about what the compiler itself is allowed to know.

I'm no expert in the LLVM reference, but this is how we use the word "arbitrary" in pure mathematics, and I assume it is used in the same way here.

---

<div class="post-metadata">

**Author:** ![alice](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/alice/32/9777_2.png) [@alice](https://users.rust-lang.org/u/alice)\
**Post date:** [August 18, 2021, 8:56pm UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/37 "2021-08-18T20:56:50Z")

</div>

In general, I don't think the LLVM reference ever says anything like "the compiler is not allowed to know X", and I think it would be bad design for a language reference to say something like that. There are always much better ways of writing this in the reference, e.g. "it is guaranteed that the frozen value is the value actually stored in RAM at that location" or something like that.

---

<div class="post-metadata">

**Author:** ![riking](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/riking/32/2351_2.png) [@riking](https://users.rust-lang.org/u/riking)\
**Post date:** [August 19, 2021, 1:20am UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/38 "2021-08-19T01:20:12Z")

</div>

I think that this is actually an interesting case that deserves a little more examination.

As written, if we take your comment on the `touch_pointer` function to be correct, the code does have undefined behavior (as stated above by others).

What would happen if I replaced the C of `touch_pointer` with a function that did, in fact, initialize the 4096 bytes of memory pointed to? The memory would then be initialized, and the program would execute without UB.

You can't decide whether that snippet has UB or not without checking the actual definition of `touch_pointer`. This kind of **whole program analysis** is one of the major sticking points of C code that Rust set out to prevent. (Which is why you need `unsafe` to bring in this ambiguity.)

> [@newpavlov](#):
>
> it may break with static linking and LTO.

Another way of saying the same thing. The compiler can't decide whether that snippet has UB or not without checking the actual definition of `touch_pointer`. The "UB bomb" will "explode" when the veil of ignorance is stripped away.

---

<div class="post-metadata">

**Author:** ![newpavlov](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/newpavlov/32/6207_2.png) [@newpavlov](https://users.rust-lang.org/u/newpavlov)\
**Post date:** [August 21, 2021, 12:32am UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/39 "2021-08-21T00:32:47Z")

</div>

> [@riking](#):
>
> As written, if we take your comment on the `touch_pointer` function to be correct, the code does have undefined behavior (as stated above by others).

In my opinion the snippet does not contain UB and it should not change in the presence of static linking and LTO. But I agree that it _may_.

UB is a matter of language specification and since Rust does not have a proper specification the answer can go both ways. In my opinion, it's reasonable to treat `extern` functions as "unoptimizable" in the spec, no matter how it gets linked and whether LTO is enabled. Same should be applied to pages which are returned by `memmap` and other similar syscalls. I don't think that they should be included into language specification (as `memcpy`). If compiler does not know anything about their semantics, then it has no choice but to treat them as any other unoptimazible `extern fn`.

Note that treating "raw" syscalls in such fashion does not prevent us from adding to the spec their aliases, which will have semantic meaning for compiler and thus could provide additional optimization opportunities, e.g. marking page memory returned by a hypothetical `memmap` alias with `undef` and marking it "pure", so its call may be eliminated if a returned page is not used anywhere.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cad97/32/5076_2.png) [@CAD97](https://users.rust-lang.org/u/CAD97)\
**Post date:** [August 21, 2021, 3:26am UTC](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092/40 "2021-08-21T03:26:40Z")

</div>

> [@newpavlov](#):
>
> it's reasonable to treat `extern` functions as "unoptimizable" in the spec,

What does that mean in the spec, though?

> [@newpavlov](#):
>
> then it has no choice but to treat them as any other unoptimazible `extern fn`

While this may be true, remember that the language model is operationable (defined by operations over state) and the as-if rule, not by some specification of what optimizations are allowed to do.

So sure, calling an `extern fn` that may potentially initialize the memory must behave properly in the case that the memory is initialized. But remember: MADV\_FREE means that uninitialized memory can be non-deterministic _in practice_, not just poison in the abstract machine.

The manifestation of UB in this case would be because when you use the value, the compiler assumes that it's a normal value. But if it's uninitialized in a MADV\_FREE page, two spurious reads may return different values the compiler assumes are equivalent, and you have UB manifesting because of it.

Is this manifestation rare and contrived? Kind of. Will it work fine most of the time? Sure. But importantly, it's _still UB_ and it will _still misbehave_, and an optimization barrier doesn't prevent the UB from manifesting.

The _only_ way to prevent miscompilations/misbehavior is to eliminate the UB of using an undefined value by `freeze`ing the value to a non-deterministic but consistent non-undef memory value.

[Previous page](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092.md?page=1)

[Next page](https://users.rust-lang.org/t/is-it-possible-to-read-uninitialized-memory-without-invoking-ub/63092.md?page=3)
