From cfec1270d7121bbd022a51227322d282d65e8637 Mon Sep 17 00:00:00 2001 From: William Zijie Zhang Date: Mon, 9 Nov 2026 13:36:11 -0800 Subject: [PATCH] Raise instead of crashing on misuse of the problem bindings - Derivative calls before their init step segfaulted; they now raise RuntimeError naming the init call to make. - u, lagrange and theta lengths were unchecked, so the engine read past the buffer and returned wrong values; they now raise ValueError. - update_params with no parameters, and register_params with a constant node, called exit(1) in the engine; they now raise ValueError first. Co-Authored-By: Claude Opus 5.5 --- sparsediffpy/_bindings/problem/common.h | 26 ++++++ .../_bindings/problem/constraint_forward.h | 3 +- sparsediffpy/_bindings/problem/gradient.h | 6 ++ sparsediffpy/_bindings/problem/hessian.h | 21 ++++- sparsediffpy/_bindings/problem/jacobian.h | 12 +++ .../_bindings/problem/objective_forward.h | 3 +- .../_bindings/problem/register_params.h | 8 ++ .../_bindings/problem/update_params.h | 20 ++++- tests/test_bindings.py | 90 +++++++++++++++++++ 9 files changed, 179 insertions(+), 10 deletions(-) create mode 100644 tests/test_bindings.py diff --git a/sparsediffpy/_bindings/problem/common.h b/sparsediffpy/_bindings/problem/common.h index 43e44c7..e390373 100644 --- a/sparsediffpy/_bindings/problem/common.h +++ b/sparsediffpy/_bindings/problem/common.h @@ -6,6 +6,7 @@ #include #include "problem.h" +#include "subexpr.h" /* Also need expr types for capsule handling */ #include "../atoms/common.h" @@ -21,4 +22,29 @@ static void problem_capsule_destructor(PyObject *capsule) } } +/* Convert obj to a contiguous double array of exactly n entries (at least n if + * at_least is set). Returns a new reference, or NULL with an exception set. The + * engine reads these buffers without knowing their length, so a short array + * would make it read past the end. */ +static PyArrayObject *double_array_of_size(PyObject *obj, npy_intp n, int at_least, + const char *name) +{ + PyArrayObject *arr = + (PyArrayObject *) PyArray_FROM_OTF(obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); + if (!arr) + { + return NULL; + } + npy_intp size = PyArray_SIZE(arr); + if (at_least ? size < n : size != n) + { + PyErr_Format(PyExc_ValueError, "%s must have %s%zd entries, got %zd", name, + at_least ? "at least " : "", (Py_ssize_t) n, + (Py_ssize_t) size); + Py_DECREF(arr); + return NULL; + } + return arr; +} + #endif /* PROBLEM_COMMON_H */ diff --git a/sparsediffpy/_bindings/problem/constraint_forward.h b/sparsediffpy/_bindings/problem/constraint_forward.h index 9449db2..ed8c3d5 100644 --- a/sparsediffpy/_bindings/problem/constraint_forward.h +++ b/sparsediffpy/_bindings/problem/constraint_forward.h @@ -20,8 +20,7 @@ static PyObject *py_problem_constraint_forward(PyObject *self, PyObject *args) return NULL; } - PyArrayObject *u_array = - (PyArrayObject *) PyArray_FROM_OTF(u_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); + PyArrayObject *u_array = double_array_of_size(u_obj, prob->n_vars, 0, "u"); if (!u_array) { return NULL; diff --git a/sparsediffpy/_bindings/problem/gradient.h b/sparsediffpy/_bindings/problem/gradient.h index 34b9b34..b9d1f98 100644 --- a/sparsediffpy/_bindings/problem/gradient.h +++ b/sparsediffpy/_bindings/problem/gradient.h @@ -19,6 +19,12 @@ static PyObject *py_problem_gradient(PyObject *self, PyObject *args) return NULL; } + if (!prob->jacobian) + { + PyErr_SetString(PyExc_RuntimeError, "call problem_init_derivatives first"); + return NULL; + } + problem_gradient(prob); npy_intp size = prob->n_vars; diff --git a/sparsediffpy/_bindings/problem/hessian.h b/sparsediffpy/_bindings/problem/hessian.h index cbc6428..7ecc0fe 100644 --- a/sparsediffpy/_bindings/problem/hessian.h +++ b/sparsediffpy/_bindings/problem/hessian.h @@ -34,9 +34,15 @@ static PyObject *py_problem_hessian(PyObject *self, PyObject *args) return NULL; } + if (!prob->lagrange_hessian) + { + PyErr_SetString(PyExc_RuntimeError, "call problem_init_derivatives first"); + return NULL; + } + /* Convert lagrange to contiguous C array */ - PyArrayObject *lagrange_arr = (PyArrayObject *) PyArray_FROM_OTF( - lagrange_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); + PyArrayObject *lagrange_arr = double_array_of_size( + lagrange_obj, prob->total_constraint_size, 0, "lagrange"); if (!lagrange_arr) { return NULL; @@ -182,9 +188,16 @@ static PyObject *py_problem_eval_hessian_vals_coo(PyObject *self, PyObject *args return NULL; } + if (!prob->lagrange_hessian_coo) + { + PyErr_SetString(PyExc_RuntimeError, + "call problem_init_hessian_coo_lower_triangular first"); + return NULL; + } + /* Convert lagrange to contiguous C array */ - PyArrayObject *lagrange_arr = (PyArrayObject *) PyArray_FROM_OTF( - lagrange_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); + PyArrayObject *lagrange_arr = double_array_of_size( + lagrange_obj, prob->total_constraint_size, 0, "lagrange"); if (!lagrange_arr) { return NULL; diff --git a/sparsediffpy/_bindings/problem/jacobian.h b/sparsediffpy/_bindings/problem/jacobian.h index 73dc6f9..89941a9 100644 --- a/sparsediffpy/_bindings/problem/jacobian.h +++ b/sparsediffpy/_bindings/problem/jacobian.h @@ -19,6 +19,12 @@ static PyObject *py_problem_jacobian(PyObject *self, PyObject *args) return NULL; } + if (!prob->jacobian) + { + PyErr_SetString(PyExc_RuntimeError, "call problem_init_derivatives first"); + return NULL; + } + if (prob->n_constraints == 0) { /* Return empty CSR components */ @@ -161,6 +167,12 @@ static PyObject *py_problem_eval_jacobian_vals(PyObject *self, PyObject *args) return NULL; } + if (!prob->jacobian) + { + PyErr_SetString(PyExc_RuntimeError, "call problem_init_derivatives first"); + return NULL; + } + problem_jacobian(prob); CSR_matrix *jac = prob->jacobian; diff --git a/sparsediffpy/_bindings/problem/objective_forward.h b/sparsediffpy/_bindings/problem/objective_forward.h index 51b1060..44491f5 100644 --- a/sparsediffpy/_bindings/problem/objective_forward.h +++ b/sparsediffpy/_bindings/problem/objective_forward.h @@ -20,8 +20,7 @@ static PyObject *py_problem_objective_forward(PyObject *self, PyObject *args) return NULL; } - PyArrayObject *u_array = - (PyArrayObject *) PyArray_FROM_OTF(u_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); + PyArrayObject *u_array = double_array_of_size(u_obj, prob->n_vars, 0, "u"); if (!u_array) { return NULL; diff --git a/sparsediffpy/_bindings/problem/register_params.h b/sparsediffpy/_bindings/problem/register_params.h index 346eb33..9b0369a 100644 --- a/sparsediffpy/_bindings/problem/register_params.h +++ b/sparsediffpy/_bindings/problem/register_params.h @@ -49,6 +49,14 @@ static PyObject *py_problem_register_params(PyObject *self, PyObject *args) "invalid parameter capsule in list"); return NULL; } + /* the engine calls exit(1) on a constant node */ + if (((parameter_expr *) node)->param_id == PARAM_FIXED) + { + free(param_nodes); + PyErr_SetString(PyExc_ValueError, + "constant node passed as a parameter"); + return NULL; + } param_nodes[i] = node; } diff --git a/sparsediffpy/_bindings/problem/update_params.h b/sparsediffpy/_bindings/problem/update_params.h index 547de0c..843f974 100644 --- a/sparsediffpy/_bindings/problem/update_params.h +++ b/sparsediffpy/_bindings/problem/update_params.h @@ -21,8 +21,24 @@ static PyObject *py_problem_update_params(PyObject *self, PyObject *args) return NULL; } - PyArrayObject *theta_array = (PyArrayObject *) PyArray_FROM_OTF( - theta_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); + /* the engine calls exit(1) when no parameters are registered */ + if (prob->n_param_nodes == 0) + { + PyErr_SetString(PyExc_ValueError, "no parameters registered"); + return NULL; + } + + /* each node copies theta[param_id : param_id + size] */ + npy_intp theta_size = 0; + for (int i = 0; i < prob->n_param_nodes; i++) + { + expr *node = prob->param_nodes[i]; + npy_intp end = ((parameter_expr *) node)->param_id + node->size; + theta_size = end > theta_size ? end : theta_size; + } + + PyArrayObject *theta_array = + double_array_of_size(theta_obj, theta_size, 1, "theta"); if (!theta_array) { return NULL; diff --git a/tests/test_bindings.py b/tests/test_bindings.py new file mode 100644 index 0000000..66fa3f0 --- /dev/null +++ b/tests/test_bindings.py @@ -0,0 +1,90 @@ +"""Misuse of the raw problem bindings raises instead of crashing the process.""" + +import numpy as np +import pytest + +from sparsediffpy import _sparsediffengine as C + +N = 3 + + +def make_problem(param=None): + """minimize sum(exp(x)) subject to exp(x), optionally with x scaled by param.""" + x = C.make_variable(N, 1, 0, N) + if param is not None: + x = C.make_param_scalar_mult(param, x) + con = C.make_exp(C.make_variable(N, 1, 0, N)) + return C.make_problem(C.make_sum(C.make_exp(x), -1), [con], False) + + +def forwarded(prob): + u = np.ones(N) + C.problem_objective_forward(prob, u) + C.problem_constraint_forward(prob, u) + return prob + + +@pytest.mark.parametrize("call", [ + lambda p: C.problem_gradient(p), + lambda p: C.problem_jacobian(p), + lambda p: C.problem_eval_jacobian_vals(p), + lambda p: C.problem_hessian(p, 1.0, np.ones(N)), +], ids=["gradient", "jacobian", "eval_jacobian_vals", "hessian"]) +def test_derivatives_before_init(call): + with pytest.raises(RuntimeError, match="problem_init_derivatives"): + call(forwarded(make_problem())) + + +def test_hessian_coo_values_before_coo_init(): + prob = forwarded(make_problem()) + C.problem_init_derivatives(prob) + with pytest.raises(RuntimeError, match="coo"): + C.problem_eval_hessian_vals_coo(prob, 1.0, np.ones(N)) + + +@pytest.mark.parametrize("call", [ + lambda p: C.problem_objective_forward(p, np.ones(N - 1)), + lambda p: C.problem_constraint_forward(p, np.ones(N + 1)), + lambda p: C.problem_hessian(p, 1.0, np.ones(1)), +], ids=["objective_u", "constraint_u", "lagrange"]) +def test_wrong_lengths(call): + prob = forwarded(make_problem()) + C.problem_init_derivatives(prob) + C.problem_gradient(prob) + C.problem_jacobian(prob) + with pytest.raises(ValueError, match="must have"): + call(prob) + + +def test_update_params_without_params(): + with pytest.raises(ValueError, match="no parameters"): + C.problem_update_params(make_problem(), np.ones(1)) + + +def test_short_theta(): + param = C.make_parameter(1, 1, 2, N, np.ones(1)) # reads theta[2] + prob = make_problem(param) + C.problem_register_params(prob, [param]) + with pytest.raises(ValueError, match="at least 3"): + C.problem_update_params(prob, np.ones(2)) + C.problem_update_params(prob, np.full(3, 2.0)) + assert C.problem_objective_forward(prob, np.ones(N)) == pytest.approx( + N * np.exp(2.0)) + + +def test_register_constant_as_parameter(): + const = C.make_parameter(1, 1, -1, N, np.ones(1)) + with pytest.raises(ValueError, match="constant"): + C.problem_register_params(make_problem(), [const]) + + +def test_correct_sequence_still_works(): + prob = make_problem() + C.problem_init_derivatives(prob) + forwarded(prob) + np.testing.assert_allclose(C.problem_gradient(prob), np.e * np.ones(N)) + data, _, _, shape = C.problem_jacobian(prob) + assert shape == (N, N) + np.testing.assert_allclose(data, np.e * np.ones(N)) + data, _, _, _ = C.problem_hessian(prob, 1.0, np.ones(N)) + np.testing.assert_allclose(data, 2 * np.e * np.ones(N))