Skip to content

Lookup exported statics when encountering an unsupported imported static - #5230

Merged
RalfJung merged 1 commit into
rust-lang:masterfrom
hkalbasi:push-ruquxrvxyplk
Aug 5, 2026
Merged

Lookup exported statics when encountering an unsupported imported static#5230
RalfJung merged 1 commit into
rust-lang:masterfrom
hkalbasi:push-ruquxrvxyplk

Conversation

@hkalbasi

@hkalbasi hkalbasi commented Jul 27, 2026

Copy link
Copy Markdown
Member

Fix #5186

The implemented behavior is fairly liberal and doesn't emit immediate UB as long as allocation size and alignment matches. This enables patterns like this:

#[no_mangle]
static INTERIOR_MUT: SyncUnsafeCell<i32> = SyncUnsafeCell::new(42);

fn main() {
    unsafe {
        extern "C" {
            static mut INTERIOR_MUT: i32;
        }
        (&raw mut INTERIOR_MUT).write(7);
    }
}

I don't know if there is a decided semantic for these statics, tell me if it needs to be changed.

@rustbot rustbot added the S-waiting-on-review Status: Waiting for a review to complete label Jul 27, 2026
@hkalbasi
hkalbasi force-pushed the push-ruquxrvxyplk branch from 4f0be6f to a7c9d47 Compare July 27, 2026 12:28
@hkalbasi

Copy link
Copy Markdown
Member Author

Also, I treated extern "C" and extern "Rust" as equal, which might be wrong.

@hkalbasi
hkalbasi force-pushed the push-ruquxrvxyplk branch from a7c9d47 to beb66f6 Compare July 27, 2026 13:06
@RalfJung

RalfJung commented Aug 1, 2026

Copy link
Copy Markdown
Member

Also, I treated extern "C" and extern "Rust" as equal, which might be wrong.

Hm, good question... @bjorn3 does the "ABI" make any difference for statics?

@RalfJung RalfJung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes a lot of sense, thank you. :)

@rustbot author

View changes since this review

Comment thread src/shims/foreign_items.rs Outdated
Comment thread src/shims/foreign_items.rs Outdated
Comment thread src/machine.rs
)
} else {
throw_unsup_format!("extern static `{link_name}` is not supported by Miri")
// Look for a Rust static with this symbol name in the crate graph.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For exported functions, we are very careful to check that the shims we support do not overlap with an exported function. We should do the same here: if a static is both in machine.extern_statics and exported, we should error.

@hkalbasi hkalbasi Aug 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added this error, I also added check for conflicts between function shims and bad static definitions, and static shims and bad function definitions. Two more cases are left (function shim used by static declaration and static shim used by function declaration), but they don't fit nicely in the miri code, and given that miri can't detect all possible link problems, and that I'm not a fan of this diagnostic, and that it wasn't diagnosed (the function case) before, I tend to not add them, at least in this PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two more cases are left (function shim used by static declaration and static shim used by function declaration),

What do you mean by that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

https://godbolt.org/z/r1GbWfz9M

This is accepted currently in miri master, but you may want to reject it (since environ conflicts with miri static shim). This PR improves the situation (by rejecting another case when a fn shim is shadowed by a static), but still allows this one. It is somehow irrelevant to this PR (it has a sister case for static shims which is more relevant) I just noticed it and wanted to share it with you.

@RalfJung RalfJung Aug 2, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah that would be better to reject. It seems relevant to this PR to me -- before this PR, Miri entirely ignored the fact that one can also have statics in that global namespace. Now that we're teaching Miri about this we should do it properly: check_shim_symbol_clash should use lookup_exported_symbol (not the new wrappers that filter for functions / statics), and the logic for statics should invoke check_shim_symbol_clash. No need to have separate codepaths. Then we should get the intended behavior fairly easily?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I already use lookup_exported_symbol (that's the case that I added support) and the other one indeed needs a separate code path (or a refactor to put all shims (static and fn) in a single symbol hashmap so that a shim get selected in this case).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh I see... one of those directions is fairly easy since we have a list of Miri-shimmed statics we can check against, but the other direction is hard since we don't have a list of all Miri-shimmed functions.

For now, let's open an issue and leave a FIXME referencing the issue.

Comment thread src/machine.rs Outdated
Comment thread src/machine.rs Outdated
Comment thread tests/pass/extern_static.rs
Comment thread tests/pass/extern_static/type_confusion.rs Outdated
Comment thread tests/pass/extern_static.rs
Comment thread tests/pass/extern_static.rs Outdated
Comment thread tests/pass/extern_static.rs
@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Aug 1, 2026
@hkalbasi
hkalbasi force-pushed the push-ruquxrvxyplk branch 2 times, most recently from c746e37 to 83b2acd Compare August 2, 2026 11:23
@rustbot

This comment has been minimized.

@RalfJung RalfJung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread src/machine.rs Outdated
Comment thread src/machine.rs Outdated
Comment thread src/machine.rs Outdated
extern "C" {
static mut S: i32;
}
// The declaration is fine, the act of write is UB.

@RalfJung RalfJung Aug 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is actually a good question, I do not know if the declaration is fine. Maybe LLVM can assume that this memory is implicitly writable. I think we should error on any mutability mismatch.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I made it UB. I would be surprised if llvm actually does unprovoked writes, specially since that allocations are mutable as a whole. I don't want llvm to kill my &'static i32 obtained from a (i32, Cell) static, since it thinks the whole allocation is mutable.

But making things UB is easier than doing research. If it bites someone, they can check llvm and relax it if there is no problem.

Comment thread tests/fail/extern_static/write_immutable.rs
Comment thread tests/pass/extern_static.rs Outdated
Comment thread tests/pass/extern_static.rs
@hkalbasi
hkalbasi force-pushed the push-ruquxrvxyplk branch from e77d5c2 to 3750e9b Compare August 4, 2026 13:21
@rustbot

rustbot commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment thread tests/fail/extern_static/mut_mismatch3.rs Outdated

@RalfJung RalfJung left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a few minor nits. :)

(Also remember to rustbot ready when you want another round of review.)

View changes since this review

Comment thread src/machine.rs Outdated
Comment thread src/machine.rs Outdated
Comment thread src/shims/sig.rs
Comment thread src/shims/sig.rs Outdated
@hkalbasi

hkalbasi commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Aug 5, 2026
@RalfJung

RalfJung commented Aug 5, 2026

Copy link
Copy Markdown
Member

This looks great, thanks! Please squash the commits. You can squash manually if there are multiple independent commits you want to preserve, or use ./miri squash (make sure to pick a suitable commit message). Then write @rustbot ready after you force-pushed the squashed PR.

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: Waiting for the PR author to address review comments and removed S-waiting-on-review Status: Waiting for a review to complete labels Aug 5, 2026
@hkalbasi
hkalbasi force-pushed the push-ruquxrvxyplk branch from ebf676e to 2547ff2 Compare August 5, 2026 20:51
@hkalbasi

hkalbasi commented Aug 5, 2026

Copy link
Copy Markdown
Member Author

@rustbot ready

Btw was the temporary commits helpful in reviewing? I'm using jj and it's easier for me to edit my last commit than to create temporary commits, but IIRC there was a rustbot message inviting people to not do rebases in the middle of reviews. I didn't see that message in this and other PRs. Is this restriction lifted? Or maybe it was another project?

@rustbot rustbot added S-waiting-on-review Status: Waiting for a review to complete and removed S-waiting-on-author Status: Waiting for the PR author to address review comments labels Aug 5, 2026
@RalfJung
RalfJung enabled auto-merge August 5, 2026 21:18
@RalfJung

RalfJung commented Aug 5, 2026

Copy link
Copy Markdown
Member

We used to have such a message, but these days we have "view changes since review" links for reviewing which is good enough for small PRs like this. So it's fine to force-push, though the diff is still easier to deal with if you only "amend", not "amend + rebase" (or whatever the jj terms for that are).

@RalfJung
RalfJung added this pull request to the merge queue Aug 5, 2026
Merged via the queue into rust-lang:master with commit 9eef8ed Aug 5, 2026
14 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Waiting for a review to complete label Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lookup exported statics when encountering an unsupported imported static

3 participants