Skip to content

Commit

Permalink
fix: off by one error in forward jump calculation (python#19)
Browse files Browse the repository at this point in the history
* fix: off by one error in forward jump calculation

* Test: fixed test for python#18

* test: remove broken test

---------

Co-authored-by: Jules <[email protected]>
  • Loading branch information
Fidget-Spinner and JuliaPoo authored Jun 4, 2023
1 parent 3fd00f7 commit f876fdb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Python/tier2.c
Original file line number Diff line number Diff line change
Expand Up @@ -1694,7 +1694,7 @@ _PyTier2_Code_DetectAndEmitBB(
prev_type_guard->op.code == CHECK_LIST
);
#define END() goto end;
#define JUMPBY(x) i += x + 1;
#define JUMPBY(x) i += x;
#define DISPATCH() write_i = emit_i(write_i, specop, curr->op.arg); \
write_i = copy_cache_entries(write_i, curr+1, caches); \
i += caches; \
Expand Down Expand Up @@ -2067,8 +2067,14 @@ _PyTier2_Code_DetectAndEmitBB(

// Jumps may be the end of a basic block if they are conditional (a branch).
if (IS_JUMP_OPCODE(opcode)) {
#if BB_DEBUG
fprintf(stderr, "Encountered a forward jump\n");
#endif
// Unconditional forward jump... continue with the BB without writing the jump.
if (opcode == JUMP_FORWARD) {
#if BB_DEBUG
fprintf(stderr, "Encountered an unconditional forward jump\n");
#endif
// JUMP offset (oparg) + current instruction + cache entries
JUMPBY(oparg);
continue;
Expand Down
14 changes: 14 additions & 0 deletions tier2_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,5 +462,19 @@ def f(y,z,w):

trigger_tier2(f, (1,1,1.))

# As long as it doesn't crash, everything's good.

######################################################################
# Tests for: Tier 2 unconditional forward jump #
######################################################################
with TestInfo("tier 2 unconditional forward jumps"):
# See https://github.com/pylbbv/pylbbv/issues/17 for more information.
def f(x):
for _ in [1]:
break
x+x # Force it to be optimisable

trigger_tier2(f, (1,))

# As long as it doesn't crash, everything's good.
print("Tests completed ^-^")

0 comments on commit f876fdb

Please sign in to comment.