Repository navigation
Closed
Description
Debug-mode bounds checks for array/slice indexing (§4.3a, §4.8a)
Summary
The spec requires array and slice indexing to be bounds-checked in debug mode. The current indexed-place codegen emits raw address calculation for array/slice-like places, so arbitrary indexing can read or write out of bounds instead of panicking with a useful diagnostic.
Spec
docs/with-specification.md:1416-1452 covers fixed arrays and states indexing is bounds-checked in debug mode. docs/with-specification.md:2028-2055 covers slices and repeats the debug-mode bounds-check requirement.
Current state
src/CodegenDispatch.w:943-952computes projected types forPK_INDEX.src/CodegenDispatch.w:1133-1191lowersPK_INDEXto pointer arithmetic/GEP for arrays, pointers,Vec,str, and slices. There is no emitted comparison against length/capacity before the pointer is used.src/MirLower.w:4471-4542lowersforover slices with a loop counter guarded bycounter < len; that loop shape is safe but does not cover arbitrary user indexing.- Existing
Vec.get-style runtime paths have explicit validity checks in codegen, but bracket indexing is a separate place-projection path. - The current code has
Codegen.emit_runtime_panicatsrc/CodegenDispatch.w:12770-12778, which can be reused for debug checks, though Bug: todo()/unreachable() lower to LLVM unreachable, not a panic (§29.10, §4.10, §14.11.1) #544 notes panic exit-code consistency still needs cleanup.
Implementation plan
- Identify the compiler's debug/release mode flag used by codegen. The check must be emitted only for debug-mode builds as specified.
- Add a helper in
CodegenDispatch.wthat, for aPK_INDEXprojection, computes the dynamic length when available:- fixed array: static element count,
- slice: loaded length,
str: byte length if byte indexing is the current semantics,Vec: current length,- raw pointer: no bounds metadata, so no check.
- Before emitting the GEP/load/store for bounded indexable types, branch to a panic block when
idx < 0for signed indices oridx >= len. Use the existing panic substrate rather than undefined behavior. - Ensure stores and address-taking projections go through the same checked helper as loads. The fix must cover both read and write indexing.
- Keep optimization-friendly lowering for statically proven safe loops, but do not special-case away checks unless the proof is local and deterministic.
Test plan
- Add behavior tests with debug expectations for out-of-bounds array and slice indexing. Use
//! expect-exit///! expect-stderronce panic exit-code behavior is settled. - Add in-bounds read/write tests to show normal indexing still works.
- Add a test for a slice produced from an array, not just
Vec.get. - Add a negative or no-op note for raw pointer indexing because pointers do not carry bounds metadata.
- Run
with build,with build :fixpoint, andwith build :test.
Acceptance criteria
- Debug-mode array indexing checks lower and upper bounds before load/store/address projection.
- Debug-mode slice indexing checks lower and upper bounds before load/store/address projection.
- Raw pointer indexing remains outside this guarantee unless separate metadata is added.
- Release-mode behavior remains consistent with the project policy for debug-only checks.
- Tests prove both success and failure paths.
Dependencies
No hard dependency. Coordinate panic message/exit-code expectations with #544 if tests assert the exact panic exit code.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 4.3.2.8, 4.8.2.5.
Metadata
Metadata
Assignees
Labels
No labels
Activity
ehartford commentedon Jun 12, 2026
Implemented in a1076dc. Added debug-mode bounds checks for fixed arrays, slices, strings, and Vec indexing; added MIR slice rvalue lowering for array/slice ranges; fixed leading-open range parsing (
arr[..]); taught Sema/MIR/C emit about slice.len(); and added emit-C slice structs plus slice indexing/range emission. Positive and negative behavior coverage added for in-bounds array/slice/Vec use, array load/store OOB, negative array index, slice element OOB, slice range OOB, and Vec OOB. Verification passed:with build,with build :fixpoint,with build :test,with build :test-green; generated C for the in-bounds slice test also emits and compiles to an object.