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

markshannon webhook-mailer at python.org
Wed Feb 16 06:26:18 EST 2022


https://github.com/python/cpython/commit/d4e4ef107a9fea257981d7701f023177b704a44f
commit: d4e4ef107a9fea257981d7701f023177b704a44f
branch: 3.10
author: Mark Shannon <mark at hotpy.org>
committer: markshannon <mark at hotpy.org>
date: 2022-02-16T11:26:02Z
summary:

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

files:
A Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst
M Lib/test/test_dis.py
M Python/compile.c
M Python/importlib.h
M Python/importlib_external.h
M Python/importlib_zipimport.h

diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index 000549f5b6f11..23a171853636f 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 0000000000000..7324182677abf
--- /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 a7f62bbcbb33e..1416eb119c9e7 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);
 
diff --git a/Python/importlib.h b/Python/importlib.h
index ab3e69b22b662..dd1a9f172c04f 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 62e47b13804b4..dcf5505fa74fc 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 07c9dd574040f..3c476841a8c20 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,



More information about the Python-checkins mailing list