Skip to content

Stress test of main: int-overflow and deep-nesting segfaults, stale cache on -0.0, Parameter aliasing #37

Closed

Description

@dance858

Found while stress-testing main (08784c2, engine 211019f). Severity: segfault > wrong numbers > bad error.

Segfaults

1. Shapes with > INT_MAX elements. Python validates d1, d2 but not d1·d2 against C int.

s = sp.Scope(); x = s.Variable(1, 1)
sp.compile(sp.broadcast(x, (65536, 65537)))  # Bus error in new_broadcast / problem_init_derivatives

Expected a ValueError.

2. Deep nesting overflows the C stack in forward. 100k nested atoms work, 150k crash. _Builder is iterative, so the expression compiles first.

e = x
for _ in range(150_000): e = sp.sin(e)
sp.compile(e).forward()  # SIGSEGV

Wrong numbers

3. set_values keeps the cache when only the sign of a zero changes. np.array_equal(-0.0, 0.0) is True.

s = sp.Scope(); y = s.Variable(1, 1); x = s.Variable(1, 1)
f = sp.compile(sp.atan2(y, x))
s.set_values([0.0, -1.0]);  f.forward()  # pi
s.set_values([-0.0, -1.0]); f.forward()  # pi (stale), expected -pi

y ** -1 likewise returns +inf instead of -inf. Comparing bytes instead of values would fix it.

4. Parameter.value keeps a view of the caller's array. _flatten_shaped uses ravel('F'), which returns a view for 1-D or F-ordered input. Mutating that array changes p.value without bumping _param_version.

arr = np.ones(2); p = s.Parameter(2, 1, value=arr)
f = sp.compile(sp.sum(p * x)); x.value = [1., 2.]; f.forward()  # 3
arr[:] = 10; x.value = [1., 2.]; f.forward()  # still 3; p.value implies 30

The value updates only after an unrelated Parameter is assigned. The same hole skips the quad_form symmetry check: with an F-ordered Q.value mutated to [[1, 5], [0, 1]], gradient() returns [2, 14] where the true value is [12, 9].

Bad errors / API

5. Read-only caches can be bypassed. J = f.jacobian(); J.data = np.zeros(...) corrupts the cached Jacobian. prob.gradient().setflags(write=True) succeeds because the array owns its data.

6. Dense constants aren't copied. e = A @ x; A[0, 0] = 100; sp.compile(e) picks up the mutation, so compiling the same expression twice can give different results.

7. Minor. Variable(True, 2) is accepted as shape (1, 2). x[5:2] raises "Shape dimensions must be positive". sp.logistic is softplus log(1+eˣ), but the docstring only says "Elementwise logistic".

Earlier reports, rechecked on main

  • index_flat casts to int32 before checking, so [2**32+1] and 1.7 select index 1. Still present.
  • A failed quad_form(x, Q) leaves Q marked symmetric. Still present.
  • entr(0) gives NaN and an inf Jacobian entry. Still present.
  • x[[]] error mentions "dtype float64", and string values are still accepted. Still present. Complex values now raise TypeError, which is fixed.

No problems found

  • 2000 random expression trees matched finite differences for forward, Jacobian and Hessian, including after Parameter updates. They covered every atom, broadcasting, indexing, transpose, dense, sparse and Parameter matmul, reductions, and shared subexpressions.
  • Cache behaviour held under NaN values, variables added after a Problem, shared scopes and expressions, and repeated hessian() calls.
  • Non-canonical CSR constants (duplicate or unsorted entries) gave correct results.

🤖 Generated with Claude Code

Activity

  1. dance858 commented on Nov 13, 2026

    @dance858
    CollaboratorAuthor

    Fixes are open for two of these:

    Each PR adds a regression test. The other items are still open.

    🤖 Generated with Claude Code

  2. dance858 commented on Nov 13, 2026

    @dance858
    CollaboratorAuthor

    Closing: the most important items are handled.

    🤖 Generated with Claude Code

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