From 6491ee0fd3c714a0915c8ef097d91e09a37987e5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 22 Aug 2026 13:13:39 +0200 Subject: [PATCH 1/2] document that extern statics may be bigger than the declared size --- src/items/external-blocks.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index 8218b8f1c3..e4fb6118dd 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -74,6 +74,10 @@ Extern statics can be either immutable or mutable just like [statics] outside of r[items.extern.static.read-only] 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 to bytes inside of an `UnsafeCell`. +r[items.extern.static.size] +The actual memory that the extern static resolves to must have *at least* the size and alignment of the type that it was declared with in the extern block. +If the actual memory is bigger, then it is permitted to access that extra memory. + r[items.extern.abi] ## ABI From ec8f4ef9c9d523203a85af8546b57fdf11601426 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 24 Aug 2026 16:40:40 +0200 Subject: [PATCH 2/2] add UB: invalid extern static --- src/behavior-considered-undefined.md | 3 +++ src/items/external-blocks.md | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/behavior-considered-undefined.md b/src/behavior-considered-undefined.md index 935f0dd9fa..04a65fe520 100644 --- a/src/behavior-considered-undefined.md +++ b/src/behavior-considered-undefined.md @@ -61,6 +61,9 @@ r[undefined.invalid] r[undefined.asm] * Incorrect use of inline assembly. For more details, refer to the [rules] to follow when writing code that uses inline assembly. +r[undefined.extern-static] +* Declaring an `extern static` with some size/alignment/mutability, when the actual symbol this resolves to is smaller / less aligned / less mutable. + r[undefined.runtime] * Violating assumptions of the Rust runtime. Most assumptions of the Rust runtime are currently not explicitly documented. * For assumptions specifically related to unwinding, see the [panic documentation][unwinding-ffi]. diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index e4fb6118dd..86a9f907c2 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -75,7 +75,7 @@ r[items.extern.static.read-only] 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 to bytes inside of an `UnsafeCell`. r[items.extern.static.size] -The actual memory that the extern static resolves to must have *at least* the size and alignment of the type that it was declared with in the extern block. +The actual memory that the extern static resolves to [must have][extern-static-ub] *at least* the size and alignment of the type that it was declared with in the extern block. If the actual memory is bigger, then it is permitted to access that extra memory. r[items.extern.abi] @@ -470,6 +470,7 @@ Attributes on extern function parameters follow the same rules and restrictions [`verbatim` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-verbatim [`whole-archive` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-whole-archive [attributes]: ../attributes.md +[extern-static-ub]: ../behavior-considered-undefined.md#r-undefined.extern-static [functions]: functions.md [regular function parameters]: functions.md#attributes-on-function-parameters [statics]: static-items.md