From 793476d6bba798543b154f4afd2975f23c3f5409 Mon Sep 17 00:00:00 2001 From: Elliot Berman Date: Wed, 18 Dec 2024 11:22:57 -0800 Subject: [PATCH 1/2] holding-cell: Rely less on environment variables We can use cc::Build::new() to get the compiler and clang-binutils to find objcopy. Signed-off-by: Elliot Berman --- .cargo/config.toml | 5 +- Cargo.lock | 291 +++++++++++++++++++++++++- README.md | 11 + vmm/Cargo.toml | 1 + vmm/build.rs | 34 +-- vmm/tests/holding_cell/holding-cell.c | 2 + 6 files changed, 325 insertions(+), 19 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index b73eeb4..8dd1ecf 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -7,7 +7,6 @@ linker = "android-ndk/arm64/bin/aarch64-linux-android-clang" runner = "runners/adb_runner.sh" [env] -CC_aarch64-linux-android = { value = "android-ndk/arm64/bin/aarch64-linux-android-clang", relative = true } -OBJCOPY = { value = "android-ndk/arm64/bin/llvm-objcopy", relative = true } -AR_aarch64-linux-android = { value = "android-ndk/arm64/bin/llvm-ar", relative = true } +CC_aarch64_linux_android = { value = "android-ndk/arm64/bin/aarch64-linux-android-clang", relative = true } +AR_aarch64_linux_android = { value = "android-ndk/arm64/bin/llvm-ar", relative = true } RUST_TEST_THREADS = "1" diff --git a/Cargo.lock b/Cargo.lock index b5e62f2..f49bde1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + [[package]] name = "aho-corasick" version = "1.1.3" @@ -11,6 +26,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anstream" version = "0.6.18" @@ -72,12 +96,38 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + [[package]] name = "bindgen" version = "0.71.1" @@ -125,6 +175,54 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "camino" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-binutils" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec9a0e2ad2c6ec1516256bf9d0f07841a55265f2167e5d1c88ed1f99c73900da" +dependencies = [ + "anyhow", + "cargo_metadata", + "clap 2.34.0", + "regex", + "rustc-cfg", + "rustc-demangle", + "rustc_version", + "serde", + "toml", +] + +[[package]] +name = "cargo-platform" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e35af189006b9c0f00a064685c727031e3ed2d8020f7ba284d78cc2671bd36ea" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4acbb09d9ee8e23699b9634375c72795d095bf268439da88562cf9b501f181fa" +dependencies = [ + "camino", + "cargo-platform", + "semver", + "serde", + "serde_json", +] + [[package]] name = "cc" version = "1.2.3" @@ -169,6 +267,21 @@ dependencies = [ "libloading", ] +[[package]] +name = "clap" +version = "2.34.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c" +dependencies = [ + "ansi_term", + "atty", + "bitflags 1.3.2", + "strsim 0.8.0", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "clap" version = "4.5.23" @@ -188,7 +301,7 @@ dependencies = [ "anstream", "anstyle", "clap_lex", - "strsim", + "strsim 0.11.1", ] [[package]] @@ -261,6 +374,28 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "failure" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" +dependencies = [ + "backtrace", + "failure_derive", +] + +[[package]] +name = "failure_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", + "synstructure", +] + [[package]] name = "fdt" version = "0.1.5" @@ -344,6 +479,12 @@ dependencies = [ "slab", ] +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + [[package]] name = "glob" version = "0.3.1" @@ -380,7 +521,7 @@ name = "gunyah-test-vmm" version = "0.1.0" dependencies = [ "anyhow", - "clap", + "clap 4.5.23", "derive_more", "gunyah", "page_size", @@ -394,6 +535,15 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.9" @@ -424,6 +574,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + [[package]] name = "libc" version = "0.2.168" @@ -492,6 +648,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "miniz_oxide" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ffbe83022cedc1d264172192511ae958937694cd57ce297164951b8b3568394" +dependencies = [ + "adler2", +] + [[package]] name = "mio" version = "0.8.11" @@ -569,10 +734,19 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", ] +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.20.2" @@ -733,6 +907,21 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "rustc-cfg" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ad221fe7cd09334f8735dcc157b1178e343f43dfaefcd1b09d7fd4fc0921b6f" +dependencies = [ + "failure", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + [[package]] name = "rustc-hash" version = "2.1.0" @@ -761,6 +950,12 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + [[package]] name = "same-file" version = "1.0.6" @@ -796,6 +991,41 @@ name = "semver" version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3cb6eb87a131f756572d7fb904f6e7b68633f09cca868c5df1c4b8d1a694bbba" +dependencies = [ + "serde", +] + +[[package]] +name = "serde" +version = "1.0.216" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.216" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.90", +] + +[[package]] +name = "serde_json" +version = "1.0.133" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] [[package]] name = "serial_test" @@ -849,6 +1079,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "strsim" version = "0.11.1" @@ -877,6 +1113,27 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", + "unicode-xid", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thiserror" version = "1.0.69" @@ -897,12 +1154,33 @@ dependencies = [ "syn 2.0.90", ] +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "unicode-ident" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "unicode-xid" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" + [[package]] name = "utf8-width" version = "0.1.7" @@ -915,6 +1193,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "vm-fdt" version = "0.2.0" @@ -933,6 +1217,7 @@ version = "0.1.0" dependencies = [ "anyhow", "bindgen", + "cargo-binutils", "cc", "claim", "core_affinity", diff --git a/README.md b/README.md index 8d0649b..a7bee2b 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ To use this on an Android device, be sure to check the instructions below on setting up the NDK. Assuming `adb shell` and `adb push` work, you can run: ```sh +rustup component add llvm-tools + cargo test --workspace -- --test-threads 1 ``` @@ -17,6 +19,15 @@ Android NDK set up. If you don't, you can do it for this project by running linker configurations from [target.aarch64-linux-android] in .cargo/config.toml [here](./.cargo/config.toml#L2-L3). +```sh +rustup target add aarch64-linux-android +rustup component add llvm-tools +./setup-android.sh + +# build/test as usual +cargo build +``` + ## Dev Container If you don't have/want the rust toolchain installed, you can use ./tools/dev_container. diff --git a/vmm/Cargo.toml b/vmm/Cargo.toml index 7f9c71b..256a83c 100644 --- a/vmm/Cargo.toml +++ b/vmm/Cargo.toml @@ -33,4 +33,5 @@ pow2 = "0.1.1" [build-dependencies] bindgen = "0.71.1" +cargo-binutils = "0.3.6" cc = "1.2.3" diff --git a/vmm/build.rs b/vmm/build.rs index f6c1735..bb482e1 100644 --- a/vmm/build.rs +++ b/vmm/build.rs @@ -32,23 +32,22 @@ fn build_unsafe_read(out_dir: &Path) { .expect("Couldn't write bindings!"); } -fn main() { +fn build_holding_cell(out_dir: &Path) { for source in HOLDING_CELL_SOURCES { println!("cargo:rerun-if-changed={}", source); } println!("cargo:rerun-if-changed=tests/holding_cell/holding-cell.lds"); - println!("cargo:rerun-if-changed=build.rs"); - let cc = env::var("CC_aarch64-linux-android") - .or(env::var("CC_aarch64_linux_android")) - .expect("CC_aarch64-linux-android is unset"); - let objcopy = env::var("OBJCOPY").expect("OBJCOPY is unset"); - let out_dir = env::var("OUT_DIR").unwrap(); + let compiler = cc::Build::new().get_compiler(); - build_unsafe_read(&PathBuf::from(&out_dir)); + let elf_path = out_dir.join("holding-cell.elf"); + let elf_str = elf_path.to_str().unwrap(); - assert!(Command::new(cc) - .args(["-o", &format!("{}/holding-cell.elf", &out_dir)]) + let bin_path = out_dir.join("holding-cell.bin"); + let bin_str = bin_path.to_str().unwrap(); + + assert!(Command::new(compiler.path()) + .args(["-o", elf_str]) .args(HOLDING_CELL_SOURCES) .arg("-Os") .arg("-static") @@ -60,19 +59,28 @@ fn main() { "-fomit-frame-pointer", "-fno-exceptions", "-fno-asynchronous-unwind-tables", - "-fno-unwind-tables" + "-fno-unwind-tables", ]) .stderr(stderr()) .status() .unwrap() .success()); + let objcopy = cargo_binutils::Tool::Objcopy.path().unwrap(); assert!(Command::new(objcopy) .args(["-O", "binary"]) - .arg(format!("{}/holding-cell.elf", &out_dir)) - .arg(format!("{}/holding-cell.bin", &out_dir)) + .arg(elf_str) + .arg(bin_str) .stderr(stderr()) .status() .unwrap() .success()); } + +fn main() { + println!("cargo:rerun-if-changed=build.rs"); + + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + build_unsafe_read(&out_dir); + build_holding_cell(&out_dir); +} diff --git a/vmm/tests/holding_cell/holding-cell.c b/vmm/tests/holding_cell/holding-cell.c index 724ee5e..da89ea8 100644 --- a/vmm/tests/holding_cell/holding-cell.c +++ b/vmm/tests/holding_cell/holding-cell.c @@ -130,6 +130,8 @@ int main() { if (COMMAND_HOLD(word)) tmp = *holding_cell; + (void)tmp; + switch (nargs) { case 0: *holding_cell = cmd->cb0(); From 6b289eee93342561ce6e78a5c9086a2cf9bfeaa0 Mon Sep 17 00:00:00 2001 From: Elliot Berman Date: Wed, 18 Dec 2024 11:25:58 -0800 Subject: [PATCH 2/2] GitHub: Fix rust workflow Add llvm-tools first and build using aarch64-unknown-linux-gnu target. Signed-off-by: Elliot Berman --- .github/workflows/rust.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yaml b/.github/workflows/rust.yaml index 1ebb244..7af738f 100644 --- a/.github/workflows/rust.yaml +++ b/.github/workflows/rust.yaml @@ -10,7 +10,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Run Clippy - run: cargo clippy --all-targets --all-features + - name: Setup build + run: sudo apt-get install gcc-aarch64-linux-gnu && rustup component add llvm-tools && rustup target add aarch64-unknown-linux-gnu + - name: Clippy + run: cargo clippy --all-targets --all-features --target aarch64-unknown-linux-gnu - name: Run Format run: cargo fmt --all -- --check