Skip to content

[3.10] bpo-46724: Use JUMP_ABSOLUTE for all backward jumps. (GH-31326) #31354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/test_dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ def jumpy():
Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=21, argval=42, argrepr='to 42', offset=36, starts_line=None, is_jump_target=False),
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False),
Instruction(opname='JUMP_FORWARD', opcode=110, arg=5, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False),
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=42, starts_line=7, is_jump_target=True),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True),
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),
Expand All @@ -1069,7 +1069,7 @@ def jumpy():
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False),
Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False),
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=46, argval=92, argrepr='to 92', offset=88, starts_line=None, is_jump_target=False),
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False),
Instruction(opname='JUMP_FORWARD', opcode=110, arg=6, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False),
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=92, starts_line=11, is_jump_target=True),
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=28, argval=56, argrepr='to 56', offset=94, starts_line=None, is_jump_target=False),
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=96, starts_line=19, is_jump_target=True),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make sure that all backwards jumps use the ``JUMP_ABSOLUTE`` instruction, rather
than ``JUMP_FORWARD`` with an argument of ``(2**32)+offset``.
31 changes: 31 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ typedef struct basicblock_ {
unsigned b_nofallthrough : 1;
/* Basic block exits scope (it ends with a return or raise) */
unsigned b_exit : 1;
/* Used by compiler passes to mark whether they have visited a basic block. */
unsigned b_visited : 1;
/* depth of stack upon entry of block, computed by stackdepth() */
int b_startdepth;
/* instruction offset for block, computed by assemble_jump_offsets() */
Expand Down Expand Up @@ -6701,6 +6703,31 @@ assemble_emit(struct assembler *a, struct instr *i)
return 1;
}

static void
normalize_jumps(struct assembler *a)
{
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
b->b_visited = 0;
}
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
b->b_visited = 1;
if (b->b_iused == 0) {
continue;
}
struct instr *last = &b->b_instr[b->b_iused-1];
if (last->i_opcode == JUMP_ABSOLUTE) {
if (last->i_target->b_visited == 0) {
last->i_opcode = JUMP_FORWARD;
}
}
if (last->i_opcode == JUMP_FORWARD) {
if (last->i_target->b_visited == 1) {
last->i_opcode = JUMP_ABSOLUTE;
}
}
}
}

static void
assemble_jump_offsets(struct assembler *a, struct compiler *c)
{
Expand Down Expand Up @@ -7137,6 +7164,10 @@ assemble(struct compiler *c, int addNone)
}
propagate_line_numbers(&a);
guarantee_lineno_for_exits(&a, c->u->u_firstlineno);

/* Order of basic blocks must have been determined by now */
normalize_jumps(&a);

/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(&a, c);

Expand Down
8 changes: 4 additions & 4 deletions Python/importlib.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Python/importlib_external.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Python/importlib_zipimport.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const unsigned char _Py_M__zipimport[] = {
3,141,2,130,1,124,5,125,1,124,3,160,13,124,6,161,
1,1,0,89,0,110,15,119,0,124,4,106,14,100,6,64,
0,100,7,107,3,114,89,116,4,100,5,124,1,100,3,141,
2,130,1,113,91,113,33,122,6,116,15,124,1,25,0,125,
2,130,1,110,1,113,33,122,6,116,15,124,1,25,0,125,
7,87,0,110,17,4,0,116,16,121,114,1,0,1,0,1,
0,116,17,124,1,131,1,125,7,124,7,116,15,124,1,60,
0,89,0,110,1,119,0,124,7,124,0,95,18,124,1,124,
Expand Down Expand Up @@ -653,7 +653,7 @@ const unsigned char _Py_M__zipimport[] = {
100,2,141,2,130,1,119,0,9,0,124,1,160,7,100,16,
161,1,125,3,116,8,124,3,131,1,100,5,107,0,144,1,
114,55,116,14,100,17,131,1,130,1,124,3,100,0,100,5,
133,2,25,0,100,18,107,3,144,1,114,66,144,2,113,85,
133,2,25,0,100,18,107,3,144,1,114,66,144,1,110,19,
116,8,124,3,131,1,100,16,107,3,144,1,114,77,116,14,
100,17,131,1,130,1,116,15,124,3,100,19,100,20,133,2,
25,0,131,1,125,13,116,15,124,3,100,20,100,9,133,2,
Expand Down Expand Up @@ -1004,9 +1004,9 @@ const unsigned char _Py_M__zipimport[] = {
1,0,1,0,89,0,113,9,119,0,124,8,100,4,25,0,
125,9,116,8,124,0,106,4,124,8,131,2,125,10,100,0,
125,11,124,5,114,91,122,10,116,9,124,0,124,9,124,7,
124,1,124,10,131,5,125,11,87,0,113,96,4,0,116,10,
124,1,124,10,131,5,125,11,87,0,110,25,4,0,116,10,
121,90,1,0,125,12,1,0,122,8,124,12,125,3,87,0,
89,0,100,0,125,12,126,12,113,96,100,0,125,12,126,12,
89,0,100,0,125,12,126,12,110,10,100,0,125,12,126,12,
119,1,119,0,116,11,124,9,124,10,131,2,125,11,124,11,
100,0,117,0,114,101,113,9,124,8,100,4,25,0,125,9,
124,11,124,6,124,9,102,3,2,0,1,0,83,0,124,3,
Expand Down