@@ -625,6 +625,7 @@ _PyJit_translate_single_bytecode_to_trace(
625625 int trace_length = _tstate -> jit_tracer_state .prev_state .code_curr_size ;
626626 _PyUOpInstruction * trace = _tstate -> jit_tracer_state .code_buffer ;
627627 int max_length = _tstate -> jit_tracer_state .prev_state .code_max_size ;
628+ int exit_op = stop_tracing_opcode == 0 ? _EXIT_TRACE : stop_tracing_opcode ;
628629
629630 _Py_CODEUNIT * this_instr = _tstate -> jit_tracer_state .prev_state .instr ;
630631 _Py_CODEUNIT * target_instr = this_instr ;
@@ -691,8 +692,11 @@ _PyJit_translate_single_bytecode_to_trace(
691692 }
692693
693694 if (stop_tracing_opcode != 0 ) {
694- ADD_TO_TRACE (stop_tracing_opcode , 0 , 0 , target );
695- goto done ;
695+ // gh-143183: It's important we rewind to the last known proper target.
696+ // The current target might be garbage as stop tracing usually indicates
697+ // we are in something that we can't trace.
698+ DPRINTF (2 , "Told to stop tracing\n" );
699+ goto unsupported ;
696700 }
697701
698702 DPRINTF (2 , "%p %d: %s(%d) %d %d\n" , old_code , target , _PyOpcode_OpName [opcode ], oparg , needs_guard_ip , old_stack_level );
@@ -703,10 +707,6 @@ _PyJit_translate_single_bytecode_to_trace(
703707 }
704708#endif
705709
706- if (opcode == ENTER_EXECUTOR ) {
707- goto full ;
708- }
709-
710710 if (!_tstate -> jit_tracer_state .prev_state .dependencies_still_valid ) {
711711 goto full ;
712712 }
@@ -720,11 +720,6 @@ _PyJit_translate_single_bytecode_to_trace(
720720
721721 if (oparg > 0xFFFF ) {
722722 DPRINTF (2 , "Unsupported: oparg too large\n" );
723- goto unsupported ;
724- }
725-
726- // TODO (gh-140277): The constituent use one extra stack slot. So we need to check for headroom.
727- if (opcode == BINARY_OP_SUBSCR_GETITEM && old_stack_level + 1 > old_code -> co_stacksize ) {
728723 unsupported :
729724 {
730725 // Rewind to previous instruction and replace with _EXIT_TRACE.
@@ -738,14 +733,15 @@ _PyJit_translate_single_bytecode_to_trace(
738733 int32_t old_target = (int32_t )uop_get_target (curr );
739734 curr ++ ;
740735 trace_length ++ ;
741- curr -> opcode = _EXIT_TRACE ;
736+ curr -> opcode = exit_op ;
742737 curr -> format = UOP_FORMAT_TARGET ;
743738 curr -> target = old_target ;
744739 }
745740 goto done ;
746741 }
747742 }
748743
744+
749745 if (opcode == NOP ) {
750746 return 1 ;
751747 }
0 commit comments