10000 gh-99110: Initialize frame->previous in init_frame to fix segmentation fault by byllyfish · Pull Request #100182 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-99110: Initialize frame->previous in init_frame to fix segmentation fault #100182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 23, 2022
Prev Previous commit
Next Next commit
Add frame_new function to _testcapimodule.c.
Move frame test from test_python_api.py to test_frame.py.
  • Loading branch information
byllyfish committed Dec 18, 2022
commit 4e70dc50580212e74aac4c12dd3255933fe85224
30 changes: 0 additions & 30 deletions Lib/test/test_ctypes/test_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,36 +81,6 @@ def test_pyobject_repr(self):
self.assertEqual(repr(py_object(42)), "py_object(42)")
self.assertEqual(repr(py_object(object)), "py_object(%r)" % object)

def test_PyFrame_New_f_back(self):
"""Test that accessing `f_back` does not cause a segmentation fault on
a frame created with ctypes (GH-99110)."""
# Adapted from:
# https://naleraphael.github.io/blog/posts/devlog_create_a_builtin_frame_object/

p_memtype = POINTER(c_ulong if sizeof(c_void_p) == 8 else c_uint)
pythonapi.PyFrame_New.argtypes = (
p_memtype, # PyThreadState *tstate
p_memtype, # PyCodeObject *code
py_object, # PyObject *globals
py_object # PyObject *locals
)
pythonapi.PyFrame_New.restype = py_object # PyFrameObject*
pythonapi.PyThreadState_Get.argtypes = None
pythonapi.PyThreadState_Get.restype = p_memtype

def dummy():
pass

frame = pythonapi.PyFrame_New(
pythonapi.PyThreadState_Get(), # thread state
cast(id(dummy.__code__), p_memtype), # a code object
globals(),
locals(),
)

# The following line should not cause a segmentation fault.
self.assertEqual(frame.f_back, None)


if __name__ == "__main__":
unittest.main()
9 changes: 9 additions & 0 deletions Lib/test/test_frame.py
Original file line number Diff line number Diff line change
8000 Expand Up @@ -408,6 +408,15 @@ def test_frame_get_generator(self):
frame = next(gen)
self.assertIs(gen, _testcapi.frame_getgenerator(frame))

def test_frame_fback_api(self):
"""Test that accessing `f_back` does not cause a segmentation fault on
a frame created with `PyFrame_New` (GH-99110)."""
def dummy():
pass

frame = _testcapi.frame_new(dummy.__code__, globals(), locals())
# The following line should not cause a segmentation fault.
self.assertEqual(frame.f_back, None)

if __name__ == "__main__":
unittest.main()
18 changes: 18 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#define PY_SSIZE_T_CLEAN

#include "Python.h"
#include "frameobject.h" // PyFrame_New
#include "marshal.h" // PyMarshal_WriteLongToFile
#include "structmember.h" // for offsetof(), T_OBJECT
#include <float.h> // FLT_MAX
Expand Down Expand Up @@ -2911,6 +2912,22 @@ frame_getlasti(PyObject *self, PyObject *frame)
return PyLong_FromLong(lasti);
}

static PyObject *
frame_new(PyObject *self, PyObject *args)
{
PyObject *code, *globals, *locals;
if (!PyArg_ParseTuple(args, "OOO", &code, &globals, &locals)) {
return NULL;
}
if (!PyCode_Check(code)) {
PyErr_SetString(PyExc_TypeError, "argument must be a code object");
return NULL;
}
PyThreadState *tstate = PyThreadState_Get();

return (PyObject *)PyFrame_New(tstate, (PyCodeObject *)code, globals, locals);
}

static PyObject *
test_frame_getvar(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -3352,6 +3369,7 @@ static PyMethodDef TestMethods[] = {
{"frame_getgenerator", frame_getgenerator, METH_O, NULL},
{"frame_getbuiltins", frame_getbuiltins, METH_O, NULL},
{"frame_getlasti", frame_getlasti, METH_O, NULL},
{"frame_new", frame_new, METH_VARARGS, NULL},
{"frame_getvar", test_frame_getvar, METH_VARARGS, NULL},
{"frame_getvarstring", test_frame_getvarstring, METH_VARARGS, NULL},
{"eval_get_func_name", eval_get_func_name, METH_O, NULL},
Expand Down
2944
0