python / python/cpython

Segfault/debug abort because _PyModule_GetFilenameUTF8 asserts module filenames must be utf-8 (called by crossinterp)

オープン
#156,122 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

extension-modules topic-subinterpreters type-crash
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Crash report

What happened?
import __main__
from concurrent import interpreters

__main__.__file__ = "bad\ud800.py"

interp = interpreters.create()
interp.call(lambda x: x, [1])
sstagg@sapphire ~/s/t/cpython (main)> ./python.exe temp/crossinterp-main.py
Assertion failed: (size >= 0), function _PyModule_GetFilenameUTF8, file moduleobject.c, line 995.
fish: Job 1, './python.exe temp/crossinterp-m…' terminated by signal SIGABRT (Abort)

on a non-debug build:

fish: Job 1, 'python' terminated by signal SIGSEGV (Address boundary error)

because the result is passed to strncpy:

LLDB
Process 31843 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
       frame #0: 0x000000019c4a0e14 libsystem_platform.dylib`_platform_strcpy + 4
libsystem_platform.dylib`_platform_strcpy:
->  0x19c4a0e14 <+4>:  ldr    q0, [x9]
    0x19c4a0e18 <+8>:  str    q0, [sp, #-0x10]!
    0x19c4a0e1c <+12>: ands   x8, x1, #0xf
    0x19c4a0e20 <+16>: b.eq   0x19c4a0e7c    ; <+108>
Target 0: (python) stopped.
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
   * frame #0: 0x000000019c4a0e14 libsystem_platform.dylib`_platform_strcpy + 4
     frame #1: 0x00000001007e30c0 libpython3.14.dylib`_PyModule_GetFilenameUTF8 + 344
     frame #2: 0x00000001008f0fa4 libpython3.14.dylib`_PyPickle_GetXIData + 484
     frame #3: 0x00000001008f0d1c libpython3.14.dylib`_PyObject_GetXIData + 120
     frame #4: 0x00000001008f55e4 libpython3.14.dylib`_tuple_shared + 200
     frame #5: 0x00000001008f0bb4 libpython3.14.dylib`_get_xidata + 344
     frame #6: 0x00000001008f0ce8 libpython3.14.dylib`_PyObject_GetXIData + 68
     frame #7: 0x00000001000c5354 _interpreters.cpython-314-darwin.so`interp_call + 308
     frame #8: 0x00000001007e1904 libpython3.14.dylib`cfunction_call + 92
     frame #9: 0x0000000100785d08 libpython3.14.dylib`_PyObject_MakeTpCall + 304
     frame #10: 0x00000001008c64e0 libpython3.14.dylib`_PyEval_EvalFrameDefault + 15148
     frame #11: 0x00000001008c24b0 libpython3.14.dylib`PyEval_EvalCode + 416
     frame #12: 0x00000001008bec1c libpython3.14.dylib`builtin_exec + 1392
     frame #13: 0x00000001008c599c libpython3.14.dylib`_PyEval_EvalFrameDefault + 12264
     frame #14: 0x00000001008c26dc libpython3.14.dylib`_PyEval_Vector + 496
     frame #15: 0x00000001007864d8 libpython3.14.dylib`_PyVectorcall_Call + 116
     frame #16: 0x00000001009674bc libpython3.14.dylib`pymain_start_pyrepl + 628
     frame #17: 0x000000010096704c libpython3.14.dylib`pymain_run_stdin + 640
     frame #18: 0x00000001009665c0 libpython3.14.dylib`Py_RunMain + 1584
     frame #19: 0x000000010096682c libpython3.14.dylib`pymain_main + 320
     frame #20: 0x00000001009668d0 libpython3.14.dylib`Py_BytesMain + 44
     frame #21: 0x000000019c0c3e88 dyld`start + 6688
(lldb)

_PyModule_GetFilenameUTF8 only appears to be called by crossinterp module, but it does this:

https://github.com/python/cpython/blob/91d71dd67074d4599b6bd49cc933f41f8bd57058/Objects/moduleobject.c#L994-L995

Which means that if a module filename has a lone surrogate, then it PyUnicode_AsUTF8AndSize retusns and sets size -1, which triggers the abort.

I'm not sure what should happen here, should lone surrogates in module filenames be allowed? or should this 'just work' by not trying to coerce to utf8?

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

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

Python 3.16.0a0 (heads/main:e8158d1a02d, Aug 20 2026, 15:09:24) [Clang 21.0.0 (clang-2100.3.27.1)]

Linked PRs
  • gh-156218
  • gh-156221

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Objects/moduleobject.c の _PyModule_GetFilenameUTF8 付近から始め、traceback に示されているインタープリター間の呼び出し経路を、_PyPickle_GetXIData を含めて追跡します。lone-surrogate を含む main.file の提供された例を再現し、意図されたファイル名の処理を明らかにして、呼び出しが abort や segfault を起こさなくなったことを示す回帰テストを追加します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。