@@ -144,10 +144,6 @@ incorrect_keys(PyObject *obj, uint32_t version)
144144
145145#define CURRENT_FRAME_IS_INIT_SHIM () (ctx->frame->code == ((PyCodeObject *)&_Py_InitCleanup))
146146
147- #define WITHIN_STACK_BOUNDS () \
148- (CURRENT_FRAME_IS_INIT_SHIM() || (STACK_LEVEL() >= 0 && STACK_LEVEL() <= STACK_SIZE()))
149-
150-
151147#define GETLOCAL (idx ) ((ctx->frame->locals[idx]))
152148
153149#define REPLACE_OP (INST , OP , ARG , OPERAND ) \
@@ -192,6 +188,27 @@ incorrect_keys(PyObject *obj, uint32_t version)
192188
193189#define JUMP_TO_LABEL (label ) goto label;
194190
191+ static int
192+ check_stack_bounds (JitOptContext * ctx , JitOptRef * stack_pointer , int offset , int opcode )
193+ {
194+ int stack_level = (int )(stack_pointer + (offset ) - ctx -> frame -> stack );
195+ int should_check = !CURRENT_FRAME_IS_INIT_SHIM () ||
196+ (opcode == _RETURN_VALUE ) ||
197+ (opcode == _RETURN_GENERATOR ) ||
198+ (opcode == _YIELD_VALUE );
199+ if (should_check && (stack_level < 0 || stack_level > STACK_SIZE ())) {
200+ ctx -> contradiction = true;
201+ ctx -> done = true;
202+ return 1 ;
203+ }
204+ return 0 ;
205+ }
206+
207+ #define CHECK_STACK_BOUNDS (offset ) \
208+ if (check_stack_bounds(ctx, stack_pointer, offset, opcode)) { \
209+ break; \
210+ } \
211+
195212static int
196213optimize_to_bool (
197214 _PyUOpInstruction * this_instr ,
0 commit comments