Skip to content

wasm: rm static mut #142727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

wasm: rm static mut #142727

wants to merge 1 commit into from

Conversation

hkBst
Copy link
Member

@hkBst hkBst commented Jun 19, 2025

More #125035. I'm not sure this is correct, but it compiles.

@rustbot
Copy link
Collaborator

rustbot commented Jun 19, 2025

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 19, 2025

use crate::alloc::{GlobalAlloc, Layout, System};

static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::Dlmalloc::new();
struct SyncDlmalloc(dlmalloc::Dlmalloc);
unsafe impl Sync for SyncDlmalloc {}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is only safe if we're in WASM without threads, is this file already gated for that?

Copy link
Member Author

Choose a reason for hiding this comment

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

The same file also contains a Lock impl for the case there are threads, which is used to guard the Allocator impl against unsynchronized accesses.

@tgross35
Copy link
Contributor

Is this motivated by the oldish issue to deprecate static mut in favor of a wrapper type? If so, that was eventually decided against. So I don't think this is needed because the Dlmalloc isn't ever mutably accessed via a & reference, which is what the UnsafeCell would enable. And as @juntyr pointed out, Dlmalloc does not have Sync in its API guarantees (it could choose to use a Cell internally) and there isn't anything that SyncDlmalloc adds to make it such.

Fyi you should be able to sanity check the behavior with ./x miri library/ --target wasm32-unknown-unknown - not that that would catch incorrect impls, but the existing version of this should pass fine.

Cc @RalfJung since you could explain some of this much better than me.

@RalfJung
Copy link
Member

RalfJung commented Jun 20, 2025

It's up to t-libs what they find more clear here. The alternative would be to leave the static mut and then silence the lint e.g. by writing (&mut * &raw mut DLMALLOC). Either way, much more important than static mut vs static is having proper safety comments.

@hkBst
Copy link
Member Author

hkBst commented Jun 20, 2025

Is this motivated by the oldish issue to deprecate static mut in favor of a wrapper type?

It's motivated by #125035, but I admit that I was under the impression that we should get rid of all uses of "static mut", but I see #53639 was closed as not planned.

So should we just try and remove most uses of "static mut" that can easily be replaced by something other than SyncUnsafeCell, or something along those lines?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants