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

Incorrect code generated from inline assembly with inout register on riscv32im-unknown-none-elf #128212

Open
shkoo opened this issue Jul 25, 2024 · 6 comments
Labels
A-inline-assembly Area: inline asm!(..) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade O-riscv Target: RISC-V architecture P-high High priority

Comments

@shkoo
Copy link
Contributor

shkoo commented Jul 25, 2024

I tried compiling this code with a target of riscv32im-unknown-none-elf:

#![no_std]

static mut MY_BUFFER: [u32; 4] = [0u32; 4];

#[no_mangle]
unsafe fn using_inout() {
    let mut start = MY_BUFFER.as_mut_ptr();
    ::core::arch::asm!(
            "ecall",
            inout("a0") start);
    _ = start;
}

The assembly generated is incorrect, since it doesn't add in the lower 16 bits of the address of MY_BUFFER before calling the ecall:

using_inout:
        lui     a0, %hi(example::MY_BUFFER::he36bd30b506a9b69)
        ecall
        ret

As a workaround, changing the inout to inlateout seems to fix the problem:

using_inlateout:
        lui     a0, %hi(example::MY_BUFFER::he36bd30b506a9b69)
        addi    a0, a0, %lo(example::MY_BUFFER::he36bd30b506a9b69)
        ecall
        ret

Here is a compiler explorer link for rust nightly that demonstrates the problem:

https://godbolt.org/z/xMjoTo5oY

(For reference in case it gets fixed in nightly, it's also present in rust 1.80.0)

@shkoo shkoo added the C-bug Category: This is a bug. label Jul 25, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 25, 2024
@tgross35 tgross35 added O-riscv Target: RISC-V architecture A-inline-assembly Area: inline asm!(..) labels Jul 25, 2024
@tgross35
Copy link
Contributor

I haven't looked into this too much yet, but do you know if it reproduces using C's inline assembly fed through clang? This seems like it could be an LLVM problem.

@flaub
Copy link
Contributor

flaub commented Jul 26, 2024

It appears that this bug starts to appear in 1.77.

Using riscv32i-unknown-none-elf target:

1.76: https://godbolt.org/z/bKosKqvfW
1.77: https://godbolt.org/z/MYacc9na7

@flaub
Copy link
Contributor

flaub commented Jul 26, 2024

Could it be that this is the same underlying issue?
#126221

@tgross35
Copy link
Contributor

Did you mean between 1.77 and 1.78? The two links look the same to me.

It looks like this has to be a regression in LLVM, which was bumped to version 18 in 1.78, because we seem to generate the exact same IR. Could you file an issue with LLVM?

https://godbolt.org/z/Te9efcnMz

Could it be that this is the same underlying issue? #126221

It looks like it, I will close that one because this one has more detail.

@tgross35 tgross35 added WG-llvm Working group: LLVM backend code generation and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jul 26, 2024
@nikic nikic added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness and removed WG-llvm Working group: LLVM backend code generation labels Jul 26, 2024
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jul 26, 2024
@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

Being an unsound issue, maybe let's see what we can do. Compile target is Tier 2.

@rustbot label -I-prioritize +P-high

@rustbot rustbot added P-high High priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 26, 2024
flaub added a commit to risc0/risc0 that referenced this issue Jul 26, 2024
flaub added a commit to risc0/risc0 that referenced this issue Jul 26, 2024
SchmErik added a commit to risc0/risc0 that referenced this issue Jul 26, 2024
See: rust-lang/rust#128212

---------

Co-authored-by: Erik Kaneda <erik@risczero.com>
SchmErik pushed a commit to risc0/risc0 that referenced this issue Jul 26, 2024
SchmErik pushed a commit to risc0/risc0 that referenced this issue Jul 26, 2024
@tgross35 tgross35 added the llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade label Jul 27, 2024
@tgross35
Copy link
Contributor

Fix applied to LLVM 19.x llvm/llvm-project#100843

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inline-assembly Area: inline asm!(..) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness llvm-fixed-upstream Issue expected to be fixed by the next major LLVM upgrade O-riscv Target: RISC-V architecture P-high High priority
Projects
None yet
Development

No branches or pull requests

6 participants