cross icon indicating copy to clipboard operation
cross copied to clipboard

Cross uses shared references to static mutables (UB / Rust 2024 Edition)

Open sylv256 opened this issue 1 year ago • 9 comments

Checklist

Describe your issue

Cross will stop working in Rust 2024 because it uses an outdated feature. Please migrate from mutable statics to support the latest Rust edition.

What target(s) are you cross-compiling for?

x86_64-pc-windows-gnu

Which operating system is the host (e.g computer cross is on) running?

  • [X] macOS
  • [ ] Windows
  • [ ] Linux / BSD
  • [ ] other OS (specify in description)

What architecture is the host?

  • [ ] x86_64 / AMD64
  • [ ] arm32
  • [X] arm64 (including Mac M1)

What container engine is cross using?

  • [X] docker
  • [ ] podman
  • [ ] other container engine (specify in description)

cross version

cross 0.2.5 (ac4c11c 2024-09-24)

Example

Warnings like this are repeated multiple times while compiling.

warning: creating a shared reference to mutable static is discouraged
  --> src/temp.rs:91:8
   |
91 |     Ok(DIRS.last().expect("should not be empty").path())
   |        ^^^^^^^^^^^ shared reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

Additional information / notes

No response

sylv256 avatar Sep 26 '24 04:09 sylv256

I dont see how this is an issue, it will not stop working when Rust 2024 is released, we will still be on 2021 edition. When we update to 2024 we will have to move to the alternatives, but right now its fine

Emilgardis avatar Sep 26 '24 04:09 Emilgardis

The reason why it's being deprecated is quite important. I'd suggest reading that before deciding it's unnecessary to refactor for now.

sylv256 avatar Sep 26 '24 21:09 sylv256

Ok, that reason is more compelling. The current implementation shouldnt trigger immediate UB as we've reasoned about it (we do uphold the conditions), but we should change it to not chance it.

Emilgardis avatar Sep 27 '24 05:09 Emilgardis

In some CI setups, running cargo install cross --git https://github.com/cross-rs/cross may fail actually.

Users of the very popular actions-rust-lang/setup-rust-toolchain which is often used transitively in CI setups would experience CI errors, because the action sets -D warnings by default.

I figured I'd give a heads up here.

Edit: In case it's not obvious, you can use RUSTFLAGS="" cargo install cross --git https://github.com/cross-rs/cross (bash) or similar as a temporary workaround.

Sewer56 avatar Nov 01 '24 23:11 Sewer56

I started seeing a ton of these warnings after upgrading to Rust 1.83.0

warning: creating a shared reference to mutable static is discouraged
   --> src/docker/local.rs:168:34
    |
168 |     let is_terminated = unsafe { crate::errors::TERMINATED.load(Ordering::SeqCst) };
    |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ shared reference to mutable static
    |
    = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
    = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives
    = note: `#[warn(static_mut_refs)]` on by default

warning: creating a shared reference to mutable static is discouraged
   --> src/docker/shared.rs:568:17
    |
568 |             if !CHILD_CONTAINER.exists.swap(true, Ordering::SeqCst) {
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ shared reference to mutable static
    |
    = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
    = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a shared reference to mutable static is discouraged
   --> src/docker/shared.rs:592:18
    |
592 |         unsafe { CHILD_CONTAINER.exists() }
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^ shared reference to mutable static
    |
    = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
    = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a mutable reference to mutable static is discouraged
   --> src/docker/shared.rs:604:13
    |
604 |             CHILD_CONTAINER.exit();
    |             ^^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static
    |
    = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
    = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a mutable reference to mutable static is discouraged
   --> src/docker/shared.rs:630:13
    |
630 |             CHILD_CONTAINER.finish(is_tty, msg_info);
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static
    |
    = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
    = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a shared reference to mutable static is discouraged
  --> src/errors.rs:27:9
   |
27 |     if !TERMINATED.swap(true, Ordering::SeqCst) && temp::has_tempfiles() {
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ shared reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a mutable reference to mutable static is discouraged
   --> src/errors.rs:105:5
    |
105 |     docker::CHILD_CONTAINER.terminate();
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static
    |
    = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
    = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a shared reference to mutable static is discouraged
  --> src/temp.rs:22:15
   |
22 |     unsafe { !FILES.is_empty() || !DIRS.is_empty() }
   |               ^^^^^^^^^^^^^^^^ shared reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a shared reference to mutable static is discouraged
  --> src/temp.rs:22:36
   |
22 |     unsafe { !FILES.is_empty() || !DIRS.is_empty() }
   |                                    ^^^^^^^^^^^^^^^ shared reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a mutable reference to mutable static is discouraged
  --> src/temp.rs:31:5
   |
31 |     FILES.clear();
   |     ^^^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a mutable reference to mutable static is discouraged
  --> src/temp.rs:32:5
   |
32 |     DIRS.clear();
   |     ^^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a mutable reference to mutable static is discouraged
  --> src/temp.rs:41:5
   |
41 |     FILES.push(file);
   |     ^^^^^^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a mutable reference to mutable static is discouraged
  --> src/temp.rs:42:8
   |
42 |     Ok(FILES.last_mut().expect("file list should not be empty"))
   |        ^^^^^^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a mutable reference to mutable static is discouraged
  --> src/temp.rs:48:5
   |
48 |     FILES.pop()
   |     ^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a mutable reference to mutable static is discouraged
  --> src/temp.rs:90:5
   |
90 |     DIRS.push(dir);
   |     ^^^^^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: creating a shared reference to mutable static is discouraged
  --> src/temp.rs:91:8
   |
91 |     Ok(DIRS.last().expect("should not be empty").path())
   |        ^^^^^^^^^^^ shared reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a mutable reference to mutable static is discouraged
  --> src/temp.rs:97:5
   |
97 |     DIRS.pop()
   |     ^^^^^^^^^^ mutable reference to mutable static
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
   = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives

warning: `cross` (lib) generated 17 warnings
    Finished `release` profile [optimized] target(s) in 49.78s

UebelAndre avatar Nov 28 '24 18:11 UebelAndre

Hello and sorry for asking: any news here? because I get:

Cargo.toml: TOML parse error at line 12, column 11
   |
12 | edition = "2024"
   |           ^^^^^^
unknown variant `2024`, expected one of `2015`, `2018`, `2021`

jb-alvarado avatar May 23 '25 09:05 jb-alvarado

Hello and sorry for asking: any news here? because I get:

Cargo.toml: TOML parse error at line 12, column 11
   |
12 | edition = "2024"
   |           ^^^^^^
unknown variant `2024`, expected one of `2015`, `2018`, `2021`

Update your version of Rust.

sylv256 avatar May 23 '25 20:05 sylv256

In CI, I have -D warnings set globally, which means that cargo install cross --git https://github.com/cross-rs/cross no longer works in the latest stable version of Rust.

error: creating a shared reference to mutable static --> src/docker/local.rs:168:34 | 168 | let is_terminated = unsafe { crate::errors::TERMINATED.load(Ordering::SeqCst) }; … error: could not compile cross (lib) due to 17 previous errors

kornelski avatar Aug 21 '25 16:08 kornelski

My solution was to switch to using cargo-zigbuild, which you can also use within the context of goreleaser.

marclove avatar Aug 26 '25 12:08 marclove