Skip to content

Debug-mode stack and memory safety checks #69

Description

@tetsuo-cpp

Summary

Add a debug compilation mode that inserts stack overflow/underflow checks and optional bounds checks on loads/stores, producing clear failures instead of silent corruption or CUDA illegal-memory accesses.

Motivation

Release lowering trusts the programmer:

  • Fixed stack of 256 cells with no underflow/overflow traps
  • @ / ! / shared variants perform raw address arithmetic
  • Failures often surface as wrong answers or GPU faults far from the offending Forth word

A debug mode is the natural complement to compile-time stack-effect checking.

Proposed design

warpforthc kernel.forth -o k.ptx --debug
# or: -O0 --check-stack --check-bounds

Stack checks

  • Before pop: if SP == 0 → trap (e.g. gpu.printf + assert, or write a well-known error code to a status buffer)
  • Before push: if SP >= 255 → trap
  • Optional high-water mark side channel for tests

Memory checks (optional, costlier)

  • When dynamic extents or declared sizes are known, check index in range before load/store
  • Shared vs global: optional assert that S@ addresses fall in shared attribution (hard; best-effort)

Performance

  • Default off for release / demos
  • On for gpu_test debug configurations and local development

Acceptance criteria

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions