gh-141976: Check stack bounds in JIT optimizer (GH-142201)

This commit is contained in:
Ken Jin 2025-12-05 04:28:08 +08:00 committed by GitHub
parent 2dac9e6016
commit b3bf212898
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 212 additions and 6 deletions

View file

@ -144,10 +144,6 @@ incorrect_keys(PyObject *obj, uint32_t version)
#define CURRENT_FRAME_IS_INIT_SHIM() (ctx->frame->code == ((PyCodeObject *)&_Py_InitCleanup))
#define WITHIN_STACK_BOUNDS() \
(CURRENT_FRAME_IS_INIT_SHIM() || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
#define GETLOCAL(idx) ((ctx->frame->locals[idx]))
#define REPLACE_OP(INST, OP, ARG, OPERAND) \
@ -192,6 +188,27 @@ incorrect_keys(PyObject *obj, uint32_t version)
#define JUMP_TO_LABEL(label) goto label;
static int
check_stack_bounds(JitOptContext *ctx, JitOptRef *stack_pointer, int offset, int opcode)
{
int stack_level = (int)(stack_pointer + (offset) - ctx->frame->stack);
int should_check = !CURRENT_FRAME_IS_INIT_SHIM() ||
(opcode == _RETURN_VALUE) ||
(opcode == _RETURN_GENERATOR) ||
(opcode == _YIELD_VALUE);
if (should_check && (stack_level < 0 || stack_level > STACK_SIZE())) {
ctx->contradiction = true;
ctx->done = true;
return 1;
}
return 0;
}
#define CHECK_STACK_BOUNDS(offset) \
if (check_stack_bounds(ctx, stack_pointer, offset, opcode)) { \
break; \
} \
static int
optimize_to_bool(
_PyUOpInstruction *this_instr,

File diff suppressed because it is too large Load diff