-
Notifications
You must be signed in to change notification settings - Fork 62
fix(rom-setup): key asm/ROM caches on their build inputs #1079
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
Open
RogerTaule
wants to merge
4
commits into
pre-develop-1.0.0-beta
Choose a base branch
from
fix/cache-undeterminism
base: pre-develop-1.0.0-beta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| name: asm cache invalidation | ||
|
|
||
| # Guards that the rom-setup asm/ROM cache key tracks every input that determines | ||
| # the generated binaries (the linked C libs, the embedded float ELF, and the | ||
| # transpiler / emulator-asm sources). Mutating any of them must change the cache | ||
| # filename; otherwise stale binaries get silently reused. See | ||
| # tools/test_asm_cache_invalidation.sh and rom-setup/build.rs. | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: | ||
| - develop | ||
| - main | ||
| - "pre-develop-[0-9]+.[0-9]+.[0-9]*" | ||
| paths: | ||
| - "rom-setup/**" | ||
| - "core/src/**" | ||
| - "emulator-asm/**" | ||
| - "lib-c/**" | ||
| - "lib-float/c/lib/ziskfloat.elf" | ||
|
RogerTaule marked this conversation as resolved.
|
||
| - "tools/test_asm_cache_invalidation.sh" | ||
| workflow_dispatch: | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref || github.run_id }} | ||
| cancel-in-progress: true | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| cache-invalidation: | ||
| name: asm cache key tracks its inputs | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 30 | ||
| env: | ||
| CARGO_NET_GIT_FETCH_WITH_CLI: "true" | ||
| steps: | ||
| - name: Free disk space | ||
| uses: jlumbroso/free-disk-space@v1.3.1 | ||
| with: | ||
| tool-cache: true | ||
| android: true | ||
| dotnet: true | ||
| haskell: true | ||
| large-packages: true | ||
| docker-images: true | ||
| swap-storage: false | ||
|
|
||
| - name: Checkout sources | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| cd "$GITHUB_WORKSPACE/tools/test-env" | ||
| sudo ./install_deps.sh | ||
|
|
||
| - name: Run cache-invalidation test | ||
| run: ./tools/test_asm_cache_invalidation.sh --ci | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| //! Bakes two fingerprints of the *source* inputs behind the on-disk caches, so | ||
| //! a change forces regeneration (the caches are keyed by filename, and none of | ||
| //! these inputs has a runtime-hashable compiled artifact). | ||
| //! | ||
| //! `ZISK_ROM_INPUTS_HASH` covers the transpiler (`zisk-core`) plus the float ELF | ||
| //! `elf2rom` embeds, and keys the ROM/verkey cache (`utils.rs`). | ||
| //! `ZISK_ASM_INPUTS_HASH` covers the ROM inputs plus the asm build: | ||
| //! `emulator-asm/src` and the *source* of the linked libs (`ziskclib/src`, | ||
| //! `lib-c/c/src`). It keys the asm-binary cache (`asm_setup.rs`). Hashing lib | ||
| //! source rather than the compiled archives keeps the key a build-time constant, | ||
| //! so the path a consumer computes always matches what the producer generates. | ||
| //! | ||
| //! The ROM inputs are a subset of the asm inputs; keeping them separate avoids | ||
| //! regenerating the expensive ROM/verkey on an asm-only change. | ||
|
|
||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| fn main() { | ||
| let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
| // rom-setup lives at <workspace>/rom-setup. | ||
| let ws = manifest.parent().expect("rom-setup has a parent dir"); | ||
|
|
||
| let core_src = ws.join("core/src"); | ||
|
RogerTaule marked this conversation as resolved.
|
||
| let emu_src = ws.join("emulator-asm/src"); | ||
|
|
||
| // We `rerun-if-changed` each existing file below, not the dirs (cargo's | ||
| // directory watch doesn't reliably fire on file *adds*). That's sufficient: | ||
| // a new source file only affects the output once it's referenced — a `mod` | ||
| // in `lib.rs`, an entry in `emulator-asm/Makefile`, or an `.asm`/`.inc` | ||
| // include — and editing that referencing file (which IS watched) triggers | ||
| // the rerun that picks the new file up. | ||
|
|
||
| // Deny-list (fail-safe): `zisk-core` files that don't affect the transpiled | ||
| // ROM — the Rust-emulator runtime and CLI binaries. Anything else is hashed | ||
| // by default, so a new transpiler module is covered automatically. | ||
| let rom_excluded = | ||
| [ws.join("core/src/bin"), core_src.join("inst_context.rs"), core_src.join("mem.rs")]; | ||
| let mut rom_files = collect_files(&core_src, &rom_excluded); | ||
| rom_files.push(ws.join("lib-float/c/lib/ziskfloat.elf")); // embedded into the ROM | ||
| let rom_hash = hash_files(blake3::Hasher::new(), ws, rom_files); | ||
|
|
||
| // The asm binaries additionally link two libs; hashing their *source* (not | ||
| // the compiled archives) keeps the key a pure build-time constant, so the | ||
| // path a consumer computes always matches what the producer generates. The | ||
| // libs' build *config* (ziskclib's manifest, lib-c's Makefile) is folded in | ||
| // too, since it changes the produced libs without touching their src/. | ||
| let mut asm_files = collect_files(&emu_src, &[]); | ||
| asm_files.push(ws.join("emulator-asm/Makefile")); | ||
| asm_files.extend(collect_files(&ws.join("ziskclib/src"), &[])); // -> libziskclib.a | ||
| asm_files.push(ws.join("ziskclib/Cargo.toml")); | ||
| asm_files.extend(collect_files(&ws.join("lib-c/c/src"), &[])); // -> libziskc.a | ||
|
RogerTaule marked this conversation as resolved.
|
||
| asm_files.push(ws.join("lib-c/c/Makefile")); | ||
| let mut seeded = blake3::Hasher::new(); | ||
| seeded.update(rom_hash.as_bytes()); | ||
| seeded.update(&[0xffu8]); | ||
| let asm_hash = hash_files(seeded, ws, asm_files); | ||
|
|
||
| println!("cargo:rustc-env=ZISK_ROM_INPUTS_HASH={rom_hash}"); | ||
| println!("cargo:rustc-env=ZISK_ASM_INPUTS_HASH={asm_hash}"); | ||
| } | ||
|
|
||
| /// Fold a list of files (workspace-relative path + content) into `hasher` and | ||
| /// return a 12-hex digest, emitting `rerun-if-changed` for each. | ||
| fn hash_files(mut hasher: blake3::Hasher, ws: &Path, mut files: Vec<PathBuf>) -> String { | ||
| files.sort(); // deterministic, independent of readdir order | ||
| for f in &files { | ||
| println!("cargo:rerun-if-changed={}", f.display()); | ||
| let rel = f.strip_prefix(ws).unwrap_or(f); | ||
| hasher.update(rel.to_string_lossy().as_bytes()); | ||
| hasher.update(&[0u8]); | ||
| let bytes = std::fs::read(f) | ||
| .unwrap_or_else(|e| panic!("rom-setup build.rs: cannot read {}: {e}", f.display())); | ||
| hasher.update(&(bytes.len() as u64).to_le_bytes()); | ||
| hasher.update(&bytes); | ||
| hasher.update(&[0xffu8]); | ||
| } | ||
| hasher.finalize().to_hex().as_str()[..12].to_string() | ||
| } | ||
|
|
||
| /// Recursively collect files under `dir` (no-op if absent), skipping anything | ||
| /// matched by `excluded` (an exact path or an ancestor dir). | ||
| fn collect_files(dir: &Path, excluded: &[PathBuf]) -> Vec<PathBuf> { | ||
| let is_excluded = |p: &Path| excluded.iter().any(|e| p == e || p.starts_with(e)); | ||
| let mut out = Vec::new(); | ||
| let mut stack = vec![dir.to_path_buf()]; | ||
| while let Some(d) = stack.pop() { | ||
| let Ok(entries) = std::fs::read_dir(&d) else { continue }; | ||
| for entry in entries.flatten() { | ||
| let path = entry.path(); | ||
| if is_excluded(&path) { | ||
| continue; | ||
| } | ||
| if path.is_dir() { | ||
| stack.push(path); | ||
| } else { | ||
| out.push(path); | ||
| } | ||
| } | ||
| } | ||
| out | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| //! Throwaway probe: prints the asm-binary cache base name for a fixed guest ELF | ||
| //! identity, so a wrapper script can mutate an input (lib / float / transpiler) | ||
| //! and observe whether the cache filename changes. | ||
| //! | ||
| //! `get_assembly_file_paths_from_id` is `pub` on both the base branch and the | ||
| //! fix branch, so the same probe runs on either — the point is that on base the | ||
| //! name is invariant to lib/float/transpiler changes (the bug), and on the fix | ||
| //! branch it changes (the fix). | ||
|
|
||
| fn main() { | ||
| let [mt, _rh, _mo] = rom_setup::get_assembly_file_paths_from_id( | ||
| "probe", | ||
| "deadbeefcafef00d", | ||
| std::path::Path::new("/tmp"), | ||
| false, | ||
| ); | ||
| println!("{}", mt.file_name().unwrap().to_string_lossy()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.