Skip to content

Commit d4e4ef1

Browse files
authored
[3.10] bpo-46724: Use JUMP_ABSOLUTE for all backward jumps. (GH-31326) (GH-31354)
1 parent 3d407b9 commit d4e4ef1

File tree

6 files changed

+47
-14
lines changed

6 files changed

+47
-14
lines changed

Lib/test/test_dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ def jumpy():
10441044
Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
10451045
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=21, argval=42, argrepr='to 42', offset=36, starts_line=None, is_jump_target=False),
10461046
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
1047-
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False),
1047+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=5, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False),
10481048
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=42, starts_line=7, is_jump_target=True),
10491049
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True),
10501050
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=46, starts_line=None, is_jump_target=False),
@@ -1069,7 +1069,7 @@ def jumpy():
10691069
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False),
10701070
Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False),
10711071
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=46, argval=92, argrepr='to 92', offset=88, starts_line=None, is_jump_target=False),
1072-
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False),
1072+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=6, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False),
10731073
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=92, starts_line=11, is_jump_target=True),
10741074
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=28, argval=56, argrepr='to 56', offset=94, starts_line=None, is_jump_target=False),
10751075
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=96, starts_line=19, is_jump_target=True),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make sure that all backwards jumps use the ``JUMP_ABSOLUTE`` instruction, rather
2+
than ``JUMP_FORWARD`` with an argument of ``(2**32)+offset``.

Python/compile.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ typedef struct basicblock_ {
128128
unsigned b_nofallthrough : 1;
129129
/* Basic block exits scope (it ends with a return or raise) */
130130
unsigned b_exit : 1;
131+
/* Used by compiler passes to mark whether they have visited a basic block. */
132+
unsigned b_visited : 1;
131133
/* depth of stack upon entry of block, computed by stackdepth() */
132134
int b_startdepth;
133135
/* instruction offset for block, computed by assemble_jump_offsets() */
@@ -6701,6 +6703,31 @@ assemble_emit(struct assembler *a, struct instr *i)
67016703
return 1;
67026704
}
67036705

6706+
static void
6707+
normalize_jumps(struct assembler *a)
6708+
{
6709+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6710+
b->b_visited = 0;
6711+
}
6712+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6713+
b->b_visited = 1;
6714+
if (b->b_iused == 0) {
6715+
continue;
6716+
}
6717+
struct instr *last = &b->b_instr[b->b_iused-1];
6718+
if (last->i_opcode == JUMP_ABSOLUTE) {
6719+
if (last->i_target->b_visited == 0) {
6720+
last->i_opcode = JUMP_FORWARD;
6721+
}
6722+
}
6723+
if (last->i_opcode == JUMP_FORWARD) {
6724+
if (last->i_target->b_visited == 1) {
6725+
last->i_opcode = JUMP_ABSOLUTE;
6726+
}
6727+
}
6728+
}
6729+
}
6730+
67046731
static void
67056732
assemble_jump_offsets(struct assembler *a, struct compiler *c)
67066733
{
@@ -7137,6 +7164,10 @@ assemble(struct compiler *c, int addNone)
71377164
}
71387165
propagate_line_numbers(&a);
71397166
guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
7167+
7168+
/* Order of basic blocks must have been determined by now */
7169+
normalize_jumps(&a);
7170+
71407171
/* Can't modify the bytecode after computing jump offsets. */
71417172
assemble_jump_offsets(&a, c);
71427173

Python/importlib.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/importlib_external.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/importlib_zipimport.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const unsigned char _Py_M__zipimport[] = {
130130
3,141,2,130,1,124,5,125,1,124,3,160,13,124,6,161,
131131
1,1,0,89,0,110,15,119,0,124,4,106,14,100,6,64,
132132
0,100,7,107,3,114,89,116,4,100,5,124,1,100,3,141,
133-
2,130,1,113,91,113,33,122,6,116,15,124,1,25,0,125,
133+
2,130,1,110,1,113,33,122,6,116,15,124,1,25,0,125,
134134
7,87,0,110,17,4,0,116,16,121,114,1,0,1,0,1,
135135
0,116,17,124,1,131,1,125,7,124,7,116,15,124,1,60,
136136
0,89,0,110,1,119,0,124,7,124,0,95,18,124,1,124,
@@ -653,7 +653,7 @@ const unsigned char _Py_M__zipimport[] = {
653653
100,2,141,2,130,1,119,0,9,0,124,1,160,7,100,16,
654654
161,1,125,3,116,8,124,3,131,1,100,5,107,0,144,1,
655655
114,55,116,14,100,17,131,1,130,1,124,3,100,0,100,5,
656-
133,2,25,0,100,18,107,3,144,1,114,66,144,2,113,85,
656+
133,2,25,0,100,18,107,3,144,1,114,66,144,1,110,19,
657657
116,8,124,3,131,1,100,16,107,3,144,1,114,77,116,14,
658658
100,17,131,1,130,1,116,15,124,3,100,19,100,20,133,2,
659659
25,0,131,1,125,13,116,15,124,3,100,20,100,9,133,2,
@@ -1004,9 +1004,9 @@ const unsigned char _Py_M__zipimport[] = {
10041004
1,0,1,0,89,0,113,9,119,0,124,8,100,4,25,0,
10051005
125,9,116,8,124,0,106,4,124,8,131,2,125,10,100,0,
10061006
125,11,124,5,114,91,122,10,116,9,124,0,124,9,124,7,
1007-
124,1,124,10,131,5,125,11,87,0,113,96,4,0,116,10,
1007+
124,1,124,10,131,5,125,11,87,0,110,25,4,0,116,10,
10081008
121,90,1,0,125,12,1,0,122,8,124,12,125,3,87,0,
1009-
89,0,100,0,125,12,126,12,113,96,100,0,125,12,126,12,
1009+
89,0,100,0,125,12,126,12,110,10,100,0,125,12,126,12,
10101010
119,1,119,0,116,11,124,9,124,10,131,2,125,11,124,11,
10111011
100,0,117,0,114,101,113,9,124,8,100,4,25,0,125,9,
10121012
124,11,124,6,124,9,102,3,2,0,1,0,83,0,124,3,

0 commit comments

Comments
 (0)