Repository navigation
Description
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_derivativesExpected 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() # SIGSEGVWrong 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 -piy ** -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 30The 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_flatcasts to int32 before checking, so[2**32+1]and1.7select index 1. Still present.- A failed
quad_form(x, Q)leavesQmarked 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 raiseTypeError, 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
dance858 commentedon Nov 13, 2026
Fixes are open for two of these:
ValueErrorwhere the shape is created (in_check_shapeandNode.__init__), so they no longer reach C.Parameter.valuealiasing the caller's array: Copy Parameter values on assignment #39 copies the value on assignment. This also closes thequad_formsymmetry bypass.Each PR adds a regression test. The other items are still open.
🤖 Generated with Claude Code
dance858 commentedon Nov 13, 2026
Closing: the most important items are handled.
Parameter.valuealiasing (Copy Parameter values on assignment #39).index_flatchecking indices before converting them to int32 (Validate index_flat indices before casting to int32 #40).🤖 Generated with Claude Code