Skip to content
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

cases_generator inserts code to save the stack pointer in the incorrect location when statements containing escaping calls are next to preprocessor directives #126211

Closed
mpage opened this issue Oct 30, 2024 · 1 comment
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@mpage
Copy link
Contributor

mpage commented Oct 30, 2024

Bug report

Bug description:

The cases generator will insert code to save the stack pointer at the incorrect location if the statement containing an escaping call is next to a preprocessor directive. For example, running the cases generator on the following code:

#ifdef Py_GIL_DISABLED
int increfed = _Py_TryIncrefCompare(&entries[index].me_value, res_o);
DEOPT_IF(!increfed);
#else
Py_INCREF(res_o);
#endif

will produce

_PyFrame_SetStackPointer(frame, stack_pointer);
#ifdef Py_GIL_DISABLED
int increfed = _Py_TryIncrefCompare(&entries[index].me_value, res_o);
stack_pointer = _PyFrame_GetStackPointer(frame);
DEOPT_IF(!increfed, LOAD_GLOBAL);
#else
Py_INCREF(res_o);
#endif

Notice that _PyFrame_SetStackPointer(frame, stack_pointer); is placed before the #ifdef Py_GIL_DISABLED, rather than after it.

I think this is caused by the way that the we determine the beginning of the statement that includes the escaping call. We search backwards from an escaping call to find the beginning of statement, stopping when we encounter one of a handful of tokens:

while True:
tkn = node.block.tokens[idx-1]
if tkn.kind in {"SEMI", "LBRACE", "RBRACE"}:
break
idx -= 1
assert idx > 0

The set of terminal tokens does not include CMACRO. When we encounter such a token we'll treat it as part of the statement containing the escaping call.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

@mpage mpage added the type-bug An unexpected behavior, bug, or error label Oct 30, 2024
@mpage mpage self-assigned this Oct 30, 2024
mpage added a commit to mpage/cpython that referenced this issue Oct 31, 2024
mpage added a commit that referenced this issue Nov 1, 2024
… escaping calls (#126213)

The cases generator inserts code to save and restore the stack pointer around
statements that contain escaping calls. To find the beginning of such statements,
we would walk backwards from the escaping call until we encountered a token that
was treated as a statement terminator. This set of terminators should include
preprocessor directives.
@brandtbucher
Copy link
Member

This can be closed, right?

@mpage mpage closed this as completed Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants