Skip to content

Commit 2673a25

Browse files
committed
Merge branch 'wf/issue-89-final'
2 parents c1fd55a + d9130ba commit 2673a25

9 files changed

Lines changed: 312 additions & 125 deletions

File tree

‎.gitattributes‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# Auto detect text files and perform LF normalization
22
* text=auto
3+
4+
# ELF images (such as the loader test fixture) are binary: no line-ending
5+
# conversion, no text diff
6+
*.elf binary

‎.github/workflows/build.yml‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,24 @@ jobs:
4646

4747
- name: Build
4848
run: cargo build --release --target ${{ matrix.target }}
49+
50+
test:
51+
name: Test (${{ matrix.os }})
52+
runs-on: ${{ matrix.os }}
53+
54+
strategy:
55+
fail-fast: false
56+
matrix:
57+
os: [ubuntu-latest, windows-latest, macos-latest]
58+
59+
steps:
60+
- uses: actions/checkout@v4
61+
62+
- uses: dtolnay/rust-toolchain@stable
63+
64+
- name: Install Linux build tools
65+
if: runner.os == 'Linux'
66+
run: sudo apt-get update && sudo apt-get install -y musl-tools pkg-config libgtk-3-dev
67+
68+
- name: Test
69+
run: cargo test --workspace
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ELF loader fixture
2+
3+
`fixture.elf` is a small RV32 executable for the ELF loader tests. It is
4+
committed so that `cargo test` works on a clean checkout, without building
5+
`rust-to-raven` or `c-to-raven` first.
6+
7+
| File | What it is |
8+
|---|---|
9+
| `fixture.S` | The program: it prints a line, adds one to a word in `.data` and exits with 42. |
10+
| `fixture.ld` | Linker script. It puts `.rodata` at `0x10000`, `.text` at `0x11000` and `.data`/`.bss` at `0x12000`, each in its own `PT_LOAD`. |
11+
| `fixture.elf` | The linked image (about 1 KB). |
12+
13+
Used by:
14+
15+
- `crates/raven-engine/tests/support/falcon_program_elf.rs` (the loader tests);
16+
- `loading_elf_resets_pipeline_to_entry_pc` and
17+
`elf_fixture_runs_to_its_exit_code_in_every_mode` in
18+
`tests/support/ui_app_internal.rs`.
19+
20+
The tests check the layout: an executable and a non-executable `PT_LOAD`, the
21+
read-only data below the code (as lld lays out `rust-to-raven`), an entry point
22+
past the start of `.text`, a `.bss` tail to zero, and the `greeting`,
23+
`counter`, `scratch`, `bump` and `_start` symbols. If you change the program,
24+
keep those properties, or change the tests with it.
25+
26+
## Rebuilding
27+
28+
Run this in this folder. You need clang with the RISC-V target and lld:
29+
30+
```sh
31+
clang --target=riscv32-unknown-elf -march=rv32imaf -mabi=ilp32 -nostdlib -static \
32+
-fuse-ld=lld -Wl,-T,fixture.ld -Wl,--nmagic fixture.S -o fixture.elf
33+
```
34+
35+
`--nmagic` turns off page alignment in the file. The segments stay on separate
36+
pages in memory, but the file does not carry 4 KiB of padding before each one.
37+
38+
On NixOS, run it inside `nix-shell -p llvmPackages_21.clang-unwrapped lld`. The
39+
default clang wrapper is set up for the host, not for a bare-metal target.
40+
41+
The committed file was built with clang and lld 21.1.8. `.gitattributes` marks
42+
`*.elf` as binary, so git does not change its line endings.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* ELF fixture for the loader tests in tests/support/falcon_program_elf.rs
3+
* and for the ELF tests in the root crate's tests/support/ui_app_internal.rs.
4+
*
5+
* It is laid out like a rust-to-raven build: read-only data at the bottom of
6+
* the image, code on the page above it, then .data and .bss (see fixture.ld).
7+
* `bump` sits ahead of `_start`, so the entry point is not the first word of
8+
* the text segment.
9+
*
10+
* Run on RAVEN, it prints the greeting, adds one to `counter` and exits with
11+
* the new value: 41 + 1 = 42. See README.md for how to rebuild fixture.elf.
12+
*/
13+
14+
.section .rodata
15+
.globl greeting
16+
.type greeting, @object
17+
greeting:
18+
.ascii "hello from the ELF fixture\n"
19+
.Lgreeting_end:
20+
.size greeting, .Lgreeting_end - greeting
21+
22+
.data
23+
.p2align 2
24+
.globl counter
25+
.type counter, @object
26+
counter:
27+
.word 41
28+
.size counter, 4
29+
30+
// Never touched by the code: it gives the data segment a bss tail
31+
// (p_memsz > p_filesz) that the loader has to zero.
32+
.bss
33+
.p2align 2
34+
.globl scratch
35+
.type scratch, @object
36+
scratch:
37+
.zero 16
38+
.size scratch, 16
39+
40+
.text
41+
// bump(a0 = &word): *word += 1. a0 is left unchanged.
42+
.globl bump
43+
.type bump, @function
44+
bump:
45+
lw t0, 0(a0)
46+
addi t0, t0, 1
47+
sw t0, 0(a0)
48+
ret
49+
.size bump, . - bump
50+
51+
.globl _start
52+
.type _start, @function
53+
_start:
54+
li a0, 1 // write(1, greeting, len)
55+
la a1, greeting
56+
li a2, .Lgreeting_end - greeting
57+
li a7, 64
58+
ecall
59+
60+
la a0, counter // counter += 1
61+
call bump
62+
63+
lw a0, 0(a0) // exit(counter)
64+
li a7, 93
65+
ecall
66+
.size _start, . - _start
1.06 KB
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* Linker script for the ELF loader fixture (fixture.S).
2+
lld, linking rust-to-raven without a script, puts the read-only data at the
3+
bottom of the image and the code above it. The loader tests check that
4+
order, so it is spelled out here: .rodata at 0x10000, .text on the next
5+
page and .data/.bss on the page after that, each in its own PT_LOAD.
6+
0x10000 keeps the whole image inside the 1 MiB RAM the tests give it. */
7+
OUTPUT_ARCH( "riscv" )
8+
ENTRY( _start )
9+
10+
PHDRS
11+
{
12+
rodata PT_LOAD FLAGS(4); /* R */
13+
text PT_LOAD FLAGS(5); /* R X */
14+
data PT_LOAD FLAGS(6); /* R W */
15+
}
16+
17+
SECTIONS
18+
{
19+
. = 0x00010000;
20+
.rodata : { *(.rodata .rodata.*) } :rodata
21+
. = ALIGN(0x1000);
22+
.text : { *(.text .text.*) } :text
23+
. = ALIGN(0x1000);
24+
.data : { *(.data .data.*) } :data
25+
.bss : { *(.bss .bss.*) } :data
26+
}

‎crates/raven-engine/tests/support/falcon_program_elf.rs‎

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
use super::*;
22
use crate::falcon::Ram;
3+
use crate::falcon::decoder::decode;
4+
use crate::falcon::instruction::Instruction;
35

6+
/// The ELF fixture committed under tests/fixtures/elf (its README says how to
7+
/// rebuild it). It lives in the repository so these tests run on a clean
8+
/// checkout, without a rust-to-raven or c-to-raven build lying around.
49
fn elf_path() -> std::path::PathBuf {
5-
let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
6-
let rels = [
7-
"rust-to-raven/rust-to-raven.elf",
8-
"c-to-raven/c-to-raven.elf",
9-
];
10-
11-
for root in manifest_dir.ancestors() {
12-
for rel in rels {
13-
let path = root.join(rel);
14-
if path.exists() {
15-
return path;
16-
}
17-
}
18-
}
19-
20-
panic!("no versioned ELF fixture found under rust-to-raven/ or c-to-raven/");
10+
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/elf/fixture.elf")
2111
}
2212

2313
fn elf_bytes() -> Vec<u8> {
24-
std::fs::read(elf_path()).expect("failed to read ELF fixture")
14+
let path = elf_path();
15+
std::fs::read(&path)
16+
.unwrap_or_else(|e| panic!("failed to read ELF fixture {}: {e}", path.display()))
17+
}
18+
19+
/// Address of `name` in the symbol table `load_elf` returned.
20+
fn symbol_addr(info: &ElfInfo, name: &str) -> u32 {
21+
info.symbols
22+
.iter()
23+
.find(|(_, names)| names.iter().any(|n| n == name))
24+
.map(|(&addr, _)| addr)
25+
.unwrap_or_else(|| panic!("symbol `{name}` missing from the ELF symbol table"))
2526
}
2627

2728
fn read_u16(bytes: &[u8], off: usize) -> u16 {
@@ -87,6 +88,61 @@ fn elf_text_segment_loaded_into_ram() {
8788
// first word at entry point must be readable and non-zero
8889
let word = mem.load32(info.entry).expect("load32 at entry failed");
8990
assert_ne!(word, 0, "entry point word is zero — segment not loaded");
91+
92+
// The disassembler gets the same bytes as RAM, and the word at the entry
93+
// point is the fixture's first instruction, `li a0, 1`.
94+
let off = (info.entry - info.text_base) as usize;
95+
assert_eq!(info.text_bytes[off..off + 4], word.to_le_bytes());
96+
assert!(
97+
matches!(decode(word), Ok(Instruction::Addi { rd: 10, rs1: 0, imm: 1 })),
98+
"entry word 0x{word:08x} is not `li a0, 1`"
99+
);
100+
}
101+
102+
#[test]
103+
fn elf_data_segment_loaded_and_bss_tail_zeroed() {
104+
let bytes = elf_bytes();
105+
let mut mem = Ram::new(1024 * 1024);
106+
let info = load_elf(&bytes, &mut mem).expect("load_elf failed");
107+
let counter = symbol_addr(&info, "counter");
108+
let scratch = symbol_addr(&info, "scratch");
109+
110+
// Load again over RAM that is not zero, so a bss tail left alone shows up.
111+
let mut mem = Ram::new(1024 * 1024);
112+
for i in 0..16 {
113+
mem.store8(scratch + i, 0xAA).unwrap();
114+
}
115+
load_elf(&bytes, &mut mem).expect("load_elf failed");
116+
117+
assert_eq!(mem.load32(counter).unwrap(), 41, ".data word not loaded");
118+
for i in 0..16 {
119+
assert_eq!(mem.load8(scratch + i).unwrap(), 0, "bss byte {i} not zeroed");
120+
}
121+
}
122+
123+
#[test]
124+
fn elf_symbols_and_sections_parsed() {
125+
let bytes = elf_bytes();
126+
let mut mem = Ram::new(1024 * 1024);
127+
let info = load_elf(&bytes, &mut mem).expect("load_elf failed");
128+
129+
assert_eq!(symbol_addr(&info, "_start"), info.entry);
130+
assert_eq!(symbol_addr(&info, "bump"), info.text_base);
131+
assert_eq!(symbol_addr(&info, "greeting"), info.data_base);
132+
assert!(
133+
info.symbols.values().flatten().all(|n| !n.starts_with('$')),
134+
"mapping symbols ($x, $d) should be filtered out"
135+
);
136+
137+
let names: Vec<&str> = info.sections.iter().map(|s| s.name.as_str()).collect();
138+
assert_eq!(names, [".rodata", ".data", ".bss"]);
139+
let rodata = &info.sections[0];
140+
assert_eq!(rodata.addr, info.data_base);
141+
assert_eq!(rodata.bytes, b"hello from the ELF fixture\n");
142+
let bss = &info.sections[2];
143+
assert_eq!(bss.addr, symbol_addr(&info, "scratch"));
144+
assert_eq!(bss.size, 16);
145+
assert!(bss.bytes.is_empty(), ".bss has no file bytes");
90146
}
91147

92148
#[test]

‎tests/support/falcon_program_elf.rs‎

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)