From 56330cea444445161e5aff986ee3f045e316453a Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Tue, 15 Feb 2022 09:35:16 +0000 Subject: [PATCH 1/2] bpo-46724: Use `JUMP_ABSOLUTE` for all backward jumps. (GH-31326) Backport of GH-31326, plus supporting code from 3.11. --- Lib/test/test_dis.py | 4 +-- .../2022-02-14-14-44-06.bpo-46724.jym_K6.rst | 2 ++ Python/compile.c | 31 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 000549f5b6f114..23a171853636f8 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -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), @@ -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), diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst new file mode 100644 index 00000000000000..7324182677abfd --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst @@ -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``. diff --git a/Python/compile.c b/Python/compile.c index a7f62bbcbb33e4..1416eb119c9e76 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -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() */ @@ -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) { @@ -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); From 15fbe6de0e2774e5ab5d0a58f29f2a4dd6d23e94 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Tue, 15 Feb 2022 10:12:36 +0000 Subject: [PATCH 2/2] Regen importlib --- Python/importlib.h | 8 ++++---- Python/importlib_external.h | 8 ++++---- Python/importlib_zipimport.h | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Python/importlib.h b/Python/importlib.h index ab3e69b22b6625..dd1a9f172c04f1 100644 --- a/Python/importlib.h +++ b/Python/importlib.h @@ -782,8 +782,8 @@ const unsigned char _Py_M__importlib_bootstrap[] = { 103,0,110,1,100,2,125,5,124,4,124,0,124,1,124,5, 100,4,141,3,83,0,124,3,100,2,117,0,114,67,116,0, 124,1,100,5,131,2,114,65,122,7,124,1,160,4,124,0, - 161,1,125,3,87,0,113,67,4,0,116,5,121,64,1,0, - 1,0,1,0,100,2,125,3,89,0,113,67,119,0,100,6, + 161,1,125,3,87,0,110,13,4,0,116,5,121,64,1,0, + 1,0,1,0,100,2,125,3,89,0,110,3,119,0,100,6, 125,3,116,6,124,0,124,1,124,2,124,3,100,7,141,4, 83,0,41,8,122,53,82,101,116,117,114,110,32,97,32,109, 111,100,117,108,101,32,115,112,101,99,32,98,97,115,101,100, @@ -812,9 +812,9 @@ const unsigned char _Py_M__importlib_bootstrap[] = { 122,5,124,0,106,4,125,5,87,0,110,11,4,0,116,1, 121,59,1,0,1,0,1,0,100,0,125,5,89,0,110,1, 119,0,124,2,100,0,117,0,114,87,124,5,100,0,117,0, - 114,85,122,5,124,1,106,5,125,2,87,0,113,87,4,0, + 114,85,122,5,124,1,106,5,125,2,87,0,110,13,4,0, 116,1,121,84,1,0,1,0,1,0,100,0,125,2,89,0, - 113,87,119,0,124,5,125,2,122,5,124,0,106,6,125,6, + 110,3,119,0,124,5,125,2,122,5,124,0,106,6,125,6, 87,0,110,11,4,0,116,1,121,103,1,0,1,0,1,0, 100,0,125,6,89,0,110,1,119,0,122,7,116,7,124,0, 106,8,131,1,125,7,87,0,110,11,4,0,116,1,121,122, diff --git a/Python/importlib_external.h b/Python/importlib_external.h index 62e47b13804b40..dcf5505fa74fc3 100644 --- a/Python/importlib_external.h +++ b/Python/importlib_external.h @@ -977,8 +977,8 @@ const unsigned char _Py_M__importlib_bootstrap_external[] = { 8,0,0,0,67,0,0,0,115,54,1,0,0,124,1,100, 1,117,0,114,29,100,2,125,1,116,0,124,2,100,3,131, 2,114,28,122,7,124,2,160,1,124,0,161,1,125,1,87, - 0,113,57,4,0,116,2,121,27,1,0,1,0,1,0,89, - 0,113,57,119,0,110,28,116,3,160,4,124,1,161,1,125, + 0,110,38,4,0,116,2,121,27,1,0,1,0,1,0,89, + 0,110,30,119,0,110,28,116,3,160,4,124,1,161,1,125, 1,116,5,124,1,131,1,115,57,122,9,116,6,116,3,160, 7,161,0,124,1,131,2,125,1,87,0,110,9,4,0,116, 8,121,56,1,0,1,0,1,0,89,0,110,1,119,0,116, @@ -986,11 +986,11 @@ const unsigned char _Py_M__importlib_bootstrap_external[] = { 5,124,4,95,11,124,2,100,1,117,0,114,99,116,12,131, 0,68,0,93,21,92,2,125,5,125,6,124,1,160,13,116, 14,124,6,131,1,161,1,114,96,124,5,124,0,124,1,131, - 2,125,2,124,2,124,4,95,15,1,0,113,99,113,75,100, + 2,125,2,124,2,124,4,95,15,1,0,110,3,113,75,100, 1,83,0,124,3,116,16,117,0,114,131,116,0,124,2,100, 6,131,2,114,130,122,7,124,2,160,17,124,0,161,1,125, 7,87,0,110,9,4,0,116,2,121,124,1,0,1,0,1, - 0,89,0,113,134,119,0,124,7,114,130,103,0,124,4,95, + 0,89,0,110,10,119,0,124,7,114,130,103,0,124,4,95, 18,110,3,124,3,124,4,95,18,124,4,106,18,103,0,107, 2,114,153,124,1,114,153,116,19,124,1,131,1,100,7,25, 0,125,8,124,4,106,18,160,20,124,8,161,1,1,0,124, diff --git a/Python/importlib_zipimport.h b/Python/importlib_zipimport.h index 07c9dd574040f6..3c476841a8c20c 100644 --- a/Python/importlib_zipimport.h +++ b/Python/importlib_zipimport.h @@ -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, @@ -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, @@ -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,