Skip to content

Debug-mode bounds checks for array/slice indexing (§4.3a, §4.8a) #428

Closed

Description

@ehartford

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-952 computes projected types for PK_INDEX.
  • src/CodegenDispatch.w:1133-1191 lowers PK_INDEX to 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-4542 lowers for over slices with a loop counter guarded by counter < 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_panic at src/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

  1. Identify the compiler's debug/release mode flag used by codegen. The check must be emitted only for debug-mode builds as specified.
  2. Add a helper in CodegenDispatch.w that, for a PK_INDEX projection, 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.
  3. Before emitting the GEP/load/store for bounded indexable types, branch to a panic block when idx < 0 for signed indices or idx >= len. Use the existing panic substrate rather than undefined behavior.
  4. Ensure stores and address-taking projections go through the same checked helper as loads. The fix must cover both read and write indexing.
  5. 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-stderr once 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, and with 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.

Activity

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions