Advisory: Miscompilation with opt-level="z"
#503
Replies: 4 comments 9 replies
-
rust-lang/rust#119802 is likely to fix this, as it updates LLVM to include the expected fix. |
Beta Was this translation helpful? Give feedback.
-
Adding this to your global [target.'cfg(all(target_arch = "arm", target_os = "none"))']
rustflags = ["-C", "llvm-args=--enable-machine-outliner=never"] |
Beta Was this translation helpful? Give feedback.
-
What is the right place to monitor status of this issue and fix release? All linked issues are "closed/merged". Does it mean that next release 1.76 will have fix included? |
Beta Was this translation helpful? Give feedback.
-
Unless it gets back ported to beta, it won't be in stable until 1.77, in eight weeks time. |
Beta Was this translation helpful? Give feedback.
-
Summary
LLVM17, which was introduced in
nightly-2023-08-09
, and stable1.73.0
, contains a regression in anoptimization pass called "machine outlining", which reduces code size by merging repeated segments of
machine code, creating an "outlined function" which replaces the repeated code.
In some cases, this optimization pass does not correctly consider side effects of calling the outlined
function, causing the Link Register (LR) to become corrupted, and for the resulting program to act incorrectly,
including unexpected crashes, or jumping to incorrect locations, including RAM data regions.
This regression is subject to certain combinations of factors, and does not always reliably trigger. However,
this regression is possible for all Cortex-M targets, when the "machine outlining" optimization pass is active.
We believe this optimization pass is only active when
opt-level = "z"
is selected.It is advised that ALL users of rustc stable >= 1.73.0, or rustc nightly >= 2023-08-09 apply one of the
workarounds specified below. As of 2023-12-21, the regression has not been fixed in rustc.
Technical details
Links to context:
rustc
issuerustc
Potentially Affected Configurations
rustc
:opt-level = "z"
in any of your profilesRUSTFLAGS=-Cllvm-args=--enable-machine-outliner=...
to any value other than "never"
Workarounds
There are currently three potential workarounds, any of which are sufficient to avoid this regression:
rustc
not affected. See the affected configurations above for affected versions."z"
, such as2
,3
, or"s"
.-Cllvm-args=--enable-machine-outliner=never
.Determining if you may be affected
Determining whether you are affected requires looking at your rustc version, and your optimization settings.
Compiler Version
You can determine your currently active version of rust by running the
rustup show
command.You may also see the version overridden for some reason, such as a
rust-toolchain
file:If you see a version matching:
rustc 1.xx.y
, where the1.xx.y
version is >= 1.73.0rustc 1.xx.y-beta
, where the1.xx.y
version is >= 1.73.0rustc 1.xx.y-nightly (SHA1 yyyy-mm-dd)
, where theyyyy-mm-dd
date is >= 2023-08-09Your compiler version is affected. Note that as overrides may be set on a folder/project basis, you may
need to check all potentially affected projects.
Optimization Settings
Optimization settings are set in the
Cargo.toml
of your binary project.By default, the
debug
/dev
profile, used bycargo build
, usesopt-level=0
, which is not affected.By default, the
release
/optimized
profile, used bycargo build --release
, usesopt-level=3
,which is not affected.
If your profiles contain a section containing
opt-level = "z"
:Then you will be affected in relevant compiler versions. note that
profile.NAME
can be any profile,including
profile.dev
,profile.release
, and others.Thanks
Thanks to @peter9477 for finding the bug and providing a reproduction case.
Beta Was this translation helpful? Give feedback.
All reactions