Skip to content

frameobject: undefined 32-bit left shifts in stack_swap() #161666

Open

Description

@PavlNekrasov

Crash report

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

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Output from running 'python -VV' on the command line:

No response

Linked PRs

Activity

  1. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions