Skip to content

Fix undefined left shifts in stack_swap() (Objects/frameobject.c) - #161422

Closed
PavlNekrasov wants to merge 1 commit into
python:mainfrom
PavlNekrasov:fix-frame-stack-swap-shift
Closed

PavlNekrasov wants to merge 1 commit into
python:mainfrom
PavlNekrasov:fix-frame-stack-swap-shift

Conversation

@PavlNekrasov

Copy link
Copy Markdown

stack_swap() in Objects/frameobject.c shifted MASK and top as
32-bit values and converted the result to int64_t, while the value-stack
model is a 63-bit int64_t and shift = BITS_PER_BLOCK*(n-1) can reach 60.
The shifts overflow / drop bits (undefined behavior) and corrupt the stack
model used by frame_setlineno() to validate line jumps.

import dis
import sys

fors = " ".join(f"for v{i} in range(1)" for i in range(11))
ns = {}
exec(f"def f():\n    return [0 {fors}]\n", ns)
f = ns["f"]

triggered = False
def tracer(frame, event, arg):
    global triggered
    if event == "line" and frame.f_code is f.__code__ and not triggered:
        triggered = True
        frame.f_lineno = frame.f_lineno  # mark_stacks -> stack_swap
    return tracer

sys.settrace(tracer)
try:
    f()
finally:
    sys.settrace(None)

UBSan output:

Objects/frameobject.c:1323:44: runtime error: shift exponent 33 is too large for 32-bit type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior Objects/frameobject.c:1323:44 in 
Objects/frameobject.c:1323:62: runtime error: shift exponent 33 is too large for 32-bit type 'unsigned int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior Objects/frameobject.c:1323:62 in 

Found by Linux Verification Center (linuxtesting.org) with SVACE

Widen MASK and top to int64_t before shifting: shift can reach 60.
@bedevere-app

bedevere-app Bot commented Nov 5, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz picnixz added the AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. label Nov 5, 2026
@bedevere-app

bedevere-app Bot commented Nov 5, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

We believe this contribution does not meet our guidelines for using AI tools, so we're closing it. If you think we've misunderstood your contribution, please let us know. You're welcome to explain your approach or ask questions about the guidelines.

Please read our guidelines for using AI tools.

@bedevere-app bedevere-app Bot closed this Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-guidelines-not-met The contribution does not meet our guidelines for using AI tools and is closed automatically. awaiting review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants