mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
GH-91079: Revert "GH-91079: Implement C stack limits using addresses, not counters. (GH-130007)" for now (GH130413)
Revert "GH-91079: Implement C stack limits using addresses, not counters. (GH-130007)" for now
Unfortunatlely, the change broke some buildbots.
This reverts commit 2498c22fa0
.
This commit is contained in:
parent
0ff1611574
commit
ef29104f7d
47 changed files with 1466 additions and 1220 deletions
|
@ -738,7 +738,7 @@ class SequenceConstructorVisitor(EmitVisitor):
|
|||
class PyTypesDeclareVisitor(PickleVisitor):
|
||||
|
||||
def visitProduct(self, prod, name):
|
||||
self.emit("static PyObject* ast2obj_%s(struct ast_state *state, void*);" % name, 0)
|
||||
self.emit("static PyObject* ast2obj_%s(struct ast_state *state, struct validator *vstate, void*);" % name, 0)
|
||||
if prod.attributes:
|
||||
self.emit("static const char * const %s_attributes[] = {" % name, 0)
|
||||
for a in prod.attributes:
|
||||
|
@ -759,7 +759,7 @@ class PyTypesDeclareVisitor(PickleVisitor):
|
|||
ptype = "void*"
|
||||
if is_simple(sum):
|
||||
ptype = get_c_type(name)
|
||||
self.emit("static PyObject* ast2obj_%s(struct ast_state *state, %s);" % (name, ptype), 0)
|
||||
self.emit("static PyObject* ast2obj_%s(struct ast_state *state, struct validator *vstate, %s);" % (name, ptype), 0)
|
||||
for t in sum.types:
|
||||
self.visitConstructor(t, name)
|
||||
|
||||
|
@ -1734,8 +1734,8 @@ add_attributes(struct ast_state *state, PyObject *type, const char * const *attr
|
|||
|
||||
/* Conversion AST -> Python */
|
||||
|
||||
static PyObject* ast2obj_list(struct ast_state *state, asdl_seq *seq,
|
||||
PyObject* (*func)(struct ast_state *state, void*))
|
||||
static PyObject* ast2obj_list(struct ast_state *state, struct validator *vstate, asdl_seq *seq,
|
||||
PyObject* (*func)(struct ast_state *state, struct validator *vstate, void*))
|
||||
{
|
||||
Py_ssize_t i, n = asdl_seq_LEN(seq);
|
||||
PyObject *result = PyList_New(n);
|
||||
|
@ -1743,7 +1743,7 @@ static PyObject* ast2obj_list(struct ast_state *state, asdl_seq *seq,
|
|||
if (!result)
|
||||
return NULL;
|
||||
for (i = 0; i < n; i++) {
|
||||
value = func(state, asdl_seq_GET_UNTYPED(seq, i));
|
||||
value = func(state, vstate, asdl_seq_GET_UNTYPED(seq, i));
|
||||
if (!value) {
|
||||
Py_DECREF(result);
|
||||
return NULL;
|
||||
|
@ -1753,7 +1753,7 @@ static PyObject* ast2obj_list(struct ast_state *state, asdl_seq *seq,
|
|||
return result;
|
||||
}
|
||||
|
||||
static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o)
|
||||
static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), struct validator *Py_UNUSED(vstate), void *o)
|
||||
{
|
||||
PyObject *op = (PyObject*)o;
|
||||
if (!op) {
|
||||
|
@ -1765,7 +1765,7 @@ static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o)
|
|||
#define ast2obj_identifier ast2obj_object
|
||||
#define ast2obj_string ast2obj_object
|
||||
|
||||
static PyObject* ast2obj_int(struct ast_state *Py_UNUSED(state), long b)
|
||||
static PyObject* ast2obj_int(struct ast_state *Py_UNUSED(state), struct validator *Py_UNUSED(vstate), long b)
|
||||
{
|
||||
return PyLong_FromLong(b);
|
||||
}
|
||||
|
@ -2014,7 +2014,7 @@ class ObjVisitor(PickleVisitor):
|
|||
def func_begin(self, name):
|
||||
ctype = get_c_type(name)
|
||||
self.emit("PyObject*", 0)
|
||||
self.emit("ast2obj_%s(struct ast_state *state, void* _o)" % (name), 0)
|
||||
self.emit("ast2obj_%s(struct ast_state *state, struct validator *vstate, void* _o)" % (name), 0)
|
||||
self.emit("{", 0)
|
||||
self.emit("%s o = (%s)_o;" % (ctype, ctype), 1)
|
||||
self.emit("PyObject *result = NULL, *value = NULL;", 1)
|
||||
|
@ -2022,15 +2022,17 @@ class ObjVisitor(PickleVisitor):
|
|||
self.emit('if (!o) {', 1)
|
||||
self.emit("Py_RETURN_NONE;", 2)
|
||||
self.emit("}", 1)
|
||||
self.emit('if (Py_EnterRecursiveCall("during ast construction")) {', 1)
|
||||
self.emit("if (++vstate->recursion_depth > vstate->recursion_limit) {", 1)
|
||||
self.emit("PyErr_SetString(PyExc_RecursionError,", 2)
|
||||
self.emit('"maximum recursion depth exceeded during ast construction");', 3)
|
||||
self.emit("return NULL;", 2)
|
||||
self.emit("}", 1)
|
||||
|
||||
def func_end(self):
|
||||
self.emit("Py_LeaveRecursiveCall();", 1)
|
||||
self.emit("vstate->recursion_depth--;", 1)
|
||||
self.emit("return result;", 1)
|
||||
self.emit("failed:", 0)
|
||||
self.emit("Py_LeaveRecursiveCall();", 1)
|
||||
self.emit("vstate->recursion_depth--;", 1)
|
||||
self.emit("Py_XDECREF(value);", 1)
|
||||
self.emit("Py_XDECREF(result);", 1)
|
||||
self.emit("return NULL;", 1)
|
||||
|
@ -2048,7 +2050,7 @@ class ObjVisitor(PickleVisitor):
|
|||
self.visitConstructor(t, i + 1, name)
|
||||
self.emit("}", 1)
|
||||
for a in sum.attributes:
|
||||
self.emit("value = ast2obj_%s(state, o->%s);" % (a.type, a.name), 1)
|
||||
self.emit("value = ast2obj_%s(state, vstate, o->%s);" % (a.type, a.name), 1)
|
||||
self.emit("if (!value) goto failed;", 1)
|
||||
self.emit('if (PyObject_SetAttr(result, state->%s, value) < 0)' % a.name, 1)
|
||||
self.emit('goto failed;', 2)
|
||||
|
@ -2056,7 +2058,7 @@ class ObjVisitor(PickleVisitor):
|
|||
self.func_end()
|
||||
|
||||
def simpleSum(self, sum, name):
|
||||
self.emit("PyObject* ast2obj_%s(struct ast_state *state, %s_ty o)" % (name, name), 0)
|
||||
self.emit("PyObject* ast2obj_%s(struct ast_state *state, struct validator *vstate, %s_ty o)" % (name, name), 0)
|
||||
self.emit("{", 0)
|
||||
self.emit("switch(o) {", 1)
|
||||
for t in sum.types:
|
||||
|
@ -2074,7 +2076,7 @@ class ObjVisitor(PickleVisitor):
|
|||
for field in prod.fields:
|
||||
self.visitField(field, name, 1, True)
|
||||
for a in prod.attributes:
|
||||
self.emit("value = ast2obj_%s(state, o->%s);" % (a.type, a.name), 1)
|
||||
self.emit("value = ast2obj_%s(state, vstate, o->%s);" % (a.type, a.name), 1)
|
||||
self.emit("if (!value) goto failed;", 1)
|
||||
self.emit("if (PyObject_SetAttr(result, state->%s, value) < 0)" % a.name, 1)
|
||||
self.emit('goto failed;', 2)
|
||||
|
@ -2115,7 +2117,7 @@ class ObjVisitor(PickleVisitor):
|
|||
self.emit("for(i = 0; i < n; i++)", depth+1)
|
||||
# This cannot fail, so no need for error handling
|
||||
self.emit(
|
||||
"PyList_SET_ITEM(value, i, ast2obj_{0}(state, ({0}_ty)asdl_seq_GET({1}, i)));".format(
|
||||
"PyList_SET_ITEM(value, i, ast2obj_{0}(state, vstate, ({0}_ty)asdl_seq_GET({1}, i)));".format(
|
||||
field.type,
|
||||
value
|
||||
),
|
||||
|
@ -2124,9 +2126,9 @@ class ObjVisitor(PickleVisitor):
|
|||
)
|
||||
self.emit("}", depth)
|
||||
else:
|
||||
self.emit("value = ast2obj_list(state, (asdl_seq*)%s, ast2obj_%s);" % (value, field.type), depth)
|
||||
self.emit("value = ast2obj_list(state, vstate, (asdl_seq*)%s, ast2obj_%s);" % (value, field.type), depth)
|
||||
else:
|
||||
self.emit("value = ast2obj_%s(state, %s);" % (field.type, value), depth, reflow=False)
|
||||
self.emit("value = ast2obj_%s(state, vstate, %s);" % (field.type, value), depth, reflow=False)
|
||||
|
||||
|
||||
class PartingShots(StaticVisitor):
|
||||
|
@ -2138,8 +2140,28 @@ PyObject* PyAST_mod2obj(mod_ty t)
|
|||
if (state == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
PyObject *result = ast2obj_mod(state, t);
|
||||
|
||||
int starting_recursion_depth;
|
||||
/* Be careful here to prevent overflow. */
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
if (!tstate) {
|
||||
return NULL;
|
||||
}
|
||||
struct validator vstate;
|
||||
vstate.recursion_limit = Py_C_RECURSION_LIMIT;
|
||||
int recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
|
||||
starting_recursion_depth = recursion_depth;
|
||||
vstate.recursion_depth = starting_recursion_depth;
|
||||
|
||||
PyObject *result = ast2obj_mod(state, &vstate, t);
|
||||
|
||||
/* Check that the recursion depth counting balanced correctly */
|
||||
if (result && vstate.recursion_depth != starting_recursion_depth) {
|
||||
PyErr_Format(PyExc_SystemError,
|
||||
"AST constructor recursion depth mismatch (before=%d, after=%d)",
|
||||
starting_recursion_depth, vstate.recursion_depth);
|
||||
return NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -2283,6 +2305,11 @@ def generate_module_def(mod, metadata, f, internal_h):
|
|||
#include "structmember.h"
|
||||
#include <stddef.h>
|
||||
|
||||
struct validator {
|
||||
int recursion_depth; /* current recursion depth */
|
||||
int recursion_limit; /* recursion limit */
|
||||
};
|
||||
|
||||
// Forward declaration
|
||||
static int init_types(void *arg);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue