Skip to content

--verdict overflows the stack on a ~ parameter of some datatypes: model_at's cycle check misses cycles through ∀-typed fields #1181

Closed

Description

@Vacrin

What you did

bend model.bend --verdict

What happened

$ bend model.bend --check-only
ALL PROOFS CHECK
Use --verdict for mathematical validity.
$ bend model.bend --verdict
Error: the machine stack overflowed (a deep recursion, or a literal too large to expand)

The file

import Base

type W<-F: Nat -> Type> is Type:
  C{g: Nat -> W<F>}

def f(~w: W<(n => Nat)>, y: Nat) -> Nat:
  y

A root with a ~ parameter needs a model of the parameter's type. model_at
(safe.ts#L431)
builds one by walking constructors, and stops cycles with a path of datatype keys
(L446).
The key is term_key(term_lower(F, d))
(L443).
It includes the levels of binders inside F's arguments, so the same datatype gets a different key at
depth d and at d+1. The ∀-typed field g: Nat -> W<F> bumps d
(L439),
so the path never repeats. This isn't a deep-but-finite recursion like #993: the walk never ends.

Control: with W<-F: Type> at W<Nat> (no binder in the argument), the same program gets
ALL PROOFS CHECK from --verdict.

A second shape grows memory instead. In a nested datatype, the keys grow without bound, and
constructor-first order tries NCons forever:

import Base

type Nest<-A: Type> is Type:
  NCons{x: A, rest: Nest<Pair(A, A)>}
  NNil{}

def f(~n: Nest<Nat>, y: Nat) -> Nat:
  y

--verdict on this passed 32 GB before it was killed. Run it only under a memory cap.

Fix direction. Key a datatype without its binder levels (or lower it at a fixed depth), and bound
the walk, treating exhaustion as "no model" (out of scope) instead of crashing.

Impact. A crash, or unbounded memory, on programs bend2 accepts. It fails closed: no verdict is
printed. Like any unbounded run, it is a resource-exhaustion risk for tools that run --verdict on
untrusted files without a cap.

bend --version

bend 2.0.32

uname -sm

Linux x86_64

clang --version (the first line)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions