Skip to content

zkalloc fix: safe arena routing#215

Open
TomWambsgans wants to merge 2 commits into
mainfrom
fix_zkalloc
Open

zkalloc fix: safe arena routing#215
TomWambsgans wants to merge 2 commits into
mainfrom
fix_zkalloc

Conversation

@TomWambsgans
Copy link
Copy Markdown
Collaborator

(size-routing + sticky-System realloc) (mirors Barnadrot/zk-alloc#9)

(replaces the "flush_rayon" ugly fix)

Barnadrot/zk-alloc#9)

(replaces the "flush_rayon" ugly fix)

Co-authored-by: Barnadrot <kbarna.drot@gmail.com>
)

- realloc: switch copy_nonoverlapping -> copy. When a Vec grows inside the
  arena, the freshly-bumped destination can overlap the source within the
  same slab, violating copy_nonoverlapping's precondition (UB).
- begin_phase: panic if a phase is already active. Phases must stay flat
  in a bump allocator: a nested begin would recycle the slab and overwrite
  the outer phase's still-live data. Rather than refcounting (per PR #10),
  we make the violation impossible via an  assert.

Co-authored-by: Barnadrot <kbarna.drot@gmail.com>
Barnadrot added a commit to Barnadrot/leanMultisig that referenced this pull request May 11, 2026
Profile attributes 1.30% of P-core cycles to libc allocator helpers
(_int_malloc + cfree + malloc) inside the zk-alloc build — those are
sub-MIN_ARENA_BYTES allocations being routed to System during the
active phase. Lowering the floor pulls the 1024..4095-byte band back
into the bump-pointer fast path; ideally we recover most of that 1.30%.

Why this is still phase-crossing safe:
- The ~1.5 KB crossbeam_deque::Injector blocks named in the original
  comment fall in [0, 1024), so they still route to System.
- Sticky-System realloc (PR leanEthereum#215) keeps any Vec that was first
  allocated in System in System through growth — a Vec allocated at
  64 B and grown to 4 KiB inside a phase stays in System.
- The known phase-crossing patterns (Registry slots, HashMap entries,
  rayon job frames) are all sub-KB per the comment; they remain
  routed to System.

Risk: if there is a phase-crossing allocation in the 1024..4095-byte
band we have not enumerated, it will end up in arena memory and be
recycled across phase boundaries. Correctness gate must pass before
benchmark, and prove_loop's 5-warm-proof pattern is the same shape that
would expose such a bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant