]> git.lizzy.rs Git - rust.git/commit - src/tools/miri
Auto merge of #99182 - RalfJung:mitigate-uninit, r=scottmcm
authorbors <bors@rust-lang.org>
Thu, 28 Jul 2022 01:11:10 +0000 (01:11 +0000)
committerbors <bors@rust-lang.org>
Thu, 28 Jul 2022 01:11:10 +0000 (01:11 +0000)
commit48316dfea1914d25189fa441e7310449ed76a446
treee2f3111e4059b72bb6dc6a087f083916affc6bad
parentada80a13b97a01176a1660453060e296a72cf1bb
parent7b4149474b2fdf8b30cb99fdd17303b94746ebce
Auto merge of #99182 - RalfJung:mitigate-uninit, r=scottmcm

mem::uninitialized: mitigate many incorrect uses of this function

Alternative to https://github.com/rust-lang/rust/pull/98966: fill memory with `0x01` rather than leaving it uninit. This is definitely bitewise valid for all `bool` and nonnull types, and also those `Option<&T>` that we started putting `noundef` on. However it is still invalid for `char` and some enums, and on references the `dereferenceable` attribute is still violated, so the generated LLVM IR still has UB -- but in fewer cases, and `dereferenceable` is hopefully less likely to cause problems than clearly incorrect range annotations.

This can make using `mem::uninitialized` a lot slower, but that function has been deprecated for years and we keep telling everyone to move to `MaybeUninit` because it is basically impossible to use `mem::uninitialized` correctly. For the cases where that hasn't helped (and all the old code out there that nobody will ever update), we can at least mitigate the effect of using this API. Note that this is *not* in any way a stable guarantee -- it is still UB to call `mem::uninitialized::<bool>()`, and Miri will call it out as such.

This is somewhat similar to https://github.com/rust-lang/rust/pull/87032, which proposed to make `uninitialized` return a buffer filled with 0x00. However
- That PR also proposed to reduce the situations in which we panic, which I don't think we should do at this time.
- The 0x01 bit pattern means that nonnull requirements are satisfied, which (due to references) is the most common validity invariant.

`@5225225` I hope I am using `cfg(sanitize)` the right way; I was not sure for which ones to test here.
Cc https://github.com/rust-lang/rust/issues/66151
Fixes https://github.com/rust-lang/rust/issues/87675
library/core/src/lib.rs
library/core/src/mem/mod.rs