Repository navigation
Closed
Labels
A-incr-compArea: Incremental compilationArea: Incremental compilationC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.Performance or correctness regression from stable to nightly.
Description
Instructions are getting omitted from naked_asm blocks after fixing a compilation error and then rebuilding with cargo run. A cargo clean and cargo run is then required to get a correct executable.
As a minimal demonstration of this problem this code contains a deliberate error and fails to build:
#![feature(naked_functions)]
#![no_main]
use core::arch::naked_asm;
#[unsafe(no_mangle)]
#[naked]
extern "C" fn add_u64(x: u64, y: u64) -> u64 {
unsafe { naked_asm!("add x0, x0, w1", "ret") }
}
#[unsafe(no_mangle)]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
println!("{}", add_u64(3u64, 7u64));
0
}
cargo run
Compiling rust_asm v0.1.0 (/Users/me/rust_asm)
error: too few operands for instruction
|
note: instantiated into assembly here
--> <inline asm>:5:1
|
5 | add x0, x0, w1
| ^^^^^^^^^^^^^^
We fix the source by changing w1 into x1. Which now builds without error but produces the wrong result:
#![feature(naked_functions)]
#![no_main]
use core::arch::naked_asm;
#[unsafe(no_mangle)]
#[naked]
extern "C" fn add_u64(x: u64, y: u64) -> u64 {
unsafe { naked_asm!("add x0, x0, x1", "ret") }
}
#[unsafe(no_mangle)]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
println!("{}", add_u64(3u64, 7u64));
0
}
cargo run
Compiling rust_asm v0.1.0 (/Users/me/rust_asm)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
Running `target/debug/rust_asm`
3
Looking at the disassembled executable with objdump -d target/debug/rust_asm | less we find the add instruction is missing from the add_u64() function:
00000001000018d0 <_add_u64>:
1000018d0: d65f03c0 ret
Rebuilding with cargo clean and cargo run then produces the correct result:
cargo clean
Removed 41 files, 1.0MiB total
cargo run
Compiling rust_asm v0.1.0 (/Users/me/rust_asm)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.36s
Running `target/debug/rust_asm`
10
And of course the add instruction is now in the executable where we expect it to be:
00000001000018cc <_add_u64>:
1000018cc: 8b010000 add x0, x0, x1
1000018d0: d65f03c0 ret
Meta
rustc --version --verbose
rustc 1.87.0-nightly (249cb8431 2025-03-12)
binary: rustc
commit-hash: 249cb84316401daf040832cdbb8a45e0f5ab6af8
commit-date: 2025-03-12
host: aarch64-apple-darwin
release: 1.87.0-nightly
LLVM version: 20.1.0
Metadata
Metadata
Assignees
Labels
A-incr-compArea: Incremental compilationArea: Incremental compilationC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.Performance or correctness regression from stable to nightly.
Activity
bjorn3 commentedon Apr 5, 2025
cc @folkertdev
compiler-errors commentedon Apr 5, 2025
@ZiCog: The code that you shared is identical. I assume you meant to write
unsafe { naked_asm!("add x0, x0, w1", "ret") }in the first code snippet.compiler-errors commentedon Apr 5, 2025
I also can't seem to reproduce the bug on either
aarch64-apple-darwinoraarch64-unknown-linux-gnu, either on nightly for 2025-03-12 or today's nightly.ZiCog commentedon Apr 5, 2025
Sorry, you are right, that should be
unsafe { naked_asm!("add x0, x0, w1", "ret") }in the first snippet. I have changed it in the report. I was being so careful...I just did a
rustup updateon my MacBook Pro M1 and it won't install anything newer than 2025-03-12 for the lack ofrls. Anything I can do about that?OS = Sequoia 15.3.1
Problem is still here for me.
compiler-errors commentedon Apr 5, 2025
@ZiCog: I'm trying to understand anything about what could be different between my attempt to reproduce the bug here and your setup.
Is there anything special about your
Cargo.toml?Is there anything special about your setup? Does it reproduce on a fresh
cargo init'ed project with all changes and commands made through the command line (i.e. no IDE running)?Could you share any other information here?
ZiCog commentedon Apr 5, 2025
I was using the Zed editor and Warp terminal. I have now repeated the experiment with vim and the regular Mac terminal.
It turns out that starting with a new project
cargo init rust_bugand following the steps in my report the problem does not occur.BUT. Then editing that
x1tow1again to produce the build error and then changing it back tox1does indeed show the problem. It builds but produces the wrong result.In short. On a fresh project there is no problem. On a "stale" project there is.
My Cargo.toml remains as generated:
compiler-errors commentedon Apr 5, 2025
Ah, thanks @ZiCog. Repeating
x1->w1->x1was necessary. I got it to print "3" :)ZiCog commentedon Apr 5, 2025
I just tried this on an Nvidia Jetson Nano with 2025-04-04 and the problem did not show:
$ cat /etc/issue
Ubuntu 18.04.6 LTS \n \l
$ uname -a
Linux konalantie-nx 4.9.201-tegra #1 SMP PREEMPT Fri Jul 9 08:56:59 PDT 2021 aarch64 aarch64 aarch64 GNU/Linux
$ rustc --version --verbose
rustc 1.88.0-nightly (17ffbc8 2025-04-04)
binary: rustc
commit-hash: 17ffbc8
commit-date: 2025-04-04
host: aarch64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2
ZiCog commentedon Apr 5, 2025
Is there any way I can get a more recent nightly installed on my Mac Book Pro M1 ?
zachs18 commentedon Apr 6, 2025
@ZiCog
You need to remove the
rlscomponent (it has been removed and hadn't done anything for a while anyway) so thatrustupwill update to newer nightlys that no longer have it.rustup +nightly component remove rlsis the magic word IIRC, and thenrustup update nightlycompiler-errors commentedon Apr 6, 2025
I don't believe this has anything to do with
naked_asm, nor does this have anything to do with MacOS really.My rough explanation for why this occurs is:
Session 1 begins
Session 2 begins
Session 3 begins
I can actually reproduce this on linux with
-Csave-tempsand a similar setup 🤔8 remaining items
apiraino commentedon Apr 7, 2025
Assigning priority (discussion on Zulip)
@rustbot label -I-prioritize +P-high +regression-from-stable-to-nightly
Auto merge of rust-lang#139453 - compiler-errors:incr, r=jieyouxu
Auto merge of rust-lang#139453 - compiler-errors:incr, r=jieyouxu
Auto merge of #139453 - compiler-errors:incr, r=jieyouxu
ZiCog commentedon Apr 12, 2025
Yay! Confirmed. The problem no long occurs on latest nightly.
Thanks to @compiler-errors and all concerned.
Auto merge of #139453 - compiler-errors:incr, r=jieyouxu
Auto merge of #139453 - compiler-errors:incr, r=jieyouxu
Auto merge of #139453 - compiler-errors:incr, r=jieyouxu
Auto merge of #139453 - compiler-errors:incr, r=jieyouxu
Auto merge of #139453 - compiler-errors:incr, r=jieyouxu