Closed Bug 1877380 Opened 2 years ago Closed 2 years ago

[rustc 1.77] error: shared reference of mutable static is discouraged

Categories

(Core :: Gecko Profiler, defect)

defect

Tracking

()

RESOLVED FIXED
124 Branch
Tracking Status
firefox124 --- fixed

People

(Reporter: glandium, Assigned: canova)

References

Details

Attachments

(1 file)

[task 2024-01-30T06:21:02.603Z] 06:21:02     INFO -  error: shared reference of mutable static is discouraged
[task 2024-01-30T06:21:02.604Z] 06:21:02     INFO -    --> tools/profiler/rust-api/src/profiler_state.rs:76:46
[task 2024-01-30T06:21:02.604Z] 06:21:02     INFO -     |
[task 2024-01-30T06:21:02.605Z] 06:21:02     INFO -  76 |     unsafe { mem::transmute::<_, &AtomicU32>(&detail::RacyFeatures_sActiveAndFeatures) }
[task 2024-01-30T06:21:02.605Z] 06:21:02     INFO -     |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ shared reference of mutable static
[task 2024-01-30T06:21:02.605Z] 06:21:02     INFO -     |
[task 2024-01-30T06:21:02.606Z] 06:21:02     INFO -     = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
[task 2024-01-30T06:21:02.606Z] 06:21:02     INFO -     = note: reference of mutable static is a hard error from 2024 edition
[task 2024-01-30T06:21:02.607Z] 06:21:02     INFO -     = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
[task 2024-01-30T06:21:02.607Z] 06:21:02     INFO -     = note: `-D static-mut-ref` implied by `-D warnings`
[task 2024-01-30T06:21:02.607Z] 06:21:02     INFO -     = help: to override `-D warnings` add `#[allow(static_mut_ref)]`
[task 2024-01-30T06:21:02.607Z] 06:21:02     INFO -  help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
[task 2024-01-30T06:21:02.608Z] 06:21:02     INFO -     |
[task 2024-01-30T06:21:02.608Z] 06:21:02     INFO -  76 |     unsafe { mem::transmute::<_, &AtomicU32>(addr_of!(detail::RacyFeatures_sActiveAndFeatures)) }
[task 2024-01-30T06:21:02.608Z] 06:21:02     INFO -     |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[task 2024-01-30T06:21:02.608Z] 06:21:02     INFO -  [swgl 0.1.0] exit status: 0
[task 2024-01-30T06:21:02.608Z] 06:21:02     INFO -  error: could not compile `gecko-profiler` (lib) due to 1 previous error

(This is when building with current rust nightly (1.77))

Summary: error: shared reference of mutable static is discouraged → [rustc 1.77] error: shared reference of mutable static is discouraged

Thanks for filing! Looking into it now.

This variable is an atomic variable here:
https://searchfox.org/mozilla-central/rev/2a867dd1ab015c3ef24b774a57709fb3b3dc4961/tools/profiler/core/platform.cpp#1631

We are getting this C++ atomic variable here directly instead of using FFIs
because this is much faster and this function has to be as fast as possible
(because it will be used before adding each marker to make sure the profiler is
running.). It's used by is_active and can_accept_markers functions above
this file.

It looks like Rust 1.77 starts to give some warnings/errors for these cases
where we have shared references. This should be safe for us, because we already
know that this function can be racy. So this is something we considered and
decided that it's better to be faster.

Assignee: nobody → canaltinova
Status: NEW → ASSIGNED
Attachment #9377122 - Attachment description: Bug 1877380 - Use std::ptr::addr_of! instead of a reference for RacyFeatures_sActiveAndFeatures r?aabh!,#profiler → Bug 1877380 - Use AtomicU32::from_ptr implementation instead of a reference for RacyFeatures_sActiveAndFeatures r?aabh!,#profiler
Pushed by canaltinova@gmail.com: https://hg.mozilla.org/integration/autoland/rev/b5929265064e Use AtomicU32::from_ptr implementation instead of a reference for RacyFeatures_sActiveAndFeatures r=aabh,profiler-reviewers,glandium
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 124 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: