Skip to content

Commit 1d69d84

Browse files
DinoVfacebook-github-bot
authored andcommitted
Add patch 'fix-nondeterministic-codegen'
Summary: This diff adds a patch to the `3.14` Meta-internal fork. In Python 3.14 NOP instructions can be removed and then that space can later be re-used when a new instruction is added. When this happens not all of the memory is zero'd and when the new instruction is appended not all of the fields are initialized. This leads to some randomness in the generation of the exception handling table. Reviewed By: alexmalyshev Differential Revision: D80723451 fbshipit-source-id: 664ff320dc0254fa3b857b6d2ef59b8b4796cf96
1 parent 767dc35 commit 1d69d84

2 files changed

Lines changed: 44 additions & 42 deletions

File tree

Lib/test/test_dis.py

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -454,52 +454,53 @@ def foo(a: int, b: str) -> str:
454454
"""
455455

456456
dis_traceback = """\
457-
%4d RESUME 0
457+
%4d RESUME 0
458458
459-
%4d NOP
459+
%4d NOP
460460
461-
%4d L1: LOAD_SMALL_INT 1
462-
LOAD_SMALL_INT 0
463-
--> BINARY_OP 11 (/)
464-
POP_TOP
461+
%4d L1: LOAD_SMALL_INT 1
462+
LOAD_SMALL_INT 0
463+
--> BINARY_OP 11 (/)
464+
POP_TOP
465465
466-
%4d L2: LOAD_FAST_CHECK 1 (tb)
467-
RETURN_VALUE
466+
%4d L2: LOAD_FAST_CHECK 1 (tb)
467+
RETURN_VALUE
468468
469-
-- L3: PUSH_EXC_INFO
469+
-- L3: PUSH_EXC_INFO
470470
471-
%4d LOAD_GLOBAL 0 (Exception)
472-
CHECK_EXC_MATCH
473-
POP_JUMP_IF_FALSE 24 (to L7)
474-
NOT_TAKEN
475-
STORE_FAST 0 (e)
471+
%4d LOAD_GLOBAL 0 (Exception)
472+
CHECK_EXC_MATCH
473+
POP_JUMP_IF_FALSE 24 (to L9)
474+
L4: NOT_TAKEN
475+
L5: STORE_FAST 0 (e)
476476
477-
%4d L4: LOAD_FAST 0 (e)
478-
LOAD_ATTR 2 (__traceback__)
479-
STORE_FAST 1 (tb)
480-
L5: POP_EXCEPT
481-
LOAD_CONST 1 (None)
482-
STORE_FAST 0 (e)
483-
DELETE_FAST 0 (e)
477+
%4d L6: LOAD_FAST 0 (e)
478+
LOAD_ATTR 2 (__traceback__)
479+
STORE_FAST 1 (tb)
480+
L7: POP_EXCEPT
481+
LOAD_CONST 1 (None)
482+
STORE_FAST 0 (e)
483+
DELETE_FAST 0 (e)
484484
485-
%4d LOAD_FAST 1 (tb)
486-
RETURN_VALUE
485+
%4d LOAD_FAST 1 (tb)
486+
RETURN_VALUE
487487
488-
-- L6: LOAD_CONST 1 (None)
489-
STORE_FAST 0 (e)
490-
DELETE_FAST 0 (e)
491-
RERAISE 1
488+
-- L8: LOAD_CONST 1 (None)
489+
STORE_FAST 0 (e)
490+
DELETE_FAST 0 (e)
491+
RERAISE 1
492492
493-
%4d L7: RERAISE 0
493+
%4d L9: RERAISE 0
494494
495-
-- L8: COPY 3
496-
POP_EXCEPT
497-
RERAISE 1
495+
-- L10: COPY 3
496+
POP_EXCEPT
497+
RERAISE 1
498498
ExceptionTable:
499499
L1 to L2 -> L3 [0]
500-
L3 to L4 -> L8 [1] lasti
501-
L4 to L5 -> L6 [1] lasti
502-
L6 to L8 -> L8 [1] lasti
500+
L3 to L4 -> L10 [1] lasti
501+
L5 to L6 -> L10 [1] lasti
502+
L6 to L7 -> L8 [1] lasti
503+
L8 to L10 -> L10 [1] lasti
503504
""" % (TRACEBACK_CODE.co_firstlineno,
504505
TRACEBACK_CODE.co_firstlineno + 1,
505506
TRACEBACK_CODE.co_firstlineno + 2,
@@ -568,11 +569,11 @@ def _with(c):
568569
%4d L3: PUSH_EXC_INFO
569570
WITH_EXCEPT_START
570571
TO_BOOL
571-
POP_JUMP_IF_TRUE 2 (to L4)
572-
NOT_TAKEN
573-
RERAISE 2
574-
L4: POP_TOP
575-
L5: POP_EXCEPT
572+
POP_JUMP_IF_TRUE 2 (to L6)
573+
L4: NOT_TAKEN
574+
L5: RERAISE 2
575+
L6: POP_TOP
576+
L7: POP_EXCEPT
576577
POP_TOP
577578
POP_TOP
578579
POP_TOP
@@ -582,12 +583,13 @@ def _with(c):
582583
LOAD_CONST 1 (None)
583584
RETURN_VALUE
584585
585-
-- L6: COPY 3
586+
-- L8: COPY 3
586587
POP_EXCEPT
587588
RERAISE 1
588589
ExceptionTable:
589590
L1 to L2 -> L3 [2] lasti
590-
L3 to L5 -> L6 [4] lasti
591+
L3 to L4 -> L8 [4] lasti
592+
L5 to L7 -> L8 [4] lasti
591593
""" % (_with.__code__.co_firstlineno,
592594
_with.__code__.co_firstlineno + 1,
593595
_with.__code__.co_firstlineno + 2,

Python/flowgraph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ basicblock_addop(basicblock *b, int opcode, int oparg, location loc)
199199
cfg_instr *i = &b->b_instr[off];
200200
i->i_opcode = opcode;
201201
i->i_oparg = oparg;
202-
i->i_target = NULL;
203202
i->i_loc = loc;
204203

205204
return SUCCESS;
@@ -1096,6 +1095,7 @@ basicblock_remove_redundant_nops(basicblock *bb) {
10961095
assert(dest <= bb->b_iused);
10971096
int num_removed = bb->b_iused - dest;
10981097
bb->b_iused = dest;
1098+
memset(&bb->b_instr[dest], 0, sizeof(cfg_instr) * num_removed);
10991099
return num_removed;
11001100
}
11011101

0 commit comments

Comments
 (0)