-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-139540: Fix executor deallocation crash when JIT compilation fails #139952
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Fix memory leak in JIT executor on compilation failure | ||
|
||
The executor object passed to the TIER1_TO_TIER2 macro was not | ||
decremented if the Tier 2 code returned with an exception set but | ||
without a fatal error (i.e., not returning NULL). | ||
|
||
This change moves the Py_DECREF call inside the TIER1_TO_TIER2 | ||
macro to ensure the executor's reference count is always decremented | ||
after its execution, regardless of the outcome. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,12 +351,12 @@ _PyFrame_SetStackPointer(frame, stack_pointer) | |
|
||
/* Tier-switching macros. */ | ||
|
||
#define TIER1_TO_TIER2(EXECUTOR) \ | ||
#define TIER1_TO_TIER2(EXECUTOR) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this changed? please revert. |
||
do { \ | ||
OPT_STAT_INC(traces_executed); \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this changed? please revert. |
||
next_instr = _Py_jit_entry((EXECUTOR), frame, stack_pointer, tstate); \ | ||
frame = tstate->current_frame; \ | ||
stack_pointer = _PyFrame_GetStackPointer(frame); \ | ||
Py_DECREF(EXECUTOR); \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should we decref it here? |
||
if (next_instr == NULL) { \ | ||
next_instr = frame->instr_ptr; \ | ||
JUMP_TO_LABEL(error); \ | ||
|
@@ -413,4 +413,3 @@ check_periodics(PyThreadState *tstate) { | |
} | ||
return 0; | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1202,6 +1202,7 @@ make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFil | |
if (executor == NULL) { | ||
return NULL; | ||
} | ||
_PyObject_GC_TRACK(executor); | ||
|
||
/* Initialize exits */ | ||
_PyExecutorObject *cold = _PyExecutor_GetColdExecutor(); | ||
|
@@ -1257,7 +1258,6 @@ make_executor_from_uops(_PyUOpInstruction *buffer, int length, const _PyBloomFil | |
return NULL; | ||
} | ||
#endif | ||
_PyObject_GC_TRACK(executor); | ||
return executor; | ||
} | ||
|
||
|
@@ -1507,6 +1507,7 @@ _PyExecutor_GetColdExecutor(void) | |
if (cold == NULL) { | ||
Py_FatalError("Cannot allocate core JIT code"); | ||
} | ||
_PyObject_GC_TRACK(cold); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No this was correct, my bad. I didn't read the function correctly. The problem is that we're setting it immortal afterwards and I'm not sure it will be happy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, thanks for the correction. The intent was to track it for consistency immediately after allocation. My assumption is the GC correctly handles a tracked object that later becomes immortal. Is that a safe assumption, or should we explicitly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That I don't know. The problem here is whether we want an immortal cold executor or not. That's why I wanted to wait for Mark's output here, and didn't write a PR already. |
||
((_PyUOpInstruction *)cold->trace)->opcode = _COLD_EXIT; | ||
#ifdef _Py_JIT | ||
cold->jit_code = NULL; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it just to hold a reference to the executor?