Skip to content

Commit d64a68f

Browse files
authored
Unrolled build for rust-lang#131755
Rollup merge of rust-lang#131755 - jfrimmel:avr-rjmp-offset-regression-test, r=jieyouxu Regression test for AVR `rjmp` offset This adds a regression test for rust-lang#129301 by minimizing the code in the linked issue and putting it into a `#![no_core]`-compatible format, so that it can easily be used within an `rmake`-test. This needs to be a `rmake` test (opposed to a `tests/assembly` one), since the linked issue describes, that the problem only occurs if the code is directly compiled. Note, that `lld` is used instead of `avr-gcc`; see the [comments](rust-lang#131755 (comment)) [below](rust-lang#131755 (comment)). Closes rust-lang#129301. To show, that the test actually catches the wrong behavior, this can be tested with a faulty rustc: ```bash $ rustup install nightly-2024-08-19 $ rustc +nightly-2024-08-19 -C opt-level=s -C panic=abort --target avr-unknown-gnu-atmega328 -Clinker=build/x86_64-unknown-linux-gnu/ci-llvm/bin/lld -Clink-arg='--entry=main' -o compiled tests/run-make/avr-rjmp-offset/avr-rjmp-offsets.rs $ llvm-objdump -d compiled | grep '<main>' -A 6 000110b4 <main>: 110b4: 81 e0 ldi r24, 0x1 110b6: 92 e0 ldi r25, 0x2 110b8: 85 b9 out 0x5, r24 110ba: 95 b9 out 0x5, r25 110bc: fe cf rjmp .-4 ``` One can see, that the wrong label offset (`4` instead of `6`) is produced, which would trigger an assertion in the test case. This would be a good candidate for using the `minicore` proposed in rust-lang#130693. Since this is not yet merged, there is a FIXME. r? Patryk27 I think, you are the yet-to-be official target maintainer, hence I'll assign to you. `@rustbot` label +O-AVR
2 parents acfdb8d + a35ed2f commit d64a68f

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! This test case is a `#![no_core]`-version of the MVCE presented in #129301.
2+
//!
3+
//! The function [`delay()`] is removed, as it is not necessary to trigger the
4+
//! wrong behavior and would require some additional lang items.
5+
#![feature(no_core, lang_items, intrinsics, rustc_attrs)]
6+
#![no_core]
7+
#![no_main]
8+
#![allow(internal_features)]
9+
10+
use minicore::ptr;
11+
12+
#[no_mangle]
13+
pub fn main() -> ! {
14+
let port_b = 0x25 as *mut u8; // the I/O-address of PORTB
15+
16+
// a simple loop with some trivial instructions within. This loop label has
17+
// to be placed correctly before the `ptr::write_volatile()` (some LLVM ver-
18+
// sions did place it after the first loop instruction, causing unsoundness)
19+
loop {
20+
unsafe { ptr::write_volatile(port_b, 1) };
21+
unsafe { ptr::write_volatile(port_b, 2) };
22+
}
23+
}
24+
25+
// FIXME: replace with proper minicore once available (#130693)
26+
mod minicore {
27+
#[lang = "sized"]
28+
pub trait Sized {}
29+
30+
#[lang = "copy"]
31+
pub trait Copy {}
32+
impl Copy for u32 {}
33+
impl Copy for &u32 {}
34+
impl<T: ?Sized> Copy for *mut T {}
35+
36+
pub mod ptr {
37+
#[inline]
38+
#[rustc_diagnostic_item = "ptr_write_volatile"]
39+
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
40+
extern "rust-intrinsic" {
41+
#[rustc_nounwind]
42+
pub fn volatile_store<T>(dst: *mut T, val: T);
43+
}
44+
unsafe { volatile_store(dst, src) };
45+
}
46+
}
47+
}
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//@ needs-llvm-components: avr
2+
//@ needs-rust-lld
3+
//! Regression test for #129301/llvm-project#106722 within `rustc`.
4+
//!
5+
//! Some LLVM-versions had wrong offsets in the local labels, causing the first
6+
//! loop instruction to be missed. This test therefore contains a simple loop
7+
//! with trivial instructions in it, to see, where the label is placed.
8+
//!
9+
//! This must be a `rmake`-test and cannot be a `tests/assembly`-test, since the
10+
//! wrong output is only produced with direct assembly generation, but not when
11+
//! "emit-asm" is used, as described in the issue description of #129301:
12+
//! https://github.com/rust-lang/rust/issues/129301#issue-2475070770
13+
use run_make_support::{llvm_objdump, rustc};
14+
15+
fn main() {
16+
rustc()
17+
.input("avr-rjmp-offsets.rs")
18+
.opt_level("s")
19+
.panic("abort")
20+
.target("avr-unknown-gnu-atmega328")
21+
// normally one links with `avr-gcc`, but this is not available in CI,
22+
// hence this test diverges from the default behavior to enable linking
23+
// at all, which is necessary for the test (to resolve the labels). To
24+
// not depend on a special linker script, the main-function is marked as
25+
// the entry function, causing the linker to not remove it.
26+
.linker("rust-lld")
27+
.link_arg("--entry=main")
28+
.output("compiled")
29+
.run();
30+
31+
let disassembly = llvm_objdump().disassemble().input("compiled").run().stdout_utf8();
32+
33+
// search for the following instruction sequence:
34+
// ```disassembly
35+
// 00000080 <main>:
36+
// 80: 81 e0 ldi r24, 0x1
37+
// 82: 92 e0 ldi r25, 0x2
38+
// 84: 85 b9 out 0x5, r24
39+
// 86: 95 b9 out 0x5, r25
40+
// 88: fd cf rjmp .-6
41+
// ```
42+
// This matches on all instructions, since the size of the instructions be-
43+
// fore the relative jump has an impact on the label offset. Old versions
44+
// of the Rust compiler did produce a label `rjmp .-4` (misses the first
45+
// instruction in the loop).
46+
assert!(disassembly.contains("<main>"), "no main function in output");
47+
disassembly
48+
.trim()
49+
.lines()
50+
.skip_while(|&line| !line.contains("<main>"))
51+
.inspect(|line| println!("{line}"))
52+
.skip(1)
53+
.zip(["ldi\t", "ldi\t", "out\t", "out\t", "rjmp\t.-6"])
54+
.for_each(|(line, expected_instruction)| {
55+
assert!(
56+
line.contains(expected_instruction),
57+
"expected instruction `{expected_instruction}`, got `{line}`"
58+
);
59+
});
60+
}

0 commit comments

Comments
 (0)