Unverified 提交 83d029c2 authored 作者: David laid's avatar David laid 提交者: GitHub

Validate that output_subset indices are within bounds in CVM

上级 33d9421a
...@@ -775,8 +775,17 @@ static PyObject *CLazyLinker_call(PyObject *_self, PyObject *args, ...@@ -775,8 +775,17 @@ static PyObject *CLazyLinker_call(PyObject *_self, PyObject *args,
err = 1; err = 1;
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"Some elements of output_subset list are not int"); "Some elements of output_subset list are not int");
break;
}
long idx = PyLong_AsLong(elem);
if (idx < 0 || idx >= self->n_output_vars) {
err = 1;
PyErr_Format(PyExc_IndexError,
"output_subset index %ld is out of range [0, %zd)",
idx, self->n_output_vars);
break;
} }
output_subset[PyLong_AsLong(elem)] = 1; output_subset[idx] = 1;
} }
} }
} }
...@@ -981,7 +990,7 @@ static PyTypeObject lazylinker_ext_CLazyLinkerType = { ...@@ -981,7 +990,7 @@ static PyTypeObject lazylinker_ext_CLazyLinkerType = {
}; };
static PyObject *get_version(PyObject *dummy, PyObject *args) { static PyObject *get_version(PyObject *dummy, PyObject *args) {
PyObject *result = PyFloat_FromDouble(0.31); PyObject *result = PyFloat_FromDouble(0.32);
return result; return result;
} }
......
...@@ -14,7 +14,7 @@ from pytensor.link.c.cmodule import GCC_compiler ...@@ -14,7 +14,7 @@ from pytensor.link.c.cmodule import GCC_compiler
_logger = logging.getLogger(__file__) _logger = logging.getLogger(__file__)
force_compile = False force_compile = False
version = 0.31 # must match constant returned in function get_version() version = 0.32 # must match constant returned in function get_version()
lazylinker_ext: ModuleType | None = None lazylinker_ext: ModuleType | None = None
......
...@@ -544,3 +544,32 @@ def test_Stack_updates(): ...@@ -544,3 +544,32 @@ def test_Stack_updates():
assert res == [np.array(1.0), np.array(2.0)] assert res == [np.array(1.0), np.array(2.0)]
assert storage_map[a][0] == np.array(2.0) assert storage_map[a][0] == np.array(2.0)
@pytest.mark.parametrize(
"linker", [VMLinker(allow_partial_eval=True, use_cloop=False), "cvm"]
)
def test_partial_function_output_subset_oob(linker):
"""Regression test: out-of-bounds output_subset must raise, not segfault."""
x = scalar("input")
y = x**2
f = function(
[x], [y + 7, y - 9, y / 14.0], mode=Mode(optimizer=None, linker=linker)
)
# Index equal to n_output_vars (off-by-one)
with pytest.raises((IndexError, Exception)):
f(3, output_subset=[len(f.output_storage)])
# Large out-of-bounds index
with pytest.raises((IndexError, Exception)):
f(3, output_subset=[100])
# Negative index: the CVM C code must reject it; the Python Stack VM
# delegates to Python list indexing which naturally wraps negatives.
if linker == "cvm":
with pytest.raises((IndexError, Exception)):
f(3, output_subset=[-1])
# Verify the function still works after the error cases
utt.assert_allclose(f(5), np.array([32.0, 16.0, 1.7857142857142858]))
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论