From 1cab0146619342ded41d418e18beaaa6509d1594 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 26 May 2024 09:37:46 +0200 Subject: [PATCH 1/2] more explicitly explain the UB around immutable extern statics --- src/behavior-considered-undefined.md | 2 +- src/items/external-blocks.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/behavior-considered-undefined.md b/src/behavior-considered-undefined.md index 756b86db0..3e4c8a04d 100644 --- a/src/behavior-considered-undefined.md +++ b/src/behavior-considered-undefined.md @@ -50,7 +50,7 @@ Please read the [Rustonomicon] before writing unsafe code. types are passed in a (nested) field of a compound type, but not behind pointer indirections. * Mutating immutable bytes. All bytes inside a [`const`] item are immutable. - The bytes owned by an immutable binding are immutable, unless those bytes are part of an [`UnsafeCell`]. + The bytes owned by an immutable binding or immutable `static` are immutable, unless those bytes are part of an [`UnsafeCell`]. Moreover, the bytes [pointed to] by a shared reference, including transitively through other references (both shared and mutable) and `Box`es, are immutable; transitivity includes those references stored in fields of compound types. diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index 5b911a36c..95d16d6ce 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -56,6 +56,8 @@ of initializing the static. Extern statics can be either immutable or mutable just like [statics] outside of external blocks. An immutable static *must* be initialized before any Rust code is executed. It is not enough for the static to be initialized before Rust code reads from it. +Once Rust code runs, mutating an immutable static (from inside or outside Rust) is UB, +except if the mutation happens inside an `UnsafeCell`. ## ABI From 7c7feea9f82a24a3101476da73d7ecbb0a3106e5 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Wed, 3 Jul 2024 19:57:13 +0000 Subject: [PATCH 2/2] Add clarification from nikomatsakis Niko requested this clarification about whether "happens inside an `UnsafeCell` meant "happens to bytes inside of an `UnsafeCell`", so let's go ahead and just add that language to the text. --- src/items/external-blocks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index 95d16d6ce..8e1aa2fcd 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -57,7 +57,7 @@ Extern statics can be either immutable or mutable just like [statics] outside of An immutable static *must* be initialized before any Rust code is executed. It is not enough for the static to be initialized before Rust code reads from it. Once Rust code runs, mutating an immutable static (from inside or outside Rust) is UB, -except if the mutation happens inside an `UnsafeCell`. +except if the mutation happens to bytes inside of an `UnsafeCell`. ## ABI