Skip to content

Directly called __lazy_import__ crashes with user-defined filters #45

@johnslavik

Description

@johnslavik

The problem occurs while calling the filter using vectorcall.

To repro:

import sys

def li_filter(importing_module: str, imported_module: str, fromlist: tuple[str, ...]) -> bool:
    return False

sys.set_lazy_imports_filter(li_filter)

__lazy_import__('wave')

Trace:

 0x00007ffff7cd00af in __assert_fail (assertion=<optimized out>, file=<optimized out>, line=<optimized out>, function=<optimized out>)
    at assert.c:127
#6  0x00000000005c270e in _PyStackRef_FromPyObjectNew (obj=<optimized out>) at ./Include/internal/pycore_stackref.h:582
#7  0x00000000005e37a0 in _PyEval_Vector (tstate=0xa0db80 <_PyRuntime+359040>, func=0x7fffe9ccc590, locals=locals@entry=0x0, 
    args=0x7fffffffc8f0, argcount=3, kwnames=0x0) at Python/ceval.c:2465
#8  0x0000000000492f31 in _PyFunction_Vectorcall (func=<optimized out>, stack=<optimized out>, nargsf=<optimized out>, 
    kwnames=<optimized out>) at Objects/call.c:413
#9  0x0000000000493245 in _PyObject_VectorcallTstate (tstate=0xa0db80 <_PyRuntime+359040>, 
    callable=callable@entry=<function at remote 0x7fffe9ccc590>, args=args@entry=0x7fffffffc8f0, nargsf=nargsf@entry=3, 
    kwnames=kwnames@entry=0x0) at ./Include/internal/pycore_call.h:136
#10 0x000000000049330d in PyObject_Vectorcall (callable=callable@entry=<function at remote 0x7fffe9ccc590>, args=args@entry=0x7fffffffc8f0, 
    nargsf=nargsf@entry=3, kwnames=kwnames@entry=0x0) at Objects/call.c:327
#11 0x000000000062e010 in _PyImport_LazyImportModuleLevelObject (tstate=tstate@entry=0xa0db80 <_PyRuntime+359040>, name=name@entry='wave', 
    builtins={'__name__': 'builtins', '__doc__': "Built-in functions, types, exceptions, and other objects.\n\nThis module provides direct access to all 'built-in'\nidentifiers of Python; for example, builtins.len is\nthe full name for the built-in function len().\n\nThis module is not normally accessed explicitly by most\napplications, but can be useful in modules that provide\nobjects with the same name as a built-in value, but in\nwhich the built-in of that name is also needed.", '__package__': '', '__loader__': <type at remote 0xac86e0>, '__spec__': <ModuleSpec(_cached=None, _set_fileattr=False, _uninitialized_submodules=[], loader=<type at remote 0xac86e0>, loader_state=None, name='builtins', origin='built-in', submodule_search_locations=None) at remote 0x7fffe9d30e20>, '__build_class__': <built-in method __build_class__ of module object at remote 0x7fffe9dbc3b0>, '__import__': <built-in method __import__ of module object at remote 0x7fffe9dbc3b0>, '__lazy_import__': <built-in method __lazy_import__ of module object ...(truncated), 
    globals=globals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    locals=locals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    fromlist=fromlist@entry=0x0, level=0) at Python/import.c:4449
#12 0x00000000005c012d in builtin___lazy_import___impl (module=module@entry=<module at remote 0x7fffe9dbc3b0>, name=name@entry='wave', 
    globals={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, globals@entry=0x0, 
    locals={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, locals@entry=0x0, 
    fromlist=fromlist@entry=0x0, level=level@entry=0) at Python/bltinmodule.c:340
#13 0x00000000005c02ec in builtin___lazy_import__ (module=<module at remote 0x7fffe9dbc3b0>, args=<optimized out>, 
    args@entry=0x7fffffffcbd8, nargs=nargs@entry=1, kwnames=kwnames@entry=0x0) at Python/clinic/bltinmodule.c.h:205
#14 0x00000000004e5763 in cfunction_vectorcall_FASTCALL_KEYWORDS (
    func=<built-in method __lazy_import__ of module object at remote 0x7fffe9dbc3b0>, args=0x7fffffffcbd8, nargsf=<optimized out>, 
    kwnames=0x0) at Objects/methodobject.c:465
#15 0x0000000000493245 in _PyObject_VectorcallTstate (tstate=0xa0db80 <_PyRuntime+359040>, 
    callable=callable@entry=<built-in method __lazy_import__ of module object at remote 0x7fffe9dbc3b0>, args=args@entry=0x7fffffffcbd8, 
    nargsf=9223372036854775809, kwnames=kwnames@entry=0x0) at ./Include/internal/pycore_call.h:136
#16 0x000000000049330d in PyObject_Vectorcall (
    callable=callable@entry=<built-in method __lazy_import__ of module object at remote 0x7fffe9dbc3b0>, args=args@entry=0x7fffffffcbd8, 
    nargsf=<optimized out>, kwnames=kwnames@entry=0x0) at Objects/call.c:327
#17 0x00000000005cc2c0 in _PyEval_EvalFrameDefault (tstate=0xa0db80 <_PyRuntime+359040>, frame=0x7ffff7fb0020, throwflag=0)
    at Python/generated_cases.c.h:1664
#18 0x00000000005e3699 in _PyEval_EvalFrame (tstate=tstate@entry=0xa0db80 <_PyRuntime+359040>, frame=frame@entry=0x7ffff7fb0020, 
    throwflag=throwflag@entry=0) at ./Include/internal/pycore_ceval.h:119
#19 0x00000000005e384b in _PyEval_Vector (tstate=tstate@entry=0xa0db80 <_PyRuntime+359040>, func=func@entry=0x7fffe9c46690, 
    locals=locals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    args=args@entry=0x0, argcount=argcount@entry=0, kwnames=kwnames@entry=0x0) at Python/ceval.c:2483
#20 0x00000000005e391e in PyEval_EvalCode (co=co@entry=<code at remote 0x7fffe9dc7600>, 
    globals=globals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    locals=locals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>})
    at Python/ceval.c:1009
#21 0x000000000065ac97 in run_eval_code_obj (tstate=tstate@entry=0xa0db80 <_PyRuntime+359040>, co=co@entry=0x7fffe9dc7600, 
    globals=globals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    locals=locals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>})
    at Python/pythonrun.c:1366
#22 0x000000000065ae5e in run_mod (mod=mod@entry=0xbb5a78, filename=filename@entry='/home/bswck/Python/cpython/t.py', 
    globals=globals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    locals=locals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    flags=flags@entry=0x7fffffffcfb8, arena=arena@entry=0x7fffe9cc21a0, interactive_src=0x0, generate_new_source=0)
    at Python/pythonrun.c:1469
#23 0x000000000065b647 in pyrun_file (fp=fp@entry=0xa85520, filename=filename@entry='/home/bswck/Python/cpython/t.py', 
    start=start@entry=257, 
    globals=globals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    locals=locals@entry={'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <SourceFileLoader(name='__main__', path='/home/bswck/Python/cpython/t.py') at remote 0x7fffe9dad660>, '__spec__': None, '__builtins__': <module at remote 0x7fffe9dbc3b0>, '__file__': '/home/bswck/Python/cpython/t.py', 'sys': <module at remote 0x7fffe9d0be30>, 'li_filter': <function at remote 0x7fffe9ccc590>}, 
    closeit=closeit@entry=1, flags=0x7fffffffcfb8) at Python/pythonrun.c:1294
#24 0x000000000065ccc2 in _PyRun_SimpleFileObject (fp=fp@entry=0xa85520, filename=filename@entry='/home/bswck/Python/cpython/t.py', 
    closeit=closeit@entry=1, flags=flags@entry=0x7fffffffcfb8) at Python/pythonrun.c:518
#25 0x000000000065ce86 in _PyRun_AnyFileObject (fp=fp@entry=0xa85520, filename=filename@entry='/home/bswck/Python/cpython/t.py', 
    closeit=closeit@entry=1, flags=flags@entry=0x7fffffffcfb8) at Python/pythonrun.c:81
#26 0x0000000000681f9f in pymain_run_file_obj (program_name=program_name@entry='/home/bswck/Python/cpython/python', 
    filename=filename@entry='/home/bswck/Python/cpython/t.py', skip_source_first_line=0) at Modules/main.c:410
#27 0x00000000006820ab in pymain_run_file (config=config@entry=0x9d8c48 <_PyRuntime+142152>) at Modules/main.c:429
#28 0x0000000000682ac4 in pymain_run_python (exitcode=exitcode@entry=0x7fffffffd11c) at Modules/main.c:691
#29 0x0000000000682cf6 in Py_RunMain () at Modules/main.c:772
#30 0x0000000000682d4a in pymain_main (args=args@entry=0x7fffffffd160) at Modules/main.c:802
#31 0x0000000000682dca in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at Modules/main.c:826
#32 0x0000000000401cef in main (argc=<optimized out>, argv=<optimized out>) at ./Programs/python.c:15

Fault handler:

❯ ./python -X dev t.py
python: ./Include/internal/pycore_stackref.h:582: _PyStackRef_FromPyObjectNew: Assertion `obj != NULL' failed.
Fatal Python error: Aborted

Current thread 0x00007fa00b2edbc0 [python] (most recent call first):
  File "/home/bswck/Python/cpython/t.py", line 8 in <module>

Current thread's C stack trace (most recent call first):
  Binary file "./python", at _Py_DumpStack+0x31 [0x67505b]
  Binary file "./python" [0x68ca71]
  Binary file "./python" [0x68d12b]
  Binary file "/lib64/libc.so.6", at +0x1a070 [0x7fa00b309070]
  Binary file "/lib64/libc.so.6", at +0x73e9c [0x7fa00b362e9c]
  Binary file "/lib64/libc.so.6", at gsignal+0x1e [0x7fa00b308f3e]
  Binary file "/lib64/libc.so.6", at abort+0x26 [0x7fa00b2f06d0]
  Binary file "/lib64/libc.so.6", at __assert_perror_fail+0x0 [0x7fa00b2f0639]
  Binary file "./python" [0x5c270e]
  Binary file "./python" [0x5e37a0]
  Binary file "./python", at _PyFunction_Vectorcall+0x51 [0x492f31]
  Binary file "./python" [0x493245]
  Binary file "./python", at PyObject_Vectorcall+0x23 [0x49330d]
  Binary file "./python" [0x62e010]
  Binary file "./python" [0x5c012d]
  Binary file "./python" [0x5c02ec]
  Binary file "./python" [0x4e5763]
  Binary file "./python" [0x493245]
  Binary file "./python", at PyObject_Vectorcall+0x23 [0x49330d]
  Binary file "./python", at _PyEval_EvalFrameDefault+0x39d6 [0x5cc2c0]
  Binary file "./python" [0x5e3699]
  Binary file "./python" [0x5e384b]
  Binary file "./python", at PyEval_EvalCode+0xb4 [0x5e391e]
  Binary file "./python" [0x65ac97]
  Binary file "./python" [0x65ae5e]
  Binary file "./python" [0x65b647]
  Binary file "./python" [0x65ccc2]
  Binary file "./python" [0x65ce86]
  Binary file "./python" [0x681f9f]
  Binary file "./python" [0x6820ab]
  Binary file "./python" [0x682ac4]
  Binary file "./python", at Py_RunMain+0x22 [0x682cf6]
  <truncated rest of calls>
Aborted (core dumped)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions