Skip to content
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

[AVR] Invalid asm code "ldd rn, X+N" compiles and produces corrupted binary #109360

Closed
Slickis opened this issue Mar 19, 2023 · 4 comments
Closed
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug. llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes O-AVR Target: AVR processors (ATtiny, ATmega, etc.)

Comments

@Slickis
Copy link

Slickis commented Mar 19, 2023

AVR assembler instruction ldd is defined only for registers Y and Z, but not for X.
asm!() macro accepts it.

I tried this code:

#[no_mangle]
extern "C" fn main() {
    unsafe {
        ::core::arch::asm!("
                ldd   {tmp}, X+0
                ldd   {tmp}, X+1
            ",
            tmp = out(reg) _,
            in("X") &0xAABB_u16,
        );
    }
}

I expected to see this happen: compilation error.

Instead, this happened: rustc produces a binary with a corrupted code.

000000a6 <main>:
  a6:    a0 e0           ldi    r26, 0x00    ; 0
  a8:    b1 e0           ldi    r27, 0x01    ; 1
  aa:    88 81           ld     r24, Y
  ac:    89 81           ldd    r24, Y+1     ; 0x01
  ae:    08 95           ret

Pointer (0x0100) to a value is loaded into register X (r26:r27) following by reads from *Y.

Meta

rustc --version --verbose:

rustc 1.70.0-nightly (22f247c6f 2023-03-13)
binary: rustc
commit-hash: 22f247c6f3ed388cb702d01c2ff27da658a8b353
commit-date: 2023-03-13
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7

Steps to reproduce:

repo: https://github.com/Slickis/rustc-ldd-x

git clone https://github.com/Slickis/rustc-ldd-x
cd rustc-ldd-x
make lss
@Slickis Slickis added the C-bug Category: This is a bug. label Mar 19, 2023
@saethlin saethlin added the O-AVR Target: AVR processors (ATtiny, ATmega, etc.) label Mar 19, 2023
@workingjubilee workingjubilee added the A-inline-assembly Area: Inline assembly (`asm!(…)`) label Mar 27, 2023
@workingjubilee
Copy link
Member

workingjubilee commented Mar 27, 2023

Rust does not reject illegal assembly. It does not necessarily even inspect it. For the string of the asm! macro, Rust performs the formatting instantiation and allows the assembler to do the rest. We only control the operands.

@workingjubilee workingjubilee added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Mar 27, 2023
@workingjubilee
Copy link
Member

I believe this is more of a documentation issue, not a bug we can solve here. This could be reported to LLVM regarding the AVR assembler implementation they use, as I believe they host the assembler for this. However, ultimately, I expect are a million other followup issues like this, and if they don't care to fix it, then, well, it won't be, and I don't think rustc should carry around an assembler linter inside it that can check every instruction you could possibly give it for every single architecture.

To this end I have opened rust-lang/reference#1344

@workingjubilee
Copy link
Member

Reported upstream as llvm/llvm-project#62012

Hope I got the details correct!

@workingjubilee workingjubilee added the llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes label Jul 17, 2023
@Patryk27
Copy link
Contributor

fwiw, this got fixed with #114048 🙂

@nikic nikic closed this as completed Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug. llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade, or backported fixes O-AVR Target: AVR processors (ATtiny, ATmega, etc.)
Projects
None yet
Development

No branches or pull requests

5 participants