air: make symbolic arena handles safe and generation-checked#179
Open
latifkasuli wants to merge 1 commit into
Open
air: make symbolic arena handles safe and generation-checked#179latifkasuli wants to merge 1 commit into
latifkasuli wants to merge 1 commit into
Conversation
c6f9fd4 to
c09c85a
Compare
eacd019 to
9b2f632
Compare
c5a3050 to
9dc5d68
Compare
Replace raw `u32` arena offsets with opaque `SymbolicNodeRef<F>` handles that carry arena_id, generation, and offset. Validate all three before any `read_unaligned`, turning four classes of UB into deterministic errors: 1. Forgeable handles: `SymbolicNodeRef` fields are private, so downstream code can no longer construct arbitrary indices. 2. Stale handles after clear: `clear_arena()` bumps a generation counter; old handles fail with `StaleGeneration`. 3. Cross-thread misuse: each thread-local arena gets a unique `arena_id` from a global `AtomicU32`; handles from another thread fail with `WrongArena`. 4. Index truncation: `alloc_node` uses `u32::try_from(offset)` instead of a bare `as u32` cast. `SymbolicExpression` remains `Copy`. The only downstream migration is `Operation(u32)` -> `Operation(SymbolicNodeRef<F>)`, which is a mechanical change (one call site in `rec_aggregation::compilation`). Closes leanEthereum#170
5508d33 to
3a22222
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #170.
The thread-local symbolic expression arena in
crates/backend/air/src/symbolic.rsused rawu32byte offsets as public handles into aVec<u8>. BecauseSymbolicExpression::Operation(u32)and safeget_node(u32)were public, downstream code could forge handles or keep stale handles and cause uncheckedread_unalignedreads.This PR closes the full issue, not just the immediate OOB symptom:
clear_arena().u32offset.fetch_add.What this PR does
u32with an opaqueSymbolicNodeRef<F>handle carryingarena_id,generation, andoffsetwith private fields.Vec<u8>withArenaState { arena_id, generation, bytes }.clear_arena()to invalidate outstanding handles.try_get_nodeand keepsget_nodeas the panic-on-invalid convenience wrapper.read_unaligned.u32.rec_aggregation::compilationcaches and dot-product detection helpers to key bySymbolicNodeRef<F>.SymbolicExpressionandSymbolicNodeRefCopy.Files changed
crates/backend/air/src/symbolic.rsArenaState,SymbolicNodeRef,SymbolicNodeAccessError, checked arena id/allocation helpers,try_get_node,clear_arena, updatedalloc_nodeandget_node, 8 unit testscrates/backend/air/Cargo.tomlkoala-bearas dev-dependency for testscrates/rec_aggregation/src/compilation.rsOperationcache/dot-product codegen fromu32offsets toSymbolicNodeRef<F>handlesCargo.lockTest plan
Eight unit tests in
symbolic::tests:roundtrip_alloc_get-- basic alloc + read roundtripstale_handle_rejected_after_clear-- handle from previous generation returnsStaleGenerationold_handle_cannot_read_new_generation_bytes-- old handle cannot read data written in a new generation at the same offsetwrong_thread_handle_rejected-- handle created on one thread returnsWrongArenaon another threadout_of_bounds_handle_rejected-- invalid in-module handle with large offset returnsOutOfBoundsoffset_truncation_detected-- allocation range fails before exceeding theu32offset rangearena_id_overflow_detected-- arena id allocation fails instead of wrappingarithmetic_produces_valid_handles-- arithmetic on non-constant expressions produces valid operation handlesSymbolicExpression<KoalaBear>andSymbolicNodeRef<KoalaBear>areCopyVerified after rebase:
cargo fmt --check cargo test -p mt-air cargo check --workspace cargo clippy --workspace --all-targets -- -D warnings