Skip to content

document that extern statics may be bigger than the declared size - #2334

Open
RalfJung wants to merge 1 commit into
rust-lang:masterfrom
RalfJung:extern-static-oversize
Open

document that extern statics may be bigger than the declared size#2334
RalfJung wants to merge 1 commit into
rust-lang:masterfrom
RalfJung:extern-static-oversize

Conversation

@RalfJung

@RalfJung RalfJung commented Aug 22, 2026

Copy link
Copy Markdown
Member

I am fairly sure we discussed at some point whether it's okay to access memory beyond the declared size of an extern static, and @nikic confirmed that this is fine. But I couldn't find any official place where we document this. So, let's add it to the Reference.

I wondered if we also need the converse on the UB page: it is UB to even start the program if an extern static ends up being backed by memory that is smaller or less-aligned than the declared type. What do you think?

Cc @rust-lang/opsem

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Aug 22, 2026

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 by creating a raw pointer to the extern static and then doing appropriate pointer arithmetic.

@Jules-Bertholet Jules-Bertholet Aug 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps be more explicit about the need to use &raw (and avoid an intermediate reference)?

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants