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

Đang mở
#161,666 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, python
Lĩnh vực
backend

Hướng nghiên cứu

The undefined shifts are in stack_swap() in Objects/frameobject.c, around line 1323, where MASK and top are shifted as 32-bit values. Read it alongside mark_stacks and frame_setlineno(), which call it. Run the reproducer under UBSan to see the shift warnings. Done means the shifts operate on 64-bit values and the sanitizer run is clean. The issue already links PR gh-161667, so check that before starting.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

interpreter-core type-crash
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
  • gh-161667
Ngôn ngữ chính
Python
Star
78.2k
Fork
36.6k
Merge trung bình
19 giờ 57 phút
Pull request đã merge (30 ngày)
630

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.