diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-06-07 15:41:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-07 15:41:39 (GMT) |
commit | 357626676035d2bb12ea92e0edf3c7b383d627ec (patch) | |
tree | 576271214cbdf8e351d3987233fbeb5af3531d52 /Modules | |
parent | dd492d9c352eb0fa2bc48ea9acc47e47a7fab8a0 (diff) | |
download | cpython-357626676035d2bb12ea92e0edf3c7b383d627ec.zip cpython-357626676035d2bb12ea92e0edf3c7b383d627ec.tar.gz cpython-357626676035d2bb12ea92e0edf3c7b383d627ec.tar.bz2 |
bpo-37169: Rewrite _PyObject_IsFreed() unit tests (GH-13888) (GH-13895)
Replace two Python function calls with a single one to ensure that no
memory allocation is done between the invalid object is created and
when _PyObject_IsFreed() is called.
(cherry picked from commit 3bf0f3ad2046ac674d8e8a2c074a5a8b3327797d)
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_testcapimodule.c | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c index c1ae237..07aadea 100644 --- a/Modules/_testcapimodule.c +++ b/Modules/_testcapimodule.c @@ -4489,15 +4489,17 @@ test_pymem_getallocatorsname(PyObject *self, PyObject *args) static PyObject* -pyobject_is_freed(PyObject *self, PyObject *op) +test_pyobject_is_freed(const char *test_name, PyObject *op) { - int res = _PyObject_IsFreed(op); - return PyBool_FromLong(res); + if (!_PyObject_IsFreed(op)) { + return raiseTestError(test_name, "object is not seen as freed"); + } + Py_RETURN_NONE; } static PyObject* -pyobject_uninitialized(PyObject *self, PyObject *args) +check_pyobject_uninitialized_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) { PyObject *op = (PyObject *)PyObject_Malloc(sizeof(PyObject)); if (op == NULL) { @@ -4506,12 +4508,12 @@ pyobject_uninitialized(PyObject *self, PyObject *args) /* Initialize reference count to avoid early crash in ceval or GC */ Py_REFCNT(op) = 1; /* object fields like ob_type are uninitialized! */ - return op; + return test_pyobject_is_freed("check_pyobject_uninitialized_is_freed", op); } static PyObject* -pyobject_forbidden_bytes(PyObject *self, PyObject *args) +check_pyobject_forbidden_bytes_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) { /* Allocate an incomplete PyObject structure: truncate 'ob_type' field */ PyObject *op = (PyObject *)PyObject_Malloc(offsetof(PyObject, ob_type)); @@ -4522,12 +4524,12 @@ pyobject_forbidden_bytes(PyObject *self, PyObject *args) Py_REFCNT(op) = 1; /* ob_type field is after the memory block: part of "forbidden bytes" when using debug hooks on memory allocatrs! */ - return op; + return test_pyobject_is_freed("check_pyobject_forbidden_bytes_is_freed", op); } static PyObject* -pyobject_freed(PyObject *self, PyObject *args) +check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args)) { PyObject *op = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type); if (op == NULL) { @@ -4537,7 +4539,7 @@ pyobject_freed(PyObject *self, PyObject *args) /* Reset reference count to avoid early crash in ceval or GC */ Py_REFCNT(op) = 1; /* object memory is freed! */ - return op; + return test_pyobject_is_freed("check_pyobject_freed_is_freed", op); } @@ -5264,10 +5266,9 @@ static PyMethodDef TestMethods[] = { {"pymem_api_misuse", pymem_api_misuse, METH_NOARGS}, {"pymem_malloc_without_gil", pymem_malloc_without_gil, METH_NOARGS}, {"pymem_getallocatorsname", test_pymem_getallocatorsname, METH_NOARGS}, - {"pyobject_is_freed", (PyCFunction)(void(*)(void))pyobject_is_freed, METH_O}, - {"pyobject_uninitialized", pyobject_uninitialized, METH_NOARGS}, - {"pyobject_forbidden_bytes", pyobject_forbidden_bytes, METH_NOARGS}, - {"pyobject_freed", pyobject_freed, METH_NOARGS}, + {"check_pyobject_uninitialized_is_freed", check_pyobject_uninitialized_is_freed, METH_NOARGS}, + {"check_pyobject_forbidden_bytes_is_freed", check_pyobject_forbidden_bytes_is_freed, METH_NOARGS}, + {"check_pyobject_freed_is_freed", check_pyobject_freed_is_freed, METH_NOARGS}, {"pyobject_malloc_without_gil", pyobject_malloc_without_gil, METH_NOARGS}, {"tracemalloc_track", tracemalloc_track, METH_VARARGS}, {"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS}, |