From d9b3d141bc9e3b30d8de8dd772126bb3f7ad3f2f Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Tue, 31 Oct 2023 21:32:12 +0000 Subject: [PATCH 1/2] Replace 'frame->return_offset = 1 + INLINE_CACHE_' with 'frame->return_offset = next_instr - this_instr' as can rely on C compiler to replace it with a constant --- Python/bytecodes.c | 30 ++++++++++++------------------ Python/generated_cases.c.h | 30 ++++++++++++------------------ 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index bb2d491523ca43..50db4ad2667c37 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -657,8 +657,7 @@ dummy_func( STACK_SHRINK(2); new_frame->localsplus[0] = container; new_frame->localsplus[1] = sub; - assert(1 + INLINE_CACHE_ENTRIES_BINARY_SUBSCR == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_BINARY_SUBSCR; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -978,8 +977,8 @@ dummy_func( gen->gi_frame_state = FRAME_EXECUTING; gen->gi_exc_state.previous_item = tstate->exc_info; tstate->exc_info = &gen->gi_exc_state; - assert(1 + INLINE_CACHE_ENTRIES_SEND == next_instr - this_instr); - frame->return_offset = (uint16_t)(1 + INLINE_CACHE_ENTRIES_SEND + oparg); + assert(next_instr - this_instr + oparg <= UINT16_MAX); + frame->return_offset = (uint16_t)(next_instr - this_instr + oparg); DISPATCH_INLINED(gen_frame); } if (Py_IsNone(v) && PyIter_Check(receiver)) { @@ -1016,8 +1015,8 @@ dummy_func( gen->gi_frame_state = FRAME_EXECUTING; gen->gi_exc_state.previous_item = tstate->exc_info; tstate->exc_info = &gen->gi_exc_state; - assert(1 + INLINE_CACHE_ENTRIES_SEND == next_instr - this_instr); - frame->return_offset = (uint16_t)(1 + INLINE_CACHE_ENTRIES_SEND + oparg); + assert(next_instr - this_instr + oparg <= UINT16_MAX); + frame->return_offset = (uint16_t)(next_instr - this_instr + oparg); DISPATCH_INLINED(gen_frame); } @@ -2013,8 +2012,7 @@ dummy_func( // Manipulate stack directly because we exit with DISPATCH_INLINED(). STACK_SHRINK(1); new_frame->localsplus[0] = owner; - assert(1 + INLINE_CACHE_ENTRIES_LOAD_ATTR == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_LOAD_ATTR; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -2040,8 +2038,7 @@ dummy_func( STACK_SHRINK(1); new_frame->localsplus[0] = owner; new_frame->localsplus[1] = Py_NewRef(name); - assert(1 + INLINE_CACHE_ENTRIES_LOAD_ATTR == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_LOAD_ATTR; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -2672,8 +2669,8 @@ dummy_func( tstate->exc_info = &gen->gi_exc_state; assert(next_instr[oparg].op.code == END_FOR || next_instr[oparg].op.code == INSTRUMENTED_END_FOR); - assert(1 + INLINE_CACHE_ENTRIES_FOR_ITER == next_instr - this_instr); - frame->return_offset = (uint16_t)(1 + INLINE_CACHE_ENTRIES_FOR_ITER + oparg); + assert(next_instr - this_instr + oparg <= UINT16_MAX); + frame->return_offset = (uint16_t)(next_instr - this_instr + oparg); DISPATCH_INLINED(gen_frame); } @@ -2982,8 +2979,7 @@ dummy_func( if (new_frame == NULL) { goto error; } - assert(1 + INLINE_CACHE_ENTRIES_CALL == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_CALL; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } /* Callable is not a normal Python function */ @@ -3136,8 +3132,7 @@ dummy_func( } // Manipulate stack and cache directly since we leave using DISPATCH_INLINED(). STACK_SHRINK(oparg + 2); - assert(1 + INLINE_CACHE_ENTRIES_CALL == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_CALL; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -3213,8 +3208,7 @@ dummy_func( for (int i = 0; i < oparg; i++) { init_frame->localsplus[i+1] = args[i]; } - assert(1 + INLINE_CACHE_ENTRIES_CALL == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_CALL; + frame->return_offset = next_instr - this_instr; STACK_SHRINK(oparg+2); _PyFrame_SetStackPointer(frame, stack_pointer); /* Link frames */ diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 8b6ebe4f8ab4af..860d07a6eee89a 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -919,8 +919,7 @@ STACK_SHRINK(2); new_frame->localsplus[0] = container; new_frame->localsplus[1] = sub; - assert(1 + INLINE_CACHE_ENTRIES_BINARY_SUBSCR == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_BINARY_SUBSCR; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -1404,8 +1403,8 @@ gen->gi_frame_state = FRAME_EXECUTING; gen->gi_exc_state.previous_item = tstate->exc_info; tstate->exc_info = &gen->gi_exc_state; - assert(1 + INLINE_CACHE_ENTRIES_SEND == next_instr - this_instr); - frame->return_offset = (uint16_t)(1 + INLINE_CACHE_ENTRIES_SEND + oparg); + assert(next_instr - this_instr + oparg <= UINT16_MAX); + frame->return_offset = (uint16_t)(next_instr - this_instr + oparg); DISPATCH_INLINED(gen_frame); } if (Py_IsNone(v) && PyIter_Check(receiver)) { @@ -1451,8 +1450,8 @@ gen->gi_frame_state = FRAME_EXECUTING; gen->gi_exc_state.previous_item = tstate->exc_info; tstate->exc_info = &gen->gi_exc_state; - assert(1 + INLINE_CACHE_ENTRIES_SEND == next_instr - this_instr); - frame->return_offset = (uint16_t)(1 + INLINE_CACHE_ENTRIES_SEND + oparg); + assert(next_instr - this_instr + oparg <= UINT16_MAX); + frame->return_offset = (uint16_t)(next_instr - this_instr + oparg); DISPATCH_INLINED(gen_frame); } @@ -2904,8 +2903,7 @@ // Manipulate stack directly because we exit with DISPATCH_INLINED(). STACK_SHRINK(1); new_frame->localsplus[0] = owner; - assert(1 + INLINE_CACHE_ENTRIES_LOAD_ATTR == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_LOAD_ATTR; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -2939,8 +2937,7 @@ STACK_SHRINK(1); new_frame->localsplus[0] = owner; new_frame->localsplus[1] = Py_NewRef(name); - assert(1 + INLINE_CACHE_ENTRIES_LOAD_ATTR == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_LOAD_ATTR; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -3841,8 +3838,8 @@ tstate->exc_info = &gen->gi_exc_state; assert(next_instr[oparg].op.code == END_FOR || next_instr[oparg].op.code == INSTRUMENTED_END_FOR); - assert(1 + INLINE_CACHE_ENTRIES_FOR_ITER == next_instr - this_instr); - frame->return_offset = (uint16_t)(1 + INLINE_CACHE_ENTRIES_FOR_ITER + oparg); + assert(next_instr - this_instr + oparg <= UINT16_MAX); + frame->return_offset = (uint16_t)(next_instr - this_instr + oparg); DISPATCH_INLINED(gen_frame); } @@ -4257,8 +4254,7 @@ if (new_frame == NULL) { goto error; } - assert(1 + INLINE_CACHE_ENTRIES_CALL == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_CALL; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } /* Callable is not a normal Python function */ @@ -4511,8 +4507,7 @@ } // Manipulate stack and cache directly since we leave using DISPATCH_INLINED(). STACK_SHRINK(oparg + 2); - assert(1 + INLINE_CACHE_ENTRIES_CALL == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_CALL; + frame->return_offset = next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -4639,8 +4634,7 @@ for (int i = 0; i < oparg; i++) { init_frame->localsplus[i+1] = args[i]; } - assert(1 + INLINE_CACHE_ENTRIES_CALL == next_instr - this_instr); - frame->return_offset = 1 + INLINE_CACHE_ENTRIES_CALL; + frame->return_offset = next_instr - this_instr; STACK_SHRINK(oparg+2); _PyFrame_SetStackPointer(frame, stack_pointer); /* Link frames */ From 956b467baf24eb03ad242e40b90c62066a0aea64 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Tue, 31 Oct 2023 22:12:53 +0000 Subject: [PATCH 2/2] Add explicit narrowing --- Python/bytecodes.c | 12 ++++++------ Python/generated_cases.c.h | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 50db4ad2667c37..47298afc525cf3 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -657,7 +657,7 @@ dummy_func( STACK_SHRINK(2); new_frame->localsplus[0] = container; new_frame->localsplus[1] = sub; - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -2012,7 +2012,7 @@ dummy_func( // Manipulate stack directly because we exit with DISPATCH_INLINED(). STACK_SHRINK(1); new_frame->localsplus[0] = owner; - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -2038,7 +2038,7 @@ dummy_func( STACK_SHRINK(1); new_frame->localsplus[0] = owner; new_frame->localsplus[1] = Py_NewRef(name); - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -2979,7 +2979,7 @@ dummy_func( if (new_frame == NULL) { goto error; } - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } /* Callable is not a normal Python function */ @@ -3132,7 +3132,7 @@ dummy_func( } // Manipulate stack and cache directly since we leave using DISPATCH_INLINED(). STACK_SHRINK(oparg + 2); - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -3208,7 +3208,7 @@ dummy_func( for (int i = 0; i < oparg; i++) { init_frame->localsplus[i+1] = args[i]; } - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; STACK_SHRINK(oparg+2); _PyFrame_SetStackPointer(frame, stack_pointer); /* Link frames */ diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 860d07a6eee89a..db51ebdd8609be 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -919,7 +919,7 @@ STACK_SHRINK(2); new_frame->localsplus[0] = container; new_frame->localsplus[1] = sub; - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -2903,7 +2903,7 @@ // Manipulate stack directly because we exit with DISPATCH_INLINED(). STACK_SHRINK(1); new_frame->localsplus[0] = owner; - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -2937,7 +2937,7 @@ STACK_SHRINK(1); new_frame->localsplus[0] = owner; new_frame->localsplus[1] = Py_NewRef(name); - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -4254,7 +4254,7 @@ if (new_frame == NULL) { goto error; } - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } /* Callable is not a normal Python function */ @@ -4507,7 +4507,7 @@ } // Manipulate stack and cache directly since we leave using DISPATCH_INLINED(). STACK_SHRINK(oparg + 2); - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; DISPATCH_INLINED(new_frame); } @@ -4634,7 +4634,7 @@ for (int i = 0; i < oparg; i++) { init_frame->localsplus[i+1] = args[i]; } - frame->return_offset = next_instr - this_instr; + frame->return_offset = (uint16_t)next_instr - this_instr; STACK_SHRINK(oparg+2); _PyFrame_SetStackPointer(frame, stack_pointer); /* Link frames */